blob: 2de120aad36889bb2002639dc20fb515eceb4bb1 [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");
David Tolnay8d109342017-12-25 18:24:45 -050050 roundtrip("'\\u{10__FFFF}'");
51 roundtrip("\"\\u{10_F0FF__}foo\\u{1_0_0_0__}\"");
David Tolnaye5806852017-06-01 12:49:20 -070052}
53
54#[test]
55fn fail() {
56 fn fail(p: &str) {
57 if p.parse::<TokenStream>().is_ok() {
58 panic!("should have failed to parse: {}", p);
59 }
60 }
61 fail("1x");
62 fail("1u80");
63 fail("1f320");
64 fail("' static");
65 fail("'mut");
66}
Nika Layzellf8d5f212017-12-11 14:07:02 -050067
Nika Layzellfb783e32017-12-30 14:58:27 -050068#[cfg(procmacro2_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -050069#[test]
70fn span_test() {
71 fn check_spans(p: &str, mut lines: &[(usize, usize, usize, usize)]) {
Nika Layzellf8d5f212017-12-11 14:07:02 -050072 let ts = p.parse::<TokenStream>().unwrap();
73 check_spans_internal(ts, &mut lines);
74 }
75
76 fn check_spans_internal(
77 ts: TokenStream,
78 lines: &mut &[(usize, usize, usize, usize)],
79 ) {
80 for i in ts {
81 if let Some((&(sline, scol, eline, ecol), rest)) = lines.split_first() {
82 *lines = rest;
83
Nika Layzellf8d5f212017-12-11 14:07:02 -050084 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
Nika Layzellfb783e32017-12-30 14:58:27 -0500116#[cfg(procmacro2_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500117#[cfg(not(feature = "unstable"))]
118#[test]
119fn default_span() {
120 let start = Span::call_site().start();
121 assert_eq!(start.line, 1);
122 assert_eq!(start.column, 0);
123 let end = Span::call_site().end();
124 assert_eq!(end.line, 1);
125 assert_eq!(end.column, 0);
126 let source_file = Span::call_site().source_file();
Nika Layzellfb783e32017-12-30 14:58:27 -0500127 assert_eq!(source_file.path().to_string(), "<unspecified>");
Nika Layzellf8d5f212017-12-11 14:07:02 -0500128 assert!(!source_file.is_real());
129}
130
Nika Layzellfb783e32017-12-30 14:58:27 -0500131#[cfg(procmacro2_unstable)]
Nika Layzellddea1562017-12-11 14:25:35 -0500132#[test]
133fn span_join() {
134 let source1 =
135 "aaa\nbbb".parse::<TokenStream>().unwrap().into_iter().collect::<Vec<_>>();
136 let source2 =
137 "ccc\nddd".parse::<TokenStream>().unwrap().into_iter().collect::<Vec<_>>();
138
139 assert!(source1[0].span.source_file() != source2[0].span.source_file());
140 assert_eq!(source1[0].span.source_file(), source1[1].span.source_file());
141
142 let joined1 = source1[0].span.join(source1[1].span);
143 let joined2 = source1[0].span.join(source2[0].span);
144 assert!(joined1.is_some());
145 assert!(joined2.is_none());
146
147 let start = joined1.unwrap().start();
148 let end = joined1.unwrap().end();
149 assert_eq!(start.line, 1);
150 assert_eq!(start.column, 0);
151 assert_eq!(end.line, 2);
152 assert_eq!(end.column, 3);
153
154 assert_eq!(joined1.unwrap().source_file(), source1[0].span.source_file());
155}