diff options
author | Daiki Ihara <[email protected]> | 2020-12-03 15:05:39 +0000 |
---|---|---|
committer | Daiki Ihara <[email protected]> | 2020-12-03 15:05:39 +0000 |
commit | f48664068210b92f4884ee8e6fe8504dabcd4d9a (patch) | |
tree | d01570897ca422d64b66481f18bc78fa7afd1835 /crates/ide_db/src/helpers/insert_use | |
parent | 5a1306a43652d914035b2cf0b703f4bfd3451a33 (diff) |
Extract tests module to file in ide_db crate
Diffstat (limited to 'crates/ide_db/src/helpers/insert_use')
-rw-r--r-- | crates/ide_db/src/helpers/insert_use/tests.rs | 620 |
1 files changed, 620 insertions, 0 deletions
diff --git a/crates/ide_db/src/helpers/insert_use/tests.rs b/crates/ide_db/src/helpers/insert_use/tests.rs new file mode 100644 index 000000000..86bfa5b41 --- /dev/null +++ b/crates/ide_db/src/helpers/insert_use/tests.rs | |||
@@ -0,0 +1,620 @@ | |||
1 | use super::*; | ||
2 | |||
3 | use test_utils::assert_eq_text; | ||
4 | |||
5 | #[test] | ||
6 | fn insert_existing() { | ||
7 | check_full("std::fs", "use std::fs;", "use std::fs;") | ||
8 | } | ||
9 | |||
10 | #[test] | ||
11 | fn insert_start() { | ||
12 | check_none( | ||
13 | "std::bar::AA", | ||
14 | r" | ||
15 | use std::bar::B; | ||
16 | use std::bar::D; | ||
17 | use std::bar::F; | ||
18 | use std::bar::G;", | ||
19 | r" | ||
20 | use std::bar::AA; | ||
21 | use std::bar::B; | ||
22 | use std::bar::D; | ||
23 | use std::bar::F; | ||
24 | use std::bar::G;", | ||
25 | ) | ||
26 | } | ||
27 | |||
28 | #[test] | ||
29 | fn insert_start_indent() { | ||
30 | mark::check!(insert_use_indent_after); | ||
31 | check_none( | ||
32 | "std::bar::AA", | ||
33 | r" | ||
34 | use std::bar::B; | ||
35 | use std::bar::D;", | ||
36 | r" | ||
37 | use std::bar::AA; | ||
38 | use std::bar::B; | ||
39 | use std::bar::D;", | ||
40 | ) | ||
41 | } | ||
42 | |||
43 | #[test] | ||
44 | fn insert_middle() { | ||
45 | check_none( | ||
46 | "std::bar::EE", | ||
47 | r" | ||
48 | use std::bar::A; | ||
49 | use std::bar::D; | ||
50 | use std::bar::F; | ||
51 | use std::bar::G;", | ||
52 | r" | ||
53 | use std::bar::A; | ||
54 | use std::bar::D; | ||
55 | use std::bar::EE; | ||
56 | use std::bar::F; | ||
57 | use std::bar::G;", | ||
58 | ) | ||
59 | } | ||
60 | |||
61 | #[test] | ||
62 | fn insert_middle_indent() { | ||
63 | check_none( | ||
64 | "std::bar::EE", | ||
65 | r" | ||
66 | use std::bar::A; | ||
67 | use std::bar::D; | ||
68 | use std::bar::F; | ||
69 | use std::bar::G;", | ||
70 | r" | ||
71 | use std::bar::A; | ||
72 | use std::bar::D; | ||
73 | use std::bar::EE; | ||
74 | use std::bar::F; | ||
75 | use std::bar::G;", | ||
76 | ) | ||
77 | } | ||
78 | |||
79 | #[test] | ||
80 | fn insert_end() { | ||
81 | check_none( | ||
82 | "std::bar::ZZ", | ||
83 | r" | ||
84 | use std::bar::A; | ||
85 | use std::bar::D; | ||
86 | use std::bar::F; | ||
87 | use std::bar::G;", | ||
88 | r" | ||
89 | use std::bar::A; | ||
90 | use std::bar::D; | ||
91 | use std::bar::F; | ||
92 | use std::bar::G; | ||
93 | use std::bar::ZZ;", | ||
94 | ) | ||
95 | } | ||
96 | |||
97 | #[test] | ||
98 | fn insert_end_indent() { | ||
99 | mark::check!(insert_use_indent_before); | ||
100 | check_none( | ||
101 | "std::bar::ZZ", | ||
102 | r" | ||
103 | use std::bar::A; | ||
104 | use std::bar::D; | ||
105 | use std::bar::F; | ||
106 | use std::bar::G;", | ||
107 | r" | ||
108 | use std::bar::A; | ||
109 | use std::bar::D; | ||
110 | use std::bar::F; | ||
111 | use std::bar::G; | ||
112 | use std::bar::ZZ;", | ||
113 | ) | ||
114 | } | ||
115 | |||
116 | #[test] | ||
117 | fn insert_middle_nested() { | ||
118 | check_none( | ||
119 | "std::bar::EE", | ||
120 | r" | ||
121 | use std::bar::A; | ||
122 | use std::bar::{D, Z}; // example of weird imports due to user | ||
123 | use std::bar::F; | ||
124 | use std::bar::G;", | ||
125 | r" | ||
126 | use std::bar::A; | ||
127 | use std::bar::EE; | ||
128 | use std::bar::{D, Z}; // example of weird imports due to user | ||
129 | use std::bar::F; | ||
130 | use std::bar::G;", | ||
131 | ) | ||
132 | } | ||
133 | |||
134 | #[test] | ||
135 | fn insert_middle_groups() { | ||
136 | check_none( | ||
137 | "foo::bar::GG", | ||
138 | r" | ||
139 | use std::bar::A; | ||
140 | use std::bar::D; | ||
141 | |||
142 | use foo::bar::F; | ||
143 | use foo::bar::H;", | ||
144 | r" | ||
145 | use std::bar::A; | ||
146 | use std::bar::D; | ||
147 | |||
148 | use foo::bar::F; | ||
149 | use foo::bar::GG; | ||
150 | use foo::bar::H;", | ||
151 | ) | ||
152 | } | ||
153 | |||
154 | #[test] | ||
155 | fn insert_first_matching_group() { | ||
156 | check_none( | ||
157 | "foo::bar::GG", | ||
158 | r" | ||
159 | use foo::bar::A; | ||
160 | use foo::bar::D; | ||
161 | |||
162 | use std; | ||
163 | |||
164 | use foo::bar::F; | ||
165 | use foo::bar::H;", | ||
166 | r" | ||
167 | use foo::bar::A; | ||
168 | use foo::bar::D; | ||
169 | use foo::bar::GG; | ||
170 | |||
171 | use std; | ||
172 | |||
173 | use foo::bar::F; | ||
174 | use foo::bar::H;", | ||
175 | ) | ||
176 | } | ||
177 | |||
178 | #[test] | ||
179 | fn insert_missing_group_std() { | ||
180 | check_none( | ||
181 | "std::fmt", | ||
182 | r" | ||
183 | use foo::bar::A; | ||
184 | use foo::bar::D;", | ||
185 | r" | ||
186 | use std::fmt; | ||
187 | |||
188 | use foo::bar::A; | ||
189 | use foo::bar::D;", | ||
190 | ) | ||
191 | } | ||
192 | |||
193 | #[test] | ||
194 | fn insert_missing_group_self() { | ||
195 | check_none( | ||
196 | "self::fmt", | ||
197 | r" | ||
198 | use foo::bar::A; | ||
199 | use foo::bar::D;", | ||
200 | r" | ||
201 | use foo::bar::A; | ||
202 | use foo::bar::D; | ||
203 | |||
204 | use self::fmt;", | ||
205 | ) | ||
206 | } | ||
207 | |||
208 | #[test] | ||
209 | fn insert_no_imports() { | ||
210 | check_full( | ||
211 | "foo::bar", | ||
212 | "fn main() {}", | ||
213 | r"use foo::bar; | ||
214 | |||
215 | fn main() {}", | ||
216 | ) | ||
217 | } | ||
218 | |||
219 | #[test] | ||
220 | fn insert_empty_file() { | ||
221 | // empty files will get two trailing newlines | ||
222 | // this is due to the test case insert_no_imports above | ||
223 | check_full( | ||
224 | "foo::bar", | ||
225 | "", | ||
226 | r"use foo::bar; | ||
227 | |||
228 | ", | ||
229 | ) | ||
230 | } | ||
231 | |||
232 | #[test] | ||
233 | fn insert_empty_module() { | ||
234 | mark::check!(insert_use_no_indent_after); | ||
235 | check( | ||
236 | "foo::bar", | ||
237 | "mod x {}", | ||
238 | r"{ | ||
239 | use foo::bar; | ||
240 | }", | ||
241 | None, | ||
242 | true, | ||
243 | ) | ||
244 | } | ||
245 | |||
246 | #[test] | ||
247 | fn insert_after_inner_attr() { | ||
248 | check_full( | ||
249 | "foo::bar", | ||
250 | r"#![allow(unused_imports)]", | ||
251 | r"#![allow(unused_imports)] | ||
252 | |||
253 | use foo::bar;", | ||
254 | ) | ||
255 | } | ||
256 | |||
257 | #[test] | ||
258 | fn insert_after_inner_attr2() { | ||
259 | check_full( | ||
260 | "foo::bar", | ||
261 | r"#![allow(unused_imports)] | ||
262 | |||
263 | #![no_std] | ||
264 | fn main() {}", | ||
265 | r"#![allow(unused_imports)] | ||
266 | |||
267 | #![no_std] | ||
268 | |||
269 | use foo::bar; | ||
270 | fn main() {}", | ||
271 | ); | ||
272 | } | ||
273 | |||
274 | #[test] | ||
275 | fn inserts_after_single_line_inner_comments() { | ||
276 | check_none( | ||
277 | "foo::bar::Baz", | ||
278 | "//! Single line inner comments do not allow any code before them.", | ||
279 | r#"//! Single line inner comments do not allow any code before them. | ||
280 | |||
281 | use foo::bar::Baz;"#, | ||
282 | ); | ||
283 | } | ||
284 | |||
285 | #[test] | ||
286 | fn inserts_after_multiline_inner_comments() { | ||
287 | check_none( | ||
288 | "foo::bar::Baz", | ||
289 | r#"/*! Multiline inner comments do not allow any code before them. */ | ||
290 | |||
291 | /*! Still an inner comment, cannot place any code before. */ | ||
292 | fn main() {}"#, | ||
293 | r#"/*! Multiline inner comments do not allow any code before them. */ | ||
294 | |||
295 | /*! Still an inner comment, cannot place any code before. */ | ||
296 | |||
297 | use foo::bar::Baz; | ||
298 | fn main() {}"#, | ||
299 | ) | ||
300 | } | ||
301 | |||
302 | #[test] | ||
303 | fn inserts_after_all_inner_items() { | ||
304 | check_none( | ||
305 | "foo::bar::Baz", | ||
306 | r#"#![allow(unused_imports)] | ||
307 | /*! Multiline line comment 2 */ | ||
308 | |||
309 | |||
310 | //! Single line comment 1 | ||
311 | #![no_std] | ||
312 | //! Single line comment 2 | ||
313 | fn main() {}"#, | ||
314 | r#"#![allow(unused_imports)] | ||
315 | /*! Multiline line comment 2 */ | ||
316 | |||
317 | |||
318 | //! Single line comment 1 | ||
319 | #![no_std] | ||
320 | //! Single line comment 2 | ||
321 | |||
322 | use foo::bar::Baz; | ||
323 | fn main() {}"#, | ||
324 | ) | ||
325 | } | ||
326 | |||
327 | #[test] | ||
328 | fn merge_groups() { | ||
329 | check_last("std::io", r"use std::fmt;", r"use std::{fmt, io};") | ||
330 | } | ||
331 | |||
332 | #[test] | ||
333 | fn merge_groups_last() { | ||
334 | check_last( | ||
335 | "std::io", | ||
336 | r"use std::fmt::{Result, Display};", | ||
337 | r"use std::fmt::{Result, Display}; | ||
338 | use std::io;", | ||
339 | ) | ||
340 | } | ||
341 | |||
342 | #[test] | ||
343 | fn merge_last_into_self() { | ||
344 | check_last("foo::bar::baz", r"use foo::bar;", r"use foo::bar::{self, baz};"); | ||
345 | } | ||
346 | |||
347 | #[test] | ||
348 | fn merge_groups_full() { | ||
349 | check_full( | ||
350 | "std::io", | ||
351 | r"use std::fmt::{Result, Display};", | ||
352 | r"use std::{fmt::{Result, Display}, io};", | ||
353 | ) | ||
354 | } | ||
355 | |||
356 | #[test] | ||
357 | fn merge_groups_long_full() { | ||
358 | check_full("std::foo::bar::Baz", r"use std::foo::bar::Qux;", r"use std::foo::bar::{Baz, Qux};") | ||
359 | } | ||
360 | |||
361 | #[test] | ||
362 | fn merge_groups_long_last() { | ||
363 | check_last("std::foo::bar::Baz", r"use std::foo::bar::Qux;", r"use std::foo::bar::{Baz, Qux};") | ||
364 | } | ||
365 | |||
366 | #[test] | ||
367 | fn merge_groups_long_full_list() { | ||
368 | check_full( | ||
369 | "std::foo::bar::Baz", | ||
370 | r"use std::foo::bar::{Qux, Quux};", | ||
371 | r"use std::foo::bar::{Baz, Quux, Qux};", | ||
372 | ) | ||
373 | } | ||
374 | |||
375 | #[test] | ||
376 | fn merge_groups_long_last_list() { | ||
377 | check_last( | ||
378 | "std::foo::bar::Baz", | ||
379 | r"use std::foo::bar::{Qux, Quux};", | ||
380 | r"use std::foo::bar::{Baz, Quux, Qux};", | ||
381 | ) | ||
382 | } | ||
383 | |||
384 | #[test] | ||
385 | fn merge_groups_long_full_nested() { | ||
386 | check_full( | ||
387 | "std::foo::bar::Baz", | ||
388 | r"use std::foo::bar::{Qux, quux::{Fez, Fizz}};", | ||
389 | r"use std::foo::bar::{Baz, Qux, quux::{Fez, Fizz}};", | ||
390 | ) | ||
391 | } | ||
392 | |||
393 | #[test] | ||
394 | fn merge_groups_long_last_nested() { | ||
395 | check_last( | ||
396 | "std::foo::bar::Baz", | ||
397 | r"use std::foo::bar::{Qux, quux::{Fez, Fizz}};", | ||
398 | r"use std::foo::bar::Baz; | ||
399 | use std::foo::bar::{Qux, quux::{Fez, Fizz}};", | ||
400 | ) | ||
401 | } | ||
402 | |||
403 | #[test] | ||
404 | fn merge_groups_full_nested_deep() { | ||
405 | check_full( | ||
406 | "std::foo::bar::quux::Baz", | ||
407 | r"use std::foo::bar::{Qux, quux::{Fez, Fizz}};", | ||
408 | r"use std::foo::bar::{Qux, quux::{Baz, Fez, Fizz}};", | ||
409 | ) | ||
410 | } | ||
411 | |||
412 | #[test] | ||
413 | fn merge_groups_full_nested_long() { | ||
414 | check_full( | ||
415 | "std::foo::bar::Baz", | ||
416 | r"use std::{foo::bar::Qux};", | ||
417 | r"use std::{foo::bar::{Baz, Qux}};", | ||
418 | ); | ||
419 | } | ||
420 | |||
421 | #[test] | ||
422 | fn merge_groups_last_nested_long() { | ||
423 | check_full( | ||
424 | "std::foo::bar::Baz", | ||
425 | r"use std::{foo::bar::Qux};", | ||
426 | r"use std::{foo::bar::{Baz, Qux}};", | ||
427 | ); | ||
428 | } | ||
429 | |||
430 | #[test] | ||
431 | fn merge_groups_skip_pub() { | ||
432 | check_full( | ||
433 | "std::io", | ||
434 | r"pub use std::fmt::{Result, Display};", | ||
435 | r"pub use std::fmt::{Result, Display}; | ||
436 | use std::io;", | ||
437 | ) | ||
438 | } | ||
439 | |||
440 | #[test] | ||
441 | fn merge_groups_skip_pub_crate() { | ||
442 | check_full( | ||
443 | "std::io", | ||
444 | r"pub(crate) use std::fmt::{Result, Display};", | ||
445 | r"pub(crate) use std::fmt::{Result, Display}; | ||
446 | use std::io;", | ||
447 | ) | ||
448 | } | ||
449 | |||
450 | #[test] | ||
451 | #[ignore] // FIXME: Support this | ||
452 | fn split_out_merge() { | ||
453 | check_last( | ||
454 | "std::fmt::Result", | ||
455 | r"use std::{fmt, io};", | ||
456 | r"use std::fmt::{self, Result}; | ||
457 | use std::io;", | ||
458 | ) | ||
459 | } | ||
460 | |||
461 | #[test] | ||
462 | fn merge_into_module_import() { | ||
463 | check_full("std::fmt::Result", r"use std::{fmt, io};", r"use std::{fmt::{self, Result}, io};") | ||
464 | } | ||
465 | |||
466 | #[test] | ||
467 | fn merge_groups_self() { | ||
468 | check_full("std::fmt::Debug", r"use std::fmt;", r"use std::fmt::{self, Debug};") | ||
469 | } | ||
470 | |||
471 | #[test] | ||
472 | fn merge_mod_into_glob() { | ||
473 | check_full("token::TokenKind", r"use token::TokenKind::*;", r"use token::TokenKind::{*, self};") | ||
474 | // FIXME: have it emit `use token::TokenKind::{self, *}`? | ||
475 | } | ||
476 | |||
477 | #[test] | ||
478 | fn merge_self_glob() { | ||
479 | check_full("self", r"use self::*;", r"use self::{*, self};") | ||
480 | // FIXME: have it emit `use {self, *}`? | ||
481 | } | ||
482 | |||
483 | #[test] | ||
484 | fn merge_glob_nested() { | ||
485 | check_full( | ||
486 | "foo::bar::quux::Fez", | ||
487 | r"use foo::bar::{Baz, quux::*};", | ||
488 | r"use foo::bar::{Baz, quux::{self::*, Fez}};", | ||
489 | ) | ||
490 | } | ||
491 | |||
492 | #[test] | ||
493 | fn merge_nested_considers_first_segments() { | ||
494 | check_full( | ||
495 | "hir_ty::display::write_bounds_like_dyn_trait", | ||
496 | r"use hir_ty::{autoderef, display::{HirDisplayError, HirFormatter}, method_resolution};", | ||
497 | r"use hir_ty::{autoderef, display::{HirDisplayError, HirFormatter, write_bounds_like_dyn_trait}, method_resolution};", | ||
498 | ); | ||
499 | } | ||
500 | |||
501 | #[test] | ||
502 | fn skip_merge_last_too_long() { | ||
503 | check_last( | ||
504 | "foo::bar", | ||
505 | r"use foo::bar::baz::Qux;", | ||
506 | r"use foo::bar; | ||
507 | use foo::bar::baz::Qux;", | ||
508 | ); | ||
509 | } | ||
510 | |||
511 | #[test] | ||
512 | fn skip_merge_last_too_long2() { | ||
513 | check_last( | ||
514 | "foo::bar::baz::Qux", | ||
515 | r"use foo::bar;", | ||
516 | r"use foo::bar; | ||
517 | use foo::bar::baz::Qux;", | ||
518 | ); | ||
519 | } | ||
520 | |||
521 | #[test] | ||
522 | fn insert_short_before_long() { | ||
523 | check_none( | ||
524 | "foo::bar", | ||
525 | r"use foo::bar::baz::Qux;", | ||
526 | r"use foo::bar; | ||
527 | use foo::bar::baz::Qux;", | ||
528 | ); | ||
529 | } | ||
530 | |||
531 | #[test] | ||
532 | fn merge_last_fail() { | ||
533 | check_merge_only_fail( | ||
534 | r"use foo::bar::{baz::{Qux, Fez}};", | ||
535 | r"use foo::bar::{baaz::{Quux, Feez}};", | ||
536 | MergeBehaviour::Last, | ||
537 | ); | ||
538 | } | ||
539 | |||
540 | #[test] | ||
541 | fn merge_last_fail1() { | ||
542 | check_merge_only_fail( | ||
543 | r"use foo::bar::{baz::{Qux, Fez}};", | ||
544 | r"use foo::bar::baaz::{Quux, Feez};", | ||
545 | MergeBehaviour::Last, | ||
546 | ); | ||
547 | } | ||
548 | |||
549 | #[test] | ||
550 | fn merge_last_fail2() { | ||
551 | check_merge_only_fail( | ||
552 | r"use foo::bar::baz::{Qux, Fez};", | ||
553 | r"use foo::bar::{baaz::{Quux, Feez}};", | ||
554 | MergeBehaviour::Last, | ||
555 | ); | ||
556 | } | ||
557 | |||
558 | #[test] | ||
559 | fn merge_last_fail3() { | ||
560 | check_merge_only_fail( | ||
561 | r"use foo::bar::baz::{Qux, Fez};", | ||
562 | r"use foo::bar::baaz::{Quux, Feez};", | ||
563 | MergeBehaviour::Last, | ||
564 | ); | ||
565 | } | ||
566 | |||
567 | fn check( | ||
568 | path: &str, | ||
569 | ra_fixture_before: &str, | ||
570 | ra_fixture_after: &str, | ||
571 | mb: Option<MergeBehaviour>, | ||
572 | module: bool, | ||
573 | ) { | ||
574 | let mut syntax = ast::SourceFile::parse(ra_fixture_before).tree().syntax().clone(); | ||
575 | if module { | ||
576 | syntax = syntax.descendants().find_map(ast::Module::cast).unwrap().syntax().clone(); | ||
577 | } | ||
578 | let file = super::ImportScope::from(syntax).unwrap(); | ||
579 | let path = ast::SourceFile::parse(&format!("use {};", path)) | ||
580 | .tree() | ||
581 | .syntax() | ||
582 | .descendants() | ||
583 | .find_map(ast::Path::cast) | ||
584 | .unwrap(); | ||
585 | |||
586 | let rewriter = insert_use(&file, path, mb); | ||
587 | let result = rewriter.rewrite(file.as_syntax_node()).to_string(); | ||
588 | assert_eq_text!(&result, ra_fixture_after); | ||
589 | } | ||
590 | |||
591 | fn check_full(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) { | ||
592 | check(path, ra_fixture_before, ra_fixture_after, Some(MergeBehaviour::Full), false) | ||
593 | } | ||
594 | |||
595 | fn check_last(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) { | ||
596 | check(path, ra_fixture_before, ra_fixture_after, Some(MergeBehaviour::Last), false) | ||
597 | } | ||
598 | |||
599 | fn check_none(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) { | ||
600 | check(path, ra_fixture_before, ra_fixture_after, None, false) | ||
601 | } | ||
602 | |||
603 | fn check_merge_only_fail(ra_fixture0: &str, ra_fixture1: &str, mb: MergeBehaviour) { | ||
604 | let use0 = ast::SourceFile::parse(ra_fixture0) | ||
605 | .tree() | ||
606 | .syntax() | ||
607 | .descendants() | ||
608 | .find_map(ast::Use::cast) | ||
609 | .unwrap(); | ||
610 | |||
611 | let use1 = ast::SourceFile::parse(ra_fixture1) | ||
612 | .tree() | ||
613 | .syntax() | ||
614 | .descendants() | ||
615 | .find_map(ast::Use::cast) | ||
616 | .unwrap(); | ||
617 | |||
618 | let result = try_merge_imports(&use0, &use1, mb); | ||
619 | assert_eq!(result.map(|u| u.to_string()), None); | ||
620 | } | ||