diff options
-rw-r--r-- | crates/ra_analysis/src/mock_analysis.rs | 28 | ||||
-rw-r--r-- | crates/ra_analysis/tests/tests.rs | 18 |
2 files changed, 44 insertions, 2 deletions
diff --git a/crates/ra_analysis/src/mock_analysis.rs b/crates/ra_analysis/src/mock_analysis.rs index 960529404..b37371499 100644 --- a/crates/ra_analysis/src/mock_analysis.rs +++ b/crates/ra_analysis/src/mock_analysis.rs | |||
@@ -55,6 +55,26 @@ impl MockAnalysis { | |||
55 | (res, position) | 55 | (res, position) |
56 | } | 56 | } |
57 | 57 | ||
58 | /// Same as `with_files`, but requires that a single file contains two `<|>` marker, | ||
59 | /// whose range is also returned. | ||
60 | pub fn with_files_and_range(fixture: &str) -> (MockAnalysis, FileRange) { | ||
61 | let mut range = None; | ||
62 | let mut res = MockAnalysis::new(); | ||
63 | for entry in parse_fixture(fixture) { | ||
64 | if entry.text.contains(CURSOR_MARKER) { | ||
65 | assert!( | ||
66 | range.is_none(), | ||
67 | "only two marker (<|>) per fixture is allowed" | ||
68 | ); | ||
69 | range = Some(res.add_file_with_range(&entry.meta, &entry.text)); | ||
70 | } else { | ||
71 | res.add_file(&entry.meta, &entry.text); | ||
72 | } | ||
73 | } | ||
74 | let range = range.expect("expected two marker (<|>)"); | ||
75 | (res, range) | ||
76 | } | ||
77 | |||
58 | pub fn add_file(&mut self, path: &str, text: &str) -> FileId { | 78 | pub fn add_file(&mut self, path: &str, text: &str) -> FileId { |
59 | let file_id = FileId((self.files.len() + 1) as u32); | 79 | let file_id = FileId((self.files.len() + 1) as u32); |
60 | self.files.push((path.to_string(), text.to_string())); | 80 | self.files.push((path.to_string(), text.to_string())); |
@@ -102,12 +122,18 @@ impl MockAnalysis { | |||
102 | } | 122 | } |
103 | } | 123 | } |
104 | 124 | ||
105 | /// Creates analysis from a multi-file fixture, returns positions marked with <|>. | 125 | /// Creates analysis from a multi-file fixture, returns positions marked with a <|>. |
106 | pub fn analysis_and_position(fixture: &str) -> (Analysis, FilePosition) { | 126 | pub fn analysis_and_position(fixture: &str) -> (Analysis, FilePosition) { |
107 | let (mock, position) = MockAnalysis::with_files_and_position(fixture); | 127 | let (mock, position) = MockAnalysis::with_files_and_position(fixture); |
108 | (mock.analysis(), position) | 128 | (mock.analysis(), position) |
109 | } | 129 | } |
110 | 130 | ||
131 | /// Creates analysis from a multi-file fixture, returns ranges marked with two <|>. | ||
132 | pub fn analysis_and_range(fixture: &str) -> (Analysis, FileRange) { | ||
133 | let (mock, range) = MockAnalysis::with_files_and_range(fixture); | ||
134 | (mock.analysis(), range) | ||
135 | } | ||
136 | |||
111 | /// Creates analysis for a single file. | 137 | /// Creates analysis for a single file. |
112 | pub fn single_file(code: &str) -> (Analysis, FileId) { | 138 | pub fn single_file(code: &str) -> (Analysis, FileId) { |
113 | let mut mock = MockAnalysis::new(); | 139 | let mut mock = MockAnalysis::new(); |
diff --git a/crates/ra_analysis/tests/tests.rs b/crates/ra_analysis/tests/tests.rs index 3045c2e78..ce6f6f3fa 100644 --- a/crates/ra_analysis/tests/tests.rs +++ b/crates/ra_analysis/tests/tests.rs | |||
@@ -2,7 +2,7 @@ use ra_syntax::TextRange; | |||
2 | use test_utils::{assert_eq_dbg, assert_eq_text}; | 2 | use test_utils::{assert_eq_dbg, assert_eq_text}; |
3 | 3 | ||
4 | use ra_analysis::{ | 4 | use ra_analysis::{ |
5 | mock_analysis::{analysis_and_position, single_file, single_file_with_position, MockAnalysis}, | 5 | mock_analysis::{analysis_and_position, analysis_and_range, single_file, single_file_with_position, MockAnalysis}, |
6 | AnalysisChange, CrateGraph, FileId, FnSignatureInfo, | 6 | AnalysisChange, CrateGraph, FileId, FnSignatureInfo, |
7 | }; | 7 | }; |
8 | 8 | ||
@@ -10,6 +10,22 @@ fn get_signature(text: &str) -> (FnSignatureInfo, Option<usize>) { | |||
10 | let (analysis, position) = single_file_with_position(text); | 10 | let (analysis, position) = single_file_with_position(text); |
11 | analysis.resolve_callable(position).unwrap().unwrap() | 11 | analysis.resolve_callable(position).unwrap().unwrap() |
12 | } | 12 | } |
13 | #[test] | ||
14 | fn test_type_of() { | ||
15 | let (analysis, range) = analysis_and_range( | ||
16 | " | ||
17 | //- /lib.rs | ||
18 | pub fn foo() -> u32 { | ||
19 | 1 | ||
20 | }; | ||
21 | |||
22 | let <|>foo_test<|> = foo(); | ||
23 | ", | ||
24 | ); | ||
25 | |||
26 | let type_name = analysis.type_of(range).unwrap().unwrap(); | ||
27 | assert_eq_dbg("u32", &type_name); | ||
28 | } | ||
13 | 29 | ||
14 | #[test] | 30 | #[test] |
15 | fn approximate_resolve_works_in_items() { | 31 | fn approximate_resolve_works_in_items() { |