blob: a4ad74e45a402dd51dbd4c70381ad0605caa7f03 [file] [log] [blame]
David Tolnaye5806852017-06-01 12:49:20 -07001extern crate proc_macro2;
2
Nika Layzellf8d5f212017-12-11 14:07:02 -05003use proc_macro2::{Term, Literal, TokenStream, TokenNode, Span};
David Tolnaye5806852017-06-01 12:49:20 -07004
5#[test]
6fn symbols() {
Alex Crichton1a7f7622017-07-05 17:47:15 -07007 assert_eq!(Term::intern("foo").as_str(), "foo");
8 assert_eq!(Term::intern("bar").as_str(), "bar");
David Tolnaye5806852017-06-01 12:49:20 -07009}
10
11#[test]
12fn literals() {
Alex Crichton1a7f7622017-07-05 17:47:15 -070013 assert_eq!(Literal::string("foo").to_string(), "\"foo\"");
14 assert_eq!(Literal::string("\"").to_string(), "\"\\\"\"");
David Tolnaye5806852017-06-01 12:49:20 -070015}
16
17#[test]
18fn roundtrip() {
19 fn roundtrip(p: &str) {
20 println!("parse: {}", p);
21 let s = p.parse::<TokenStream>().unwrap().to_string();
22 println!("first: {}", s);
23 let s2 = s.to_string().parse::<TokenStream>().unwrap().to_string();
24 assert_eq!(s, s2);
25 }
26 roundtrip("a");
27 roundtrip("<<");
28 roundtrip("<<=");
29 roundtrip("
30 /// a
31 wut
32 ");
33 roundtrip("
34 1
35 1.0
36 1f32
37 2f64
38 1usize
39 4isize
40 4e10
41 1_000
42 1_0i32
43 8u8
44 9
45 0
46 0xffffffffffffffffffffffffffffffff
47 ");
48 roundtrip("'a");
49 roundtrip("'static");
50}
51
52#[test]
53fn fail() {
54 fn fail(p: &str) {
55 if p.parse::<TokenStream>().is_ok() {
56 panic!("should have failed to parse: {}", p);
57 }
58 }
59 fail("1x");
60 fail("1u80");
61 fail("1f320");
62 fail("' static");
63 fail("'mut");
64}
Nika Layzellf8d5f212017-12-11 14:07:02 -050065
66#[test]
67fn span_test() {
68 fn check_spans(p: &str, mut lines: &[(usize, usize, usize, usize)]) {
Nika Layzellf8d5f212017-12-11 14:07:02 -050069 let ts = p.parse::<TokenStream>().unwrap();
70 check_spans_internal(ts, &mut lines);
71 }
72
73 fn check_spans_internal(
74 ts: TokenStream,
75 lines: &mut &[(usize, usize, usize, usize)],
76 ) {
77 for i in ts {
78 if let Some((&(sline, scol, eline, ecol), rest)) = lines.split_first() {
79 *lines = rest;
80
Nika Layzellf8d5f212017-12-11 14:07:02 -050081 let start = i.span.start();
82 assert_eq!(start.line, sline, "sline did not match for {}", i);
83 assert_eq!(start.column, scol, "scol did not match for {}", i);
84
85 let end = i.span.end();
86 assert_eq!(end.line, eline, "eline did not match for {}", i);
87 assert_eq!(end.column, ecol, "ecol did not match for {}", i);
88
89 match i.kind {
90 TokenNode::Group(_, stream) =>
91 check_spans_internal(stream, lines),
92 _ => {}
93 }
94 }
95 }
96 }
97
98 check_spans("\
99/// This is a document comment
100testing 123
101{
102 testing 234
103}", &[
104 (1, 0, 1, 30),
105 (2, 0, 2, 7),
106 (2, 8, 2, 11),
107 (3, 0, 5, 1),
108 (4, 2, 4, 9),
109 (4, 10, 4, 13),
110]);
111}
112
113#[cfg(not(feature = "unstable"))]
114#[test]
115fn default_span() {
116 let start = Span::call_site().start();
117 assert_eq!(start.line, 1);
118 assert_eq!(start.column, 0);
119 let end = Span::call_site().end();
120 assert_eq!(end.line, 1);
121 assert_eq!(end.column, 0);
122 let source_file = Span::call_site().source_file();
123 assert_eq!(source_file.as_str(), "<unspecified>");
124 assert!(!source_file.is_real());
125}
126
Nika Layzellddea1562017-12-11 14:25:35 -0500127#[test]
128fn span_join() {
129 let source1 =
130 "aaa\nbbb".parse::<TokenStream>().unwrap().into_iter().collect::<Vec<_>>();
131 let source2 =
132 "ccc\nddd".parse::<TokenStream>().unwrap().into_iter().collect::<Vec<_>>();
133
134 assert!(source1[0].span.source_file() != source2[0].span.source_file());
135 assert_eq!(source1[0].span.source_file(), source1[1].span.source_file());
136
137 let joined1 = source1[0].span.join(source1[1].span);
138 let joined2 = source1[0].span.join(source2[0].span);
139 assert!(joined1.is_some());
140 assert!(joined2.is_none());
141
142 let start = joined1.unwrap().start();
143 let end = joined1.unwrap().end();
144 assert_eq!(start.line, 1);
145 assert_eq!(start.column, 0);
146 assert_eq!(end.line, 2);
147 assert_eq!(end.column, 3);
148
149 assert_eq!(joined1.unwrap().source_file(), source1[0].span.source_file());
150}