diff options
author | darksv <[email protected]> | 2018-09-15 13:35:30 +0100 |
---|---|---|
committer | darksv <[email protected]> | 2018-09-15 13:35:30 +0100 |
commit | 46cee0415c957f3262ad68cbe617b642f29f347e (patch) | |
tree | d87e17ab13142a1a2d60b1d8012c3b09cc7f2815 /crates/libsyntax2 | |
parent | 16ad5384f0c0427922b2d2d05025412fc8e4addf (diff) |
move reparsing tests
Diffstat (limited to 'crates/libsyntax2')
-rw-r--r-- | crates/libsyntax2/src/lib.rs | 4 | ||||
-rw-r--r-- | crates/libsyntax2/src/reparsing.rs | 135 | ||||
-rw-r--r-- | crates/libsyntax2/tests/test/main.rs | 130 |
3 files changed, 140 insertions, 129 deletions
diff --git a/crates/libsyntax2/src/lib.rs b/crates/libsyntax2/src/lib.rs index 014cdafee..886195660 100644 --- a/crates/libsyntax2/src/lib.rs +++ b/crates/libsyntax2/src/lib.rs | |||
@@ -27,6 +27,10 @@ extern crate parking_lot; | |||
27 | extern crate smol_str; | 27 | extern crate smol_str; |
28 | extern crate text_unit; | 28 | extern crate text_unit; |
29 | 29 | ||
30 | #[cfg(test)] | ||
31 | #[macro_use] | ||
32 | extern crate test_utils; | ||
33 | |||
30 | pub mod algo; | 34 | pub mod algo; |
31 | pub mod ast; | 35 | pub mod ast; |
32 | mod lexer; | 36 | mod lexer; |
diff --git a/crates/libsyntax2/src/reparsing.rs b/crates/libsyntax2/src/reparsing.rs index 723ea2b8b..1df2ca094 100644 --- a/crates/libsyntax2/src/reparsing.rs +++ b/crates/libsyntax2/src/reparsing.rs | |||
@@ -186,3 +186,138 @@ fn merge_errors( | |||
186 | } | 186 | } |
187 | res | 187 | res |
188 | } | 188 | } |
189 | |||
190 | #[cfg(test)] | ||
191 | use super::{File, text_utils, test_utils, utils}; | ||
192 | |||
193 | #[cfg(test)] | ||
194 | mod tests { | ||
195 | use super::{*, utils::dump_tree}; | ||
196 | |||
197 | #[test] | ||
198 | fn reparse_test() { | ||
199 | fn do_check(before: &str, replace_with: &str) { | ||
200 | let (range, before) = test_utils::extract_range(before); | ||
201 | let after = text_utils::replace_range(before.clone(), range, replace_with); | ||
202 | |||
203 | let fully_reparsed = File::parse(&after); | ||
204 | let incrementally_reparsed = { | ||
205 | let f = File::parse(&before); | ||
206 | let edit = AtomEdit { delete: range, insert: replace_with.to_string() }; | ||
207 | f.incremental_reparse(&edit).expect("cannot incrementally reparse") | ||
208 | }; | ||
209 | assert_eq_text!( | ||
210 | &dump_tree(fully_reparsed.syntax()), | ||
211 | &dump_tree(incrementally_reparsed.syntax()), | ||
212 | ) | ||
213 | } | ||
214 | |||
215 | do_check(r" | ||
216 | fn foo() { | ||
217 | let x = foo + <|>bar<|> | ||
218 | } | ||
219 | ", "baz"); | ||
220 | do_check(r" | ||
221 | fn foo() { | ||
222 | let x = foo<|> + bar<|> | ||
223 | } | ||
224 | ", "baz"); | ||
225 | do_check(r" | ||
226 | struct Foo { | ||
227 | f: foo<|><|> | ||
228 | } | ||
229 | ", ",\n g: (),"); | ||
230 | do_check(r" | ||
231 | fn foo { | ||
232 | let; | ||
233 | 1 + 1; | ||
234 | <|>92<|>; | ||
235 | } | ||
236 | ", "62"); | ||
237 | do_check(r" | ||
238 | mod foo { | ||
239 | fn <|><|> | ||
240 | } | ||
241 | ", "bar"); | ||
242 | do_check(r" | ||
243 | trait Foo { | ||
244 | type <|>Foo<|>; | ||
245 | } | ||
246 | ", "Output"); | ||
247 | do_check(r" | ||
248 | trait Foo { | ||
249 | type<|> Foo<|>; | ||
250 | } | ||
251 | ", "Output"); | ||
252 | do_check(r" | ||
253 | impl IntoIterator<Item=i32> for Foo { | ||
254 | f<|><|> | ||
255 | } | ||
256 | ", "n next("); | ||
257 | do_check(r" | ||
258 | use a::b::{foo,<|>,bar<|>}; | ||
259 | ", "baz"); | ||
260 | do_check(r" | ||
261 | pub enum A { | ||
262 | Foo<|><|> | ||
263 | } | ||
264 | ", "\nBar;\n"); | ||
265 | do_check(r" | ||
266 | foo!{a, b<|><|> d} | ||
267 | ", ", c[3]"); | ||
268 | do_check(r" | ||
269 | fn foo() { | ||
270 | vec![<|><|>] | ||
271 | } | ||
272 | ", "123"); | ||
273 | do_check(r" | ||
274 | extern { | ||
275 | fn<|>;<|> | ||
276 | } | ||
277 | ", " exit(code: c_int)"); | ||
278 | do_check(r"<|><|> | ||
279 | fn foo() -> i32 { | ||
280 | 1 | ||
281 | } | ||
282 | ", "\n\n\n \n"); | ||
283 | do_check(r" | ||
284 | fn foo() -> <|><|> {} | ||
285 | ", " \n"); | ||
286 | do_check(r" | ||
287 | fn <|>foo<|>() -> i32 { | ||
288 | 1 | ||
289 | } | ||
290 | ", "bar"); | ||
291 | do_check(r" | ||
292 | fn aa<|><|>bb() { | ||
293 | |||
294 | } | ||
295 | ", "foofoo"); | ||
296 | do_check(r" | ||
297 | fn aabb /* <|><|> */ () { | ||
298 | |||
299 | } | ||
300 | ", "some comment"); | ||
301 | do_check(r" | ||
302 | fn aabb <|><|> () { | ||
303 | |||
304 | } | ||
305 | ", " \t\t\n\n"); | ||
306 | do_check(r" | ||
307 | trait foo { | ||
308 | // comment <|><|> | ||
309 | } | ||
310 | ", "\n"); | ||
311 | do_check(r" | ||
312 | /// good <|><|>omment | ||
313 | mod { | ||
314 | } | ||
315 | ", "c"); | ||
316 | do_check(r#" | ||
317 | fn -> &str { "Hello<|><|>" } | ||
318 | "#, ", world"); | ||
319 | do_check(r#" | ||
320 | fn -> &str { // "Hello<|><|>" | ||
321 | "#, ", world"); | ||
322 | } | ||
323 | } \ No newline at end of file | ||
diff --git a/crates/libsyntax2/tests/test/main.rs b/crates/libsyntax2/tests/test/main.rs index 2b68673c6..5a8879fce 100644 --- a/crates/libsyntax2/tests/test/main.rs +++ b/crates/libsyntax2/tests/test/main.rs | |||
@@ -9,9 +9,8 @@ use std::{ | |||
9 | fmt::Write, | 9 | fmt::Write, |
10 | }; | 10 | }; |
11 | 11 | ||
12 | use test_utils::extract_range; | ||
13 | use libsyntax2::{ | 12 | use libsyntax2::{ |
14 | File, AtomEdit, | 13 | File, |
15 | utils::{dump_tree, check_fuzz_invariants}, | 14 | utils::{dump_tree, check_fuzz_invariants}, |
16 | }; | 15 | }; |
17 | 16 | ||
@@ -24,133 +23,6 @@ fn lexer_tests() { | |||
24 | } | 23 | } |
25 | 24 | ||
26 | #[test] | 25 | #[test] |
27 | fn reparse_test() { | ||
28 | fn do_check(before: &str, replace_with: &str) { | ||
29 | let (range, before) = extract_range(before); | ||
30 | let after = libsyntax2::text_utils::replace_range(before.clone(), range, replace_with); | ||
31 | |||
32 | let fully_reparsed = File::parse(&after); | ||
33 | let incrementally_reparsed = { | ||
34 | let f = File::parse(&before); | ||
35 | let edit = AtomEdit { delete: range, insert: replace_with.to_string() }; | ||
36 | f.incremental_reparse(&edit).expect("cannot incrementally reparse") | ||
37 | }; | ||
38 | assert_eq_text!( | ||
39 | &dump_tree(fully_reparsed.syntax()), | ||
40 | &dump_tree(incrementally_reparsed.syntax()), | ||
41 | ) | ||
42 | } | ||
43 | |||
44 | do_check(r" | ||
45 | fn foo() { | ||
46 | let x = foo + <|>bar<|> | ||
47 | } | ||
48 | ", "baz"); | ||
49 | do_check(r" | ||
50 | fn foo() { | ||
51 | let x = foo<|> + bar<|> | ||
52 | } | ||
53 | ", "baz"); | ||
54 | do_check(r" | ||
55 | struct Foo { | ||
56 | f: foo<|><|> | ||
57 | } | ||
58 | ", ",\n g: (),"); | ||
59 | do_check(r" | ||
60 | fn foo { | ||
61 | let; | ||
62 | 1 + 1; | ||
63 | <|>92<|>; | ||
64 | } | ||
65 | ", "62"); | ||
66 | do_check(r" | ||
67 | mod foo { | ||
68 | fn <|><|> | ||
69 | } | ||
70 | ", "bar"); | ||
71 | do_check(r" | ||
72 | trait Foo { | ||
73 | type <|>Foo<|>; | ||
74 | } | ||
75 | ", "Output"); | ||
76 | do_check(r" | ||
77 | trait Foo { | ||
78 | type<|> Foo<|>; | ||
79 | } | ||
80 | ", "Output"); | ||
81 | do_check(r" | ||
82 | impl IntoIterator<Item=i32> for Foo { | ||
83 | f<|><|> | ||
84 | } | ||
85 | ", "n next("); | ||
86 | do_check(r" | ||
87 | use a::b::{foo,<|>,bar<|>}; | ||
88 | ", "baz"); | ||
89 | do_check(r" | ||
90 | pub enum A { | ||
91 | Foo<|><|> | ||
92 | } | ||
93 | ", "\nBar;\n"); | ||
94 | do_check(r" | ||
95 | foo!{a, b<|><|> d} | ||
96 | ", ", c[3]"); | ||
97 | do_check(r" | ||
98 | fn foo() { | ||
99 | vec![<|><|>] | ||
100 | } | ||
101 | ", "123"); | ||
102 | do_check(r" | ||
103 | extern { | ||
104 | fn<|>;<|> | ||
105 | } | ||
106 | ", " exit(code: c_int)"); | ||
107 | do_check(r"<|><|> | ||
108 | fn foo() -> i32 { | ||
109 | 1 | ||
110 | } | ||
111 | ", "\n\n\n \n"); | ||
112 | do_check(r" | ||
113 | fn foo() -> <|><|> {} | ||
114 | ", " \n"); | ||
115 | do_check(r" | ||
116 | fn <|>foo<|>() -> i32 { | ||
117 | 1 | ||
118 | } | ||
119 | ", "bar"); | ||
120 | do_check(r" | ||
121 | fn aa<|><|>bb() { | ||
122 | |||
123 | } | ||
124 | ", "foofoo"); | ||
125 | do_check(r" | ||
126 | fn aabb /* <|><|> */ () { | ||
127 | |||
128 | } | ||
129 | ", "some comment"); | ||
130 | do_check(r" | ||
131 | fn aabb <|><|> () { | ||
132 | |||
133 | } | ||
134 | ", " \t\t\n\n"); | ||
135 | do_check(r" | ||
136 | trait foo { | ||
137 | // comment <|><|> | ||
138 | } | ||
139 | ", "\n"); | ||
140 | do_check(r" | ||
141 | /// good <|><|>omment | ||
142 | mod { | ||
143 | } | ||
144 | ", "c"); | ||
145 | do_check(r#" | ||
146 | fn -> &str { "Hello<|><|>" } | ||
147 | "#, ", world"); | ||
148 | do_check(r#" | ||
149 | fn -> &str { // "Hello<|><|>" | ||
150 | "#, ", world"); | ||
151 | } | ||
152 | |||
153 | #[test] | ||
154 | fn parser_tests() { | 26 | fn parser_tests() { |
155 | dir_tests(&["parser/inline", "parser/ok", "parser/err"], |text| { | 27 | dir_tests(&["parser/inline", "parser/ok", "parser/err"], |text| { |
156 | let file = File::parse(text); | 28 | let file = File::parse(text); |