]> git.proxmox.com Git - rustc.git/blob - src/libsyntax_expand/tokenstream/tests.rs
New upstream version 1.41.1+dfsg1
[rustc.git] / src / libsyntax_expand / tokenstream / tests.rs
1 use crate::tests::string_to_stream;
2
3 use syntax::ast::Name;
4 use syntax::token;
5 use syntax::tokenstream::{TokenStream, TokenStreamBuilder, TokenTree};
6 use syntax::with_default_globals;
7 use syntax_pos::{Span, BytePos};
8 use smallvec::smallvec;
9
10 fn string_to_ts(string: &str) -> TokenStream {
11 string_to_stream(string.to_owned())
12 }
13
14 fn sp(a: u32, b: u32) -> Span {
15 Span::with_root_ctxt(BytePos(a), BytePos(b))
16 }
17
18 #[test]
19 fn test_concat() {
20 with_default_globals(|| {
21 let test_res = string_to_ts("foo::bar::baz");
22 let test_fst = string_to_ts("foo::bar");
23 let test_snd = string_to_ts("::baz");
24 let eq_res = TokenStream::from_streams(smallvec![test_fst, test_snd]);
25 assert_eq!(test_res.trees().count(), 5);
26 assert_eq!(eq_res.trees().count(), 5);
27 assert_eq!(test_res.eq_unspanned(&eq_res), true);
28 })
29 }
30
31 #[test]
32 fn test_to_from_bijection() {
33 with_default_globals(|| {
34 let test_start = string_to_ts("foo::bar(baz)");
35 let test_end = test_start.trees().collect();
36 assert_eq!(test_start, test_end)
37 })
38 }
39
40 #[test]
41 fn test_eq_0() {
42 with_default_globals(|| {
43 let test_res = string_to_ts("foo");
44 let test_eqs = string_to_ts("foo");
45 assert_eq!(test_res, test_eqs)
46 })
47 }
48
49 #[test]
50 fn test_eq_1() {
51 with_default_globals(|| {
52 let test_res = string_to_ts("::bar::baz");
53 let test_eqs = string_to_ts("::bar::baz");
54 assert_eq!(test_res, test_eqs)
55 })
56 }
57
58 #[test]
59 fn test_eq_3() {
60 with_default_globals(|| {
61 let test_res = string_to_ts("");
62 let test_eqs = string_to_ts("");
63 assert_eq!(test_res, test_eqs)
64 })
65 }
66
67 #[test]
68 fn test_diseq_0() {
69 with_default_globals(|| {
70 let test_res = string_to_ts("::bar::baz");
71 let test_eqs = string_to_ts("bar::baz");
72 assert_eq!(test_res == test_eqs, false)
73 })
74 }
75
76 #[test]
77 fn test_diseq_1() {
78 with_default_globals(|| {
79 let test_res = string_to_ts("(bar,baz)");
80 let test_eqs = string_to_ts("bar,baz");
81 assert_eq!(test_res == test_eqs, false)
82 })
83 }
84
85 #[test]
86 fn test_is_empty() {
87 with_default_globals(|| {
88 let test0: TokenStream = Vec::<TokenTree>::new().into_iter().collect();
89 let test1: TokenStream =
90 TokenTree::token(token::Ident(Name::intern("a"), false), sp(0, 1)).into();
91 let test2 = string_to_ts("foo(bar::baz)");
92
93 assert_eq!(test0.is_empty(), true);
94 assert_eq!(test1.is_empty(), false);
95 assert_eq!(test2.is_empty(), false);
96 })
97 }
98
99 #[test]
100 fn test_dotdotdot() {
101 with_default_globals(|| {
102 let mut builder = TokenStreamBuilder::new();
103 builder.push(TokenTree::token(token::Dot, sp(0, 1)).joint());
104 builder.push(TokenTree::token(token::Dot, sp(1, 2)).joint());
105 builder.push(TokenTree::token(token::Dot, sp(2, 3)));
106 let stream = builder.build();
107 assert!(stream.eq_unspanned(&string_to_ts("...")));
108 assert_eq!(stream.trees().count(), 1);
109 })
110 }