]> git.lizzy.rs Git - rust.git/blob - crates/parser/src/tests/top_entries.rs
Merge #11481
[rust.git] / crates / parser / src / tests / top_entries.rs
1 use expect_test::expect;
2
3 use crate::TopEntryPoint;
4
5 #[test]
6 fn source_file() {
7     check(
8         TopEntryPoint::SourceFile,
9         "",
10         expect![[r#"
11         SOURCE_FILE
12     "#]],
13     );
14
15     check(
16         TopEntryPoint::SourceFile,
17         "struct S;",
18         expect![[r#"
19         SOURCE_FILE
20           STRUCT
21             STRUCT_KW "struct"
22             WHITESPACE " "
23             NAME
24               IDENT "S"
25             SEMICOLON ";"
26     "#]],
27     );
28
29     check(
30         TopEntryPoint::SourceFile,
31         "@error@",
32         expect![[r#"
33         SOURCE_FILE
34           ERROR
35             AT "@"
36           MACRO_CALL
37             PATH
38               PATH_SEGMENT
39                 NAME_REF
40                   IDENT "error"
41           ERROR
42             AT "@"
43         error 0: expected an item
44         error 6: expected BANG
45         error 6: expected `{`, `[`, `(`
46         error 6: expected SEMICOLON
47         error 6: expected an item
48     "#]],
49     );
50 }
51
52 #[test]
53 fn macro_stmt() {
54     check(
55         TopEntryPoint::MacroStmts,
56         "",
57         expect![[r#"
58             MACRO_STMTS
59         "#]],
60     );
61     check(
62         TopEntryPoint::MacroStmts,
63         "#!/usr/bin/rust",
64         expect![[r##"
65             MACRO_STMTS
66               ERROR
67                 SHEBANG "#!/usr/bin/rust"
68             error 0: expected expression
69         "##]],
70     );
71     check(
72         TopEntryPoint::MacroStmts,
73         "let x = 1 2 struct S;",
74         expect![[r#"
75             MACRO_STMTS
76               LET_STMT
77                 LET_KW "let"
78                 WHITESPACE " "
79                 IDENT_PAT
80                   NAME
81                     IDENT "x"
82                 WHITESPACE " "
83                 EQ "="
84                 WHITESPACE " "
85                 LITERAL
86                   INT_NUMBER "1"
87               WHITESPACE " "
88               EXPR_STMT
89                 LITERAL
90                   INT_NUMBER "2"
91               WHITESPACE " "
92               STRUCT
93                 STRUCT_KW "struct"
94                 WHITESPACE " "
95                 NAME
96                   IDENT "S"
97                 SEMICOLON ";"
98         "#]],
99     );
100 }
101
102 #[test]
103 fn macro_items() {
104     check(
105         TopEntryPoint::MacroItems,
106         "",
107         expect![[r#"
108             MACRO_ITEMS
109         "#]],
110     );
111     check(
112         TopEntryPoint::MacroItems,
113         "#!/usr/bin/rust",
114         expect![[r##"
115             MACRO_ITEMS
116               ERROR
117                 SHEBANG "#!/usr/bin/rust"
118             error 0: expected an item
119         "##]],
120     );
121     check(
122         TopEntryPoint::MacroItems,
123         "struct S; foo!{}",
124         expect![[r#"
125             MACRO_ITEMS
126               STRUCT
127                 STRUCT_KW "struct"
128                 WHITESPACE " "
129                 NAME
130                   IDENT "S"
131                 SEMICOLON ";"
132               WHITESPACE " "
133               MACRO_CALL
134                 PATH
135                   PATH_SEGMENT
136                     NAME_REF
137                       IDENT "foo"
138                 BANG "!"
139                 TOKEN_TREE
140                   L_CURLY "{"
141                   R_CURLY "}"
142         "#]],
143     );
144 }
145
146 #[test]
147 fn macro_pattern() {
148     check(
149         TopEntryPoint::Pattern,
150         "",
151         expect![[r#"
152             ERROR
153             error 0: expected pattern
154         "#]],
155     );
156     check(
157         TopEntryPoint::Pattern,
158         "Some(_)",
159         expect![[r#"
160             TUPLE_STRUCT_PAT
161               PATH
162                 PATH_SEGMENT
163                   NAME_REF
164                     IDENT "Some"
165               L_PAREN "("
166               WILDCARD_PAT
167                 UNDERSCORE "_"
168               R_PAREN ")"
169         "#]],
170     );
171
172     check(
173         TopEntryPoint::Pattern,
174         "None leftover tokens",
175         expect![[r#"
176             ERROR
177               IDENT_PAT
178                 NAME
179                   IDENT "None"
180               WHITESPACE " "
181               IDENT "leftover"
182               WHITESPACE " "
183               IDENT "tokens"
184         "#]],
185     );
186
187     check(
188         TopEntryPoint::Pattern,
189         "@err",
190         expect![[r#"
191             ERROR
192               ERROR
193                 AT "@"
194               IDENT "err"
195             error 0: expected pattern
196         "#]],
197     );
198 }
199
200 #[test]
201 fn type_() {
202     check(
203         TopEntryPoint::Type,
204         "",
205         expect![[r#"
206             ERROR
207             error 0: expected type
208         "#]],
209     );
210
211     check(
212         TopEntryPoint::Type,
213         "Option<!>",
214         expect![[r#"
215             PATH_TYPE
216               PATH
217                 PATH_SEGMENT
218                   NAME_REF
219                     IDENT "Option"
220                   GENERIC_ARG_LIST
221                     L_ANGLE "<"
222                     TYPE_ARG
223                       NEVER_TYPE
224                         BANG "!"
225                     R_ANGLE ">"
226         "#]],
227     );
228     check(
229         TopEntryPoint::Type,
230         "() () ()",
231         expect![[r#"
232             ERROR
233               TUPLE_TYPE
234                 L_PAREN "("
235                 R_PAREN ")"
236               WHITESPACE " "
237               L_PAREN "("
238               R_PAREN ")"
239               WHITESPACE " "
240               L_PAREN "("
241               R_PAREN ")"
242         "#]],
243     );
244     check(
245         TopEntryPoint::Type,
246         "$$$",
247         expect![[r#"
248             ERROR
249               ERROR
250                 DOLLAR "$"
251               DOLLAR "$"
252               DOLLAR "$"
253             error 0: expected type
254         "#]],
255     );
256 }
257
258 #[test]
259 fn expr() {
260     check(
261         TopEntryPoint::Expr,
262         "",
263         expect![[r#"
264             ERROR
265             error 0: expected expression
266         "#]],
267     );
268     check(
269         TopEntryPoint::Expr,
270         "2 + 2 == 5",
271         expect![[r#"
272         BIN_EXPR
273           BIN_EXPR
274             LITERAL
275               INT_NUMBER "2"
276             WHITESPACE " "
277             PLUS "+"
278             WHITESPACE " "
279             LITERAL
280               INT_NUMBER "2"
281           WHITESPACE " "
282           EQ2 "=="
283           WHITESPACE " "
284           LITERAL
285             INT_NUMBER "5"
286     "#]],
287     );
288     check(
289         TopEntryPoint::Expr,
290         "let _ = 0;",
291         expect![[r#"
292             ERROR
293               LET_EXPR
294                 LET_KW "let"
295                 WHITESPACE " "
296                 WILDCARD_PAT
297                   UNDERSCORE "_"
298                 WHITESPACE " "
299                 EQ "="
300                 WHITESPACE " "
301                 LITERAL
302                   INT_NUMBER "0"
303               SEMICOLON ";"
304         "#]],
305     );
306 }
307
308 #[track_caller]
309 fn check(entry: TopEntryPoint, input: &str, expect: expect_test::Expect) {
310     let (parsed, _errors) = super::parse(entry, input);
311     expect.assert_eq(&parsed)
312 }