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