blob: 248ead282317024f8a212573e6d331f8bb403f07 [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)]) {
69 eprintln!("checking {:?}", p);
70 let ts = p.parse::<TokenStream>().unwrap();
71 check_spans_internal(ts, &mut lines);
72 }
73
74 fn check_spans_internal(
75 ts: TokenStream,
76 lines: &mut &[(usize, usize, usize, usize)],
77 ) {
78 for i in ts {
79 if let Some((&(sline, scol, eline, ecol), rest)) = lines.split_first() {
80 *lines = rest;
81
82 eprintln!("span = {:?}", i.span);
83
84 let start = i.span.start();
85 assert_eq!(start.line, sline, "sline did not match for {}", i);
86 assert_eq!(start.column, scol, "scol did not match for {}", i);
87
88 let end = i.span.end();
89 assert_eq!(end.line, eline, "eline did not match for {}", i);
90 assert_eq!(end.column, ecol, "ecol did not match for {}", i);
91
92 match i.kind {
93 TokenNode::Group(_, stream) =>
94 check_spans_internal(stream, lines),
95 _ => {}
96 }
97 }
98 }
99 }
100
101 check_spans("\
102/// This is a document comment
103testing 123
104{
105 testing 234
106}", &[
107 (1, 0, 1, 30),
108 (2, 0, 2, 7),
109 (2, 8, 2, 11),
110 (3, 0, 5, 1),
111 (4, 2, 4, 9),
112 (4, 10, 4, 13),
113]);
114}
115
116#[cfg(not(feature = "unstable"))]
117#[test]
118fn default_span() {
119 let start = Span::call_site().start();
120 assert_eq!(start.line, 1);
121 assert_eq!(start.column, 0);
122 let end = Span::call_site().end();
123 assert_eq!(end.line, 1);
124 assert_eq!(end.column, 0);
125 let source_file = Span::call_site().source_file();
126 assert_eq!(source_file.as_str(), "<unspecified>");
127 assert!(!source_file.is_real());
128}
129
Nika Layzellddea1562017-12-11 14:25:35 -0500130#[test]
131fn span_join() {
132 let source1 =
133 "aaa\nbbb".parse::<TokenStream>().unwrap().into_iter().collect::<Vec<_>>();
134 let source2 =
135 "ccc\nddd".parse::<TokenStream>().unwrap().into_iter().collect::<Vec<_>>();
136
137 assert!(source1[0].span.source_file() != source2[0].span.source_file());
138 assert_eq!(source1[0].span.source_file(), source1[1].span.source_file());
139
140 let joined1 = source1[0].span.join(source1[1].span);
141 let joined2 = source1[0].span.join(source2[0].span);
142 assert!(joined1.is_some());
143 assert!(joined2.is_none());
144
145 let start = joined1.unwrap().start();
146 let end = joined1.unwrap().end();
147 assert_eq!(start.line, 1);
148 assert_eq!(start.column, 0);
149 assert_eq!(end.line, 2);
150 assert_eq!(end.column, 3);
151
152 assert_eq!(joined1.unwrap().source_file(), source1[0].span.source_file());
153}