blob: b09225ee4cd3ac431eb836ecad2fd27a0ae66087 [file] [log] [blame]
Michael Layzellbe1e24d2017-06-04 21:15:23 -04001#![cfg(all(feature = "extra-traits", feature = "full"))]
Nika Layzella2a1a4a2017-11-19 11:33:17 -05002#![feature(rustc_private)]
3
David Tolnayc5ab8c62017-12-26 16:43:39 -05004#[macro_use]
Michael Layzellbe1e24d2017-06-04 21:15:23 -04005extern crate syn;
David Tolnay8c91b882017-12-28 23:04:32 -05006use syn::{BinOp, Expr, ExprBinary, ExprGroup, ExprLit, Lit, LitKind};
David Tolnay42eaae12017-12-26 23:05:18 -05007use syn::token::Group;
Michael Layzellbe1e24d2017-06-04 21:15:23 -04008
9extern crate proc_macro2;
10use proc_macro2::*;
11
David Tolnayc7a5d3d2017-06-04 12:11:05 -070012mod common;
13
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070014fn tt(k: TokenNode) -> TokenTree {
Michael Layzellbe1e24d2017-06-04 21:15:23 -040015 TokenTree {
16 span: Span::default(),
17 kind: k,
18 }
19}
20
David Tolnay8c91b882017-12-28 23:04:32 -050021fn expr<T: Into<Expr>>(t: T) -> Expr {
22 t.into()
Michael Layzellbe1e24d2017-06-04 21:15:23 -040023}
24
25fn lit<T: Into<Literal>>(t: T) -> Expr {
David Tolnay8c91b882017-12-28 23:04:32 -050026 Expr::Lit(ExprLit {
27 attrs: Vec::new(),
28 lit: Lit {
29 value: LitKind::Other(t.into()),
30 span: Span::default(),
31 },
Michael Layzellbe1e24d2017-06-04 21:15:23 -040032 })
33}
34
35#[test]
36fn test_grouping() {
37 let raw: TokenStream = vec![
Alex Crichton605643b2017-07-05 18:35:14 -070038 tt(TokenNode::Literal(Literal::i32(1))),
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070039 tt(TokenNode::Op('+', Spacing::Alone)),
David Tolnay51382052017-12-27 13:46:21 -050040 tt(TokenNode::Group(
41 Delimiter::None,
42 vec![
43 tt(TokenNode::Literal(Literal::i32(2))),
44 tt(TokenNode::Op('+', Spacing::Alone)),
45 tt(TokenNode::Literal(Literal::i32(3))),
46 ].into_iter()
47 .collect(),
48 )),
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070049 tt(TokenNode::Op('*', Spacing::Alone)),
Alex Crichton605643b2017-07-05 18:35:14 -070050 tt(TokenNode::Literal(Literal::i32(4))),
David Tolnay51382052017-12-27 13:46:21 -050051 ].into_iter()
52 .collect();
Michael Layzellbe1e24d2017-06-04 21:15:23 -040053
54 assert_eq!(raw.to_string(), "1i32 + 2i32 + 3i32 * 4i32");
55
David Tolnay51382052017-12-27 13:46:21 -050056 assert_eq!(
57 common::parse::syn::<Expr>(raw),
58 expr(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -050059 attrs: Vec::new(),
David Tolnay51382052017-12-27 13:46:21 -050060 left: Box::new(lit(Literal::i32(1))),
61 op: BinOp::Add(<Token![+]>::default()),
62 right: Box::new(expr(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -050063 attrs: Vec::new(),
David Tolnay51382052017-12-27 13:46:21 -050064 left: Box::new(expr(ExprGroup {
David Tolnay8c91b882017-12-28 23:04:32 -050065 attrs: Vec::new(),
David Tolnay51382052017-12-27 13:46:21 -050066 group_token: Group::default(),
67 expr: Box::new(expr(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -050068 attrs: Vec::new(),
David Tolnay51382052017-12-27 13:46:21 -050069 left: Box::new(lit(Literal::i32(2))),
70 op: BinOp::Add(<Token![+]>::default()),
71 right: Box::new(lit(Literal::i32(3))),
72 })),
Michael Layzellbe1e24d2017-06-04 21:15:23 -040073 })),
David Tolnay51382052017-12-27 13:46:21 -050074 op: BinOp::Mul(<Token![*]>::default()),
75 right: Box::new(lit(Literal::i32(4))),
Michael Layzellbe1e24d2017-06-04 21:15:23 -040076 })),
David Tolnay51382052017-12-27 13:46:21 -050077 })
78 );
Michael Layzellbe1e24d2017-06-04 21:15:23 -040079}
80
81#[test]
82fn test_invalid_grouping() {
83 let raw: TokenStream = vec![
Alex Crichton605643b2017-07-05 18:35:14 -070084 tt(TokenNode::Literal(Literal::i32(1))),
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070085 tt(TokenNode::Op('+', Spacing::Alone)),
David Tolnay51382052017-12-27 13:46:21 -050086 tt(TokenNode::Group(
87 Delimiter::None,
88 vec![
89 tt(TokenNode::Literal(Literal::i32(2))),
90 tt(TokenNode::Op('+', Spacing::Alone)),
91 ].into_iter()
92 .collect(),
93 )),
Alex Crichton605643b2017-07-05 18:35:14 -070094 tt(TokenNode::Literal(Literal::i32(3))),
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070095 tt(TokenNode::Op('*', Spacing::Alone)),
Alex Crichton605643b2017-07-05 18:35:14 -070096 tt(TokenNode::Literal(Literal::i32(4))),
David Tolnay51382052017-12-27 13:46:21 -050097 ].into_iter()
98 .collect();
Michael Layzellbe1e24d2017-06-04 21:15:23 -040099
100 assert_eq!(raw.to_string(), "1i32 + 2i32 + 3i32 * 4i32");
101
David Tolnay51382052017-12-27 13:46:21 -0500102 assert_eq!(
103 common::parse::syn::<Expr>(raw),
104 expr(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -0500105 attrs: Vec::new(),
David Tolnay51382052017-12-27 13:46:21 -0500106 left: Box::new(expr(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -0500107 attrs: Vec::new(),
David Tolnay51382052017-12-27 13:46:21 -0500108 left: Box::new(lit(Literal::i32(1))),
109 op: BinOp::Add(<Token![+]>::default()),
110 right: Box::new(lit(Literal::i32(2))),
111 })),
David Tolnayf8db7ba2017-11-11 22:52:16 -0800112 op: BinOp::Add(<Token![+]>::default()),
David Tolnay51382052017-12-27 13:46:21 -0500113 right: Box::new(expr(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -0500114 attrs: Vec::new(),
David Tolnay51382052017-12-27 13:46:21 -0500115 left: Box::new(lit(Literal::i32(3))),
116 op: BinOp::Mul(<Token![*]>::default()),
117 right: Box::new(lit(Literal::i32(4))),
118 })),
119 })
120 );
Michael Layzellbe1e24d2017-06-04 21:15:23 -0400121}