1 use racer;
2 
3 use racer::{complete_from_file, Coordinate, MatchType};
4 use std::path::Path;
5 
6 use racer_testutils::*;
7 
8 #[test]
completes_fn()9 fn completes_fn() {
10     let src = "
11     fn  apple() {
12     }
13 
14     fn main() {
15         let b = ap~
16     }";
17 
18     let got = get_one_completion(src, None);
19     assert_eq!("apple", got.matchstr);
20 }
21 
22 #[test]
finds_fn_docs()23 fn finds_fn_docs() {
24     let src = "
25     /// Orange
26     /// juice
27     fn apple() {
28     }
29 
30     fn main() {
31         apple~
32     }";
33 
34     let got = get_one_completion(src, None);
35     assert_eq!("apple", got.matchstr);
36     assert_eq!("Orange\njuice", got.docs);
37 }
38 
39 #[test]
finds_struct_docs()40 fn finds_struct_docs() {
41     let src = "
42     /// Orange
43     /// juice
44     struct Apple {
45     }
46 
47     fn main() {
48         Apple~
49     }";
50 
51     let got = get_one_completion(src, None);
52     assert_eq!("Apple", got.matchstr);
53     assert_eq!("Orange\njuice", got.docs);
54 }
55 
56 #[test]
finds_struct_field_docs()57 fn finds_struct_field_docs() {
58     let src = "
59     struct Foo {
60         /// Hello docs
61         ///
62         /// How are you?
63         #[allow(dead_code)]
64         hello: String,
65     }
66 
67     fn do_things(f: Foo) -> String {
68         f.h~ello.clone()
69     }
70     ";
71 
72     let got = get_definition(src, None);
73     assert_eq!("hello", got.matchstr);
74     assert_eq!("Hello docs\n\nHow are you?", got.docs);
75 }
76 
77 #[test]
finds_tuple_struct_field_docs()78 fn finds_tuple_struct_field_docs() {
79     let src = "
80     struct Bar(
81         /// Hello docs
82         String
83     );
84 
85     fn do_things(b: Bar) -> String {
86         b.~0.clone()
87     }
88     ";
89 
90     let got = get_definition(src, None);
91     assert_eq!("0", got.matchstr);
92     assert_eq!("Hello docs", got.docs);
93 }
94 
95 #[test]
completes_fn_with_substitute_file()96 fn completes_fn_with_substitute_file() {
97     let src = "
98     fn  apple() {
99     }
100 
101     fn main() {
102         let b = ap~
103     }";
104 
105     let (_pos, src) = get_pos_and_source(src);
106     let cache = racer::FileCache::default();
107     let real_file = Path::new("not_real.rs");
108     let session = racer::Session::new(&cache, None);
109     session.cache_file_contents(real_file, src);
110     let cursor = Coordinate::new(6, 18);
111     let got = complete_from_file(real_file, cursor, &session)
112         .nth(0)
113         .unwrap();
114 
115     assert_eq!(Some(Coordinate::new(2, 8)), got.coords);
116     assert_eq!("apple", got.matchstr);
117 }
118 
119 #[test]
completes_pub_fn_locally()120 fn completes_pub_fn_locally() {
121     let src = "
122     pub fn apple() {
123     }
124 
125     fn main() {
126         let b = ap~
127     }";
128 
129     let got = get_one_completion(src, None);
130     assert_eq!("apple", got.matchstr);
131 }
132 
133 #[test]
completes_pub_fn_locally_precached()134 fn completes_pub_fn_locally_precached() {
135     let src = "
136     pub fn apple() {
137     }
138 
139     fn main() {
140         let b = ap~
141     }";
142 
143     let (pos, src) = get_pos_and_source(src);
144     let f = TmpFile::new(&src);
145     let path = f.path();
146     let cache = racer::FileCache::default();
147     let session = racer::Session::new(&cache, Some(path));
148     session.cache_file_contents(path, src.clone());
149     let got = complete_from_file(path, pos, &session).nth(0).unwrap();
150     assert_eq!("apple", got.matchstr);
151 }
152 
153 #[test]
completes_pub_const_fn_locally()154 fn completes_pub_const_fn_locally() {
155     let src = "
156     pub const fn apple() {
157     }
158 
159     fn main() {
160         let b = ap~
161     }";
162 
163     let got = get_one_completion(src, None);
164     assert_eq!("apple", got.matchstr);
165 }
166 
167 #[test]
completes_local_scope_let()168 fn completes_local_scope_let() {
169     let src = "
170     fn main() {
171         let apple = 35;
172         let b = ap~
173     }";
174 
175     let got = get_one_completion(src, None);
176     assert_eq!("apple", got.matchstr);
177     assert_eq!(29, got.point.0);
178 }
179 
180 #[test]
completes_via_parent_scope_let()181 fn completes_via_parent_scope_let() {
182     let src = "
183     fn main() {
184         let mut apple = 35;
185         if foo {
186             let b = ap~
187         }
188     }";
189 
190     let got = get_one_completion(src, None);
191     assert_eq!("apple", got.matchstr);
192     assert_eq!(33, got.point.0);
193 }
194 
195 #[test]
completes_for_vec_field_and_method()196 fn completes_for_vec_field_and_method() {
197     let src = "
198     struct St
199     {
200         stfield: i32,
201     }
202 
203     impl St {
204         pub fn stmethod(&self) -> u32 {2}
205     }
206 
207     fn main()
208     {
209         let mut arr: Vec<St> = Vec::new();
210         arr.push( St{stfield: 4} );
211 
212         for it in arr
213         {
214             it.stf
215             it.stm
216         }
217     }
218     ";
219 
220     let dir = TmpDir::new();
221     let path = dir.write_file("src.rs", src);
222     let cache = racer::FileCache::default();
223     let session = racer::Session::new(&cache, None);
224     let cursor1 = Coordinate::new(18, 18);
225     let got1 = complete_from_file(&path, cursor1, &session).nth(0).unwrap();
226     assert_eq!("stfield", got1.matchstr);
227     let cursor2 = Coordinate::new(19, 18);
228     let got2 = complete_from_file(&path, cursor2, &session).nth(0).unwrap();
229     assert_eq!("stmethod", got2.matchstr);
230 }
231 
232 #[test]
completes_trait_methods()233 fn completes_trait_methods() {
234     let src = "
235     mod sub {
236         pub trait Trait {
237             fn traitf() -> bool;
238             fn traitm(&self) -> bool;
239         }
240 
241         pub struct Foo(pub bool);
242 
243         impl Trait for Foo {
244             fn traitf() -> bool { false }
245             fn traitm(&self) -> bool { true }
246         }
247     }
248 
249     fn main() { // l16
250         let t = sub::Foo(true);
251         sub::Foo::traitf();
252         t.traitm();
253     }
254     ";
255     let f = TmpFile::new(src);
256     let path = f.path();
257     let cache1 = racer::FileCache::default();
258     let session1 = racer::Session::new(&cache1, None);
259     let cursor1 = Coordinate::new(18, 18);
260     let got1 = complete_from_file(&path, cursor1, &session1)
261         .nth(0)
262         .unwrap();
263     let cache2 = racer::FileCache::default();
264     let session2 = racer::Session::new(&cache2, None);
265     let cursor2 = Coordinate::new(19, 11);
266     let got2 = complete_from_file(&path, cursor2, &session2)
267         .nth(0)
268         .unwrap();
269     println!("{:?}", got1);
270     println!("{:?}", got2);
271     assert_eq!(got1.matchstr, "traitf");
272     assert_eq!(got2.matchstr, "traitm");
273     assert_eq!(got1.contextstr, "fn traitf() -> bool");
274     assert_eq!(got2.contextstr, "fn traitm(&self) -> bool");
275 }
276 
277 #[test]
completes_trait_bounded_methods()278 fn completes_trait_bounded_methods() {
279     let src = "
280     pub trait Trait1 {}
281 
282     impl Trait1 for Foo {}
283 
284     pub trait Trait2 {
285         fn traitf() -> bool;
286         fn traitm(&self) -> bool;
287     }
288 
289     impl<T: Trait1> Trait2 for T {
290         fn traitf() -> bool { true }
291         fn traitm(&self) -> bool { false }
292     }
293 
294     pub struct Foo(pub bool);
295 
296     fn main() {
297         let t = Foo(true);
298         Foo::tra
299         t.tr
300     }";
301     let f = TmpFile::new(src);
302     let path = f.path();
303     let cache1 = racer::FileCache::default();
304     let session1 = racer::Session::new(&cache1, None);
305     let cursor1 = Coordinate::new(20, 16);
306     let got1 = complete_from_file(&path, cursor1, &session1)
307         .nth(0)
308         .unwrap();
309     let cache2 = racer::FileCache::default();
310     let session2 = racer::Session::new(&cache2, None);
311     let cursor2 = Coordinate::new(21, 12);
312     let got2 = complete_from_file(&path, cursor2, &session2)
313         .nth(0)
314         .unwrap();
315     println!("{:?}", got1);
316     println!("{:?}", got2);
317     assert_eq!(got1.matchstr, "traitf");
318     assert_eq!(got2.matchstr, "traitm");
319     assert_eq!(got1.contextstr, "fn traitf() -> bool");
320     assert_eq!(got2.contextstr, "fn traitm(&self) -> bool");
321 }
322 
323 #[test]
completes_trait_bounded_methods_generic_return()324 fn completes_trait_bounded_methods_generic_return() {
325     let src = "
326     pub trait Trait1 {
327         fn traitfn(&self) -> u32 { 2 }
328     }
329 
330     impl Trait1 for Foo {}
331 
332     pub trait Trait2 {
333         fn traitm(self) -> Self;
334     }
335 
336     impl<T: Trait1> Trait2 for T {
337         fn traitm(self) -> T { self }
338     }
339 
340     pub struct Foo(pub bool);
341 
342     impl Foo {
343         pub fn structfn(&self) -> bool {self.0}
344     }
345 
346     fn main() {
347         let t = Foo(true);
348         t.traitm().struc
349         t.traitm().traitf
350     }";
351 
352     let f = TmpFile::new(src);
353     let path = f.path();
354     let cache = racer::FileCache::default();
355     let session = racer::Session::new(&cache, None);
356     let cursor1 = Coordinate::new(24, 24);
357     let cursor2 = Coordinate::new(25, 25);
358     let got1 = complete_from_file(&path, cursor1, &session).nth(0).unwrap();
359     println!("got1: {:?}", got1);
360     assert_eq!(got1.matchstr, "structfn");
361     let got2 = complete_from_file(&path, cursor2, &session).nth(0).unwrap();
362     println!("{:?}", got2);
363     assert_eq!(got2.matchstr, "traitfn");
364 }
365 
366 #[test]
completes_iter_variable_fiedlds()367 fn completes_iter_variable_fiedlds() {
368     let src = "
369     struct St {
370         pub item: StItem,
371         pub used: bool
372     }
373 
374     struct StItem {
375         pub field: u32
376     }
377 
378     impl Iterator for St {
379         type Item = StItem;
380 
381         fn next(&mut self) -> Option<StItem> {
382             if self.used {
383                 self.used = false;
384                 return Some(self.item);
385             }
386             None
387         }
388     }
389 
390     fn main()
391     {
392         let it = St {
393             text: StItem { field: 22 },
394             used: false
395         };
396 
397         for item in it {
398             item.fie~
399         }
400     }
401     ";
402     let got = get_only_completion(src, None);
403     assert_eq!(got.matchstr, "field");
404 }
405 
406 #[test]
407 #[ignore]
completes_for_vec_iter_field_and_method()408 fn completes_for_vec_iter_field_and_method() {
409     let src = "
410     pub mod mymod;
411     use mymod::{Vec, IntoIter, IntoIterator, Option};
412     use Option::{Some, None};
413 
414     struct St
415     {
416         stfield: i32,
417     }
418 
419     impl St {
420         pub fn stmethod(&self) -> u32 {2}
421     }
422 
423     fn main()
424     {
425         let mut arr: Vec<St> = Vec::new();
426         arr.push( St{stfield: 4} );
427 
428         for it in arr.iter()
429         {
430             it.stf
431             it.stm
432         }
433     }
434     ";
435     let dir = TmpDir::new();
436     let path = dir.write_file("src.rs", src);
437     let cache = racer::FileCache::default();
438     let session = racer::Session::new(&cache, None);
439     let cursor1 = Coordinate::new(22, 18);
440     let got1 = complete_from_file(&path, cursor1, &session).nth(0).unwrap();
441     assert_eq!("stfield", got1.matchstr);
442     let cursor2 = Coordinate::new(23, 18);
443     let got2 = complete_from_file(&path, cursor2, &session).nth(0).unwrap();
444     assert_eq!("stmethod", got2.matchstr);
445 }
446 
447 #[test]
completes_trait_methods_when_at_scope_end()448 fn completes_trait_methods_when_at_scope_end() {
449     let src = "
450     mod sub {
451         pub trait Trait {
452             fn traitf() -> bool;
453             fn traitm(&self) -> bool;
454         }
455 
456         impl Trait for Foo {
457             fn traitf() -> bool { false }
458             fn traitm(&self) -> bool { true }
459         }
460 
461         pub struct Foo(pub bool);
462     }
463 
464     fn main() { // l16
465         let t = sub::Foo(true);
466         sub::Foo::traitf();
467         t.traitm();
468     }
469     ";
470 
471     let f = TmpFile::new(src);
472     let path = f.path();
473     let cache = racer::FileCache::default();
474     let session = racer::Session::new(&cache, None);
475     let cursor1 = Coordinate::new(18, 18);
476     let got1 = complete_from_file(&path, cursor1, &session).nth(0).unwrap();
477     let cursor2 = Coordinate::new(19, 11);
478     let got2 = complete_from_file(&path, cursor2, &session).nth(0).unwrap();
479     println!("{:?}", got1);
480     println!("{:?}", got2);
481     assert_eq!(got1.matchstr, "traitf");
482     assert_eq!(got2.matchstr, "traitm");
483     assert_eq!(got1.contextstr, "fn traitf() -> bool");
484     assert_eq!(got2.contextstr, "fn traitm(&self) -> bool");
485 }
486 
487 #[test]
follows_use()488 fn follows_use() {
489     let src1 = "
490     pub fn myfn() {}
491     pub fn foo() {}
492     ";
493     let src = "
494     use src1::{foo,myfn};
495     mod src1;
496     fn main() {
497         myfn~();
498     }
499     ";
500 
501     let dir = TmpDir::new();
502     let _src1 = dir.write_file("src1.rs", src1);
503     let got = get_definition(src, Some(dir));
504     assert_eq!(got.matchstr, "myfn");
505     assert_eq!(got.contextstr, "pub fn myfn()");
506 }
507 
508 #[test]
follows_use_in_braces()509 fn follows_use_in_braces() {
510     let src = "
511     mod foo {
512         pub fn myfn() {}
513         pub fn second() {}
514     }
515 
516     fn main() {
517         use foo::{
518             myfn,
519             second
520         };
521 
522         my~fn();
523     }
524     ";
525 
526     let got = get_definition(src, None);
527     assert_eq!(got.matchstr, "myfn");
528 }
529 
530 #[test]
follows_use_glob()531 fn follows_use_glob() {
532     let src3 = "
533     pub fn myfn() {}
534     pub fn foo() {}
535     ";
536     let src = "
537     use src3::*;
538     mod src3;
539     fn main() {
540         my~fn();
541     }
542     ";
543     let dir = TmpDir::new();
544     let _src3 = dir.write_file("src3.rs", src3);
545     let got = get_definition(src, Some(dir));
546     assert_eq!(got.matchstr, "myfn");
547 }
548 
549 #[test]
follows_multiple_use_globs()550 fn follows_multiple_use_globs() {
551     let src1 = "
552     pub fn src1fn() {}
553     ";
554     let src2 = "
555     pub fn src2fn() {}
556     ";
557     let src = "
558     use multiple_glob_test1::*;
559     use multiple_glob_test2::*;
560     mod multiple_glob_test1;
561     mod multiple_glob_test2;
562 
563     src~
564     ";
565 
566     let dir = TmpDir::new();
567     let _src1 = dir.write_file("multiple_glob_test1.rs", src1);
568     let _src2 = dir.write_file("multiple_glob_test2.rs", src2);
569 
570     let mut has_1 = false;
571     let mut has_2 = false;
572     let completions = get_all_completions(src, Some(dir));
573     for m in completions {
574         if m.matchstr == "src1fn" {
575             has_1 = true;
576         }
577         if m.matchstr == "src2fn" {
578             has_2 = true;
579         }
580     }
581     assert!(has_1 && has_2);
582 }
583 
584 #[test]
single_import_shadows_glob_import()585 fn single_import_shadows_glob_import() {
586     let src = "
587     use shadowed::*;
588     use shadower::Foo;
589 
590     mod shadowed {
591         pub struct Foo;
592     }
593 
594     mod shadower {
595         pub struct Foo;
596     }
597 
598     fn main() {
599         Foo~;
600     }
601     ";
602     let got = get_definition(src, None);
603     assert_eq!(got.matchstr, "Foo");
604     println!("{}", got.filepath.display());
605     println!("{:?}", got.point);
606     assert_eq!(got.coords, Some(Coordinate::new(10, 19)));
607 }
608 
609 #[test]
follows_use_self()610 fn follows_use_self() {
611     let src = "
612     use foo::use_self_test::{self, bar};
613 
614     mod foo {
615         pub mod use_self_test {
616             pub fn bar() {}
617         }
618     }
619 
620     use_s~
621     ";
622 
623     let completions = get_all_completions(src, None);
624     assert!(completions
625         .into_iter()
626         .any(|m| m.matchstr == "use_self_test"));
627 
628     let src = "
629     use use_self_test::self;
630 
631     mod use_self_test {
632     }
633 
634     use_s~
635     ";
636 
637     let completions = get_all_completions(src, None);
638     assert!(completions
639         .into_iter()
640         .any(|m| m.matchstr == "use_self_test"));
641 }
642 
643 /// This test addresses https://github.com/racer-rust/racer/issues/645 by
644 /// confirming that racer will not return duplicate results for a module.
645 #[test]
completes_mod_exactly_once()646 fn completes_mod_exactly_once() {
647     let src = "
648     mod sample {
649         pub struct Bar;
650     }
651 
652     mod happy {
653         use sample;
654 
655         fn do_things(bar: sampl~e::Bar) {
656 
657         }
658     }
659     ";
660 
661     let got = get_only_completion(src, None);
662     assert_eq!(got.matchstr, "sample");
663     assert_eq!(got.mtype, MatchType::Module);
664 }
665 
666 /// This test verifies that any result deduplication techniques
667 /// are robust enough to avoid deduplication of multiple results
668 /// which happen to share a match string.
669 #[test]
completes_mod_and_local_with_same_name()670 fn completes_mod_and_local_with_same_name() {
671     let src = "
672     mod sample {
673         pub struct Bar;
674     }
675 
676     mod happy {
677         use sample;
678 
679         fn do_things(bar: sample::Bar) {
680             let sample = bar;
681             let other = sampl~e::Bar;
682         }
683     }
684     ";
685 
686     let got = get_all_completions(src, None);
687     assert_eq!(got.len(), 2);
688     assert_eq!(got[0].matchstr, "sample");
689     assert_eq!(got[1].matchstr, "sample");
690 }
691 
692 #[test]
completes_out_of_order_mod_use_with_same_fn_name_as_mod()693 fn completes_out_of_order_mod_use_with_same_fn_name_as_mod() {
694     let src = "
695     use foo::foo;
696 
697     mod foo {
698         pub fn foo() {}
699     }
700 
701     fn main() {
702         f~
703     }";
704 
705     let mut has_module = false;
706     let mut has_function = false;
707     let completions = get_all_completions(src, None);
708     for m in completions {
709         match (&*m.matchstr, m.mtype) {
710             ("foo", MatchType::Module) => has_module = true,
711             ("foo", MatchType::Function) => has_function = true,
712             _ => (),
713         }
714     }
715     assert!(has_module && has_function);
716 }
717 
718 #[test]
ignores_self_referential_unresolved_import()719 fn ignores_self_referential_unresolved_import() {
720     let src = "use foo::foo;f~";
721 
722     let completions = get_all_completions(src, None);
723     assert!(!completions.iter().any(|m| m.matchstr == "foo"));
724 }
725 
726 #[test]
ignores_self_referential_unresolved_import_long()727 fn ignores_self_referential_unresolved_import_long() {
728     let src = "use foo::bar::foo;f~";
729 
730     let completions = get_all_completions(src, None);
731     assert!(!completions.iter().any(|m| m.matchstr == "foo"));
732 }
733 
734 #[test]
ignores_self_referential_unresolved_imports()735 fn ignores_self_referential_unresolved_imports() {
736     let src = "
737     use foo::bar;
738     use bar::baz;
739     use baz::foo;
740     f~";
741 
742     let completions = get_all_completions(src, None);
743     assert!(!completions.iter().any(|m| m.matchstr == "foo"));
744 }
745 
746 #[test]
ignores_self_referential_unresolved_imports_across_modules()747 fn ignores_self_referential_unresolved_imports_across_modules() {
748     let src = "
749     use foo::bar;
750 
751     mod foo {
752         pub use super::bar;
753     }
754     b~";
755 
756     let completions = get_all_completions(src, None);
757     assert!(!completions.iter().any(|m| m.matchstr == "bar"));
758 }
759 
760 #[test]
finds_external_mod_docs()761 fn finds_external_mod_docs() {
762     let src1 = "// Copyright notice
763 
764 //! The mods multiline
765 //! documentation
766     ";
767     let src = "
768     mod external_mod;
769     use external_mod;
770 
771     fn main() {
772         external_mod~
773     }
774     ";
775 
776     let dir = TmpDir::new();
777     let _src1 = dir.write_file("external_mod.rs", src1);
778     let got = get_one_completion(src, Some(dir));
779     assert_eq!("external_mod", got.matchstr);
780     assert_eq!("The mods multiline\ndocumentation", got.docs);
781 }
782 
783 #[test]
finds_external_struct_docs()784 fn finds_external_struct_docs() {
785     let src1 = "
786     /// Orange
787     /// juice
788     pub struct Apple {
789         pub a: u8,
790     }";
791     let src = "
792     use external_struct::Apple;
793     mod external_struct;
794 
795     fn main() {
796         Apple~
797     }";
798 
799     let dir = TmpDir::new();
800     let _src1 = dir.write_file("external_struct.rs", src1);
801     let got = get_one_completion(src, Some(dir));
802     assert_eq!("Apple", got.matchstr);
803     assert_eq!("Orange\njuice", got.docs);
804 }
805 
806 #[test]
finds_external_fn_docs()807 fn finds_external_fn_docs() {
808     let src1 = "
809     /// Orange
810     /// juice
811 
812     pub fn apple() {
813         let x = 1;
814     }";
815     let src = "
816     use external_fn::apple;
817     mod external_fn;
818 
819     fn main() {
820         apple~
821     }";
822 
823     let dir = TmpDir::new();
824     let _src1 = dir.write_file("external_fn.rs", src1);
825     let got = get_one_completion(src, Some(dir));
826     assert_eq!("apple", got.matchstr);
827     assert_eq!("Orange\njuice", got.docs);
828 }
829 
830 #[test]
keeps_newlines_in_external_mod_doc()831 fn keeps_newlines_in_external_mod_doc() {
832     // issue 683: do not remove newlines inside of mod-doc
833     let src1 = "// Copyright notice
834 
835 //! The mods multiline documentation
836 //!
837 //! with an empty line
838     ";
839     let src = "
840     mod external_mod;
841     use external_mod;
842 
843     fn main() {
844         external_mod~
845     }
846     ";
847 
848     let dir = TmpDir::new();
849     let _src1 = dir.write_file("external_mod.rs", src1);
850     let got = get_one_completion(src, Some(dir));
851     assert_eq!("external_mod", got.matchstr);
852     assert_eq!(
853         "The mods multiline documentation\n\nwith an empty line",
854         got.docs
855     );
856 }
857 
858 /// Addresses https://github.com/racer-rust/racer/issues/618
859 #[test]
always_get_all_doc_lines()860 fn always_get_all_doc_lines() {
861     let src = "
862 /// Orange
863 /// juice
864 pub fn apple() {
865     app~le()
866 }";
867 
868     let got = get_only_completion(src, None);
869     assert_eq!("apple", got.matchstr);
870     assert_eq!("Orange\njuice", got.docs);
871 }
872 
873 /// Addresses https://github.com/racer-rust/racer/issues/594
874 #[test]
find_complete_docs_with_parentheses_on_last_line()875 fn find_complete_docs_with_parentheses_on_last_line() {
876     let src = "
877 /// Hello world
878 /// (quux)
879 pub fn foo() {}
880 
881 pub fn bar() {
882     foo~()
883 }
884 ";
885     let got = get_only_completion(src, None);
886     assert_eq!("foo", got.matchstr);
887     assert_eq!("Hello world\n(quux)", got.docs);
888 }
889 
890 #[test]
completes_struct_field_via_assignment()891 fn completes_struct_field_via_assignment() {
892     let src = "
893     struct Point {
894         /// The first item.
895         first: f64,
896         second: f64
897     }
898 
899     let var = Point {first: 35, second: 22};
900     var.f~
901     ";
902 
903     let got = get_one_completion(src, None);
904     assert_eq!(got.matchstr, "first");
905     assert_eq!("The first item.", got.docs);
906 }
907 
908 #[test]
finds_defn_of_struct_field()909 fn finds_defn_of_struct_field() {
910     let src = "
911     struct Point {
912         /// The first item.
913         first: f64,
914         second: f64
915     }
916 
917     let var = Point {first: 35, second: 22};
918     var.f~irst
919     ";
920 
921     let got = get_definition(src, None);
922     assert_eq!(got.matchstr, "first");
923     assert_eq!("The first item.", got.docs);
924 }
925 
926 #[test]
finds_impl_fn()927 fn finds_impl_fn() {
928     let src = "
929     struct Foo;
930     impl Foo {
931         fn new() {}
932     }
933 
934     Foo::n~ew();
935     ";
936 
937     let got = get_definition(src, None);
938     assert_eq!(got.matchstr, "new");
939 }
940 
941 #[test]
follows_use_to_inline_mod()942 fn follows_use_to_inline_mod() {
943     let src = "
944     use foo::myfn;
945     mod foo {
946         pub fn myfn() {}
947     }
948 
949     fn main() {
950         m~yfn();
951     }
952     ";
953 
954     let got = get_definition(src, None);
955     assert_eq!(got.matchstr, "myfn");
956 }
957 
958 #[test]
finds_enum()959 fn finds_enum() {
960     let src = "
961     enum MyEnum {
962         One, Two
963     }
964 
965     fn myfn(e: M~yEnum) {}
966     ";
967 
968     let got = get_definition(src, None);
969     assert_eq!(got.matchstr, "MyEnum");
970 }
971 
972 #[test]
finds_type()973 fn finds_type() {
974     let src = "
975     type SpannedIdent = Spanned<Ident>
976     S~pannedIdent;
977     ";
978 
979     let got = get_definition(src, None);
980     assert_eq!(got.matchstr, "SpannedIdent");
981 }
982 
983 #[test]
finds_trait()984 fn finds_trait() {
985     let src = "
986     pub trait MyTrait<E: Clone> {}
987     M~yTrait
988     ";
989 
990     let got = get_definition(src, None);
991     assert_eq!(got.matchstr, "MyTrait");
992     assert_eq!(got.contextstr, "pub trait MyTrait<E: Clone>");
993 }
994 
995 #[test]
finds_macro()996 fn finds_macro() {
997     let src = "
998     macro_rules! my_macro {
999         () => {}
1000     }
1001     m~y_macro!();
1002     ";
1003 
1004     let got = get_definition(src, None);
1005     assert_eq!(got.matchstr, "my_macro!");
1006 }
1007 
1008 #[test]
finds_fn_arg()1009 fn finds_fn_arg() {
1010     let src = "
1011     fn myfn(myarg: &str) {
1012          my~arg
1013     }
1014     ";
1015 
1016     let got = get_definition(src, None);
1017     assert_eq!(got.matchstr, "myarg");
1018 }
1019 
1020 #[test]
finds_fn_arg_in_incomplete_fn()1021 fn finds_fn_arg_in_incomplete_fn() {
1022     let src = "
1023     fn myfn(myarg: &str) {
1024          my~arg
1025     ";
1026 
1027     let got = get_definition(src, None);
1028     assert_eq!(got.matchstr, "myarg");
1029 }
1030 
1031 #[test]
finds_inline_fn()1032 fn finds_inline_fn() {
1033     let src = "
1034     #[inline]
1035     fn contains<'a>(&needle: &'a str)
1036         -> bool {
1037     }
1038 
1039     conta~ins();
1040     ";
1041 
1042     let got = get_definition(src, None);
1043     assert_eq!(got.matchstr, "contains");
1044     assert_eq!(got.contextstr, "fn contains<'a>(&needle: &'a str) -> bool");
1045 }
1046 
1047 #[test]
follows_self_use()1048 fn follows_self_use() {
1049     let modsrc = "
1050     pub use self::src4::{Foo,myfn};
1051     pub mod src4;
1052     ";
1053     let src4 = "
1054     struct Foo;
1055     pub fn myfn() {}
1056     ";
1057     let src = "
1058     use mymod::{Foo,myfn};
1059     pub mod mymod;
1060 
1061     fn main() {
1062         my~fn();
1063     }
1064     ";
1065 
1066     let dir = TmpDir::new();
1067     let mymod = dir.nested_dir("mymod");
1068     let _mod = mymod.write_file("mod.rs", modsrc);
1069     let src4file = mymod.write_file("src4.rs", src4);
1070     let got = get_definition(src, Some(dir));
1071     assert_eq!(got.matchstr, "myfn");
1072     assert_eq!(src4file.path(), got.filepath);
1073     assert_eq!(28, got.point.0);
1074 }
1075 
1076 #[test]
finds_nested_submodule_file()1077 fn finds_nested_submodule_file() {
1078     let sub3src = "
1079     pub fn myfn() {}
1080     ";
1081     let src = "
1082     pub mod sub1 {
1083         pub mod sub2 {
1084             pub mod sub3;
1085         }
1086     }
1087     sub1::sub2::sub3::m~yfn();
1088     ";
1089 
1090     let dir = TmpDir::new();
1091     let sub1 = dir.nested_dir("sub1");
1092     let sub2 = sub1.nested_dir("sub2");
1093     let src3 = sub2.write_file("sub3.rs", sub3src);
1094     let got = get_definition(src, Some(dir));
1095     assert_eq!(got.matchstr, "myfn");
1096     assert_eq!(src3.path(), got.filepath);
1097 }
1098 
1099 #[test]
follows_super_in_sub_module()1100 fn follows_super_in_sub_module() {
1101     let src = "
1102     pub fn iamhere() { }
1103     mod inner { pub use super::ia~mhere; }
1104     ";
1105 
1106     let got = get_definition(src, None);
1107     assert_eq!("iamhere", got.matchstr);
1108 }
1109 
1110 #[test]
follows_super_super_in_sub_sub_module()1111 fn follows_super_super_in_sub_sub_module() {
1112     let src = "
1113     pub fn iamhere() { }
1114     mod inner { mod inner { pub use super::super::ia~mhere; } }
1115     ";
1116 
1117     let got = get_definition(src, None);
1118     assert_eq!("iamhere", got.matchstr);
1119 }
1120 
1121 #[test]
follows_super_in_local_sub_module()1122 fn follows_super_in_local_sub_module() {
1123     let src = "
1124     mod inner {
1125       pub fn iamhere() { }
1126       mod inner2 { pub use super::iamh~ere; }
1127     }
1128     ";
1129 
1130     let got = get_definition(src, None);
1131     assert_eq!("iamhere", got.matchstr);
1132 }
1133 
1134 #[test]
follows_use_to_impl()1135 fn follows_use_to_impl() {
1136     let modsrc = "
1137     pub struct Foo;
1138     impl Foo {       // impl doesn't need to be 'pub'
1139         pub fn new() -> Foo {
1140             Foo
1141         }
1142     }
1143     ";
1144     let src = "
1145     use mymod::{Foo};
1146     mod mymod;
1147     fn main() {
1148         Foo::n~ew();
1149     }
1150     ";
1151 
1152     let dir = TmpDir::new();
1153     let mod_path = dir.write_file("mymod.rs", modsrc);
1154     let got = get_definition(src, Some(dir));
1155     assert_eq!(got.matchstr, "new");
1156     assert_eq!(90, got.point.0);
1157     assert_eq!(mod_path.path(), got.filepath);
1158 }
1159 
1160 #[test]
finds_templated_impl_fn()1161 fn finds_templated_impl_fn() {
1162     let src = "
1163     struct Foo<T>;
1164     impl<T> Foo<T> {
1165         fn new() {}
1166     }
1167 
1168     Foo::n~ew();
1169     ";
1170 
1171     let got = get_definition(src, None);
1172     assert_eq!(got.matchstr, "new");
1173 }
1174 
1175 #[test]
follows_fn_to_method()1176 fn follows_fn_to_method() {
1177     let src = "
1178     struct Foo<T>;
1179     impl<T> Foo<T> {
1180         fn new() -> Foo<T> {}
1181         fn mymethod(&self) {}
1182     }
1183 
1184     fn main() {
1185         let v = Foo::new();
1186         v.my~
1187     }
1188     ";
1189 
1190     let got = get_one_completion(src, None);
1191     assert_eq!("mymethod", got.matchstr);
1192 }
1193 
1194 #[test]
simple_struct_contextstr()1195 fn simple_struct_contextstr() {
1196     let src = "
1197     struct Foo<T>;
1198 
1199     fn myfn() {
1200         let x: Foo~
1201     }
1202     ";
1203 
1204     let got = get_one_completion(src, None);
1205     assert_eq!(got.contextstr, "struct Foo<T>;");
1206 }
1207 
1208 #[test]
struct_contextstr()1209 fn struct_contextstr() {
1210     let src = "
1211     struct
1212         Foo<T> {
1213         pub fn foo1();
1214     }
1215 
1216     fn myfn() {
1217         let x: Foo~
1218     }
1219     ";
1220 
1221     let got = get_one_completion(src, None);
1222     assert_eq!(got.contextstr, "struct Foo<T>");
1223 }
1224 
1225 #[test]
follows_arg_to_method()1226 fn follows_arg_to_method() {
1227     let src = "
1228     struct Foo<T>;
1229     impl<T> Foo<T> {
1230         fn mymethod(&self) {}
1231     }
1232 
1233     fn myfn(v: &Foo) {
1234         v.my~
1235     }
1236     ";
1237 
1238     let got = get_one_completion(src, None);
1239     assert_eq!("mymethod", got.matchstr);
1240 }
1241 
1242 #[test]
follows_arg_to_enum_method()1243 fn follows_arg_to_enum_method() {
1244     let src = "
1245     enum Foo<T> {
1246        EnumVal
1247     }
1248     impl<T> Foo<T> {
1249         fn mymethod(&self) {}
1250     }
1251 
1252     fn myfn(v: &Foo) {
1253         v.my~
1254     }
1255     ";
1256 
1257     let got = get_one_completion(src, None);
1258     assert_eq!("mymethod", got.matchstr);
1259 }
1260 
1261 #[test]
finds_enum_static_method()1262 fn finds_enum_static_method() {
1263     let src = "
1264     enum Foo {
1265         Bar,
1266         Baz
1267     }
1268 
1269     impl Foo {
1270         pub fn make_baz() -> Self {
1271             Foo::Baz
1272         }
1273     }
1274 
1275     fn myfn() -> Foo {
1276         Foo::ma~ke_baz()
1277     }
1278     ";
1279 
1280     let got = get_only_completion(src, None);
1281     assert_eq!("make_baz", got.matchstr);
1282     assert_eq!(MatchType::Function, got.mtype);
1283 }
1284 
1285 #[test]
finds_enum_variants_first()1286 fn finds_enum_variants_first() {
1287     let src = "
1288     enum Foo {
1289         Bar,
1290         Baz
1291     }
1292 
1293     impl Foo {
1294         pub fn amazing() -> Self {
1295             Foo::Baz
1296         }
1297     }
1298 
1299     fn myfn() -> Foo {
1300         Foo::~Bar
1301     }
1302     ";
1303 
1304     let got = get_all_completions(src, None);
1305     assert_eq!(3, got.len());
1306     assert_eq!("Bar", got[0].matchstr);
1307     assert_eq!("Baz", got[1].matchstr);
1308     assert_eq!("amazing", got[2].matchstr);
1309 }
1310 
1311 #[test]
follows_let_method_call()1312 fn follows_let_method_call() {
1313     let src = "
1314     struct Foo;
1315     struct Bar;
1316     impl Foo {
1317         fn mymethod(&self) -> Bar {}
1318     }
1319     impl Bar {
1320         fn mybarmethod(&self) -> Bar {}
1321     }
1322 
1323     fn myfn(v: &Foo) {
1324         let f = v.mymethod();
1325         f.my~
1326     }
1327     ";
1328 
1329     let got = get_one_completion(src, None);
1330     assert_eq!("mybarmethod", got.matchstr);
1331 }
1332 
1333 #[test]
follows_chained_method_call()1334 fn follows_chained_method_call() {
1335     let src = "
1336     struct Foo;
1337     struct Bar;
1338     impl Foo {
1339         fn mymethod(&self) -> Bar {}
1340     }
1341     impl Bar {
1342         fn mybarmethod(&self) -> Bar {}
1343     }
1344 
1345     fn myfn(v: &Foo) {
1346         v.mymethod().my~
1347     }
1348     ";
1349 
1350     let got = get_one_completion(src, None);
1351     assert_eq!("mybarmethod", got.matchstr);
1352 }
1353 
1354 #[test]
follows_chained_method_call_returning_self()1355 fn follows_chained_method_call_returning_self() {
1356     let src = "
1357     struct Foo;
1358     impl Foo {
1359         fn mymethod(&self) {}
1360         fn new() -> Self {}
1361     }
1362 
1363     Foo::new().~
1364     ";
1365 
1366     let got = get_only_completion(src, None);
1367     assert_eq!("mymethod", got.matchstr);
1368 }
1369 
1370 #[test]
follows_chained_method_call_on_new_line()1371 fn follows_chained_method_call_on_new_line() {
1372     let src = "
1373     struct Foo;
1374     impl Foo {
1375         fn mymethod(&self) {}
1376         fn new() -> Self {}
1377     }
1378 
1379     Foo::
1380     // comment
1381     new()
1382     .~
1383     ";
1384 
1385     let got = get_only_completion(src, None);
1386     assert_eq!("mymethod", got.matchstr);
1387 }
1388 
1389 #[test]
discards_inner_fns()1390 fn discards_inner_fns() {
1391     let src = "
1392     struct Foo;
1393     impl<T> Foo<T> {
1394         fn mymethod(&self) -> Bar {
1395             fn inner() {
1396             }
1397         }
1398     }
1399 
1400     fn myfn(v: &Foo) {
1401         v.i~
1402     }
1403     ";
1404 
1405     let got = get_all_completions(src, None);
1406     assert!(got.is_empty(), "should not match inner function");
1407 }
1408 
1409 #[test]
differentiates_type_and_value_namespaces()1410 fn differentiates_type_and_value_namespaces() {
1411     let src = "
1412     enum MyEnum{ Foo }
1413     struct Foo;
1414     impl Foo { pub fn new() -> Foo {} }
1415     let l = Foo::n~ew();
1416     ";
1417 
1418     let got = get_definition(src, None);
1419     println!("{}", got.matchstr);
1420     println!("{:?}", got.mtype);
1421     assert_eq!("new", got.matchstr);
1422 }
1423 
1424 #[test]
finds_trait_method()1425 fn finds_trait_method() {
1426     let src = "
1427     pub trait MyTrait {
1428         fn op(self);
1429         fn trait_method(self){}
1430     }
1431 
1432     struct Foo;
1433     impl MyTrait for Foo {
1434         fn op(self) {
1435             self.trait~_method();
1436         }
1437     }";
1438 
1439     let got = get_definition(src, None);
1440     assert_eq!("trait_method", got.matchstr);
1441 }
1442 
1443 #[test]
finds_field_type()1444 fn finds_field_type() {
1445     let src = "
1446     pub struct Blah { subfield: uint }
1447 
1448     pub struct Foo {
1449         myfield : Blah
1450     }
1451 
1452     let f = Foo{ myfield: Blah { subfield: 3}};
1453     f.myfield.subfi~eld
1454     ";
1455 
1456     let got = get_definition(src, None);
1457     assert_eq!("subfield", got.matchstr);
1458 }
1459 
1460 #[test]
finds_tuple_struct_field_type()1461 fn finds_tuple_struct_field_type() {
1462     let src = "
1463     pub struct Blah(Foo);
1464 
1465     pub struct Foo {
1466         bar: usize,
1467     }
1468 
1469     let f = Blah(Foo { bar: 3 });
1470     f.0.b~ar
1471     ";
1472 
1473     let got = get_definition(src, None);
1474     assert_eq!("bar", got.matchstr);
1475 }
1476 
1477 #[test]
finds_a_generic_retval_from_a_function()1478 fn finds_a_generic_retval_from_a_function() {
1479     let src = "
1480     pub struct Blah { subfield: uint }
1481     pub struct Foo<T> {
1482         myfield: T
1483     }
1484     fn myfn() -> Foo<Blah> {}
1485     myfn().myfield.subfi~eld
1486     ";
1487 
1488     let got = get_definition(src, None);
1489     assert_eq!("subfield", got.matchstr);
1490 }
1491 
1492 #[test]
handles_an_enum_option_style_return_type()1493 fn handles_an_enum_option_style_return_type() {
1494     let src = "
1495     pub struct Blah { subfield: uint }
1496     pub enum MyOption<T> {
1497         MySome(T),
1498         MyNone
1499     }
1500     impl MyOption<T> {
1501          pub fn unwrap(&self) -> T {}
1502     }
1503     fn myfn() -> MyOption<Blah> {}
1504     let s = myfn();
1505     s.unwrap().sub~field
1506     ";
1507 
1508     let got = get_definition(src, None);
1509     assert_eq!("subfield", got.matchstr);
1510 }
1511 
1512 #[test]
finds_definition_of_const()1513 fn finds_definition_of_const() {
1514     let src = "
1515     pub const MYCONST:uint = 3;
1516     MYC~ONST
1517     ";
1518 
1519     let got = get_definition(src, None);
1520     assert_eq!("MYCONST", got.matchstr);
1521 }
1522 
1523 #[test]
finds_definition_of_static()1524 fn finds_definition_of_static() {
1525     let src = "
1526     pub static MYSTATIC:uint = 3;
1527     MYS~TATIC
1528     ";
1529 
1530     let got = get_definition(src, None);
1531     assert_eq!("MYSTATIC", got.matchstr);
1532 }
1533 
1534 #[test]
handles_dotdot_before_searchstr()1535 fn handles_dotdot_before_searchstr() {
1536     let src = "
1537     static MYLEN:uint = 30;
1538     let f = [0i32, ..M~YLEN];
1539     ";
1540 
1541     let got = get_definition(src, None);
1542     assert_eq!("MYLEN", got.matchstr);
1543 }
1544 
1545 #[test]
1546 #[ignore]
finds_definition_of_lambda_argument()1547 fn finds_definition_of_lambda_argument() {
1548     let src = "
1549     fn myfn(&|int|) {}
1550     myfn(|a|~a+3);
1551     ";
1552 
1553     let got = get_definition(src, None);
1554     assert_eq!("a", got.matchstr);
1555 }
1556 
1557 #[test]
finds_definition_of_let_tuple()1558 fn finds_definition_of_let_tuple() {
1559     let src = "
1560     let (a, b) = (2,3);
1561     ~a
1562     ";
1563 
1564     let got = get_definition(src, None);
1565     assert_eq!("a", got.matchstr);
1566 }
1567 
1568 #[test]
finds_type_of_tuple_member_via_let_type()1569 fn finds_type_of_tuple_member_via_let_type() {
1570     let src = "
1571     pub struct Blah { subfield: uint }
1572     let (a, b): (uint, Blah);
1573     b.subfi~eld
1574     ";
1575 
1576     let got = get_definition(src, None);
1577     assert_eq!("subfield", got.matchstr);
1578 }
1579 
1580 #[test]
finds_type_of_tuple_member_via_let_expr()1581 fn finds_type_of_tuple_member_via_let_expr() {
1582     let src = "
1583     pub struct Blah { subfield: uint }
1584     let (a, b) = (3, Blah{subfield:3});
1585     b.subfi~eld
1586     ";
1587 
1588     let got = get_definition(src, None);
1589     assert_eq!("subfield", got.matchstr);
1590 }
1591 
1592 #[test]
finds_type_of_struct_member_via_let_expr()1593 fn finds_type_of_struct_member_via_let_expr() {
1594     let src = "
1595     pub struct Blah { subfield: uint }
1596     pub struct Foo { field: Blah }
1597 
1598     let Foo { ref field } = Foo { field: Blah { subfield: 1 }};
1599     field.subfi~eld
1600     ";
1601 
1602     let got = get_definition(src, None);
1603     assert_eq!("subfield", got.matchstr);
1604 }
1605 
1606 #[test]
finds_type_of_tuple_member_via_fn_retval()1607 fn finds_type_of_tuple_member_via_fn_retval() {
1608     let src = "
1609     pub struct Blah { subfield: uint }
1610     fn myfn() -> (uint, Blah) {}
1611     let (a, b) = myfn();
1612     b.subfi~eld
1613     ";
1614 
1615     let got = get_definition(src, None);
1616     assert_eq!("subfield", got.matchstr);
1617 }
1618 
1619 #[test]
finds_type_of_tuple_member_in_fn_arg()1620 fn finds_type_of_tuple_member_in_fn_arg() {
1621     let src = "
1622     pub struct Blah { subfield: uint }
1623     fn myfn(a: uint, (b, c): (uint, Blah)) {
1624         c.s~ubfield
1625     }
1626     ";
1627 
1628     let got = get_definition(src, None);
1629     assert_eq!("subfield", got.matchstr);
1630 }
1631 
1632 #[test]
finds_namespaced_enum_variant()1633 fn finds_namespaced_enum_variant() {
1634     let src = "
1635     pub enum Blah { MyVariant }
1636     Blah::MyVa~riant
1637     ";
1638 
1639     let got = get_definition(src, None);
1640     assert_eq!("MyVariant", got.matchstr);
1641 }
1642 
1643 #[test]
finds_glob_imported_enum_variant()1644 fn finds_glob_imported_enum_variant() {
1645     let src = "
1646     use self::Blah::*;
1647     pub enum Blah { MyVariant, MyVariant2 }
1648     MyVa~riant
1649     ";
1650 
1651     let got = get_definition(src, None);
1652     assert_eq!("MyVariant", got.matchstr);
1653 }
1654 
1655 #[test]
finds_enum_variant_through_recursive_glob_imports()1656 fn finds_enum_variant_through_recursive_glob_imports() {
1657     let src = "
1658     use foo::*;
1659     use Bar::*;
1660 
1661     mod foo {
1662         pub enum Bar { MyVariant, MyVariant2 }
1663     }
1664     MyVa~riant
1665     ";
1666 
1667     let got = get_definition(src, None);
1668     assert_eq!("MyVariant", got.matchstr);
1669 }
1670 
1671 #[test]
1672 #[ignore]
uses_generic_arg_to_resolve_trait_method()1673 fn uses_generic_arg_to_resolve_trait_method() {
1674     let src = "
1675     pub trait MyTrait {
1676         fn trait_method(self){}
1677     }
1678     pub fn doit<T:MyTrait>(stream: &mut T) {
1679         T.trait_met~hod
1680     }
1681     ";
1682 
1683     let got = get_definition(src, None);
1684     assert_eq!("trait_method", got.matchstr);
1685 }
1686 
1687 #[test]
destructures_a_tuplestruct()1688 fn destructures_a_tuplestruct() {
1689     let src = "
1690     pub struct Blah { subfield: uint }
1691     pub struct TupleStruct(Blah);
1692     let TupleStruct(var) = TupleStruct(Blah{subfield:35});
1693     var.su~bfield
1694     ";
1695 
1696     let got = get_definition(src, None);
1697     assert_eq!("subfield", got.matchstr);
1698 }
1699 
1700 #[test]
destructures_a_tuplestruct_with_generic_arg()1701 fn destructures_a_tuplestruct_with_generic_arg() {
1702     let src = "
1703     pub struct Blah { subfield: uint }
1704     pub struct TupleStruct<T>(T);
1705     let a : TupleStruct<Blah> = TupleStruct(Blah{subfield:35});
1706     let TupleStruct(var) = a;
1707     var.su~bfield
1708     ";
1709 
1710     let got = get_definition(src, None);
1711     assert_eq!("subfield", got.matchstr);
1712 }
1713 
1714 #[test]
finds_if_let_ident_defn()1715 fn finds_if_let_ident_defn() {
1716     let src = "
1717     if let MyOption(myvar) = myvar {
1718         myvar~
1719     }
1720     ";
1721 
1722     let got = get_only_completion(src, None);
1723     assert_eq!("myvar", got.matchstr);
1724 }
1725 
1726 #[test]
doesnt_find_if_let_if_not_in_the_subscope()1727 fn doesnt_find_if_let_if_not_in_the_subscope() {
1728     let src = "
1729     let myvar = 3u32;
1730     if let MyOption(myvar) = myvar {
1731         myvar
1732     }
1733     my~var
1734     ";
1735 
1736     let got = get_definition(src, None);
1737     assert_eq!("myvar", got.matchstr);
1738     assert_eq!(9, got.point.0);
1739 }
1740 
1741 #[test]
finds_rebound_var_in_iflet()1742 fn finds_rebound_var_in_iflet() {
1743     let src = "
1744     let o: MyOption<Blah>;
1745     if let MyOption::MySome(o) = o {
1746         ~o
1747     }
1748     ";
1749 
1750     let got = get_definition(src, None);
1751     assert_eq!(56, got.point.0);
1752 }
1753 
1754 #[test]
handles_if_let()1755 fn handles_if_let() {
1756     let src = "
1757     pub struct Blah { subfield: uint }
1758     pub enum MyOption<T> {
1759         MySome(T),
1760         MyNone
1761     }
1762     let o: MyOption<Blah>;
1763     if let MyOption::MySome(a) = o {
1764         a.sub~field
1765     }
1766     ";
1767 
1768     let got = get_definition(src, None);
1769     assert_eq!("subfield", got.matchstr);
1770 }
1771 
1772 #[test]
handles_if_let_as_expression()1773 fn handles_if_let_as_expression() {
1774     let src = "
1775     pub struct Blah { subfield: uint }
1776     pub enum MyOption<T> {
1777         MySome(T),
1778         MyNone
1779     }
1780     let o: MyOption<Blah>;
1781     let foo = if let MyOption::MySome(a) = o { // iflet is an expression
1782         a.sub~field
1783     };
1784     ";
1785 
1786     let got = get_definition(src, None);
1787     assert_eq!("subfield", got.matchstr);
1788 }
1789 
1790 #[test]
finds_match_arm_var()1791 fn finds_match_arm_var() {
1792     let src = "
1793     match foo {
1794        Some(a) => ~a
1795     ";
1796 
1797     let got = get_definition(src, None);
1798     assert_eq!("a", got.matchstr);
1799 }
1800 
1801 #[test]
finds_match_arm_var_in_scope()1802 fn finds_match_arm_var_in_scope() {
1803     let src = "
1804     match foo {
1805        Some(a) => { ~a }
1806     ";
1807 
1808     let got = get_definition(src, None);
1809     assert_eq!("a", got.matchstr);
1810 }
1811 
1812 #[test]
finds_match_arm_enum()1813 fn finds_match_arm_enum() {
1814     let src = "
1815     enum MyEnum {
1816         Foo,
1817         Bar
1818     }
1819     match foo {
1820        MyEnum::Foo~ => 1,
1821        MyEnum::Bar => 2
1822     ";
1823 
1824     let got = get_definition(src, None);
1825     assert_eq!("Foo", got.matchstr);
1826 }
1827 
1828 #[test]
finds_match_arm_var_with_nested_match()1829 fn finds_match_arm_var_with_nested_match() {
1830     let src = "
1831     match foo {
1832        bar => {something}
1833        Some(a) => {
1834                let b = match blah {
1835                            None => ()
1836                }
1837                ~a
1838        }
1839     ";
1840 
1841     let got = get_definition(src, None);
1842     assert_eq!("a", got.matchstr);
1843 }
1844 
1845 #[test]
gets_type_via_match_arm()1846 fn gets_type_via_match_arm() {
1847     let src = "
1848     pub struct Blah { subfield: uint }
1849     pub enum MyOption<T> {
1850         MySome(T),
1851         MyNone
1852     }
1853     let o: MyOption<Blah>;
1854     match o {
1855         MyOption::MySome(a) => a.subfi~eld
1856     ";
1857 
1858     let got = get_definition(src, None);
1859     assert_eq!("subfield", got.matchstr);
1860 }
1861 
1862 #[test]
handles_default_arm()1863 fn handles_default_arm() {
1864     let src = "
1865     let o: MyOption<Blah>;
1866     match o {
1867         Foo => { }
1868         _ => ~o
1869     }
1870     ";
1871 
1872     let got = get_definition(src, None);
1873     assert_eq!("o", got.matchstr);
1874     assert_eq!(9, got.point.0);
1875 }
1876 
1877 #[test]
doesnt_match_rhs_of_let_in_same_stmt()1878 fn doesnt_match_rhs_of_let_in_same_stmt() {
1879     let src = "
1880     let a = 3;      // <--- should match this 'a'
1881     let a = ~a + 2;  // not this one
1882     ";
1883 
1884     let got = get_definition(src, None);
1885     assert_eq!("a", got.matchstr);
1886     assert_eq!(9, got.point.0);
1887 }
1888 
1889 #[test]
finds_unsafe_fn()1890 fn finds_unsafe_fn() {
1891     let src = "
1892     unsafe fn foo() {}
1893 
1894     fn bar() {
1895         f~oo()
1896     }
1897     ";
1898 
1899     let got = get_definition(src, None);
1900     assert_eq!(got.matchstr, "foo");
1901     assert_eq!(got.point.0, 15);
1902 }
1903 
1904 #[test]
completes_methods_on_deref_type()1905 fn completes_methods_on_deref_type() {
1906     let modsrc = "
1907     pub struct B {
1908         c: C,
1909     }
1910 
1911     pub struct C;
1912 
1913     pub trait GetOne {
1914         fn one(&self) -> u32 { 1u32 }
1915     }
1916 
1917     impl GetOne for C {}
1918 
1919     impl Deref for B {
1920         type Target = C;
1921         fn deref(&self) -> &C {
1922             &self.c
1923         }
1924     }
1925     ";
1926     let src = "
1927     mod mymod;
1928     use mymod::{B, C, GetOne};
1929 
1930     fn main() {
1931         let b: B = B{ c: C};
1932         b.o~
1933     }
1934     ";
1935 
1936     let dir = TmpDir::new();
1937     let _mymod = dir.write_file("mymod.rs", modsrc);
1938     let got = get_one_completion(src, Some(dir));
1939     assert_eq!(got.matchstr, "one");
1940 }
1941 
1942 #[test]
finds_type_of_struct_field_reference()1943 fn finds_type_of_struct_field_reference() {
1944     let src = "
1945     struct Dolor { sit: u8 }
1946 
1947     struct Lorem<'a> { ipsum: &'a Dolor }
1948 
1949     impl<'a> Lorem<'a> {
1950         fn sit(&self) {
1951             let _ = self.ipsum.s~it;
1952         }
1953     }
1954     ";
1955 
1956     let got = get_definition(src, None);
1957     assert_eq!("sit", got.matchstr);
1958 }
1959 
1960 #[test]
finds_self_param_when_fn_has_generic_closure_arg()1961 fn finds_self_param_when_fn_has_generic_closure_arg() {
1962     // issue #508
1963     let src = "
1964     struct MyOption;
1965 
1966     impl MyOption {
1967         // needs to find 'self' here to see it is a method
1968         pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Option<U> {
1969         }
1970     }
1971 
1972     let a: MyOption;
1973     a.~map()
1974     ";
1975 
1976     let got = get_definition(src, None);
1977     assert_eq!("map", got.matchstr);
1978 }
1979 
1980 #[test]
completes_static_method_containing_self()1981 fn completes_static_method_containing_self() {
1982     let src = "
1983     struct X;
1984 
1985     impl X {
1986         fn foo<T>(_: T) {
1987             struct Y;
1988             impl Y {
1989                 fn bar(&self) {}
1990             }
1991         }
1992     }
1993 
1994     X::~foo(0);
1995     ";
1996 
1997     let got = get_definition(src, None);
1998     assert_eq!("foo", got.matchstr);
1999 }
2000 
2001 #[test]
completes_methods_on_deref_generic_type()2002 fn completes_methods_on_deref_generic_type() {
2003     let modsrc = "
2004     pub struct B<T> {
2005         c: T,
2006     }
2007 
2008     pub struct C;
2009 
2010     pub trait GetOne {
2011         fn one(&self) -> u32 { 1u32 }
2012     }
2013 
2014     impl GetOne for C {}
2015 
2016     impl<T> Deref for B<T> {
2017         type Target = T;
2018         fn deref(&self) -> &T {
2019             &self.c
2020         }
2021     }
2022     ";
2023     let src = "
2024     mod mymod;
2025     use mymod::{B, C, GetOne};
2026 
2027     fn main() {
2028         let b: B<C> = B{ c: C};
2029         b.o~
2030     }
2031     ";
2032 
2033     let dir = TmpDir::new();
2034     let _mymod = dir.write_file("mymod.rs", modsrc);
2035     let got = get_one_completion(src, Some(dir));
2036     assert_eq!(got.matchstr, "one");
2037 }
2038 
2039 #[test]
completes_multiple_use_bracket()2040 fn completes_multiple_use_bracket() {
2041     // issue # 96
2042     // wo: without bracket, wi: with bracket
2043     let modfile = "
2044     pub struct StarWars {
2045         pub Vadar: u8,
2046     };
2047     pub struct StarTrek {
2048         pub Spock: u8,
2049     };";
2050     let srcwo = "
2051     mod modfile1;
2052     use modfile1::~S
2053     ";
2054     let srcwi = "
2055     mod modfile1;
2056     use modfile1::{~S
2057     ";
2058 
2059     let dir = TmpDir::new();
2060     let _mod1 = dir.write_file("modfile1.rs", modfile);
2061     let gotwo = get_all_completions(srcwo, Some(dir));
2062     let dir = TmpDir::new();
2063     let _mod1 = dir.write_file("modfile1.rs", modfile);
2064     let gotwi = get_all_completions(srcwi, Some(dir));
2065 
2066     assert_eq!(gotwo.len(), gotwi.len());
2067     for (wo, wi) in gotwo.into_iter().zip(gotwi) {
2068         assert_eq!(wo.matchstr, wi.matchstr);
2069     }
2070 }
2071 
2072 #[test]
completes_multiple_use_comma()2073 fn completes_multiple_use_comma() {
2074     // issue # 96
2075     // wo: without comma, wi: with comma
2076     let modfile = "
2077     pub struct StarWars {
2078         pub Kenobi: u8,
2079     };
2080     pub struct StarTrek {
2081         pub Spock: u8,
2082     };";
2083     let srcwo = "
2084     mod modfile2;
2085     use modfile2::~S
2086     ";
2087     let srcwi = "
2088     mod modfile2;
2089     use modfile2::{StarWars, ~S
2090     ";
2091 
2092     let dir = TmpDir::new();
2093     let _mod2 = dir.write_file("modfile2.rs", modfile);
2094     let gotwo = get_all_completions(srcwo, Some(dir));
2095     let dir = TmpDir::new();
2096     let _mod2 = dir.write_file("modfile2.rs", modfile);
2097     let gotwi = get_all_completions(srcwi, Some(dir));
2098 
2099     assert_eq!(gotwo.len(), gotwi.len());
2100     for (wo, wi) in gotwo.into_iter().zip(gotwi) {
2101         assert_eq!(wo.matchstr, wi.matchstr);
2102     }
2103 }
2104 
2105 #[test]
completes_multiple_use_newline()2106 fn completes_multiple_use_newline() {
2107     let src = "
2108     mod foo {
2109         pub struct Bar;
2110 
2111         pub fn myfn() {}
2112     }
2113 
2114     fn main() {
2115         use foo::{
2116             Bar,
2117             my~fn
2118         };
2119 
2120         myfn();
2121     }
2122     ";
2123 
2124     let got = get_all_completions(src, None);
2125     assert_eq!(got.len(), 1);
2126     assert_eq!(got[0].matchstr, "myfn");
2127 }
2128 
2129 #[test]
completes_trait_methods_in_trait_impl()2130 fn completes_trait_methods_in_trait_impl() {
2131     let src = "
2132     mod sub {
2133         pub trait Trait {
2134             fn traitf() -> bool;
2135             fn traitm(&self) -> bool;
2136         }
2137 
2138         pub struct Foo(bool);
2139 
2140         impl Trait for Foo {
2141             fn traitf() -> bool { false }
2142             fn traitm~(&self) -> bool { true }
2143         }
2144     }
2145     ";
2146 
2147     let got = get_one_completion(src, None);
2148     assert_eq!(got.matchstr, "traitm");
2149     assert_eq!(got.contextstr, "fn traitm(&self) -> bool");
2150 }
2151 
2152 /// Check if user is offered a completion for a static function defined by a trait.
2153 #[test]
completes_trait_fn_in_trait_impl()2154 fn completes_trait_fn_in_trait_impl() {
2155     let src = "
2156     mod sub {
2157         pub trait Trait {
2158             fn traitf() -> bool;
2159             fn traitm(&self) -> bool;
2160         }
2161 
2162         pub struct Foo(bool);
2163 
2164         impl Trait for Foo {
2165             fn traitf~() -> bool { false }
2166             fn traitm(&self) -> bool { true }
2167         }
2168     }
2169     ";
2170 
2171     let got = get_one_completion(src, None);
2172     assert_eq!(got.matchstr, "traitf");
2173     assert_eq!(got.contextstr, "fn traitf() -> bool");
2174 }
2175 
2176 #[test]
completes_optional_trait_fn_in_trait_impl()2177 fn completes_optional_trait_fn_in_trait_impl() {
2178     let src = "
2179     mod sub {
2180         pub trait Trait {
2181             fn traitf() -> bool {
2182                 true
2183             }
2184 
2185             fn traitm(&self) -> bool;
2186         }
2187 
2188         pub struct Foo(bool);
2189 
2190         impl Trait for Foo {
2191             fn traitf~() -> bool { false }
2192             fn traitm(&self) -> bool { true }
2193         }
2194     }
2195     ";
2196 
2197     let got = get_one_completion(src, None);
2198     assert_eq!(got.matchstr, "traitf");
2199     assert_eq!(got.contextstr, "fn traitf() -> bool");
2200 }
2201 
2202 /// Addresses https://github.com/racer-rust/racer/issues/680. In this case,
2203 /// `sub` should not be interpreted as a method name; it didn't appear after
2204 /// `fn` and therefore would need `Self::`, `self.` or another qualified name
2205 /// to be syntactically valid.
2206 #[test]
finds_mod_with_same_name_as_trait_method_in_sig()2207 fn finds_mod_with_same_name_as_trait_method_in_sig() {
2208     let src = "
2209     mod sub {
2210         pub struct Formatter;
2211 
2212         pub trait Fmt {
2213             fn sub(&self, f: &Formatter);
2214         }
2215     }
2216 
2217     struct Sample;
2218 
2219     impl sub::Fmt for Sample {
2220         fn sub(&self, f: &sub::Fo~rmatter) {
2221 
2222         }
2223     }
2224     ";
2225 
2226     let got = get_one_completion(src, None);
2227     assert_eq!(got.matchstr, "Formatter");
2228 }
2229 
2230 /// Also addresses issue #680.
2231 #[test]
finds_mod_with_same_name_as_trait_method_in_body()2232 fn finds_mod_with_same_name_as_trait_method_in_body() {
2233     let src = "
2234     mod sub {
2235         pub struct Formatter;
2236 
2237         pub trait Fmt {
2238             fn sub(&self) -> sub::Formatter;
2239         }
2240     }
2241 
2242     struct Sample;
2243 
2244     impl sub::Fmt for Sample {
2245         fn sub(&self) -> sub::Formatter {
2246             sub::Fo~rmatter
2247         }
2248     }
2249     ";
2250 
2251     let got = get_one_completion(src, None);
2252     assert_eq!(got.matchstr, "Formatter");
2253 }
2254 
2255 /// Also addresses #680
2256 #[test]
finds_fmt_formatter()2257 fn finds_fmt_formatter() {
2258     let src = r#"
2259     use std::fmt;
2260 
2261     struct Foo;
2262 
2263     impl fmt::Display for Foo {
2264         fn fmt(&self, f: &mut fmt::Formatt~er) -> fmt::Result {
2265             write!(f, "Hello")
2266         }
2267     }
2268     "#;
2269 
2270     let got = get_all_completions(src, None);
2271     assert!(!got.is_empty());
2272     assert_eq!(got[0].matchstr, "Formatter");
2273 }
2274 
2275 /// Also addresses #680
2276 #[test]
finds_fmt_method()2277 fn finds_fmt_method() {
2278     let src = r#"
2279     use std::fmt;
2280 
2281     struct Foo;
2282 
2283     impl fmt::Display for Foo {
2284         fn fm~t(&self, f: &mut fmt::Formatter) -> fmt::Result {
2285             write!(f, "Hello")
2286         }
2287     }
2288     "#;
2289 
2290     let got = get_only_completion(src, None);
2291     assert_eq!(got.matchstr, "fmt");
2292     assert_eq!(got.mtype, MatchType::Function);
2293 }
2294 
2295 #[test]
finds_field_with_same_name_as_method()2296 fn finds_field_with_same_name_as_method() {
2297     let src = "
2298     struct Foo { same_name: uint }
2299     impl Foo { fn same_name(&self){} }
2300     let a: Foo;
2301     a.same_na~me;
2302     ";
2303 
2304     let got = get_definition(src, None);
2305     assert_eq!("same_name", got.matchstr);
2306     assert_eq!(MatchType::StructField, got.mtype);
2307 }
2308 
2309 #[test]
finds_method_with_same_name_as_field()2310 fn finds_method_with_same_name_as_field() {
2311     let src = "
2312     struct Foo { same_name: uint }
2313     impl Foo { fn same_name(&self){}}
2314     let a: Foo;
2315     a.same_na~me();
2316     ";
2317     let got = get_definition(src, None);
2318     assert_eq!("same_name", got.matchstr);
2319     assert_eq!(MatchType::Function, got.mtype);
2320 }
2321 
2322 #[test]
finds_self()2323 fn finds_self() {
2324     let src = "
2325     struct Foo;
2326     impl Foo {
2327         fn foo() {
2328             Se~lf
2329         }
2330     }
2331     ";
2332 
2333     let got = get_definition(src, None);
2334     assert_eq!("Foo", got.matchstr);
2335 }
2336 
2337 #[test]
finds_self_referenced_functions()2338 fn finds_self_referenced_functions() {
2339     let src = "
2340     struct Foo;
2341     impl Foo {
2342         fn foo() {
2343             Self::myfun~ction
2344         }
2345         fn myfunction() {}
2346     }
2347     ";
2348 
2349     let got = get_definition(src, None);
2350     assert_eq!("myfunction", got.matchstr);
2351 }
2352 
2353 #[test]
closure_bracket_scope()2354 fn closure_bracket_scope() {
2355     let src = "
2356     fn main() {
2357         let y = Some(5);
2358         y.map(| x | { x~ } );
2359     }
2360     ";
2361 
2362     let got = get_definition(src, None);
2363     assert_eq!("x", got.matchstr);
2364     assert_eq!("| x |", got.contextstr);
2365 }
2366 
2367 #[test]
closure_bracket_scope_multiple_args()2368 fn closure_bracket_scope_multiple_args() {
2369     let src = "
2370     fn main() {
2371         let y = Some(5);
2372         y.map(| x,y,z,u | { x~ } );
2373     }
2374     ";
2375 
2376     let got = get_definition(src, None);
2377     assert_eq!("x", got.matchstr);
2378     assert_eq!("| x,y,z,u |", got.contextstr);
2379 }
2380 
2381 #[test]
closure_bracket_scope_multiple_args_different_definition()2382 fn closure_bracket_scope_multiple_args_different_definition() {
2383     let src = "
2384     fn main() {
2385         let y = Some(5);
2386         y.map(| x,y,z,u | { z~ } );
2387     }
2388     ";
2389 
2390     let got = get_definition(src, None);
2391     assert_eq!("z", got.matchstr);
2392     assert_eq!("| x,y,z,u |", got.contextstr);
2393 }
2394 
2395 #[test]
closure_bracket_scope_overwrite()2396 fn closure_bracket_scope_overwrite() {
2397     let src = "
2398     fn main() {
2399         let y = Some(5);
2400         y.map(| x, y | { y~ } );
2401     }
2402     ";
2403 
2404     let got = get_definition(src, None);
2405     assert_eq!("y", got.matchstr);
2406     assert_eq!("| x, y |", got.contextstr);
2407 }
2408 
2409 #[test]
closure_bracket_scope_with_types()2410 fn closure_bracket_scope_with_types() {
2411     let src = "
2412     fn main() {
2413         let y = Some(5);
2414         y.map(| x: i32, y: String | { y~ } );
2415     }
2416     ";
2417 
2418     let got = get_definition(src, None);
2419     assert_eq!("y", got.matchstr);
2420     assert_eq!("| x: i32, y: String |", got.contextstr);
2421 }
2422 
2423 #[test]
closure_bracket_scope_find_outside()2424 fn closure_bracket_scope_find_outside() {
2425     let src = "
2426     fn main() {
2427         let y = Some(5);
2428         y.map(| x: i32 | { y~ } );
2429     }
2430     ";
2431 
2432     let got = get_definition(src, None);
2433     assert_eq!("y", got.matchstr);
2434     assert_eq!("let y = Some(5);", got.contextstr);
2435 }
2436 
2437 #[test]
closure_scope()2438 fn closure_scope() {
2439     let src = "
2440     fn main() {
2441         let y = Some(5);
2442         y.map(| x | x~ );
2443     }
2444     ";
2445 
2446     let got = get_definition(src, None);
2447     assert_eq!("x", got.matchstr);
2448     assert_eq!("| x |", got.contextstr);
2449 }
2450 
2451 #[test]
closure_scope_multiple_args()2452 fn closure_scope_multiple_args() {
2453     let src = "
2454     fn main() {
2455         let y = Some(5);
2456         y.map(| x,y,z,u | x~ );
2457     }
2458     ";
2459 
2460     let got = get_definition(src, None);
2461     assert_eq!("x", got.matchstr);
2462     assert_eq!("| x,y,z,u |", got.contextstr);
2463 }
2464 
2465 #[test]
closure_scope_multiple_args_different_definition()2466 fn closure_scope_multiple_args_different_definition() {
2467     let src = "
2468     fn main() {
2469         let y = Some(5);
2470         y.map(| x,y,z,u | z~ );
2471     }
2472     ";
2473 
2474     let got = get_definition(src, None);
2475     assert_eq!("z", got.matchstr);
2476     assert_eq!("| x,y,z,u |", got.contextstr);
2477 }
2478 
2479 #[test]
closure_scope_overwrite()2480 fn closure_scope_overwrite() {
2481     let src = "
2482     fn main() {
2483         let y = Some(5);
2484         y.map(| x, y | y~ );
2485     }
2486     ";
2487 
2488     let got = get_definition(src, None);
2489     assert_eq!("y", got.matchstr);
2490     assert_eq!("| x, y |", got.contextstr);
2491 }
2492 
2493 #[test]
closure_scope_with_types()2494 fn closure_scope_with_types() {
2495     let src = "
2496     fn main() {
2497         let y = Some(5);
2498         y.map(| x: i32, y: String | y~ );
2499     }
2500     ";
2501 
2502     let got = get_definition(src, None);
2503     assert_eq!("y", got.matchstr);
2504     assert_eq!("| x: i32, y: String |", got.contextstr);
2505 }
2506 
2507 #[test]
finds_impl_with_bang()2508 fn finds_impl_with_bang() {
2509     let src = "
2510     struct Foo;
2511     impl Foo {
2512         fn invert(&self, b: bool) -> bool { !b }
2513 
2514         fn tst(&self) -> bool {
2515             self.inv~ert(false)
2516         }
2517     ";
2518 
2519     let got = get_definition(src, None);
2520     assert_eq!("invert", got.matchstr);
2521 }
2522 
2523 #[test]
ignores_impl_macro()2524 fn ignores_impl_macro() {
2525     let src = "
2526     struct Foo;
2527     impl!(Foo);
2528 
2529     impl Foo {
2530         fn tst(&self) -> bool {
2531             self.ts~t()
2532         }
2533     ";
2534 
2535     let got = get_definition(src, None);
2536     assert_eq!("tst", got.matchstr);
2537 }
2538 
2539 #[test]
closure_scope_dont_match_type_annotations()2540 fn closure_scope_dont_match_type_annotations() {
2541     let src = "
2542     struct Foo;
2543     fn main() {
2544         let y = Some(Foo);
2545         y.map(|x: Foo| Fo~o);
2546     }
2547     ";
2548 
2549     let got = get_definition(src, None);
2550     println!("{:?}", got);
2551     assert!(got.mtype.is_struct());
2552     assert_eq!(2, got.coords.unwrap().row.0);
2553 }
2554 
2555 /// The variable `i` doesn't exist in `foo`, so trying to get the definition should
2556 /// fail.
2557 #[test]
2558 #[should_panic]
closure_scope_dont_match_bitwise_or()2559 fn closure_scope_dont_match_bitwise_or() {
2560     let src = "
2561     fn foo() {
2562         i~
2563     }
2564     fn bar() {
2565         let i = 0;
2566         let x = 0 | i;
2567     }
2568     fn baz() {
2569         // 1 || 2;
2570     }
2571     ";
2572 
2573     let got = get_definition(src, None);
2574     println!("Unexpectedly found definition: {:?}", got);
2575 }
2576 
2577 #[test]
closure_scope_find_outside()2578 fn closure_scope_find_outside() {
2579     let src = "
2580     fn main() {
2581         let y = Some(5);
2582         y.map(| x: i32 | y~ );
2583     }
2584     ";
2585 
2586     let got = get_definition(src, None);
2587     assert_eq!("y", got.matchstr);
2588     assert_eq!("let y = Some(5);", got.contextstr);
2589 }
2590 
2591 #[test]
closure_scope_with_newlines()2592 fn closure_scope_with_newlines() {
2593     let src = "
2594     fn main() {
2595         let y = Some(5);
2596         y.map(|
2597 
2598 
2599 x: i32
2600 
2601 
2602 
2603 | x~ );
2604     }
2605     ";
2606 
2607     let got = get_definition(src, None);
2608     assert_eq!("x", got.matchstr);
2609     assert_eq!(
2610         "|
2611 
2612 
2613 x: i32
2614 
2615 
2616 
2617 |",
2618         got.contextstr
2619     );
2620 }
2621 
2622 #[test]
closure_bracket_scope_with_newlines()2623 fn closure_bracket_scope_with_newlines() {
2624     let src = "
2625     fn main() {
2626         let y = Some(5);
2627         y.map(|
2628 
2629 
2630 x: i32
2631 
2632 
2633 
2634 | {x~} );
2635     }
2636     ";
2637 
2638     let got = get_definition(src, None);
2639     assert_eq!("x", got.matchstr);
2640     assert_eq!(
2641         "|
2642 
2643 
2644 x: i32
2645 
2646 
2647 
2648 |",
2649         got.contextstr
2650     );
2651 }
2652 
2653 #[test]
closure_scope_nested()2654 fn closure_scope_nested() {
2655     let src = "
2656     fn main() {
2657         let y = Some(5);
2658         y.map(| x: i32 | y.map(|z| z~) );
2659     }
2660     ";
2661 
2662     let got = get_definition(src, None);
2663     assert_eq!("z", got.matchstr);
2664     assert_eq!("|z|", got.contextstr);
2665 }
2666 
2667 #[test]
closure_bracket_scope_nested()2668 fn closure_bracket_scope_nested() {
2669     let src = "
2670     fn main() {
2671         let y = Some(5);
2672         y.map(| x: i32 | { y.map(|z| { z~ }) });
2673     }
2674     ";
2675 
2676     let got = get_definition(src, None);
2677     assert_eq!("z", got.matchstr);
2678     assert_eq!("|z|", got.contextstr);
2679 }
2680 
2681 #[test]
closure_scope_nested_math_outside()2682 fn closure_scope_nested_math_outside() {
2683     let src = "
2684     fn main() {
2685         let y = Some(5);
2686         y.map(| x: i32 | y.map(|z| x~) );
2687     }
2688     ";
2689 
2690     let got = get_definition(src, None);
2691     assert_eq!("x", got.matchstr);
2692     assert_eq!("| x: i32 |", got.contextstr);
2693 }
2694 
2695 #[test]
closure_bracket_scope_nested_match_outside()2696 fn closure_bracket_scope_nested_match_outside() {
2697     let src = "
2698     fn main() {
2699         let y = Some(5);
2700         y.map(| x: i32 | { y.map(|z| { x~ }) });
2701     }
2702     ";
2703 
2704     let got = get_definition(src, None);
2705     assert_eq!("x", got.matchstr);
2706     assert_eq!("| x: i32 |", got.contextstr);
2707 }
2708 
2709 // Issue: https://github.com/racer-rust/racer/issues/754
2710 #[test]
closure_dont_detect_normal_pipes()2711 fn closure_dont_detect_normal_pipes() {
2712     let src = "
2713     enum Fruit {
2714         Apple = 1,
2715     }
2716 
2717     fn foo(ty: Fruit) -> bool {
2718         (1 as u8 | Fruit~::Apple as u8) == Fruit::Apple as u8
2719     }
2720 
2721     fn bar(ty: Fruit) -> bool {
2722         match ty {
2723             Fruit::Apple |
2724             Fruit::Apple => {
2725                 false
2726             }
2727         }
2728     }
2729     ";
2730 
2731     let got = get_definition(src, None);
2732     assert_eq!("Fruit", got.matchstr);
2733     assert!(got.mtype.is_enum());
2734 }
2735 
2736 #[test]
closure_test_curly_brackets_in_args()2737 fn closure_test_curly_brackets_in_args() {
2738     let src = "
2739     struct Foo {
2740         bar: u16
2741     }
2742 
2743     fn example() -> Result<Foo, ()> {
2744         Ok(Foo { bar: 10 })
2745     }
2746 
2747     fn main() {
2748         example().and_then(|Foo { bar }| { println!(\"{}\", bar~); Ok(()) });
2749     }
2750     ";
2751 
2752     let got = get_definition(src, None);
2753     assert_eq!("bar", got.matchstr);
2754     assert_eq!("|Foo { bar }|", got.contextstr);
2755 }
2756 
2757 #[test]
closure_test_multiple_curly_brackets_in_args()2758 fn closure_test_multiple_curly_brackets_in_args() {
2759     let src = "
2760     struct Foo {
2761         bar: u16
2762     }
2763 
2764     fn example() -> Result<Foo, ()> {
2765         Ok(Foo { bar: 10 })
2766     }
2767 
2768     fn main() {
2769         example().and_then(|Foo { bar }, Foo { ex }, Foo { b }| { println!(\"{}\", bar~); Ok(()) });
2770     }
2771     ";
2772 
2773     let got = get_definition(src, None);
2774     assert_eq!("bar", got.matchstr);
2775     assert_eq!("|Foo { bar }, Foo { ex }, Foo { b }|", got.contextstr);
2776 }
2777 
2778 #[test]
crate_restricted_fn_completes()2779 fn crate_restricted_fn_completes() {
2780     let src = r#"
2781     pub(crate) fn do_stuff() {
2782         println!("Hello");
2783     }
2784 
2785     fn more_stuff() {
2786         do_~stuff();
2787     }
2788     "#;
2789 
2790     let got = get_all_completions(src, None);
2791     assert_eq!(1, got.len());
2792     assert_eq!("do_stuff", got[0].matchstr);
2793 }
2794 
2795 #[test]
mod_restricted_fn_completes()2796 fn mod_restricted_fn_completes() {
2797     let src = r#"
2798     pub(in some::place_where) fn do_stuff() {
2799         println!("Hello");
2800     }
2801 
2802     fn more_stuff() {
2803         do_~stuff();
2804     }
2805     "#;
2806 
2807     let got = get_all_completions(src, None);
2808     assert_eq!(1, got.len());
2809     assert_eq!("do_stuff", got[0].matchstr);
2810 }
2811 
2812 #[test]
finds_definition_of_fn_arg()2813 fn finds_definition_of_fn_arg() {
2814     let src = r#"
2815     pub fn say_hello(name: String) {
2816         println!("{}", nam~e);
2817     }
2818     "#;
2819 
2820     let got = get_definition(src, None);
2821     assert_eq!(got.matchstr, "name");
2822 }
2823 
2824 #[test]
finds_definition_of_crate_restricted_fn_arg()2825 fn finds_definition_of_crate_restricted_fn_arg() {
2826     let src = r#"
2827     pub(crate) fn say_hello(name: String) {
2828         println!("{}", nam~e);
2829     }
2830     "#;
2831 
2832     let got = get_definition(src, None);
2833     assert_eq!(got.matchstr, "name");
2834 }
2835 
2836 /// This test should work, but may be failing because there is no `mod foo`
2837 /// in the generated code we parse to get the signature.
2838 #[test]
2839 #[ignore]
finds_definition_of_mod_restricted_fn_arg()2840 fn finds_definition_of_mod_restricted_fn_arg() {
2841     let src = r#"
2842     pub(in foo) fn say_hello(name: String) {
2843         println!("{}", nam~e);
2844     }
2845     "#;
2846 
2847     let got = get_definition(src, None);
2848     assert_eq!(got.matchstr, "name");
2849 }
2850 
2851 #[test]
finds_definition_of_super_restricted_fn()2852 fn finds_definition_of_super_restricted_fn() {
2853     let src = r#"
2854     pub(super) fn do_stuff() {
2855         println!("Hello");
2856     }
2857 
2858     fn more_stuff() {
2859         do_~stuff();
2860     }
2861     "#;
2862 
2863     let got = get_definition(src, None);
2864     assert_eq!("do_stuff", got.matchstr);
2865 }
2866 
2867 #[test]
crate_restricted_struct_completes()2868 fn crate_restricted_struct_completes() {
2869     let src = r#"
2870     mod codegen {
2871         pub(crate) struct Foo {
2872             pub bar: String,
2873         }
2874 
2875         fn stuff(f: Foo) -> String {
2876             f.b~ar
2877         }
2878     }
2879     "#;
2880 
2881     let got = get_all_completions(src, None);
2882     assert_eq!(1, got.len());
2883     assert_eq!("bar", got[0].matchstr);
2884 }
2885 
2886 #[test]
crate_restricted_named_struct_field_completes()2887 fn crate_restricted_named_struct_field_completes() {
2888     let src = r#"
2889     mod codegen {
2890         pub struct Foo {
2891             pub(crate) bar: String,
2892         }
2893 
2894         fn stuff(f: Foo) -> String {
2895             f.b~ar
2896         }
2897     }
2898     "#;
2899 
2900     let got = get_all_completions(src, None);
2901     assert_eq!(1, got.len());
2902     assert_eq!("bar", got[0].matchstr);
2903 }
2904 
2905 #[test]
crate_restricted_static_method_completes()2906 fn crate_restricted_static_method_completes() {
2907     let src = r#"
2908     mod codegen {
2909         pub struct Foo {
2910             pub bar: String,
2911         }
2912 
2913         impl Foo {
2914             pub(crate) fn with_bar(b: String) -> Self {
2915                 Foo { bar: b }
2916             }
2917         }
2918 
2919         fn stuff() -> String {
2920             Foo::wi~th_bar("Hello".to_string()).bar
2921         }
2922     }
2923     "#;
2924 
2925     let got = get_all_completions(src, None);
2926     assert_eq!(1, got.len());
2927     assert_eq!("with_bar", got[0].matchstr);
2928 }
2929 
2930 #[test]
crate_restricted_impl_method_completes()2931 fn crate_restricted_impl_method_completes() {
2932     let src = r#"
2933     mod codegen {
2934         pub struct Foo {
2935             bar: String,
2936         }
2937 
2938         impl Foo {
2939             pub(crate) fn get_bar(&self) -> &str {
2940                 &self.bar
2941             }
2942         }
2943 
2944         fn stuff(f: Foo) -> String {
2945             f.ge~t_bar().clone()
2946         }
2947     }
2948     "#;
2949 
2950     let got = get_all_completions(src, None);
2951     assert_eq!(1, got.len());
2952     assert_eq!("get_bar", got[0].matchstr);
2953 }
2954 
2955 /// This test _should_ pass, but the bogofile produces errors:
2956 ///
2957 /// ```ignore
2958 /// error: expected identifier, found keyword `in`
2959 ///  --> bogofile:1:5
2960 ///   |
2961 /// 1 | pub(in codegen) struct Foo {
2962 ///   |     ^^
2963 ///
2964 /// error: expected one of `)` or `::`, found `codegen`
2965 ///  --> bogofile:1:8
2966 ///   |
2967 /// 1 | pub(in codegen) struct Foo {
2968 ///   |        ^^^^^^^
2969 /// ```
2970 #[test]
2971 #[ignore]
mod_restricted_struct_completes()2972 fn mod_restricted_struct_completes() {
2973     let src = r#"
2974     mod codegen {
2975         pub(in codegen) struct Foo {
2976             pub bar: String,
2977         }
2978 
2979         fn stuff(f: Foo) -> String {
2980             f.b~ar
2981         }
2982     }
2983     "#;
2984 
2985     let got = get_all_completions(src, None);
2986     assert_eq!(1, got.len());
2987     assert_eq!("bar", got[0].matchstr);
2988 }
2989 
2990 #[test]
completes_for_global_path_in_fn_return()2991 fn completes_for_global_path_in_fn_return() {
2992     let src = "
2993     mod bar {
2994         pub struct Foo;
2995     }
2996 
2997     mod baz {
2998         fn foo() -> ::bar::F~oo {
2999             Foo
3000         }
3001     }
3002 
3003     fn main() {}
3004     ";
3005 
3006     let got = get_one_completion(src, None);
3007     assert_eq!(got.matchstr, "Foo");
3008 }
3009 
3010 #[test]
completes_for_global_path_in_trait_impl_decl()3011 fn completes_for_global_path_in_trait_impl_decl() {
3012     let src = "
3013     mod foo {
3014         pub trait Bar {}
3015     }
3016 
3017     mod baz {
3018         pub struct Test;
3019 
3020         impl ::foo::~Bar for Test {}
3021     }
3022 
3023     fn main() {}
3024     ";
3025 
3026     let got = get_only_completion(src, None);
3027     assert_eq!(got.matchstr, "Bar");
3028     assert_eq!(got.mtype, MatchType::Trait);
3029 }
3030 
3031 // Issue: https://github.com/racer-rust/racer/issues/755
3032 #[test]
completes_for_match_type_inference_let_expr()3033 fn completes_for_match_type_inference_let_expr() {
3034     let src = r#"
3035     use std::fs::File;
3036 
3037     fn main() {
3038         let f = File::open("hey");
3039 
3040         let f = match f {
3041             Ok(file) => file,
3042             Err(error) =>  {
3043                 panic!("Error opening file: {:?}", error)
3044             }
3045         };
3046         f.set_p~ermissions(/* args */);
3047     }
3048     "#;
3049 
3050     let got = get_only_completion(src, None);
3051     assert_eq!(got.matchstr, "set_permissions");
3052     assert_eq!(got.mtype, MatchType::Function);
3053 }
3054 
3055 #[test]
completes_for_match_type_inference_let_expr_with_block()3056 fn completes_for_match_type_inference_let_expr_with_block() {
3057     let src = r#"
3058     use std::fs::File;
3059 
3060     fn main() {
3061         let f = File::open("hey");
3062 
3063         let f = match f {
3064             Ok(file) => { file },
3065             Err(error) =>  {
3066                 panic!("Error opening file: {:?}", error)
3067             }
3068         };
3069         f.set_p~ermissions(/* args */);
3070     }
3071     "#;
3072 
3073     let got = get_only_completion(src, None);
3074     assert_eq!(got.matchstr, "set_permissions");
3075     assert_eq!(got.mtype, MatchType::Function);
3076 }
3077 
3078 #[test]
completes_for_match_type_inference_let_expr_with_return()3079 fn completes_for_match_type_inference_let_expr_with_return() {
3080     let src = r#"
3081     use std::fs::File;
3082 
3083     fn test() -> String {
3084         let f = File::open("hey");
3085 
3086         let f = match f {
3087             Err(error) =>  {
3088                 return "result".to_string();
3089             },
3090             Ok(file) => { file }
3091         };
3092 
3093         f.set_p~ermissions(/* args */);
3094     }
3095     "#;
3096 
3097     let got = get_only_completion(src, None);
3098     assert_eq!(got.matchstr, "set_permissions");
3099     assert_eq!(got.mtype, MatchType::Function);
3100 }
3101 
3102 #[test]
completes_for_let_if_let()3103 fn completes_for_let_if_let() {
3104     let src = r#"
3105     use std::fs::File;
3106 
3107     fn test() -> String {
3108         let f = File::open("hey");
3109 
3110         let f = if let Ok(f) = f { f } else { return "result".to_string(); };
3111 
3112         f.set_p~ermissions(/* args */);
3113     }
3114     "#;
3115 
3116     let got = get_only_completion(src, None);
3117     assert_eq!(got.matchstr, "set_permissions");
3118     assert_eq!(got.mtype, MatchType::Function);
3119 }
3120 
3121 #[test]
completes_for_match_type_inference_with_if()3122 fn completes_for_match_type_inference_with_if() {
3123     let src = r#"
3124     use std::fs::File;
3125 
3126     fn test() -> String {
3127         let f = File::open("hey");
3128 
3129         let f = match f {
3130             Err(error) =>  {
3131                 return "result".to_string();
3132             },
3133             Ok(file) => { if file.sync_data().is_ok() { return "nice".to_string(); } else { file } }
3134         };
3135 
3136         f.set_p~ermissions(/* args */);
3137     }
3138     "#;
3139 
3140     let got = get_only_completion(src, None);
3141     assert_eq!(got.matchstr, "set_permissions");
3142     assert_eq!(got.mtype, MatchType::Function);
3143 }
3144 
3145 #[test]
completes_before_first_statement()3146 fn completes_before_first_statement() {
3147     let src = r#"
3148     fn test() {
3149         ~
3150         let x = 8;
3151     }
3152     "#;
3153 
3154     let completions = get_all_completions(src, None);
3155     assert!(completions.into_iter().any(|m| m.matchstr == "std"));
3156 }
3157 
3158 #[test]
completes_between_statements()3159 fn completes_between_statements() {
3160     let src = r#"
3161     fn test() {
3162         let x = 8;
3163         ~
3164         let y = 55;
3165     }
3166     "#;
3167 
3168     let completions = get_all_completions(src, None);
3169     assert!(completions.into_iter().any(|m| m.matchstr == "std"));
3170 }
3171 
3172 // For issue 816
3173 #[test]
completes_for_let_after_comments_with_multibyte_char()3174 fn completes_for_let_after_comments_with_multibyte_char() {
3175     let src = "
3176     fn main() {
3177         let option = Some(5);
3178         let _ = match option {
3179             // multibyte comment ☆
3180             Some(variable) => {
3181                 let b = vari~;
3182                 3
3183             }
3184             None => 4,
3185         };
3186     }
3187     ";
3188     assert_eq!(get_only_completion(src, None).matchstr, "variable");
3189 }
3190 
3191 // For issue 818
3192 #[test]
completes_for_let_destracted_var_over_comment()3193 fn completes_for_let_destracted_var_over_comment() {
3194     let src = "
3195     fn main() {
3196         let option = Some(5);
3197         let _ = match option {
3198             Some(variable) /* C-style-comment*/
3199             // one -liner comment
3200             /*  nested and /* multiline
3201                                 comment*/  */
3202             => {
3203                 let b = vari~;
3204                 3
3205             }
3206             None => 4,
3207         };
3208     }
3209     ";
3210     assert_eq!(get_only_completion(src, None).matchstr, "variable");
3211 }
3212 
3213 // For issue 785
3214 #[test]
completes_methods_for_global_enum()3215 fn completes_methods_for_global_enum() {
3216     let src = r#"
3217     fn main() {
3218         let bar = Some("Hello");
3219         bar.unwrap_or_def~
3220     }
3221     "#;
3222     assert_eq!(get_only_completion(src, None).matchstr, "unwrap_or_default");
3223 }
3224 
3225 #[test]
completes_methods_for_local_enum()3226 fn completes_methods_for_local_enum() {
3227     let src = "
3228     fn main() {
3229         enum MyEnum {
3230             A
3231         }
3232         impl MyEnum {
3233             fn method(&self) {}
3234         }
3235         let bar = MyEnum::A;
3236         bar.met~
3237     }
3238     ";
3239     assert_eq!(get_only_completion(src, None).matchstr, "method");
3240 }
3241 
3242 // For Issue #815
3243 #[test]
completes_methods_after_raw_string()3244 fn completes_methods_after_raw_string() {
3245     let src = r##"
3246     fn main() {
3247         let s = r#"""#;
3248         let v = Vec::<u32>::new();
3249         v.l~
3250     }
3251     "##;
3252     assert!(get_all_completions(src, None)
3253         .iter()
3254         .any(|ma| ma.matchstr == "len"));
3255 }
3256 
3257 #[test]
completes_methods_for_tuple_struct()3258 fn completes_methods_for_tuple_struct() {
3259     let src = r"
3260         fn main() {
3261             struct A(i32, Vec<i32>);
3262             let mut a = A(0, vec![3, 4]);
3263             a.1.appen~
3264         }
3265     ";
3266     assert!(get_all_completions(src, None)
3267         .into_iter()
3268         .any(|ma| ma.matchstr == "append"));
3269 }
3270 
3271 // for use_nested_groups
3272 #[test]
follows_use_nested_from_std()3273 fn follows_use_nested_from_std() {
3274     let src = r"
3275     use std::collections::{hash_map::*, HashMap};
3276     fn main() {
3277          let h = HashMap::n~ew();
3278     }
3279     ";
3280 
3281     let got = get_definition(src, None);
3282     assert_eq!(got.matchstr, "new");
3283 
3284     let src = r"
3285     use std::collections::{hash_map::*, HashMap};
3286     fn main() {
3287          let h = HashMap::new();
3288          let a = DefaultHasher::ne~w();
3289     }
3290     ";
3291 
3292     let got = get_definition(src, None);
3293     assert_eq!(got.matchstr, "new");
3294 }
3295 
3296 // for use_nested_groups
3297 #[test]
use_tree_complete_all()3298 fn use_tree_complete_all() {
3299     let src = r"
3300     mod MyMod {
3301         pub enum MyEnum {
3302             ErrorKind1,
3303             ErrorKind2,
3304         }
3305         pub struct ErrorInfo;
3306     }
3307     use self::MyMod::{MyEnum::*, ErrorInfo};
3308     let a = Erro~
3309     ";
3310     let got = get_all_completions(src, None);
3311     assert!(got.iter().any(|ma| ma.matchstr == "ErrorKind1"));
3312     assert!(got.iter().any(|ma| ma.matchstr == "ErrorInfo"));
3313 }
3314 
3315 // for issue 847
3316 #[test]
match_statements_confusing_closure_args()3317 fn match_statements_confusing_closure_args() {
3318     let src = r#"
3319 fn main() {
3320     enum EnumA {
3321         A,
3322         B,
3323     }
3324     enum EnumB {
3325         A,
3326         B,
3327     }
3328     let a = EnumA::A;
3329     let b = EnumB::A;
3330     match a {
3331         EnumA::A => match b {
3332             EnumB::A | Enu~mB::B  => {},
3333         },
3334         EnumA::B => match b {
3335             EnumB::A | EnumB::B => {},
3336         },
3337     }
3338 }
3339 "#;
3340     let got = get_definition(src, None);
3341     assert_eq!(got.matchstr, "EnumB");
3342 }
3343 
3344 #[test]
completes_methods_for_closure_arg()3345 fn completes_methods_for_closure_arg() {
3346     let src = r"
3347         fn main() {
3348             let mut v = Vec::new();
3349             v.push(3);
3350             let s = Some(v);
3351             let x = s.map(|v: Vec<i32>| v.appen~);
3352         }
3353     ";
3354     assert!(get_all_completions(src, None)
3355         .into_iter()
3356         .any(|ma| ma.matchstr == "append"));
3357     let src = r"
3358         fn main() {
3359             let mut v = Vec::new();
3360             v.push(3);
3361             let s = Some(v);
3362             let x = s.map(|v: Vec<i32>| {
3363                 v.appen~
3364             });
3365         }
3366     ";
3367     assert!(get_all_completions(src, None)
3368         .into_iter()
3369         .any(|ma| ma.matchstr == "append"));
3370 }
3371 
3372 // for #856
3373 #[test]
finds_method_definition_in_1line_closure()3374 fn finds_method_definition_in_1line_closure() {
3375     let src = r"
3376         fn main() {
3377             let mut v = Vec::new();
3378             v.push(3);
3379             let s = Some(v);
3380             let x = s.map(|v: Vec<i32>| v.pus~h(2));
3381         }
3382     ";
3383     let got = get_definition(src, None);
3384     assert_eq!(got.matchstr, "push");
3385 }
3386 
3387 #[test]
recursive_glob_depth2()3388 fn recursive_glob_depth2() {
3389     let src = "
3390     use mod1::*;
3391     use Bar::*;
3392     mod mod1 {
3393         pub enum Bar { MyVariant, MyVariant2 }
3394     }
3395     MyVa~riant
3396     ";
3397     let got = get_definition(src, None);
3398     assert_eq!("MyVariant", got.matchstr);
3399 }
3400 
3401 #[test]
3402 #[should_panic]
recursive_glob_depth3()3403 fn recursive_glob_depth3() {
3404     let src = "
3405     use mod1::*;
3406     use mod2::*;
3407     use Bar::*;
3408 
3409     mod mod1 { pub mod mod2 {
3410         pub enum Bar { MyVariant, MyVariant2 }
3411     }}
3412     MyVa~riant
3413     ";
3414     let got = get_definition(src, None);
3415     assert_eq!("MyVariant", got.matchstr);
3416 }
3417 
3418 #[test]
completes_const_unsafe_fn()3419 fn completes_const_unsafe_fn() {
3420     let src = r"
3421     const unsafe fn unsafe_func() {}
3422     let var = unsafe_fu~
3423 ";
3424     let got = get_only_completion(src, None);
3425     assert_eq!("unsafe_func", got.matchstr);
3426     let src = r"
3427     pub const    unsafe   fn   unsafe_func() {}
3428     let var = unsafe_fu~
3429 ";
3430     let got = get_only_completion(src, None);
3431     assert_eq!("unsafe_func", got.matchstr);
3432 }
3433 
3434 #[test]
completes_fn_with_crate_visibility_modifier()3435 fn completes_fn_with_crate_visibility_modifier() {
3436     let src = r"
3437     crate unsafe fn unsafe_func() {}
3438     let var = unsafe_fu~
3439 ";
3440     let got = get_only_completion(src, None);
3441     assert_eq!("unsafe_func", got.matchstr);
3442 }
3443 
3444 // for #882
3445 #[test]
follows_complicated_use()3446 fn follows_complicated_use() {
3447     let src = "
3448     pub use std::{collections::{hash_map, HashM~
3449     ";
3450     let got = get_only_completion(src, None);
3451     assert_eq!("HashMap", got.matchstr);
3452 
3453     let src = "
3454     pub use std::{collections::{hash_map,  ~
3455     ";
3456     let got = get_all_completions(src, None);
3457     assert!(got.into_iter().any(|ma| ma.matchstr == "HashMap"));
3458 
3459     let src = "
3460     crate use std::{collections::{~
3461     ";
3462     let got = get_all_completions(src, None);
3463     assert!(got.into_iter().any(|ma| ma.matchstr == "HashMap"));
3464 }
3465 
3466 #[test]
get_completion_in_example_dir()3467 fn get_completion_in_example_dir() {
3468     let src = r"
3469     extern crate test_project;
3470     use test_project::TestStruct;
3471     fn main() {
3472         let test_struct = TestStruct::n~
3473     }
3474 ";
3475     with_test_project(|dir| {
3476         let example_dir = dir.nested_dir("examples");
3477         let got = get_only_completion(src, Some(example_dir));
3478         assert_eq!(got.matchstr, "new");
3479     })
3480 }
3481 
3482 #[test]
follows_use_crate()3483 fn follows_use_crate() {
3484     let mod_src = "
3485     pub fn myfn() {}
3486     pub fn foo() {}
3487     ";
3488     let lib_src = "
3489     mod mymod;
3490     use crate::mymod::*;
3491     fn main() {
3492         myf~
3493     }
3494     ";
3495 
3496     let dir = TmpDir::new();
3497     let _lib = dir.write_file("mymod.rs", mod_src);
3498     let got = get_only_completion(lib_src, Some(dir));
3499     assert_eq!(got.matchstr, "myfn");
3500     assert_eq!(got.contextstr, "pub fn myfn()");
3501 }
3502 
3503 #[test]
completes_static_method_for_typedef()3504 fn completes_static_method_for_typedef() {
3505     let src = "
3506     type UsizeVec = Vec<usize>;
3507     fn func() {
3508         let u = UsizeVec::with_capacity();
3509         u.append_elem~
3510     }
3511     ";
3512     let got = get_only_completion(src, None);
3513     assert_eq!(got.matchstr, "append_elements");
3514 }
3515 
3516 // for #882
3517 #[test]
finds_definition_in_use_tree()3518 fn finds_definition_in_use_tree() {
3519     let src = "
3520     pub use std::{collections::{HashMap, hash_map::DefaultHa~sher,
3521     ";
3522     let got = get_definition(src, None);
3523     assert_eq!(got.matchstr, "DefaultHasher");
3524 }
3525 
3526 // for #890
3527 #[test]
finds_fn_in_extern_block()3528 fn finds_fn_in_extern_block() {
3529     let src = r#"
3530     use std::os::raw::c_char;
3531     extern "C" {
3532         pub fn MyCFunction() -> *mut c_char;
3533     }
3534     fn main() {
3535         let ptr = MyCFunc~
3536     }
3537     "#;
3538     let got = get_only_completion(src, None);
3539     assert_eq!(got.matchstr, "MyCFunction");
3540 }
3541 
3542 // for #878
3543 #[test]
finds_std_fn_in_extern_block()3544 fn finds_std_fn_in_extern_block() {
3545     let src = r#"
3546     use std::ptr::copy_nonoverla~
3547     "#;
3548     let got = get_only_completion(src, None);
3549     assert_eq!(got.matchstr, "copy_nonoverlapping");
3550 }
3551 
3552 #[test]
complets_println()3553 fn complets_println() {
3554     let src = r#"
3555     fn main() {
3556         printl~
3557     }
3558     "#;
3559     let got = get_only_completion(src, None);
3560     assert_eq!(got.matchstr, "println!");
3561 }
3562 
3563 #[test]
doesnt_complete_cfg_if()3564 fn doesnt_complete_cfg_if() {
3565     let src = r#"
3566     fn main() {
3567         cfg_i~
3568     }
3569     "#;
3570     let got = get_all_completions(src, None);
3571     assert!(got.is_empty(), "got: {:?}", got);
3572 }
3573 
3574 #[test]
finds_def_of_println()3575 fn finds_def_of_println() {
3576     let src = r#"
3577     fn main() {
3578         printl~n!("Hello@_@");
3579     }
3580     "#;
3581     let got = get_definition(src, None);
3582     assert_eq!(got.matchstr, "println!");
3583 }
3584 
3585 #[test]
doesnt_complete_macro_after_use()3586 fn doesnt_complete_macro_after_use() {
3587     let src = r#"
3588     use printl~
3589     "#;
3590     let got = get_all_completions(src, None);
3591     assert!(got.is_empty(), "got: {:?}", got);
3592     let src = r#"
3593     macro_rules! macro {
3594         () => {}
3595     }
3596     use macr~
3597     "#;
3598     let got = get_all_completions(src, None);
3599     assert!(got.is_empty(), "got: {:?}", got);
3600 }
3601 
3602 #[test]
3603 #[ignore]
3604 // FIXME:  #1059
complets_stringify()3605 fn complets_stringify() {
3606     let src = r#"
3607     fn main() {
3608         let ident = 100;
3609         let s = stringi~
3610     }
3611     "#;
3612     let got = get_only_completion(src, None);
3613     assert_eq!(got.matchstr, "stringify!");
3614 }
3615 
3616 #[test]
completes_writeln()3617 fn completes_writeln() {
3618     let src = r#"
3619     fn main() {
3620         writel~
3621     }
3622     "#;
3623     let got = get_only_completion(src, None);
3624     assert_eq!(got.matchstr, "writeln!");
3625 }
3626 
3627 #[test]
completes_vec()3628 fn completes_vec() {
3629     let src = r#"
3630     fn main() {
3631         ve~
3632     }
3633     "#;
3634     let got = get_only_completion(src, None);
3635     assert_eq!(got.matchstr, "vec!");
3636 }
3637 
3638 #[test]
3639 #[ignore]
3640 // FIXME: #1059
finds_std_macro_doc()3641 fn finds_std_macro_doc() {
3642     let src = r#"
3643     fn main() {
3644         module_pat~h!();
3645     }
3646     "#;
3647     let got = get_definition(src, None);
3648     assert!(!got.docs.is_empty());
3649 }
3650 
3651 #[test]
finds_local_macro_doc()3652 fn finds_local_macro_doc() {
3653     let src = r#"
3654     /// my macro
3655     macro_rules! local_macro { () => {} }
3656     fn main() {
3657         local_mac~ro!();
3658     }
3659     "#;
3660     let got = get_definition(src, None);
3661     let doc = "my macro";
3662     assert_eq!(got.docs, doc);
3663 }
3664 
3665 #[test]
follows_multiline_use()3666 fn follows_multiline_use() {
3667     let src = r#"
3668     use std::{
3669         cell::RefC~
3670         collections::{
3671             hash_map::{self, HashMap},
3672             HashSet,
3673         },
3674     "#;
3675     let got = get_only_completion(src, None);
3676     assert_eq!(got.matchstr, "RefCell");
3677     let src = r#"
3678     use std::{
3679         cell::RefCell,
3680         collections::{
3681             hash_map::{self, HashM~
3682             HashSet,
3683         },
3684     "#;
3685     let got = get_only_completion(src, None);
3686     assert_eq!(got.matchstr, "HashMap");
3687 }
3688 
3689 #[test]
completes_trait_method_only_once()3690 fn completes_trait_method_only_once() {
3691     let src = "
3692     trait Trait {
3693         fn function(&self) -> usize { 5 }
3694     }
3695     struct S;
3696     impl Trait for S {
3697         type A = usize;
3698         fn function(&self) -> usize { 6 }
3699     }
3700     fn main() {
3701         let s = S {};
3702         s.fun~
3703     }
3704     ";
3705     let got = get_only_completion(src, None);
3706     assert_eq!(got.matchstr, "function");
3707 }
3708 
3709 #[test]
completes_methods_vec_macro()3710 fn completes_methods_vec_macro() {
3711     let src = "
3712     fn main() {
3713         let vec = vec![];
3714         let a = vec.append_ele~
3715     }
3716 ";
3717     let got = get_only_completion(src, None);
3718     assert_eq!(got.matchstr, "append_elements");
3719 }
3720 
3721 #[test]
completes_trait_methods_in_path()3722 fn completes_trait_methods_in_path() {
3723     let src = "
3724     let a = Default::de~
3725 ";
3726     let got = get_only_completion(src, None);
3727     assert_eq!(got.matchstr, "default");
3728 }
3729 
3730 #[test]
completes_closure_return_type()3731 fn completes_closure_return_type() {
3732     let src = r"
3733     fn second<F: Fn() -> Option<i32>>(f: F) {
3734         f().un~
3735     }
3736 ";
3737     let got = get_one_completion(src, None);
3738     assert_eq!("unwrap", got.matchstr);
3739 }
3740 
3741 #[test]
completes_generic_closure_return_type()3742 fn completes_generic_closure_return_type() {
3743     let src = r"
3744     fn second<T: Clone, F: Fn() -> T>(f: F) {
3745         f().cl~
3746     }
3747 ";
3748     let got = get_one_completion(src, None);
3749     assert_eq!("clone", got.matchstr);
3750 }
3751 
3752 #[test]
completes_impl_generic_arg_in_closure()3753 fn completes_impl_generic_arg_in_closure() {
3754     let src = r"
3755     struct Something<T> {
3756         t: T
3757     }
3758 
3759     impl<M: Clone> Something<M> {
3760         fn second<T, F: Fn(T) -> M>(f: F) {
3761             f().cl~
3762         }
3763     }
3764 ";
3765     let got = get_one_completion(src, None);
3766     assert_eq!("clone", got.matchstr);
3767 }
3768 
3769 #[test]
completes_closure_output_type_params()3770 fn completes_closure_output_type_params() {
3771     let src = r"
3772     trait Foo {
3773         fn foo(&self) -> String;
3774     }
3775 
3776     fn second<K: Foo, F: Fn() -> Option<K>>(f: F) {
3777         f().unwrap().f~
3778     }
3779 ";
3780     let got = get_one_completion(src, None);
3781     assert_eq!("foo", got.matchstr);
3782 }
3783 
3784 #[test]
completes_functions_from_trait_objects()3785 fn completes_functions_from_trait_objects() {
3786     let src = r"
3787     pub trait Foo {
3788         fn foo(&self);
3789     }
3790 
3791     fn get_foo() -> Box<Foo + Send> {
3792         unimplemented!();
3793     }
3794 
3795     fn main() {
3796         let mut t = get_foo();
3797         t.f~
3798     }
3799 ";
3800     let got = get_all_completions(src, None);
3801     assert!(got.into_iter().any(|ma| ma.matchstr == "foo"));
3802 }
3803 
3804 #[test]
import_stmt_doesnt_jump_to_closure_arg()3805 fn import_stmt_doesnt_jump_to_closure_arg() {
3806     let src = r#"
3807     use req~west;
3808     fn main() {
3809         let y = Some(32i32).map(|reqwest| "something");
3810     }
3811     "#;
3812 
3813     assert!(find_definition(src, None).is_none());
3814 }
3815 
3816 #[test]
test_resolve_global_path()3817 fn test_resolve_global_path() {
3818     let src = r#"
3819 pub fn name() {
3820 }
3821 
3822 fn main() {
3823     let name = 1;
3824     let _ = ::nam~e();
3825 }
3826 "#;
3827     let got = find_definition_with_name(src, None, "lib.rs").unwrap();
3828     assert_eq!(got.matchstr, "name");
3829     assert_eq!(got.mtype, MatchType::Function);
3830 }
3831 
3832 #[test]
test_resolve_global_path_in_modules()3833 fn test_resolve_global_path_in_modules() {
3834     let src = r#"
3835     fn foo() {
3836         let name = 1;
3837         let a = ::nam~e();
3838     }
3839 "#;
3840     with_test_project(|dir| {
3841         let srcdir = dir.nested_dir("src");
3842         let got = get_one_completion(src, Some(srcdir));
3843         assert_eq!(got.matchstr, "name");
3844         assert_eq!(got.mtype, MatchType::Function);
3845     });
3846 
3847     let src = r#"
3848     fn foo() {
3849         let a = ::TestStruct::n~
3850     }
3851 "#;
3852     with_test_project(|dir| {
3853         let srcdir = dir.nested_dir("src");
3854         let got = get_one_completion(src, Some(srcdir));
3855         assert_eq!(got.matchstr, "new");
3856     })
3857 }
3858 
3859 #[test]
completes_methods_for_as_bytes()3860 fn completes_methods_for_as_bytes() {
3861     let src = r#"
3862 fn main() {
3863     let s = "Foo".as_bytes();
3864     let t = s.conca~
3865 }
3866 "#;
3867     let got = get_only_completion(src, None);
3868     assert_eq!(got.matchstr, "concat");
3869 }
3870 
3871 #[test]
completes_crate_local_enum_variant()3872 fn completes_crate_local_enum_variant() {
3873     let src = "
3874     pub(crate) enum Enum {
3875        Variant,
3876     }
3877     fn main() {
3878         let bar = Enum::V~;
3879     }
3880     ";
3881     assert_eq!(get_only_completion(src, None).matchstr, "Variant");
3882 }
3883