aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis/tests/tests.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-01-03 17:15:12 +0000
committerAleksey Kladov <[email protected]>2019-01-03 17:15:12 +0000
commitcd32177a25a98026584a514de46461ce66f3eb56 (patch)
tree62a4dfebe2b179df6c7636100e1c13e33afe16f5 /crates/ra_analysis/tests/tests.rs
parent8a24f25482f07eecab254c93223369fa532c076f (diff)
don't create many compilation units for tests
Diffstat (limited to 'crates/ra_analysis/tests/tests.rs')
-rw-r--r--crates/ra_analysis/tests/tests.rs546
1 files changed, 0 insertions, 546 deletions
diff --git a/crates/ra_analysis/tests/tests.rs b/crates/ra_analysis/tests/tests.rs
deleted file mode 100644
index c2ef87b54..000000000
--- a/crates/ra_analysis/tests/tests.rs
+++ /dev/null
@@ -1,546 +0,0 @@
1use ra_syntax::TextRange;
2use test_utils::{assert_eq_dbg, assert_eq_text};
3
4use ra_analysis::{
5 mock_analysis::{analysis_and_position, single_file, single_file_with_position, MockAnalysis},
6 AnalysisChange, CrateGraph, FileId, FnSignatureInfo,
7};
8
9fn get_signature(text: &str) -> (FnSignatureInfo, Option<usize>) {
10 let (analysis, position) = single_file_with_position(text);
11 analysis.resolve_callable(position).unwrap().unwrap()
12}
13
14#[test]
15fn approximate_resolve_works_in_items() {
16 let (analysis, pos) = analysis_and_position(
17 "
18 //- /lib.rs
19 struct Foo;
20 enum E { X(Foo<|>) }
21 ",
22 );
23
24 let symbols = analysis.approximately_resolve_symbol(pos).unwrap().unwrap();
25 assert_eq_dbg(
26 r#"ReferenceResolution {
27 reference_range: [23; 26),
28 resolves_to: [NavigationTarget { file_id: FileId(1), name: "Foo", kind: STRUCT_DEF, range: [0; 11), ptr: Some(LocalSyntaxPtr { range: [0; 11), kind: STRUCT_DEF }) }]
29 }"#,
30 &symbols,
31 );
32}
33
34#[test]
35fn test_resolve_module() {
36 let (analysis, pos) = analysis_and_position(
37 "
38 //- /lib.rs
39 mod <|>foo;
40 //- /foo.rs
41 // empty
42 ",
43 );
44
45 let symbols = analysis.approximately_resolve_symbol(pos).unwrap().unwrap();
46 assert_eq_dbg(
47 r#"ReferenceResolution {
48 reference_range: [4; 7),
49 resolves_to: [NavigationTarget { file_id: FileId(2), name: "foo", kind: MODULE, range: [0; 0), ptr: None }]
50 }"#,
51 &symbols,
52 );
53
54 let (analysis, pos) = analysis_and_position(
55 "
56 //- /lib.rs
57 mod <|>foo;
58 //- /foo/mod.rs
59 // empty
60 ",
61 );
62
63 let symbols = analysis.approximately_resolve_symbol(pos).unwrap().unwrap();
64 assert_eq_dbg(
65 r#"ReferenceResolution {
66 reference_range: [4; 7),
67 resolves_to: [NavigationTarget { file_id: FileId(2), name: "foo", kind: MODULE, range: [0; 0), ptr: None }]
68 }"#,
69 &symbols,
70 );
71}
72
73#[test]
74fn test_unresolved_module_diagnostic() {
75 let (analysis, file_id) = single_file("mod foo;");
76 let diagnostics = analysis.diagnostics(file_id).unwrap();
77 assert_eq_dbg(
78 r#"[Diagnostic {
79 message: "unresolved module",
80 range: [4; 7),
81 fix: Some(SourceChange {
82 label: "create module",
83 source_file_edits: [],
84 file_system_edits: [CreateFile { source_root: SourceRootId(0), path: "foo.rs" }],
85 cursor_position: None }),
86 severity: Error }]"#,
87 &diagnostics,
88 );
89}
90
91#[test]
92fn test_unresolved_module_diagnostic_no_diag_for_inline_mode() {
93 let (analysis, file_id) = single_file("mod foo {}");
94 let diagnostics = analysis.diagnostics(file_id).unwrap();
95 assert_eq_dbg(r#"[]"#, &diagnostics);
96}
97
98#[test]
99fn test_resolve_parent_module() {
100 let (analysis, pos) = analysis_and_position(
101 "
102 //- /lib.rs
103 mod foo;
104 //- /foo.rs
105 <|>// empty
106 ",
107 );
108 let symbols = analysis.parent_module(pos).unwrap();
109 assert_eq_dbg(
110 r#"[NavigationTarget { file_id: FileId(1), name: "foo", kind: MODULE, range: [4; 7), ptr: None }]"#,
111 &symbols,
112 );
113}
114
115#[test]
116fn test_resolve_parent_module_for_inline() {
117 let (analysis, pos) = analysis_and_position(
118 "
119 //- /lib.rs
120 mod foo {
121 mod bar {
122 mod baz { <|> }
123 }
124 }
125 ",
126 );
127 let symbols = analysis.parent_module(pos).unwrap();
128 assert_eq_dbg(
129 r#"[NavigationTarget { file_id: FileId(1), name: "bar", kind: MODULE, range: [18; 21), ptr: None }]"#,
130 &symbols,
131 );
132}
133
134#[test]
135fn test_resolve_crate_root() {
136 let mock = MockAnalysis::with_files(
137 "
138 //- /lib.rs
139 mod foo;
140 //- /foo.rs
141 // emtpy <|>
142 ",
143 );
144 let root_file = mock.id_of("/lib.rs");
145 let mod_file = mock.id_of("/foo.rs");
146 let mut host = mock.analysis_host();
147 assert!(host.analysis().crate_for(mod_file).unwrap().is_empty());
148
149 let mut crate_graph = CrateGraph::default();
150 let crate_id = crate_graph.add_crate_root(root_file);
151 let mut change = AnalysisChange::new();
152 change.set_crate_graph(crate_graph);
153 host.apply_change(change);
154
155 assert_eq!(host.analysis().crate_for(mod_file).unwrap(), vec![crate_id]);
156}
157
158#[test]
159fn test_fn_signature_two_args_first() {
160 let (desc, param) = get_signature(
161 r#"fn foo(x: u32, y: u32) -> u32 {x + y}
162fn bar() { foo(<|>3, ); }"#,
163 );
164
165 assert_eq!(desc.name, "foo".to_string());
166 assert_eq!(desc.params, vec!("x".to_string(), "y".to_string()));
167 assert_eq!(desc.ret_type, Some("-> u32".into()));
168 assert_eq!(param, Some(0));
169}
170
171#[test]
172fn test_fn_signature_two_args_second() {
173 let (desc, param) = get_signature(
174 r#"fn foo(x: u32, y: u32) -> u32 {x + y}
175fn bar() { foo(3, <|>); }"#,
176 );
177
178 assert_eq!(desc.name, "foo".to_string());
179 assert_eq!(desc.params, vec!("x".to_string(), "y".to_string()));
180 assert_eq!(desc.ret_type, Some("-> u32".into()));
181 assert_eq!(param, Some(1));
182}
183
184#[test]
185fn test_fn_signature_for_impl() {
186 let (desc, param) = get_signature(
187 r#"struct F; impl F { pub fn new() { F{}} }
188fn bar() {let _ : F = F::new(<|>);}"#,
189 );
190
191 assert_eq!(desc.name, "new".to_string());
192 assert_eq!(desc.params, Vec::<String>::new());
193 assert_eq!(desc.ret_type, None);
194 assert_eq!(param, None);
195}
196
197#[test]
198fn test_fn_signature_for_method_self() {
199 let (desc, param) = get_signature(
200 r#"struct F;
201impl F {
202 pub fn new() -> F{
203 F{}
204 }
205
206 pub fn do_it(&self) {}
207}
208
209fn bar() {
210 let f : F = F::new();
211 f.do_it(<|>);
212}"#,
213 );
214
215 assert_eq!(desc.name, "do_it".to_string());
216 assert_eq!(desc.params, vec!["&self".to_string()]);
217 assert_eq!(desc.ret_type, None);
218 assert_eq!(param, None);
219}
220
221#[test]
222fn test_fn_signature_for_method_with_arg() {
223 let (desc, param) = get_signature(
224 r#"struct F;
225impl F {
226 pub fn new() -> F{
227 F{}
228 }
229
230 pub fn do_it(&self, x: i32) {}
231}
232
233fn bar() {
234 let f : F = F::new();
235 f.do_it(<|>);
236}"#,
237 );
238
239 assert_eq!(desc.name, "do_it".to_string());
240 assert_eq!(desc.params, vec!["&self".to_string(), "x".to_string()]);
241 assert_eq!(desc.ret_type, None);
242 assert_eq!(param, Some(1));
243}
244
245#[test]
246fn test_fn_signature_with_docs_simple() {
247 let (desc, param) = get_signature(
248 r#"
249// test
250fn foo(j: u32) -> u32 {
251 j
252}
253
254fn bar() {
255 let _ = foo(<|>);
256}
257"#,
258 );
259
260 assert_eq!(desc.name, "foo".to_string());
261 assert_eq!(desc.params, vec!["j".to_string()]);
262 assert_eq!(desc.ret_type, Some("-> u32".to_string()));
263 assert_eq!(param, Some(0));
264 assert_eq!(desc.label, "fn foo(j: u32) -> u32".to_string());
265 assert_eq!(desc.doc, Some("test".into()));
266}
267
268#[test]
269fn test_fn_signature_with_docs() {
270 let (desc, param) = get_signature(
271 r#"
272/// Adds one to the number given.
273///
274/// # Examples
275///
276/// ```
277/// let five = 5;
278///
279/// assert_eq!(6, my_crate::add_one(5));
280/// ```
281pub fn add_one(x: i32) -> i32 {
282 x + 1
283}
284
285pub fn do() {
286 add_one(<|>
287}"#,
288 );
289
290 assert_eq!(desc.name, "add_one".to_string());
291 assert_eq!(desc.params, vec!["x".to_string()]);
292 assert_eq!(desc.ret_type, Some("-> i32".to_string()));
293 assert_eq!(param, Some(0));
294 assert_eq!(desc.label, "pub fn add_one(x: i32) -> i32".to_string());
295 assert_eq!(
296 desc.doc,
297 Some(
298 r#"Adds one to the number given.
299
300# Examples
301
302```rust
303let five = 5;
304
305assert_eq!(6, my_crate::add_one(5));
306```"#
307 .into()
308 )
309 );
310}
311
312#[test]
313fn test_fn_signature_with_docs_impl() {
314 let (desc, param) = get_signature(
315 r#"
316struct addr;
317impl addr {
318 /// Adds one to the number given.
319 ///
320 /// # Examples
321 ///
322 /// ```
323 /// let five = 5;
324 ///
325 /// assert_eq!(6, my_crate::add_one(5));
326 /// ```
327 pub fn add_one(x: i32) -> i32 {
328 x + 1
329 }
330}
331
332pub fn do_it() {
333 addr {};
334 addr::add_one(<|>);
335}"#,
336 );
337
338 assert_eq!(desc.name, "add_one".to_string());
339 assert_eq!(desc.params, vec!["x".to_string()]);
340 assert_eq!(desc.ret_type, Some("-> i32".to_string()));
341 assert_eq!(param, Some(0));
342 assert_eq!(desc.label, "pub fn add_one(x: i32) -> i32".to_string());
343 assert_eq!(
344 desc.doc,
345 Some(
346 r#"Adds one to the number given.
347
348# Examples
349
350```rust
351let five = 5;
352
353assert_eq!(6, my_crate::add_one(5));
354```"#
355 .into()
356 )
357 );
358}
359
360#[test]
361fn test_fn_signature_with_docs_from_actix() {
362 let (desc, param) = get_signature(
363 r#"
364pub trait WriteHandler<E>
365where
366 Self: Actor,
367 Self::Context: ActorContext,
368{
369 /// Method is called when writer emits error.
370 ///
371 /// If this method returns `ErrorAction::Continue` writer processing
372 /// continues otherwise stream processing stops.
373 fn error(&mut self, err: E, ctx: &mut Self::Context) -> Running {
374 Running::Stop
375 }
376
377 /// Method is called when writer finishes.
378 ///
379 /// By default this method stops actor's `Context`.
380 fn finished(&mut self, ctx: &mut Self::Context) {
381 ctx.stop()
382 }
383}
384
385pub fn foo() {
386 WriteHandler r;
387 r.finished(<|>);
388}
389
390"#,
391 );
392
393 assert_eq!(desc.name, "finished".to_string());
394 assert_eq!(
395 desc.params,
396 vec!["&mut self".to_string(), "ctx".to_string()]
397 );
398 assert_eq!(desc.ret_type, None);
399 assert_eq!(param, Some(1));
400 assert_eq!(
401 desc.doc,
402 Some(
403 r#"Method is called when writer finishes.
404
405By default this method stops actor's `Context`."#
406 .into()
407 )
408 );
409}
410
411fn get_all_refs(text: &str) -> Vec<(FileId, TextRange)> {
412 let (analysis, position) = single_file_with_position(text);
413 analysis.find_all_refs(position).unwrap()
414}
415
416#[test]
417fn test_find_all_refs_for_local() {
418 let code = r#"
419 fn main() {
420 let mut i = 1;
421 let j = 1;
422 i = i<|> + j;
423
424 {
425 i = 0;
426 }
427
428 i = 5;
429 }"#;
430
431 let refs = get_all_refs(code);
432 assert_eq!(refs.len(), 5);
433}
434
435#[test]
436fn test_find_all_refs_for_param_inside() {
437 let code = r#"
438 fn foo(i : u32) -> u32 {
439 i<|>
440 }"#;
441
442 let refs = get_all_refs(code);
443 assert_eq!(refs.len(), 2);
444}
445
446#[test]
447fn test_find_all_refs_for_fn_param() {
448 let code = r#"
449 fn foo(i<|> : u32) -> u32 {
450 i
451 }"#;
452
453 let refs = get_all_refs(code);
454 assert_eq!(refs.len(), 2);
455}
456#[test]
457fn test_rename_for_local() {
458 test_rename(
459 r#"
460 fn main() {
461 let mut i = 1;
462 let j = 1;
463 i = i<|> + j;
464
465 {
466 i = 0;
467 }
468
469 i = 5;
470 }"#,
471 "k",
472 r#"
473 fn main() {
474 let mut k = 1;
475 let j = 1;
476 k = k + j;
477
478 {
479 k = 0;
480 }
481
482 k = 5;
483 }"#,
484 );
485}
486
487#[test]
488fn test_rename_for_param_inside() {
489 test_rename(
490 r#"
491 fn foo(i : u32) -> u32 {
492 i<|>
493 }"#,
494 "j",
495 r#"
496 fn foo(j : u32) -> u32 {
497 j
498 }"#,
499 );
500}
501
502#[test]
503fn test_rename_refs_for_fn_param() {
504 test_rename(
505 r#"
506 fn foo(i<|> : u32) -> u32 {
507 i
508 }"#,
509 "new_name",
510 r#"
511 fn foo(new_name : u32) -> u32 {
512 new_name
513 }"#,
514 );
515}
516
517#[test]
518fn test_rename_for_mut_param() {
519 test_rename(
520 r#"
521 fn foo(mut i<|> : u32) -> u32 {
522 i
523 }"#,
524 "new_name",
525 r#"
526 fn foo(mut new_name : u32) -> u32 {
527 new_name
528 }"#,
529 );
530}
531fn test_rename(text: &str, new_name: &str, expected: &str) {
532 let (analysis, position) = single_file_with_position(text);
533 let edits = analysis.rename(position, new_name).unwrap();
534 let mut text_edit_bulder = ra_text_edit::TextEditBuilder::default();
535 let mut file_id: Option<FileId> = None;
536 for edit in edits {
537 file_id = Some(edit.file_id);
538 for atom in edit.edit.as_atoms() {
539 text_edit_bulder.replace(atom.delete, atom.insert.clone());
540 }
541 }
542 let result = text_edit_bulder
543 .finish()
544 .apply(&*analysis.file_text(file_id.unwrap()));
545 assert_eq_text!(expected, &*result);
546}