]> git.lizzy.rs Git - rust.git/blob - tests/consts.rs
Add new lint on function naming check (the '_')
[rust.git] / tests / consts.rs
1 #![allow(plugin_as_library)]
2 #![feature(rustc_private)]
3
4 extern crate clippy;
5 extern crate syntax;
6 extern crate rustc;
7 extern crate rustc_front;
8
9 use rustc_front::hir::*;
10 use syntax::parse::token::InternedString;
11 use syntax::ptr::P;
12 use syntax::codemap::{Spanned, COMMAND_LINE_SP};
13
14 use syntax::ast::Lit_::*;
15 use syntax::ast::Lit_;
16 use syntax::ast::LitIntType::*;
17 use syntax::ast::StrStyle::*;
18 use syntax::ast::Sign::*;
19
20 use clippy::consts::{constant_simple, Constant};
21 use clippy::consts::Constant::*;
22
23 fn spanned<T>(t: T) -> Spanned<T> {
24     Spanned{ node: t, span: COMMAND_LINE_SP }
25 }
26
27 fn expr(n: Expr_) -> Expr {
28     Expr{
29         id: 1,
30         node: n,
31         span: COMMAND_LINE_SP,
32         attrs: None
33     }
34 }
35
36 fn lit(l: Lit_) -> Expr {
37     expr(ExprLit(P(spanned(l))))
38 }
39
40 fn binop(op: BinOp_, l: Expr, r: Expr) -> Expr {
41     expr(ExprBinary(spanned(op), P(l), P(r)))
42 }
43
44 fn check(expect: Constant, expr: &Expr) {
45     assert_eq!(Some(expect), constant_simple(expr))
46 }
47
48 const TRUE : Constant = ConstantBool(true);
49 const FALSE : Constant = ConstantBool(false);
50 const ZERO : Constant = ConstantInt(0, UnsuffixedIntLit(Plus));
51 const ONE : Constant = ConstantInt(1, UnsuffixedIntLit(Plus));
52 const TWO : Constant = ConstantInt(2, UnsuffixedIntLit(Plus));
53
54 #[test]
55 fn test_lit() {
56     check(TRUE, &lit(LitBool(true)));
57     check(FALSE, &lit(LitBool(false)));
58     check(ZERO, &lit(LitInt(0, UnsuffixedIntLit(Plus))));
59     check(ConstantStr("cool!".into(), CookedStr), &lit(LitStr(
60         InternedString::new("cool!"), CookedStr)));
61 }
62
63 #[test]
64 fn test_ops() {
65     check(TRUE, &binop(BiOr, lit(LitBool(false)), lit(LitBool(true))));
66     check(FALSE, &binop(BiAnd, lit(LitBool(false)), lit(LitBool(true))));
67
68     let litzero = lit(LitInt(0, UnsuffixedIntLit(Plus)));
69     let litone = lit(LitInt(1, UnsuffixedIntLit(Plus)));
70     check(TRUE, &binop(BiEq, litzero.clone(), litzero.clone()));
71     check(TRUE, &binop(BiGe, litzero.clone(), litzero.clone()));
72     check(TRUE, &binop(BiLe, litzero.clone(), litzero.clone()));
73     check(FALSE, &binop(BiNe, litzero.clone(), litzero.clone()));
74     check(FALSE, &binop(BiGt, litzero.clone(), litzero.clone()));
75     check(FALSE, &binop(BiLt, litzero.clone(), litzero.clone()));
76
77     check(ZERO, &binop(BiAdd, litzero.clone(), litzero.clone()));
78     check(TWO, &binop(BiAdd, litone.clone(), litone.clone()));
79     check(ONE, &binop(BiSub, litone.clone(), litzero.clone()));
80     check(ONE, &binop(BiMul, litone.clone(), litone.clone()));
81     check(ONE, &binop(BiDiv, litone.clone(), litone.clone()));
82 }