rust/tests/consts.rs

80 lines
2.5 KiB
Rust
Raw Normal View History

2015-08-16 08:56:09 -05:00
#![allow(plugin_as_library)]
#![feature(rustc_private)]
extern crate clippy;
2015-08-16 08:56:09 -05:00
extern crate syntax;
extern crate rustc;
extern crate rustc_front;
use rustc_front::hir::*;
use syntax::parse::token::InternedString;
2015-08-16 08:56:09 -05:00
use syntax::ptr::P;
use syntax::codemap::{Spanned, COMMAND_LINE_SP};
2016-02-12 11:35:44 -06:00
use syntax::ast::LitKind;
use syntax::ast::LitIntType;
use syntax::ast::StrStyle;
2016-02-12 11:35:44 -06:00
use clippy::consts::{constant_simple, Constant, Sign};
fn spanned<T>(t: T) -> Spanned<T> {
Spanned{ node: t, span: COMMAND_LINE_SP }
}
fn expr(n: Expr_) -> Expr {
Expr{
id: 1,
node: n,
span: COMMAND_LINE_SP,
2015-12-05 06:25:04 -06:00
attrs: None
}
}
2016-02-12 11:35:44 -06:00
fn lit(l: LitKind) -> Expr {
expr(ExprLit(P(spanned(l))))
}
fn binop(op: BinOp_, l: Expr, r: Expr) -> Expr {
expr(ExprBinary(spanned(op), P(l), P(r)))
}
fn check(expect: Constant, expr: &Expr) {
assert_eq!(Some(expect), constant_simple(expr))
2015-08-16 08:56:09 -05:00
}
2016-02-01 05:51:33 -06:00
const TRUE : Constant = Constant::Bool(true);
const FALSE : Constant = Constant::Bool(false);
2016-02-12 11:35:44 -06:00
const ZERO : Constant = Constant::Int(0, LitIntType::Unsuffixed, Sign::Plus);
const ONE : Constant = Constant::Int(1, LitIntType::Unsuffixed, Sign::Plus);
const TWO : Constant = Constant::Int(2, LitIntType::Unsuffixed, Sign::Plus);
#[test]
fn test_lit() {
2016-02-12 11:35:44 -06:00
check(TRUE, &lit(LitKind::Bool(true)));
check(FALSE, &lit(LitKind::Bool(false)));
check(ZERO, &lit(LitKind::Int(0, LitIntType::Unsuffixed)));
check(Constant::Str("cool!".into(), StrStyle::Cooked), &lit(LitKind::Str(
InternedString::new("cool!"), StrStyle::Cooked)));
}
#[test]
fn test_ops() {
2016-02-12 11:35:44 -06:00
check(TRUE, &binop(BiOr, lit(LitKind::Bool(false)), lit(LitKind::Bool(true))));
check(FALSE, &binop(BiAnd, lit(LitKind::Bool(false)), lit(LitKind::Bool(true))));
2016-02-12 11:35:44 -06:00
let litzero = lit(LitKind::Int(0, LitIntType::Unsuffixed));
let litone = lit(LitKind::Int(1, LitIntType::Unsuffixed));
check(TRUE, &binop(BiEq, litzero.clone(), litzero.clone()));
check(TRUE, &binop(BiGe, litzero.clone(), litzero.clone()));
check(TRUE, &binop(BiLe, litzero.clone(), litzero.clone()));
check(FALSE, &binop(BiNe, litzero.clone(), litzero.clone()));
check(FALSE, &binop(BiGt, litzero.clone(), litzero.clone()));
check(FALSE, &binop(BiLt, litzero.clone(), litzero.clone()));
2015-08-19 04:58:59 -05:00
check(ZERO, &binop(BiAdd, litzero.clone(), litzero.clone()));
check(TWO, &binop(BiAdd, litone.clone(), litone.clone()));
check(ONE, &binop(BiSub, litone.clone(), litzero.clone()));
check(ONE, &binop(BiMul, litone.clone(), litone.clone()));
check(ONE, &binop(BiDiv, litone.clone(), litone.clone()));
}