diff options
Diffstat (limited to 'crates/ra_ide_api/tests/test/main.rs')
-rw-r--r-- | crates/ra_ide_api/tests/test/main.rs | 376 |
1 files changed, 0 insertions, 376 deletions
diff --git a/crates/ra_ide_api/tests/test/main.rs b/crates/ra_ide_api/tests/test/main.rs deleted file mode 100644 index d4ff21c09..000000000 --- a/crates/ra_ide_api/tests/test/main.rs +++ /dev/null | |||
@@ -1,376 +0,0 @@ | |||
1 | use ra_ide_api::{ | ||
2 | mock_analysis::{single_file, single_file_with_position, single_file_with_range, MockAnalysis}, | ||
3 | AnalysisChange, CrateGraph, Edition::Edition2018, Query, NavigationTarget, | ||
4 | ReferenceSearchResult, | ||
5 | }; | ||
6 | use ra_syntax::SmolStr; | ||
7 | |||
8 | #[test] | ||
9 | fn test_resolve_crate_root() { | ||
10 | let mock = MockAnalysis::with_files( | ||
11 | " | ||
12 | //- /bar.rs | ||
13 | mod foo; | ||
14 | //- /foo.rs | ||
15 | // empty <|> | ||
16 | ", | ||
17 | ); | ||
18 | let root_file = mock.id_of("/bar.rs"); | ||
19 | let mod_file = mock.id_of("/foo.rs"); | ||
20 | let mut host = mock.analysis_host(); | ||
21 | assert!(host.analysis().crate_for(mod_file).unwrap().is_empty()); | ||
22 | |||
23 | let mut crate_graph = CrateGraph::default(); | ||
24 | let crate_id = crate_graph.add_crate_root(root_file, Edition2018); | ||
25 | let mut change = AnalysisChange::new(); | ||
26 | change.set_crate_graph(crate_graph); | ||
27 | host.apply_change(change); | ||
28 | |||
29 | assert_eq!(host.analysis().crate_for(mod_file).unwrap(), vec![crate_id]); | ||
30 | } | ||
31 | |||
32 | fn get_all_refs(text: &str) -> ReferenceSearchResult { | ||
33 | let (analysis, position) = single_file_with_position(text); | ||
34 | analysis.find_all_refs(position).unwrap().unwrap() | ||
35 | } | ||
36 | |||
37 | fn get_symbols_matching(text: &str, query: &str) -> Vec<NavigationTarget> { | ||
38 | let (analysis, _) = single_file(text); | ||
39 | analysis.symbol_search(Query::new(query.into())).unwrap() | ||
40 | } | ||
41 | |||
42 | #[test] | ||
43 | fn test_find_all_refs_for_local() { | ||
44 | let code = r#" | ||
45 | fn main() { | ||
46 | let mut i = 1; | ||
47 | let j = 1; | ||
48 | i = i<|> + j; | ||
49 | |||
50 | { | ||
51 | i = 0; | ||
52 | } | ||
53 | |||
54 | i = 5; | ||
55 | }"#; | ||
56 | |||
57 | let refs = get_all_refs(code); | ||
58 | assert_eq!(refs.len(), 5); | ||
59 | } | ||
60 | |||
61 | #[test] | ||
62 | fn test_find_all_refs_for_param_inside() { | ||
63 | let code = r#" | ||
64 | fn foo(i : u32) -> u32 { | ||
65 | i<|> | ||
66 | }"#; | ||
67 | |||
68 | let refs = get_all_refs(code); | ||
69 | assert_eq!(refs.len(), 2); | ||
70 | } | ||
71 | |||
72 | #[test] | ||
73 | fn test_find_all_refs_for_fn_param() { | ||
74 | let code = r#" | ||
75 | fn foo(i<|> : u32) -> u32 { | ||
76 | i | ||
77 | }"#; | ||
78 | |||
79 | let refs = get_all_refs(code); | ||
80 | assert_eq!(refs.len(), 2); | ||
81 | } | ||
82 | |||
83 | #[test] | ||
84 | fn test_world_symbols_with_no_container() { | ||
85 | let code = r#" | ||
86 | enum FooInner { } | ||
87 | "#; | ||
88 | |||
89 | let mut symbols = get_symbols_matching(code, "FooInner"); | ||
90 | |||
91 | let s = symbols.pop().unwrap(); | ||
92 | |||
93 | assert_eq!(s.name(), "FooInner"); | ||
94 | assert!(s.container_name().is_none()); | ||
95 | } | ||
96 | |||
97 | #[test] | ||
98 | fn test_world_symbols_include_container_name() { | ||
99 | let code = r#" | ||
100 | fn foo() { | ||
101 | enum FooInner { } | ||
102 | } | ||
103 | "#; | ||
104 | |||
105 | let mut symbols = get_symbols_matching(code, "FooInner"); | ||
106 | |||
107 | let s = symbols.pop().unwrap(); | ||
108 | |||
109 | assert_eq!(s.name(), "FooInner"); | ||
110 | assert_eq!(s.container_name(), Some(&SmolStr::new("foo"))); | ||
111 | |||
112 | let code = r#" | ||
113 | mod foo { | ||
114 | struct FooInner; | ||
115 | } | ||
116 | "#; | ||
117 | |||
118 | let mut symbols = get_symbols_matching(code, "FooInner"); | ||
119 | |||
120 | let s = symbols.pop().unwrap(); | ||
121 | |||
122 | assert_eq!(s.name(), "FooInner"); | ||
123 | assert_eq!(s.container_name(), Some(&SmolStr::new("foo"))); | ||
124 | } | ||
125 | |||
126 | #[test] | ||
127 | fn test_syntax_tree_without_range() { | ||
128 | // Basic syntax | ||
129 | let (analysis, file_id) = single_file(r#"fn foo() {}"#); | ||
130 | let syn = analysis.syntax_tree(file_id, None); | ||
131 | |||
132 | assert_eq!( | ||
133 | syn.trim(), | ||
134 | r#" | ||
135 | SOURCE_FILE@[0; 11) | ||
136 | FN_DEF@[0; 11) | ||
137 | FN_KW@[0; 2) | ||
138 | WHITESPACE@[2; 3) | ||
139 | NAME@[3; 6) | ||
140 | IDENT@[3; 6) "foo" | ||
141 | PARAM_LIST@[6; 8) | ||
142 | L_PAREN@[6; 7) | ||
143 | R_PAREN@[7; 8) | ||
144 | WHITESPACE@[8; 9) | ||
145 | BLOCK@[9; 11) | ||
146 | L_CURLY@[9; 10) | ||
147 | R_CURLY@[10; 11) | ||
148 | "# | ||
149 | .trim() | ||
150 | ); | ||
151 | |||
152 | let (analysis, file_id) = single_file( | ||
153 | r#" | ||
154 | fn test() { | ||
155 | assert!(" | ||
156 | fn foo() { | ||
157 | } | ||
158 | ", ""); | ||
159 | }"# | ||
160 | .trim(), | ||
161 | ); | ||
162 | let syn = analysis.syntax_tree(file_id, None); | ||
163 | |||
164 | assert_eq!( | ||
165 | syn.trim(), | ||
166 | r#" | ||
167 | SOURCE_FILE@[0; 60) | ||
168 | FN_DEF@[0; 60) | ||
169 | FN_KW@[0; 2) | ||
170 | WHITESPACE@[2; 3) | ||
171 | NAME@[3; 7) | ||
172 | IDENT@[3; 7) "test" | ||
173 | PARAM_LIST@[7; 9) | ||
174 | L_PAREN@[7; 8) | ||
175 | R_PAREN@[8; 9) | ||
176 | WHITESPACE@[9; 10) | ||
177 | BLOCK@[10; 60) | ||
178 | L_CURLY@[10; 11) | ||
179 | WHITESPACE@[11; 16) | ||
180 | EXPR_STMT@[16; 58) | ||
181 | MACRO_CALL@[16; 57) | ||
182 | PATH@[16; 22) | ||
183 | PATH_SEGMENT@[16; 22) | ||
184 | NAME_REF@[16; 22) | ||
185 | IDENT@[16; 22) "assert" | ||
186 | EXCL@[22; 23) | ||
187 | TOKEN_TREE@[23; 57) | ||
188 | L_PAREN@[23; 24) | ||
189 | STRING@[24; 52) | ||
190 | COMMA@[52; 53) | ||
191 | WHITESPACE@[53; 54) | ||
192 | STRING@[54; 56) | ||
193 | R_PAREN@[56; 57) | ||
194 | SEMI@[57; 58) | ||
195 | WHITESPACE@[58; 59) | ||
196 | R_CURLY@[59; 60) | ||
197 | "# | ||
198 | .trim() | ||
199 | ); | ||
200 | } | ||
201 | |||
202 | #[test] | ||
203 | fn test_syntax_tree_with_range() { | ||
204 | let (analysis, range) = single_file_with_range(r#"<|>fn foo() {}<|>"#.trim()); | ||
205 | let syn = analysis.syntax_tree(range.file_id, Some(range.range)); | ||
206 | |||
207 | assert_eq!( | ||
208 | syn.trim(), | ||
209 | r#" | ||
210 | FN_DEF@[0; 11) | ||
211 | FN_KW@[0; 2) | ||
212 | WHITESPACE@[2; 3) | ||
213 | NAME@[3; 6) | ||
214 | IDENT@[3; 6) "foo" | ||
215 | PARAM_LIST@[6; 8) | ||
216 | L_PAREN@[6; 7) | ||
217 | R_PAREN@[7; 8) | ||
218 | WHITESPACE@[8; 9) | ||
219 | BLOCK@[9; 11) | ||
220 | L_CURLY@[9; 10) | ||
221 | R_CURLY@[10; 11) | ||
222 | "# | ||
223 | .trim() | ||
224 | ); | ||
225 | |||
226 | let (analysis, range) = single_file_with_range( | ||
227 | r#"fn test() { | ||
228 | <|>assert!(" | ||
229 | fn foo() { | ||
230 | } | ||
231 | ", "");<|> | ||
232 | }"# | ||
233 | .trim(), | ||
234 | ); | ||
235 | let syn = analysis.syntax_tree(range.file_id, Some(range.range)); | ||
236 | |||
237 | assert_eq!( | ||
238 | syn.trim(), | ||
239 | r#" | ||
240 | EXPR_STMT@[16; 58) | ||
241 | MACRO_CALL@[16; 57) | ||
242 | PATH@[16; 22) | ||
243 | PATH_SEGMENT@[16; 22) | ||
244 | NAME_REF@[16; 22) | ||
245 | IDENT@[16; 22) "assert" | ||
246 | EXCL@[22; 23) | ||
247 | TOKEN_TREE@[23; 57) | ||
248 | L_PAREN@[23; 24) | ||
249 | STRING@[24; 52) | ||
250 | COMMA@[52; 53) | ||
251 | WHITESPACE@[53; 54) | ||
252 | STRING@[54; 56) | ||
253 | R_PAREN@[56; 57) | ||
254 | SEMI@[57; 58) | ||
255 | "# | ||
256 | .trim() | ||
257 | ); | ||
258 | } | ||
259 | |||
260 | #[test] | ||
261 | fn test_syntax_tree_inside_string() { | ||
262 | let (analysis, range) = single_file_with_range( | ||
263 | r#"fn test() { | ||
264 | assert!(" | ||
265 | <|>fn foo() { | ||
266 | }<|> | ||
267 | fn bar() { | ||
268 | } | ||
269 | ", ""); | ||
270 | }"# | ||
271 | .trim(), | ||
272 | ); | ||
273 | let syn = analysis.syntax_tree(range.file_id, Some(range.range)); | ||
274 | assert_eq!( | ||
275 | syn.trim(), | ||
276 | r#" | ||
277 | SOURCE_FILE@[0; 12) | ||
278 | FN_DEF@[0; 12) | ||
279 | FN_KW@[0; 2) | ||
280 | WHITESPACE@[2; 3) | ||
281 | NAME@[3; 6) | ||
282 | IDENT@[3; 6) "foo" | ||
283 | PARAM_LIST@[6; 8) | ||
284 | L_PAREN@[6; 7) | ||
285 | R_PAREN@[7; 8) | ||
286 | WHITESPACE@[8; 9) | ||
287 | BLOCK@[9; 12) | ||
288 | L_CURLY@[9; 10) | ||
289 | WHITESPACE@[10; 11) | ||
290 | R_CURLY@[11; 12) | ||
291 | "# | ||
292 | .trim() | ||
293 | ); | ||
294 | |||
295 | // With a raw string | ||
296 | let (analysis, range) = single_file_with_range( | ||
297 | r###"fn test() { | ||
298 | assert!(r#" | ||
299 | <|>fn foo() { | ||
300 | }<|> | ||
301 | fn bar() { | ||
302 | } | ||
303 | "#, ""); | ||
304 | }"### | ||
305 | .trim(), | ||
306 | ); | ||
307 | let syn = analysis.syntax_tree(range.file_id, Some(range.range)); | ||
308 | assert_eq!( | ||
309 | syn.trim(), | ||
310 | r#" | ||
311 | SOURCE_FILE@[0; 12) | ||
312 | FN_DEF@[0; 12) | ||
313 | FN_KW@[0; 2) | ||
314 | WHITESPACE@[2; 3) | ||
315 | NAME@[3; 6) | ||
316 | IDENT@[3; 6) "foo" | ||
317 | PARAM_LIST@[6; 8) | ||
318 | L_PAREN@[6; 7) | ||
319 | R_PAREN@[7; 8) | ||
320 | WHITESPACE@[8; 9) | ||
321 | BLOCK@[9; 12) | ||
322 | L_CURLY@[9; 10) | ||
323 | WHITESPACE@[10; 11) | ||
324 | R_CURLY@[11; 12) | ||
325 | "# | ||
326 | .trim() | ||
327 | ); | ||
328 | |||
329 | // With a raw string | ||
330 | let (analysis, range) = single_file_with_range( | ||
331 | r###"fn test() { | ||
332 | assert!(r<|>#" | ||
333 | fn foo() { | ||
334 | } | ||
335 | fn bar() { | ||
336 | }"<|>#, ""); | ||
337 | }"### | ||
338 | .trim(), | ||
339 | ); | ||
340 | let syn = analysis.syntax_tree(range.file_id, Some(range.range)); | ||
341 | assert_eq!( | ||
342 | syn.trim(), | ||
343 | r#" | ||
344 | SOURCE_FILE@[0; 25) | ||
345 | FN_DEF@[0; 12) | ||
346 | FN_KW@[0; 2) | ||
347 | WHITESPACE@[2; 3) | ||
348 | NAME@[3; 6) | ||
349 | IDENT@[3; 6) "foo" | ||
350 | PARAM_LIST@[6; 8) | ||
351 | L_PAREN@[6; 7) | ||
352 | R_PAREN@[7; 8) | ||
353 | WHITESPACE@[8; 9) | ||
354 | BLOCK@[9; 12) | ||
355 | L_CURLY@[9; 10) | ||
356 | WHITESPACE@[10; 11) | ||
357 | R_CURLY@[11; 12) | ||
358 | WHITESPACE@[12; 13) | ||
359 | FN_DEF@[13; 25) | ||
360 | FN_KW@[13; 15) | ||
361 | WHITESPACE@[15; 16) | ||
362 | NAME@[16; 19) | ||
363 | IDENT@[16; 19) "bar" | ||
364 | PARAM_LIST@[19; 21) | ||
365 | L_PAREN@[19; 20) | ||
366 | R_PAREN@[20; 21) | ||
367 | WHITESPACE@[21; 22) | ||
368 | BLOCK@[22; 25) | ||
369 | L_CURLY@[22; 23) | ||
370 | WHITESPACE@[23; 24) | ||
371 | R_CURLY@[24; 25) | ||
372 | |||
373 | "# | ||
374 | .trim() | ||
375 | ); | ||
376 | } | ||