blob: e0cd01e0056e88eea071ec2f10b80205409d9060 [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
Nika Layzellfb783e32017-12-30 14:58:27 -050066#[cfg(procmacro2_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -050067#[test]
68fn span_test() {
69 fn check_spans(p: &str, mut lines: &[(usize, usize, usize, usize)]) {
Nika Layzellf8d5f212017-12-11 14:07:02 -050070 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
Nika Layzellf8d5f212017-12-11 14:07:02 -050082 let start = i.span.start();
83 assert_eq!(start.line, sline, "sline did not match for {}", i);
84 assert_eq!(start.column, scol, "scol did not match for {}", i);
85
86 let end = i.span.end();
87 assert_eq!(end.line, eline, "eline did not match for {}", i);
88 assert_eq!(end.column, ecol, "ecol did not match for {}", i);
89
90 match i.kind {
91 TokenNode::Group(_, stream) =>
92 check_spans_internal(stream, lines),
93 _ => {}
94 }
95 }
96 }
97 }
98
99 check_spans("\
100/// This is a document comment
101testing 123
102{
103 testing 234
104}", &[
105 (1, 0, 1, 30),
106 (2, 0, 2, 7),
107 (2, 8, 2, 11),
108 (3, 0, 5, 1),
109 (4, 2, 4, 9),
110 (4, 10, 4, 13),
111]);
112}
113
Nika Layzellfb783e32017-12-30 14:58:27 -0500114#[cfg(procmacro2_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500115#[cfg(not(feature = "unstable"))]
116#[test]
117fn default_span() {
118 let start = Span::call_site().start();
119 assert_eq!(start.line, 1);
120 assert_eq!(start.column, 0);
121 let end = Span::call_site().end();
122 assert_eq!(end.line, 1);
123 assert_eq!(end.column, 0);
124 let source_file = Span::call_site().source_file();
Nika Layzellfb783e32017-12-30 14:58:27 -0500125 assert_eq!(source_file.path().to_string(), "<unspecified>");
Nika Layzellf8d5f212017-12-11 14:07:02 -0500126 assert!(!source_file.is_real());
127}
128
Nika Layzellfb783e32017-12-30 14:58:27 -0500129#[cfg(procmacro2_unstable)]
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}