aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_completion/src/tests
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide_completion/src/tests')
-rw-r--r--crates/ide_completion/src/tests/item_list.rs223
-rw-r--r--crates/ide_completion/src/tests/items.rs95
-rw-r--r--crates/ide_completion/src/tests/pattern.rs348
-rw-r--r--crates/ide_completion/src/tests/type_pos.rs177
-rw-r--r--crates/ide_completion/src/tests/use_tree.rs255
5 files changed, 1098 insertions, 0 deletions
diff --git a/crates/ide_completion/src/tests/item_list.rs b/crates/ide_completion/src/tests/item_list.rs
new file mode 100644
index 000000000..7c124ac37
--- /dev/null
+++ b/crates/ide_completion/src/tests/item_list.rs
@@ -0,0 +1,223 @@
1use expect_test::{expect, Expect};
2
3use crate::tests::completion_list;
4
5fn check(ra_fixture: &str, expect: Expect) {
6 let base = r#"#[rustc_builtin_macro]
7pub macro Clone {}
8enum Enum { Variant }
9struct Struct {}
10#[macro_export]
11macro_rules! foo {}
12mod bar {}
13const CONST: () = ();
14trait Trait {}
15"#;
16 let actual = completion_list(&format!("{}{}", base, ra_fixture));
17 expect.assert_eq(&actual)
18}
19
20#[test]
21fn in_mod_item_list() {
22 check(
23 r#"mod tests { $0 }"#,
24 expect![[r##"
25 kw pub(crate)
26 kw pub
27 kw unsafe
28 kw fn
29 kw const
30 kw type
31 kw impl
32 kw extern
33 kw use
34 kw trait
35 kw static
36 kw mod
37 kw enum
38 kw struct
39 kw union
40 sn tmod (Test module)
41 sn tfn (Test function)
42 sn macro_rules
43 ma foo!(…) #[macro_export] macro_rules! foo
44 "##]],
45 )
46}
47
48#[test]
49fn in_source_file_item_list() {
50 check(
51 r#"$0"#,
52 expect![[r##"
53 kw pub(crate)
54 kw pub
55 kw unsafe
56 kw fn
57 kw const
58 kw type
59 kw impl
60 kw extern
61 kw use
62 kw trait
63 kw static
64 kw mod
65 kw enum
66 kw struct
67 kw union
68 sn tmod (Test module)
69 sn tfn (Test function)
70 sn macro_rules
71 md bar
72 ma foo!(…) #[macro_export] macro_rules! foo
73 ma foo!(…) #[macro_export] macro_rules! foo
74 "##]],
75 )
76}
77
78#[test]
79fn in_item_list_after_attr() {
80 check(
81 r#"#[attr] $0"#,
82 expect![[r#"
83 kw pub(crate)
84 kw pub
85 kw unsafe
86 kw fn
87 kw const
88 kw type
89 kw impl
90 kw extern
91 kw use
92 kw trait
93 kw static
94 kw mod
95 kw enum
96 kw struct
97 kw union
98 sn tmod (Test module)
99 sn tfn (Test function)
100 sn macro_rules
101 "#]],
102 )
103}
104
105#[test]
106fn in_qualified_path() {
107 check(
108 r#"crate::$0"#,
109 expect![[r##"
110 kw pub(crate)
111 kw pub
112 kw unsafe
113 kw fn
114 kw const
115 kw type
116 kw impl
117 kw extern
118 kw use
119 kw trait
120 kw static
121 kw mod
122 kw enum
123 kw struct
124 kw union
125 md bar
126 ma foo!(…) #[macro_export] macro_rules! foo
127 "##]],
128 )
129}
130
131#[test]
132fn after_unsafe_token() {
133 check(
134 r#"unsafe $0"#,
135 expect![[r#"
136 kw fn
137 kw trait
138 kw impl
139 "#]],
140 );
141}
142
143#[test]
144fn after_visibility() {
145 check(
146 r#"pub $0"#,
147 expect![[r#"
148 kw unsafe
149 kw fn
150 kw const
151 kw type
152 kw use
153 kw trait
154 kw static
155 kw mod
156 kw enum
157 kw struct
158 kw union
159 "#]],
160 );
161}
162
163#[test]
164fn after_visibility_unsafe() {
165 // FIXME this shouldn't show `impl`
166 check(
167 r#"pub unsafe $0"#,
168 expect![[r#"
169 kw fn
170 kw trait
171 kw impl
172 "#]],
173 );
174}
175
176#[test]
177fn in_impl_assoc_item_list() {
178 check(
179 r#"impl Struct { $0 }"#,
180 expect![[r##"
181 kw pub(crate)
182 kw pub
183 kw unsafe
184 kw fn
185 kw const
186 kw type
187 md bar
188 ma foo!(…) #[macro_export] macro_rules! foo
189 ma foo!(…) #[macro_export] macro_rules! foo
190 "##]],
191 )
192}
193
194#[test]
195fn in_impl_assoc_item_list_after_attr() {
196 check(
197 r#"impl Struct { #[attr] $0 }"#,
198 expect![[r#"
199 kw pub(crate)
200 kw pub
201 kw unsafe
202 kw fn
203 kw const
204 kw type
205 "#]],
206 )
207}
208
209#[test]
210fn in_trait_assoc_item_list() {
211 check(
212 r"trait Foo { $0 }",
213 expect![[r##"
214 kw unsafe
215 kw fn
216 kw const
217 kw type
218 md bar
219 ma foo!(…) #[macro_export] macro_rules! foo
220 ma foo!(…) #[macro_export] macro_rules! foo
221 "##]],
222 );
223}
diff --git a/crates/ide_completion/src/tests/items.rs b/crates/ide_completion/src/tests/items.rs
new file mode 100644
index 000000000..b98baffd6
--- /dev/null
+++ b/crates/ide_completion/src/tests/items.rs
@@ -0,0 +1,95 @@
1//! Completions tests for item specifics overall.
2//!
3//! Except for use items which are tested in [super::use_tree] and mod declarations with are tested
4//! in [crate::completions::mod_].
5use expect_test::{expect, Expect};
6
7use crate::tests::completion_list;
8
9fn check(ra_fixture: &str, expect: Expect) {
10 let base = r#"#[rustc_builtin_macro]
11pub macro Clone {}
12enum Enum { Variant }
13struct Struct {}
14#[macro_export]
15macro_rules! foo {}
16mod bar {}
17const CONST: () = ();
18trait Trait {}
19"#;
20 let actual = completion_list(&format!("{}{}", base, ra_fixture));
21 expect.assert_eq(&actual)
22}
23
24#[test]
25fn target_type_or_trait_in_impl_block() {
26 check(
27 r#"
28impl Tra$0
29"#,
30 expect![[r##"
31 tt Trait
32 en Enum
33 st Struct
34 md bar
35 ma foo!(…) #[macro_export] macro_rules! foo
36 ma foo!(…) #[macro_export] macro_rules! foo
37 bt u32
38 "##]],
39 )
40}
41
42#[test]
43fn target_type_in_trait_impl_block() {
44 check(
45 r#"
46impl Trait for Str$0
47"#,
48 expect![[r##"
49 tt Trait
50 en Enum
51 st Struct
52 md bar
53 ma foo!(…) #[macro_export] macro_rules! foo
54 ma foo!(…) #[macro_export] macro_rules! foo
55 bt u32
56 "##]],
57 )
58}
59
60#[test]
61fn after_trait_name_in_trait_def() {
62 check(
63 r"trait A $0",
64 expect![[r#"
65 kw where
66 "#]],
67 );
68}
69
70#[test]
71fn after_trait_or_target_name_in_impl() {
72 check(
73 r"impl Trait $0",
74 expect![[r#"
75 kw where
76 kw for
77 "#]],
78 );
79}
80
81#[test]
82fn before_record_field() {
83 check(
84 r#"
85struct Foo {
86 $0
87 pub f: i32,
88}
89"#,
90 expect![[r#"
91 kw pub(crate)
92 kw pub
93 "#]],
94 )
95}
diff --git a/crates/ide_completion/src/tests/pattern.rs b/crates/ide_completion/src/tests/pattern.rs
new file mode 100644
index 000000000..1ad5ccd97
--- /dev/null
+++ b/crates/ide_completion/src/tests/pattern.rs
@@ -0,0 +1,348 @@
1//! Completions tests for pattern position.
2use expect_test::{expect, Expect};
3
4use crate::tests::completion_list;
5
6fn check(ra_fixture: &str, expect: Expect) {
7 let actual = completion_list(ra_fixture);
8 expect.assert_eq(&actual)
9}
10
11fn check_with(ra_fixture: &str, expect: Expect) {
12 let base = r#"
13enum Enum { TupleV(u32), RecordV { field: u32 }, UnitV }
14use self::Enum::TupleV;
15mod module {}
16
17static STATIC: Unit = Unit;
18const CONST: Unit = Unit;
19struct Record { field: u32 }
20struct Tuple(u32);
21struct Unit
22macro_rules! makro {}
23"#;
24 let actual = completion_list(&format!("{}\n{}", base, ra_fixture));
25 expect.assert_eq(&actual)
26}
27
28#[test]
29fn ident_rebind_pat() {
30 check(
31 r#"
32fn quux() {
33 let en$0 @ x
34}
35"#,
36 expect![[r#"
37 kw mut
38 "#]],
39 );
40}
41
42#[test]
43fn ident_ref_pat() {
44 check(
45 r#"
46fn quux() {
47 let ref en$0
48}
49"#,
50 expect![[r#"
51 kw mut
52 "#]],
53 );
54 check(
55 r#"
56fn quux() {
57 let ref en$0 @ x
58}
59"#,
60 expect![[r#"
61 kw mut
62 "#]],
63 );
64}
65
66#[test]
67fn ident_ref_mut_pat() {
68 // FIXME mut is already here, don't complete it again
69 check(
70 r#"
71fn quux() {
72 let ref mut en$0
73}
74"#,
75 expect![[r#"
76 kw mut
77 "#]],
78 );
79 check(
80 r#"
81fn quux() {
82 let ref mut en$0 @ x
83}
84"#,
85 expect![[r#"
86 kw mut
87 "#]],
88 );
89}
90
91#[test]
92fn ref_pat() {
93 check(
94 r#"
95fn quux() {
96 let &en$0
97}
98"#,
99 expect![[r#"
100 kw mut
101 "#]],
102 );
103 // FIXME mut is already here, don't complete it again
104 check(
105 r#"
106fn quux() {
107 let &mut en$0
108}
109"#,
110 expect![[r#"
111 kw mut
112 "#]],
113 );
114}
115
116#[test]
117fn refutable() {
118 check_with(
119 r#"
120fn foo() {
121 if let a$0
122}
123"#,
124 expect![[r#"
125 kw mut
126 bn Record Record { field$1 }$0
127 st Record
128 en Enum
129 bn Tuple Tuple($1)$0
130 st Tuple
131 md module
132 bn TupleV TupleV($1)$0
133 ev TupleV
134 st Unit
135 ct CONST
136 ma makro!(…) macro_rules! makro
137 "#]],
138 );
139}
140
141#[test]
142fn irrefutable() {
143 check_with(
144 r#"
145fn foo() {
146 let a$0
147}
148"#,
149 expect![[r#"
150 kw mut
151 bn Record Record { field$1 }$0
152 st Record
153 bn Tuple Tuple($1)$0
154 st Tuple
155 st Unit
156 ma makro!(…) macro_rules! makro
157 "#]],
158 );
159}
160
161#[test]
162fn in_param() {
163 check_with(
164 r#"
165fn foo(a$0) {
166}
167"#,
168 expect![[r#"
169 kw mut
170 bn Record Record { field$1 }: Record$0
171 st Record
172 bn Tuple Tuple($1): Tuple$0
173 st Tuple
174 st Unit
175 ma makro!(…) macro_rules! makro
176 "#]],
177 );
178}
179
180#[test]
181fn only_fn_like_macros() {
182 check(
183 r#"
184macro_rules! m { ($e:expr) => { $e } }
185
186#[rustc_builtin_macro]
187macro Clone {}
188
189fn foo() {
190 let x$0
191}
192"#,
193 expect![[r#"
194 kw mut
195 ma m!(…) macro_rules! m
196 "#]],
197 );
198}
199
200#[test]
201fn in_simple_macro_call() {
202 check(
203 r#"
204macro_rules! m { ($e:expr) => { $e } }
205enum E { X }
206
207fn foo() {
208 m!(match E::X { a$0 })
209}
210"#,
211 expect![[r#"
212 kw mut
213 ev E::X ()
214 en E
215 ma m!(…) macro_rules! m
216 "#]],
217 );
218}
219
220#[test]
221fn omits_private_fields_pat() {
222 check(
223 r#"
224mod foo {
225 pub struct Record { pub field: i32, _field: i32 }
226 pub struct Tuple(pub u32, u32);
227 pub struct Invisible(u32, u32);
228}
229use foo::*;
230
231fn outer() {
232 if let a$0
233}
234"#,
235 expect![[r#"
236 kw mut
237 bn Record Record { field$1, .. }$0
238 st Record
239 bn Tuple Tuple($1, ..)$0
240 st Tuple
241 st Invisible
242 md foo
243 "#]],
244 )
245}
246
247// #[test]
248// fn only_shows_ident_completion() {
249// check_edit(
250// "Foo",
251// r#"
252// struct Foo(i32);
253// fn main() {
254// match Foo(92) {
255// a$0(92) => (),
256// }
257// }
258// "#,
259// r#"
260// struct Foo(i32);
261// fn main() {
262// match Foo(92) {
263// Foo(92) => (),
264// }
265// }
266// "#,
267// );
268// }
269
270#[test]
271fn completes_self_pats() {
272 check(
273 r#"
274struct Foo(i32);
275impl Foo {
276 fn foo() {
277 match Foo(0) {
278 a$0
279 }
280 }
281}
282 "#,
283 expect![[r#"
284 kw mut
285 bn Self Self($1)$0
286 sp Self
287 bn Foo Foo($1)$0
288 st Foo
289 "#]],
290 )
291}
292
293#[test]
294fn completes_qualified_variant() {
295 check(
296 r#"
297enum Foo {
298 Bar { baz: i32 }
299}
300impl Foo {
301 fn foo() {
302 match {Foo::Bar { baz: 0 }} {
303 B$0
304 }
305 }
306}
307 "#,
308 expect![[r#"
309 kw mut
310 bn Self::Bar Self::Bar { baz$1 }$0
311 ev Self::Bar { baz: i32 }
312 bn Foo::Bar Foo::Bar { baz$1 }$0
313 ev Foo::Bar { baz: i32 }
314 sp Self
315 en Foo
316 "#]],
317 )
318}
319
320#[test]
321fn completes_in_record_field_pat() {
322 check(
323 r#"
324struct Foo { bar: Bar }
325struct Bar(u32);
326fn outer(Foo { bar: $0 }: Foo) {}
327"#,
328 expect![[r#"
329 kw mut
330 bn Foo Foo { bar$1 }$0
331 st Foo
332 bn Bar Bar($1)$0
333 st Bar
334 "#]],
335 )
336}
337
338#[test]
339fn skips_in_record_field_pat_name() {
340 check(
341 r#"
342struct Foo { bar: Bar }
343struct Bar(u32);
344fn outer(Foo { bar$0 }: Foo) {}
345"#,
346 expect![[r#""#]],
347 )
348}
diff --git a/crates/ide_completion/src/tests/type_pos.rs b/crates/ide_completion/src/tests/type_pos.rs
new file mode 100644
index 000000000..1ab47b27e
--- /dev/null
+++ b/crates/ide_completion/src/tests/type_pos.rs
@@ -0,0 +1,177 @@
1//! Completions tests for type position.
2use expect_test::{expect, Expect};
3
4use crate::tests::completion_list;
5
6fn check_with(ra_fixture: &str, expect: Expect) {
7 let base = r#"
8enum Enum { TupleV(u32), RecordV { field: u32 }, UnitV }
9use self::Enum::TupleV;
10mod module {}
11
12trait Trait {}
13static STATIC: Unit = Unit;
14const CONST: Unit = Unit;
15struct Record { field: u32 }
16struct Tuple(u32);
17struct Unit
18macro_rules! makro {}
19"#;
20 let actual = completion_list(&format!("{}\n{}", base, ra_fixture));
21 expect.assert_eq(&actual)
22}
23
24#[test]
25fn record_field_ty() {
26 check_with(
27 r#"
28struct Foo<'lt, T, const C: usize> {
29 f: $0
30}
31"#,
32 expect![[r#"
33 sp Self
34 tp T
35 tt Trait
36 en Enum
37 st Record
38 st Tuple
39 md module
40 st Foo<…>
41 st Unit
42 ma makro!(…) macro_rules! makro
43 bt u32
44 "#]],
45 )
46}
47
48#[test]
49fn tuple_struct_field() {
50 check_with(
51 r#"
52struct Foo<'lt, T, const C: usize>(f$0);
53"#,
54 expect![[r#"
55 kw pub(crate)
56 kw pub
57 sp Self
58 tp T
59 tt Trait
60 en Enum
61 st Record
62 st Tuple
63 md module
64 st Foo<…>
65 st Unit
66 ma makro!(…) macro_rules! makro
67 bt u32
68 "#]],
69 )
70}
71
72#[test]
73fn fn_return_type() {
74 check_with(
75 r#"
76fn x<'lt, T, const C: usize>() -> $0
77"#,
78 expect![[r#"
79 tp T
80 tt Trait
81 en Enum
82 st Record
83 st Tuple
84 md module
85 st Unit
86 ma makro!(…) macro_rules! makro
87 bt u32
88 "#]],
89 );
90}
91
92#[test]
93fn body_type_pos() {
94 check_with(
95 r#"
96fn foo<'lt, T, const C: usize>() {
97 let local = ();
98 let _: $0;
99}
100"#,
101 expect![[r#"
102 tp T
103 tt Trait
104 en Enum
105 st Record
106 st Tuple
107 md module
108 st Unit
109 ma makro!(…) macro_rules! makro
110 bt u32
111 "#]],
112 );
113 check_with(
114 r#"
115fn foo<'lt, T, const C: usize>() {
116 let local = ();
117 let _: self::$0;
118}
119"#,
120 expect![[r#"
121 tt Trait
122 en Enum
123 st Record
124 st Tuple
125 md module
126 st Unit
127 "#]],
128 );
129}
130
131#[test]
132fn completes_types_and_const_in_arg_list() {
133 // FIXME: we should complete the lifetime here for now
134 check_with(
135 r#"
136trait Trait2 {
137 type Foo;
138}
139
140fn foo<'lt, T: Trait2<$0>, const CONST_PARAM: usize>(_: T) {}
141"#,
142 expect![[r#"
143 ta Foo = type Foo;
144 tp T
145 cp CONST_PARAM
146 tt Trait
147 en Enum
148 st Record
149 st Tuple
150 tt Trait2
151 md module
152 st Unit
153 ct CONST
154 ma makro!(…) macro_rules! makro
155 bt u32
156 "#]],
157 );
158 check_with(
159 r#"
160trait Trait2 {
161 type Foo;
162}
163
164fn foo<'lt, T: Trait2<self::$0>, const CONST_PARAM: usize>(_: T) {}
165 "#,
166 expect![[r#"
167 tt Trait
168 en Enum
169 st Record
170 st Tuple
171 tt Trait2
172 md module
173 st Unit
174 ct CONST
175 "#]],
176 );
177}
diff --git a/crates/ide_completion/src/tests/use_tree.rs b/crates/ide_completion/src/tests/use_tree.rs
new file mode 100644
index 000000000..7e6748ccc
--- /dev/null
+++ b/crates/ide_completion/src/tests/use_tree.rs
@@ -0,0 +1,255 @@
1use expect_test::{expect, Expect};
2
3use crate::tests::completion_list;
4
5fn check(ra_fixture: &str, expect: Expect) {
6 let actual = completion_list(ra_fixture);
7 expect.assert_eq(&actual)
8}
9
10#[test]
11fn use_tree_start() {
12 cov_mark::check!(only_completes_modules_in_import);
13 check(
14 r#"
15//- /lib.rs crate:main deps:other_crate
16use f$0
17
18struct Foo;
19mod foo {}
20//- /other_crate/lib.rs crate:other_crate
21// nothing here
22"#,
23 expect![[r#"
24 kw crate::
25 kw self::
26 kw super::
27 md foo
28 md other_crate
29 "#]],
30 );
31}
32
33#[test]
34fn dont_complete_current_use() {
35 cov_mark::check!(dont_complete_current_use);
36 check(r#"use self::foo$0;"#, expect![[r#""#]]);
37 check(
38 r#"
39mod foo { pub struct S; }
40use self::{foo::*, bar$0};
41"#,
42 expect![[r#"
43 kw self
44 st S
45 md foo
46 "#]],
47 );
48}
49
50#[test]
51fn nested_use_tree() {
52 check(
53 r#"
54mod foo {
55 pub mod bar {
56 pub struct FooBar;
57 }
58}
59use foo::{bar::$0}
60"#,
61 expect![[r#"
62 st FooBar
63 "#]],
64 );
65 check(
66 r#"
67mod foo {
68 pub mod bar {
69 pub struct FooBar;
70 }
71}
72use foo::{$0}
73"#,
74 expect![[r#"
75 kw self
76 md bar
77 "#]],
78 );
79}
80
81#[test]
82fn deeply_nested_use_tree() {
83 check(
84 r#"
85mod foo {
86 pub mod bar {
87 pub mod baz {
88 pub struct FooBarBaz;
89 }
90 }
91}
92use foo::{bar::{baz::$0}}
93"#,
94 expect![[r#"
95 st FooBarBaz
96 "#]],
97 );
98 check(
99 r#"
100mod foo {
101 pub mod bar {
102 pub mod baz {
103 pub struct FooBarBaz;
104 }
105 }
106}
107use foo::{bar::{$0}}
108"#,
109 expect![[r#"
110 kw self
111 md baz
112 "#]],
113 );
114}
115
116#[test]
117fn plain_qualified_use_tree() {
118 check(
119 r#"
120use foo::$0
121
122mod foo {
123 struct Private;
124 pub struct Foo;
125}
126struct Bar;
127"#,
128 expect![[r#"
129 st Foo
130 "#]],
131 );
132}
133
134#[test]
135fn self_qualified_use_tree() {
136 check(
137 r#"
138use self::$0
139
140mod foo {}
141struct Bar;
142"#,
143 expect![[r#"
144 md foo
145 st Bar
146 "#]],
147 );
148}
149
150#[test]
151fn super_qualified_use_tree() {
152 check(
153 r#"
154mod bar {
155 use super::$0
156}
157
158mod foo {}
159struct Bar;
160"#,
161 expect![[r#"
162 kw super::
163 st Bar
164 md bar
165 md foo
166 "#]],
167 );
168}
169
170#[test]
171fn super_super_qualified_use_tree() {
172 check(
173 r#"
174mod a {
175 const A: usize = 0;
176 mod b {
177 const B: usize = 0;
178 mod c { use super::super::$0 }
179 }
180}
181"#,
182 expect![[r#"
183 kw super::
184 md b
185 ct A
186 "#]],
187 );
188}
189
190#[test]
191fn crate_qualified_use_tree() {
192 check(
193 r#"
194use crate::$0
195
196mod foo {}
197struct Bar;
198"#,
199 expect![[r#"
200 md foo
201 st Bar
202 "#]],
203 );
204}
205
206#[test]
207fn extern_crate_qualified_use_tree() {
208 check(
209 r#"
210//- /lib.rs crate:main deps:other_crate
211use other_crate::$0
212//- /other_crate/lib.rs crate:other_crate
213pub struct Foo;
214pub mod foo {}
215"#,
216 expect![[r#"
217 st Foo
218 md foo
219 "#]],
220 );
221}
222
223#[test]
224fn pub_use_tree() {
225 check(
226 r#"
227pub struct X;
228pub mod bar {}
229pub use $0;
230"#,
231 expect![[r#"
232 kw crate::
233 kw self::
234 kw super::
235 md bar
236 "#]],
237 );
238}
239
240#[test]
241fn use_tree_braces_at_start() {
242 check(
243 r#"
244struct X;
245mod bar {}
246use {$0};
247"#,
248 expect![[r#"
249 kw crate::
250 kw self::
251 kw super::
252 md bar
253 "#]],
254 );
255}