blob: 370392b65c7c454b333f5a0b30502e6b51cd0714 [file] [log] [blame]
David Tolnaye5806852017-06-01 12:49:20 -07001extern crate proc_macro2;
2
David Tolnaya13d1422018-03-31 21:27:48 +02003use std::str::{self, FromStr};
Alex Crichton8c030332018-01-16 08:07:36 -08004
David Tolnay3d9d6ad2018-05-18 10:51:55 -07005use proc_macro2::{Ident, Literal, Spacing, Span, TokenStream, TokenTree};
David Tolnaye5806852017-06-01 12:49:20 -07006
7#[test]
David Tolnay637eef42019-04-20 13:36:18 -07008fn idents() {
David Tolnay3d9d6ad2018-05-18 10:51:55 -07009 assert_eq!(
10 Ident::new("String", Span::call_site()).to_string(),
11 "String"
12 );
Alex Crichtonf3888432018-05-16 09:11:05 -070013 assert_eq!(Ident::new("fn", Span::call_site()).to_string(), "fn");
14 assert_eq!(Ident::new("_", Span::call_site()).to_string(), "_");
David Tolnay489c6422018-04-07 08:37:28 -070015}
16
17#[test]
Alex Crichtonf3888432018-05-16 09:11:05 -070018#[cfg(procmacro2_semver_exempt)]
David Tolnay637eef42019-04-20 13:36:18 -070019fn raw_idents() {
David Tolnay48ea5042018-04-23 19:17:35 -070020 assert_eq!(
Alex Crichtonf3888432018-05-16 09:11:05 -070021 Ident::new_raw("String", Span::call_site()).to_string(),
David Tolnay48ea5042018-04-23 19:17:35 -070022 "r#String"
23 );
Alex Crichtonf3888432018-05-16 09:11:05 -070024 assert_eq!(Ident::new_raw("fn", Span::call_site()).to_string(), "r#fn");
25 assert_eq!(Ident::new_raw("_", Span::call_site()).to_string(), "r#_");
David Tolnay489c6422018-04-07 08:37:28 -070026}
27
28#[test]
Alex Crichtonf3888432018-05-16 09:11:05 -070029#[should_panic(expected = "Ident is not allowed to be empty; use Option<Ident>")]
David Tolnay637eef42019-04-20 13:36:18 -070030fn ident_empty() {
Alex Crichtonf3888432018-05-16 09:11:05 -070031 Ident::new("", Span::call_site());
David Tolnay489c6422018-04-07 08:37:28 -070032}
33
34#[test]
Alex Crichtonf3888432018-05-16 09:11:05 -070035#[should_panic(expected = "Ident cannot be a number; use Literal instead")]
David Tolnay637eef42019-04-20 13:36:18 -070036fn ident_number() {
Alex Crichtonf3888432018-05-16 09:11:05 -070037 Ident::new("255", Span::call_site());
David Tolnay489c6422018-04-07 08:37:28 -070038}
39
40#[test]
Alex Crichtonf3888432018-05-16 09:11:05 -070041#[should_panic(expected = "\"a#\" is not a valid Ident")]
David Tolnay637eef42019-04-20 13:36:18 -070042fn ident_invalid() {
Alex Crichtonf3888432018-05-16 09:11:05 -070043 Ident::new("a#", Span::call_site());
David Tolnay489c6422018-04-07 08:37:28 -070044}
45
46#[test]
Alex Crichtonf3888432018-05-16 09:11:05 -070047#[should_panic(expected = "not a valid Ident")]
David Tolnay637eef42019-04-20 13:36:18 -070048fn raw_ident_empty() {
Alex Crichtonf3888432018-05-16 09:11:05 -070049 Ident::new("r#", Span::call_site());
David Tolnay489c6422018-04-07 08:37:28 -070050}
51
52#[test]
Alex Crichtonf3888432018-05-16 09:11:05 -070053#[should_panic(expected = "not a valid Ident")]
David Tolnay637eef42019-04-20 13:36:18 -070054fn raw_ident_number() {
Alex Crichtonf3888432018-05-16 09:11:05 -070055 Ident::new("r#255", Span::call_site());
David Tolnay489c6422018-04-07 08:37:28 -070056}
57
58#[test]
Alex Crichtonf3888432018-05-16 09:11:05 -070059#[should_panic(expected = "\"r#a#\" is not a valid Ident")]
David Tolnay637eef42019-04-20 13:36:18 -070060fn raw_ident_invalid() {
Alex Crichtonf3888432018-05-16 09:11:05 -070061 Ident::new("r#a#", Span::call_site());
David Tolnay489c6422018-04-07 08:37:28 -070062}
63
64#[test]
Alex Crichtonf3888432018-05-16 09:11:05 -070065#[should_panic(expected = "not a valid Ident")]
David Tolnay489c6422018-04-07 08:37:28 -070066fn lifetime_empty() {
Alex Crichtonf3888432018-05-16 09:11:05 -070067 Ident::new("'", Span::call_site());
David Tolnay489c6422018-04-07 08:37:28 -070068}
69
70#[test]
Alex Crichtonf3888432018-05-16 09:11:05 -070071#[should_panic(expected = "not a valid Ident")]
David Tolnay489c6422018-04-07 08:37:28 -070072fn lifetime_number() {
Alex Crichtonf3888432018-05-16 09:11:05 -070073 Ident::new("'255", Span::call_site());
David Tolnay489c6422018-04-07 08:37:28 -070074}
75
76#[test]
Alex Crichtonf3888432018-05-16 09:11:05 -070077#[should_panic(expected = r#""\'a#" is not a valid Ident"#)]
David Tolnay489c6422018-04-07 08:37:28 -070078fn lifetime_invalid() {
Alex Crichtonf3888432018-05-16 09:11:05 -070079 Ident::new("'a#", Span::call_site());
David Tolnaye5806852017-06-01 12:49:20 -070080}
81
82#[test]
David Tolnaye4482f42019-04-22 16:15:14 -070083fn literal_string() {
Alex Crichton1a7f7622017-07-05 17:47:15 -070084 assert_eq!(Literal::string("foo").to_string(), "\"foo\"");
85 assert_eq!(Literal::string("\"").to_string(), "\"\\\"\"");
David Tolnaye4482f42019-04-22 16:15:14 -070086 assert_eq!(Literal::string("didn't").to_string(), "\"didn't\"");
87}
88
89#[test]
90fn literal_character() {
91 assert_eq!(Literal::character('x').to_string(), "'x'");
92 assert_eq!(Literal::character('\'').to_string(), "'\\''");
93 assert_eq!(Literal::character('"').to_string(), "'\"'");
94}
95
96#[test]
97fn literal_float() {
Alex Crichtonaf5bad42018-03-27 14:45:10 -070098 assert_eq!(Literal::f32_unsuffixed(10.0).to_string(), "10.0");
David Tolnaye5806852017-06-01 12:49:20 -070099}
100
101#[test]
102fn roundtrip() {
103 fn roundtrip(p: &str) {
104 println!("parse: {}", p);
105 let s = p.parse::<TokenStream>().unwrap().to_string();
106 println!("first: {}", s);
107 let s2 = s.to_string().parse::<TokenStream>().unwrap().to_string();
108 assert_eq!(s, s2);
109 }
110 roundtrip("a");
111 roundtrip("<<");
112 roundtrip("<<=");
David Tolnayb28f38a2018-03-31 22:02:29 +0200113 roundtrip(
114 "
David Tolnaye5806852017-06-01 12:49:20 -0700115 1
116 1.0
117 1f32
118 2f64
119 1usize
120 4isize
121 4e10
122 1_000
123 1_0i32
124 8u8
125 9
126 0
127 0xffffffffffffffffffffffffffffffff
David Tolnayb28f38a2018-03-31 22:02:29 +0200128 ",
129 );
David Tolnaye5806852017-06-01 12:49:20 -0700130 roundtrip("'a");
Alex Crichtonf5479a92018-05-17 10:56:26 -0700131 roundtrip("'_");
David Tolnaye5806852017-06-01 12:49:20 -0700132 roundtrip("'static");
David Tolnay8d109342017-12-25 18:24:45 -0500133 roundtrip("'\\u{10__FFFF}'");
134 roundtrip("\"\\u{10_F0FF__}foo\\u{1_0_0_0__}\"");
David Tolnaye5806852017-06-01 12:49:20 -0700135}
136
137#[test]
138fn fail() {
139 fn fail(p: &str) {
Alex Crichtonf3888432018-05-16 09:11:05 -0700140 if let Ok(s) = p.parse::<TokenStream>() {
141 panic!("should have failed to parse: {}\n{:#?}", p, s);
David Tolnaye5806852017-06-01 12:49:20 -0700142 }
143 }
144 fail("1x");
145 fail("1u80");
146 fail("1f320");
147 fail("' static");
David Tolnaya13d1422018-03-31 21:27:48 +0200148 fail("r#1");
149 fail("r#_");
David Tolnaye5806852017-06-01 12:49:20 -0700150}
Nika Layzellf8d5f212017-12-11 14:07:02 -0500151
David Tolnay3b1f7d22019-01-28 12:22:11 -0800152#[cfg(span_locations)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500153#[test]
154fn span_test() {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700155 use proc_macro2::TokenTree;
156
Nika Layzellf8d5f212017-12-11 14:07:02 -0500157 fn check_spans(p: &str, mut lines: &[(usize, usize, usize, usize)]) {
Nika Layzellf8d5f212017-12-11 14:07:02 -0500158 let ts = p.parse::<TokenStream>().unwrap();
159 check_spans_internal(ts, &mut lines);
160 }
161
David Tolnayb28f38a2018-03-31 22:02:29 +0200162 fn check_spans_internal(ts: TokenStream, lines: &mut &[(usize, usize, usize, usize)]) {
Nika Layzellf8d5f212017-12-11 14:07:02 -0500163 for i in ts {
164 if let Some((&(sline, scol, eline, ecol), rest)) = lines.split_first() {
165 *lines = rest;
166
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700167 let start = i.span().start();
Nika Layzellf8d5f212017-12-11 14:07:02 -0500168 assert_eq!(start.line, sline, "sline did not match for {}", i);
169 assert_eq!(start.column, scol, "scol did not match for {}", i);
170
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700171 let end = i.span().end();
Nika Layzellf8d5f212017-12-11 14:07:02 -0500172 assert_eq!(end.line, eline, "eline did not match for {}", i);
173 assert_eq!(end.column, ecol, "ecol did not match for {}", i);
174
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700175 match i {
176 TokenTree::Group(ref g) => {
177 check_spans_internal(g.stream().clone(), lines);
178 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500179 _ => {}
180 }
181 }
182 }
183 }
184
David Tolnayb28f38a2018-03-31 22:02:29 +0200185 check_spans(
186 "\
Nika Layzellf8d5f212017-12-11 14:07:02 -0500187/// This is a document comment
188testing 123
189{
190 testing 234
David Tolnayb28f38a2018-03-31 22:02:29 +0200191}",
192 &[
Alex Crichton1eb96a02018-04-04 13:07:35 -0700193 (1, 0, 1, 30), // #
194 (1, 0, 1, 30), // [ ... ]
195 (1, 0, 1, 30), // doc
196 (1, 0, 1, 30), // =
197 (1, 0, 1, 30), // "This is..."
198 (2, 0, 2, 7), // testing
199 (2, 8, 2, 11), // 123
200 (3, 0, 5, 1), // { ... }
201 (4, 2, 4, 9), // testing
202 (4, 10, 4, 13), // 234
David Tolnayb28f38a2018-03-31 22:02:29 +0200203 ],
204 );
Nika Layzellf8d5f212017-12-11 14:07:02 -0500205}
206
David Tolnay1ebe3972018-01-02 20:14:20 -0800207#[cfg(procmacro2_semver_exempt)]
David Tolnay17eb0702019-01-05 12:23:17 -0800208#[cfg(not(nightly))]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500209#[test]
210fn default_span() {
211 let start = Span::call_site().start();
212 assert_eq!(start.line, 1);
213 assert_eq!(start.column, 0);
214 let end = Span::call_site().end();
215 assert_eq!(end.line, 1);
216 assert_eq!(end.column, 0);
217 let source_file = Span::call_site().source_file();
David Tolnay9cd3b4c2018-11-11 16:47:32 -0800218 assert_eq!(source_file.path().to_string_lossy(), "<unspecified>");
Nika Layzellf8d5f212017-12-11 14:07:02 -0500219 assert!(!source_file.is_real());
220}
221
David Tolnay1ebe3972018-01-02 20:14:20 -0800222#[cfg(procmacro2_semver_exempt)]
Nika Layzellddea1562017-12-11 14:25:35 -0500223#[test]
224fn span_join() {
David Tolnayb28f38a2018-03-31 22:02:29 +0200225 let source1 = "aaa\nbbb"
226 .parse::<TokenStream>()
227 .unwrap()
228 .into_iter()
229 .collect::<Vec<_>>();
230 let source2 = "ccc\nddd"
231 .parse::<TokenStream>()
232 .unwrap()
233 .into_iter()
234 .collect::<Vec<_>>();
Nika Layzellddea1562017-12-11 14:25:35 -0500235
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700236 assert!(source1[0].span().source_file() != source2[0].span().source_file());
David Tolnayb28f38a2018-03-31 22:02:29 +0200237 assert_eq!(
238 source1[0].span().source_file(),
239 source1[1].span().source_file()
240 );
Nika Layzellddea1562017-12-11 14:25:35 -0500241
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700242 let joined1 = source1[0].span().join(source1[1].span());
243 let joined2 = source1[0].span().join(source2[0].span());
Nika Layzellddea1562017-12-11 14:25:35 -0500244 assert!(joined1.is_some());
245 assert!(joined2.is_none());
246
247 let start = joined1.unwrap().start();
248 let end = joined1.unwrap().end();
249 assert_eq!(start.line, 1);
250 assert_eq!(start.column, 0);
251 assert_eq!(end.line, 2);
252 assert_eq!(end.column, 3);
253
David Tolnayb28f38a2018-03-31 22:02:29 +0200254 assert_eq!(
255 joined1.unwrap().source_file(),
256 source1[0].span().source_file()
257 );
Nika Layzellddea1562017-12-11 14:25:35 -0500258}
Alex Crichton8c030332018-01-16 08:07:36 -0800259
260#[test]
261fn no_panic() {
262 let s = str::from_utf8(b"b\'\xc2\x86 \x00\x00\x00^\"").unwrap();
263 assert!(s.parse::<proc_macro2::TokenStream>().is_err());
264}
265
Alex Crichtonf7df57c2018-01-21 21:05:11 -0800266#[test]
David Tolnay639e4ba2018-03-31 21:10:55 +0200267fn tricky_doc_comment() {
Alex Crichtonf7df57c2018-01-21 21:05:11 -0800268 let stream = "/**/".parse::<proc_macro2::TokenStream>().unwrap();
269 let tokens = stream.into_iter().collect::<Vec<_>>();
270 assert!(tokens.is_empty(), "not empty -- {:?}", tokens);
Alex Crichtond7904e52018-01-23 11:08:45 -0800271
272 let stream = "/// doc".parse::<proc_macro2::TokenStream>().unwrap();
273 let tokens = stream.into_iter().collect::<Vec<_>>();
Alex Crichton1eb96a02018-04-04 13:07:35 -0700274 assert!(tokens.len() == 2, "not length 2 -- {:?}", tokens);
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700275 match tokens[0] {
Alex Crichtonf3888432018-05-16 09:11:05 -0700276 proc_macro2::TokenTree::Punct(ref tt) => assert_eq!(tt.as_char(), '#'),
Alex Crichtond7904e52018-01-23 11:08:45 -0800277 _ => panic!("wrong token {:?}", tokens[0]),
278 }
Alex Crichton1eb96a02018-04-04 13:07:35 -0700279 let mut tokens = match tokens[1] {
280 proc_macro2::TokenTree::Group(ref tt) => {
281 assert_eq!(tt.delimiter(), proc_macro2::Delimiter::Bracket);
282 tt.stream().into_iter()
283 }
284 _ => panic!("wrong token {:?}", tokens[0]),
285 };
286
287 match tokens.next().unwrap() {
Alex Crichtonf3888432018-05-16 09:11:05 -0700288 proc_macro2::TokenTree::Ident(ref tt) => assert_eq!(tt.to_string(), "doc"),
Alex Crichton1eb96a02018-04-04 13:07:35 -0700289 t => panic!("wrong token {:?}", t),
290 }
291 match tokens.next().unwrap() {
Alex Crichtonf3888432018-05-16 09:11:05 -0700292 proc_macro2::TokenTree::Punct(ref tt) => assert_eq!(tt.as_char(), '='),
Alex Crichton1eb96a02018-04-04 13:07:35 -0700293 t => panic!("wrong token {:?}", t),
294 }
295 match tokens.next().unwrap() {
296 proc_macro2::TokenTree::Literal(ref tt) => {
297 assert_eq!(tt.to_string(), "\" doc\"");
298 }
299 t => panic!("wrong token {:?}", t),
300 }
301 assert!(tokens.next().is_none());
302
303 let stream = "//! doc".parse::<proc_macro2::TokenStream>().unwrap();
304 let tokens = stream.into_iter().collect::<Vec<_>>();
305 assert!(tokens.len() == 3, "not length 3 -- {:?}", tokens);
Alex Crichtonf7df57c2018-01-21 21:05:11 -0800306}
307
David Tolnaya13d1422018-03-31 21:27:48 +0200308#[test]
David Tolnay3a592ad2018-04-22 21:20:24 -0700309fn op_before_comment() {
310 let mut tts = TokenStream::from_str("~// comment").unwrap().into_iter();
311 match tts.next().unwrap() {
Alex Crichtonf3888432018-05-16 09:11:05 -0700312 TokenTree::Punct(tt) => {
313 assert_eq!(tt.as_char(), '~');
David Tolnay3a592ad2018-04-22 21:20:24 -0700314 assert_eq!(tt.spacing(), Spacing::Alone);
315 }
316 wrong => panic!("wrong token {:?}", wrong),
317 }
318}
319
320#[test]
David Tolnaya13d1422018-03-31 21:27:48 +0200321fn raw_identifier() {
322 let mut tts = TokenStream::from_str("r#dyn").unwrap().into_iter();
323 match tts.next().unwrap() {
Alex Crichtonf3888432018-05-16 09:11:05 -0700324 TokenTree::Ident(raw) => assert_eq!("r#dyn", raw.to_string()),
David Tolnaya13d1422018-03-31 21:27:48 +0200325 wrong => panic!("wrong token {:?}", wrong),
326 }
327 assert!(tts.next().is_none());
328}
David Tolnay034205f2018-04-22 16:45:28 -0700329
330#[test]
David Tolnayd8fcdb82018-06-02 15:43:53 -0700331fn test_debug_ident() {
332 let ident = Ident::new("proc_macro", Span::call_site());
333
334 #[cfg(not(procmacro2_semver_exempt))]
335 let expected = "Ident(proc_macro)";
336
337 #[cfg(procmacro2_semver_exempt)]
338 let expected = "Ident { sym: proc_macro, span: bytes(0..0) }";
339
340 assert_eq!(expected, format!("{:?}", ident));
341}
342
343#[test]
344fn test_debug_tokenstream() {
David Tolnay034205f2018-04-22 16:45:28 -0700345 let tts = TokenStream::from_str("[a + 1]").unwrap();
346
347 #[cfg(not(procmacro2_semver_exempt))]
348 let expected = "\
349TokenStream [
350 Group {
351 delimiter: Bracket,
352 stream: TokenStream [
Alex Crichtonf3888432018-05-16 09:11:05 -0700353 Ident {
David Tolnay5a2f7302019-04-10 15:57:36 -0700354 sym: a,
355 },
356 Punct {
357 op: '+',
358 spacing: Alone,
359 },
360 Literal {
361 lit: 1,
362 },
363 ],
364 },
365]\
366 ";
367
368 #[cfg(not(procmacro2_semver_exempt))]
369 let expected_before_trailing_commas = "\
370TokenStream [
371 Group {
372 delimiter: Bracket,
373 stream: TokenStream [
374 Ident {
David Tolnayd8fcdb82018-06-02 15:43:53 -0700375 sym: a
David Tolnay034205f2018-04-22 16:45:28 -0700376 },
Alex Crichtonf3888432018-05-16 09:11:05 -0700377 Punct {
David Tolnay034205f2018-04-22 16:45:28 -0700378 op: '+',
379 spacing: Alone
380 },
381 Literal {
382 lit: 1
383 }
384 ]
385 }
386]\
387 ";
388
389 #[cfg(procmacro2_semver_exempt)]
390 let expected = "\
391TokenStream [
392 Group {
393 delimiter: Bracket,
394 stream: TokenStream [
Alex Crichtonf3888432018-05-16 09:11:05 -0700395 Ident {
David Tolnay034205f2018-04-22 16:45:28 -0700396 sym: a,
David Tolnay5a2f7302019-04-10 15:57:36 -0700397 span: bytes(2..3),
398 },
399 Punct {
400 op: '+',
401 spacing: Alone,
402 span: bytes(4..5),
403 },
404 Literal {
405 lit: 1,
406 span: bytes(6..7),
407 },
408 ],
409 span: bytes(1..8),
410 },
411]\
412 ";
413
414 #[cfg(procmacro2_semver_exempt)]
415 let expected_before_trailing_commas = "\
416TokenStream [
417 Group {
418 delimiter: Bracket,
419 stream: TokenStream [
420 Ident {
421 sym: a,
David Tolnayd8fcdb82018-06-02 15:43:53 -0700422 span: bytes(2..3)
David Tolnay034205f2018-04-22 16:45:28 -0700423 },
Alex Crichtonf3888432018-05-16 09:11:05 -0700424 Punct {
David Tolnay034205f2018-04-22 16:45:28 -0700425 op: '+',
426 spacing: Alone,
427 span: bytes(4..5)
428 },
429 Literal {
430 lit: 1,
431 span: bytes(6..7)
432 }
433 ],
434 span: bytes(1..8)
435 }
436]\
437 ";
438
David Tolnay5a2f7302019-04-10 15:57:36 -0700439 let actual = format!("{:#?}", tts);
440 if actual.ends_with(",\n]") {
441 assert_eq!(expected, actual);
442 } else {
443 assert_eq!(expected_before_trailing_commas, actual);
444 }
David Tolnay034205f2018-04-22 16:45:28 -0700445}
Árpád Goretity4f74b682018-07-14 00:47:51 +0200446
447#[test]
448fn default_tokenstream_is_empty() {
449 let default_token_stream: TokenStream = Default::default();
450
451 assert!(default_token_stream.is_empty());
452}