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