diff options
Diffstat (limited to 'crates/ra_analysis/tests')
-rw-r--r-- | crates/ra_analysis/tests/tests.rs | 105 |
1 files changed, 57 insertions, 48 deletions
diff --git a/crates/ra_analysis/tests/tests.rs b/crates/ra_analysis/tests/tests.rs index f5683aec5..94e025677 100644 --- a/crates/ra_analysis/tests/tests.rs +++ b/crates/ra_analysis/tests/tests.rs | |||
@@ -5,38 +5,42 @@ extern crate relative_path; | |||
5 | extern crate rustc_hash; | 5 | extern crate rustc_hash; |
6 | extern crate test_utils; | 6 | extern crate test_utils; |
7 | 7 | ||
8 | use ra_syntax::TextRange; | 8 | use ra_syntax::{TextRange}; |
9 | use test_utils::{assert_eq_dbg, extract_offset}; | 9 | use test_utils::{assert_eq_dbg}; |
10 | 10 | ||
11 | use ra_analysis::{ | 11 | use ra_analysis::{ |
12 | MockAnalysis, | 12 | AnalysisChange, CrateGraph, FileId, FnDescriptor, |
13 | AnalysisChange, Analysis, CrateGraph, CrateId, FileId, FnDescriptor, | 13 | mock_analysis::{MockAnalysis, single_file, single_file_with_position, analysis_and_position}, |
14 | }; | 14 | }; |
15 | 15 | ||
16 | fn analysis(files: &[(&str, &str)]) -> Analysis { | ||
17 | MockAnalysis::with_files(files).analysis() | ||
18 | } | ||
19 | |||
20 | fn get_signature(text: &str) -> (FnDescriptor, Option<usize>) { | 16 | fn get_signature(text: &str) -> (FnDescriptor, Option<usize>) { |
21 | let (offset, code) = extract_offset(text); | 17 | let (analysis, position) = single_file_with_position(text); |
22 | let code = code.as_str(); | 18 | analysis.resolve_callable(position.file_id, position.offset).unwrap().unwrap() |
23 | |||
24 | let snap = analysis(&[("/lib.rs", code)]); | ||
25 | |||
26 | snap.resolve_callable(FileId(1), offset).unwrap().unwrap() | ||
27 | } | 19 | } |
28 | 20 | ||
29 | #[test] | 21 | #[test] |
30 | fn test_resolve_module() { | 22 | fn test_resolve_module() { |
31 | let snap = analysis(&[("/lib.rs", "mod foo;"), ("/foo.rs", "")]); | 23 | let (analysis, pos) = analysis_and_position(" |
32 | let symbols = snap.approximately_resolve_symbol(FileId(1), 4.into()).unwrap(); | 24 | //- /lib.rs |
25 | mod <|>foo; | ||
26 | //- /foo.rs | ||
27 | // empty | ||
28 | "); | ||
29 | |||
30 | let symbols = analysis.approximately_resolve_symbol(pos.file_id, pos.offset).unwrap(); | ||
33 | assert_eq_dbg( | 31 | assert_eq_dbg( |
34 | r#"[(FileId(2), FileSymbol { name: "foo", node_range: [0; 0), kind: MODULE })]"#, | 32 | r#"[(FileId(2), FileSymbol { name: "foo", node_range: [0; 0), kind: MODULE })]"#, |
35 | &symbols, | 33 | &symbols, |
36 | ); | 34 | ); |
37 | 35 | ||
38 | let snap = analysis(&[("/lib.rs", "mod foo;"), ("/foo/mod.rs", "")]); | 36 | let (analysis, pos) = analysis_and_position(" |
39 | let symbols = snap.approximately_resolve_symbol(FileId(1), 4.into()).unwrap(); | 37 | //- /lib.rs |
38 | mod <|>foo; | ||
39 | //- /foo/mod.rs | ||
40 | // empty | ||
41 | "); | ||
42 | |||
43 | let symbols = analysis.approximately_resolve_symbol(pos.file_id, pos.offset).unwrap(); | ||
40 | assert_eq_dbg( | 44 | assert_eq_dbg( |
41 | r#"[(FileId(2), FileSymbol { name: "foo", node_range: [0; 0), kind: MODULE })]"#, | 45 | r#"[(FileId(2), FileSymbol { name: "foo", node_range: [0; 0), kind: MODULE })]"#, |
42 | &symbols, | 46 | &symbols, |
@@ -45,8 +49,8 @@ fn test_resolve_module() { | |||
45 | 49 | ||
46 | #[test] | 50 | #[test] |
47 | fn test_unresolved_module_diagnostic() { | 51 | fn test_unresolved_module_diagnostic() { |
48 | let snap = analysis(&[("/lib.rs", "mod foo;")]); | 52 | let (analysis, file_id) = single_file("mod foo;"); |
49 | let diagnostics = snap.diagnostics(FileId(1)).unwrap(); | 53 | let diagnostics = analysis.diagnostics(file_id).unwrap(); |
50 | assert_eq_dbg( | 54 | assert_eq_dbg( |
51 | r#"[Diagnostic { | 55 | r#"[Diagnostic { |
52 | message: "unresolved module", | 56 | message: "unresolved module", |
@@ -62,15 +66,20 @@ fn test_unresolved_module_diagnostic() { | |||
62 | 66 | ||
63 | #[test] | 67 | #[test] |
64 | fn test_unresolved_module_diagnostic_no_diag_for_inline_mode() { | 68 | fn test_unresolved_module_diagnostic_no_diag_for_inline_mode() { |
65 | let snap = analysis(&[("/lib.rs", "mod foo {}")]); | 69 | let (analysis, file_id) = single_file("mod foo {}"); |
66 | let diagnostics = snap.diagnostics(FileId(1)).unwrap(); | 70 | let diagnostics = analysis.diagnostics(file_id).unwrap(); |
67 | assert_eq_dbg(r#"[]"#, &diagnostics); | 71 | assert_eq_dbg(r#"[]"#, &diagnostics); |
68 | } | 72 | } |
69 | 73 | ||
70 | #[test] | 74 | #[test] |
71 | fn test_resolve_parent_module() { | 75 | fn test_resolve_parent_module() { |
72 | let snap = analysis(&[("/lib.rs", "mod foo;"), ("/foo.rs", "")]); | 76 | let (analysis, pos) = analysis_and_position(" |
73 | let symbols = snap.parent_module(FileId(2)).unwrap(); | 77 | //- /lib.rs |
78 | mod foo; | ||
79 | //- /foo.rs | ||
80 | <|>// empty | ||
81 | "); | ||
82 | let symbols = analysis.parent_module(pos.file_id).unwrap(); | ||
74 | assert_eq_dbg( | 83 | assert_eq_dbg( |
75 | r#"[(FileId(1), FileSymbol { name: "foo", node_range: [0; 8), kind: MODULE })]"#, | 84 | r#"[(FileId(1), FileSymbol { name: "foo", node_range: [0; 8), kind: MODULE })]"#, |
76 | &symbols, | 85 | &symbols, |
@@ -79,23 +88,24 @@ fn test_resolve_parent_module() { | |||
79 | 88 | ||
80 | #[test] | 89 | #[test] |
81 | fn test_resolve_crate_root() { | 90 | fn test_resolve_crate_root() { |
82 | let mut host = MockAnalysis::with_files( | 91 | let mock = MockAnalysis::with_files(" |
83 | &[("/lib.rs", "mod foo;"), ("/foo.rs", "")] | 92 | //- /lib.rs |
84 | ).analysis_host(); | 93 | mod foo; |
85 | let snap = host.analysis(); | 94 | //- /foo.rs |
86 | assert!(snap.crate_for(FileId(2)).unwrap().is_empty()); | 95 | // emtpy <|> |
87 | 96 | "); | |
88 | let crate_graph = { | 97 | let root_file = mock.id_of("/lib.rs"); |
89 | let mut g = CrateGraph::new(); | 98 | let mod_file = mock.id_of("/foo.rs"); |
90 | g.add_crate_root(FileId(1)); | 99 | let mut host = mock.analysis_host(); |
91 | g | 100 | assert!(host.analysis().crate_for(mod_file).unwrap().is_empty()); |
92 | }; | 101 | |
102 | let mut crate_graph = CrateGraph::new(); | ||
103 | let crate_id = crate_graph.add_crate_root(root_file); | ||
93 | let mut change = AnalysisChange::new(); | 104 | let mut change = AnalysisChange::new(); |
94 | change.set_crate_graph(crate_graph); | 105 | change.set_crate_graph(crate_graph); |
95 | host.apply_change(change); | 106 | host.apply_change(change); |
96 | let snap = host.analysis(); | ||
97 | 107 | ||
98 | assert_eq!(snap.crate_for(FileId(2)).unwrap(), vec![CrateId(0)],); | 108 | assert_eq!(host.analysis().crate_for(mod_file).unwrap(), vec![crate_id]); |
99 | } | 109 | } |
100 | 110 | ||
101 | #[test] | 111 | #[test] |
@@ -186,12 +196,8 @@ fn bar() { | |||
186 | } | 196 | } |
187 | 197 | ||
188 | fn get_all_refs(text: &str) -> Vec<(FileId, TextRange)> { | 198 | fn get_all_refs(text: &str) -> Vec<(FileId, TextRange)> { |
189 | let (offset, code) = extract_offset(text); | 199 | let (analysis, position) = single_file_with_position(text); |
190 | let code = code.as_str(); | 200 | analysis.find_all_refs(position.file_id, position.offset).unwrap() |
191 | |||
192 | let snap = analysis(&[("/lib.rs", code)]); | ||
193 | |||
194 | snap.find_all_refs(FileId(1), offset).unwrap() | ||
195 | } | 201 | } |
196 | 202 | ||
197 | #[test] | 203 | #[test] |
@@ -226,11 +232,14 @@ fn test_find_all_refs_for_param_inside() { | |||
226 | 232 | ||
227 | #[test] | 233 | #[test] |
228 | fn test_complete_crate_path() { | 234 | fn test_complete_crate_path() { |
229 | let snap = analysis(&[ | 235 | let (analysis, position) = analysis_and_position(" |
230 | ("/lib.rs", "mod foo; struct Spam;"), | 236 | //- /lib.rs |
231 | ("/foo.rs", "use crate::Sp"), | 237 | mod foo; |
232 | ]); | 238 | struct Spam; |
233 | let completions = snap.completions(FileId(2), 13.into()).unwrap().unwrap(); | 239 | //- /foo.rs |
240 | use crate::Sp<|> | ||
241 | "); | ||
242 | let completions = analysis.completions(position.file_id, position.offset).unwrap().unwrap(); | ||
234 | assert_eq_dbg( | 243 | assert_eq_dbg( |
235 | r#"[CompletionItem { label: "foo", lookup: None, snippet: None }, | 244 | r#"[CompletionItem { label: "foo", lookup: None, snippet: None }, |
236 | CompletionItem { label: "Spam", lookup: None, snippet: None }]"#, | 245 | CompletionItem { label: "Spam", lookup: None, snippet: None }]"#, |