]> git.lizzy.rs Git - rust.git/blob - crates/ide_completion/src/tests/use_tree.rs
7e6748cccd21d5280a62f881feab0e39ecdb255e
[rust.git] / crates / ide_completion / src / tests / use_tree.rs
1 use expect_test::{expect, Expect};
2
3 use crate::tests::completion_list;
4
5 fn check(ra_fixture: &str, expect: Expect) {
6     let actual = completion_list(ra_fixture);
7     expect.assert_eq(&actual)
8 }
9
10 #[test]
11 fn use_tree_start() {
12     cov_mark::check!(only_completes_modules_in_import);
13     check(
14         r#"
15 //- /lib.rs crate:main deps:other_crate
16 use f$0
17
18 struct Foo;
19 mod 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]
34 fn 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#"
39 mod foo { pub struct S; }
40 use self::{foo::*, bar$0};
41 "#,
42         expect![[r#"
43             kw self
44             st S
45             md foo
46         "#]],
47     );
48 }
49
50 #[test]
51 fn nested_use_tree() {
52     check(
53         r#"
54 mod foo {
55     pub mod bar {
56         pub struct FooBar;
57     }
58 }
59 use foo::{bar::$0}
60 "#,
61         expect![[r#"
62             st FooBar
63         "#]],
64     );
65     check(
66         r#"
67 mod foo {
68     pub mod bar {
69         pub struct FooBar;
70     }
71 }
72 use foo::{$0}
73 "#,
74         expect![[r#"
75             kw self
76             md bar
77         "#]],
78     );
79 }
80
81 #[test]
82 fn deeply_nested_use_tree() {
83     check(
84         r#"
85 mod foo {
86     pub mod bar {
87         pub mod baz {
88             pub struct FooBarBaz;
89         }
90     }
91 }
92 use foo::{bar::{baz::$0}}
93 "#,
94         expect![[r#"
95             st FooBarBaz
96         "#]],
97     );
98     check(
99         r#"
100 mod foo {
101     pub mod bar {
102         pub mod baz {
103             pub struct FooBarBaz;
104         }
105     }
106 }
107 use foo::{bar::{$0}}
108 "#,
109         expect![[r#"
110             kw self
111             md baz
112         "#]],
113     );
114 }
115
116 #[test]
117 fn plain_qualified_use_tree() {
118     check(
119         r#"
120 use foo::$0
121
122 mod foo {
123     struct Private;
124     pub struct Foo;
125 }
126 struct Bar;
127 "#,
128         expect![[r#"
129             st Foo
130         "#]],
131     );
132 }
133
134 #[test]
135 fn self_qualified_use_tree() {
136     check(
137         r#"
138 use self::$0
139
140 mod foo {}
141 struct Bar;
142 "#,
143         expect![[r#"
144             md foo
145             st Bar
146         "#]],
147     );
148 }
149
150 #[test]
151 fn super_qualified_use_tree() {
152     check(
153         r#"
154 mod bar {
155     use super::$0
156 }
157
158 mod foo {}
159 struct Bar;
160 "#,
161         expect![[r#"
162             kw super::
163             st Bar
164             md bar
165             md foo
166         "#]],
167     );
168 }
169
170 #[test]
171 fn super_super_qualified_use_tree() {
172     check(
173         r#"
174 mod 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]
191 fn crate_qualified_use_tree() {
192     check(
193         r#"
194 use crate::$0
195
196 mod foo {}
197 struct Bar;
198 "#,
199         expect![[r#"
200             md foo
201             st Bar
202         "#]],
203     );
204 }
205
206 #[test]
207 fn extern_crate_qualified_use_tree() {
208     check(
209         r#"
210 //- /lib.rs crate:main deps:other_crate
211 use other_crate::$0
212 //- /other_crate/lib.rs crate:other_crate
213 pub struct Foo;
214 pub mod foo {}
215 "#,
216         expect![[r#"
217             st Foo
218             md foo
219         "#]],
220     );
221 }
222
223 #[test]
224 fn pub_use_tree() {
225     check(
226         r#"
227 pub struct X;
228 pub mod bar {}
229 pub use $0;
230 "#,
231         expect![[r#"
232             kw crate::
233             kw self::
234             kw super::
235             md bar
236         "#]],
237     );
238 }
239
240 #[test]
241 fn use_tree_braces_at_start() {
242     check(
243         r#"
244 struct X;
245 mod bar {}
246 use {$0};
247 "#,
248         expect![[r#"
249             kw crate::
250             kw self::
251             kw super::
252             md bar
253         "#]],
254     );
255 }