aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/tests/test/main.rs
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-01-08 19:48:48 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-01-08 19:48:48 +0000
commit46f74e33ca53a7897e9020d3de75cc76a6b89d79 (patch)
tree2bc001c8ecf58b49ac9a0da1f20d5644ce29fb3a /crates/ra_ide_api/tests/test/main.rs
parent4f4f7933b1b7ff34f8633b1686b18b2d1b994c47 (diff)
parent0c62b1bb7a49bf527780ce1f8cade5eb4fbfdb2d (diff)
Merge #471
471: rename crates to match reality r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_ide_api/tests/test/main.rs')
-rw-r--r--crates/ra_ide_api/tests/test/main.rs249
1 files changed, 249 insertions, 0 deletions
diff --git a/crates/ra_ide_api/tests/test/main.rs b/crates/ra_ide_api/tests/test/main.rs
new file mode 100644
index 000000000..d1dc07e5b
--- /dev/null
+++ b/crates/ra_ide_api/tests/test/main.rs
@@ -0,0 +1,249 @@
1mod runnables;
2
3use ra_syntax::TextRange;
4use test_utils::{assert_eq_dbg, assert_eq_text};
5
6use ra_ide_api::{
7 mock_analysis::{analysis_and_position, single_file, single_file_with_position, MockAnalysis},
8 AnalysisChange, CrateGraph, FileId, Query
9};
10
11#[test]
12fn test_unresolved_module_diagnostic() {
13 let (analysis, file_id) = single_file("mod foo;");
14 let diagnostics = analysis.diagnostics(file_id).unwrap();
15 assert_eq_dbg(
16 r#"[Diagnostic {
17 message: "unresolved module",
18 range: [4; 7),
19 fix: Some(SourceChange {
20 label: "create module",
21 source_file_edits: [],
22 file_system_edits: [CreateFile { source_root: SourceRootId(0), path: "foo.rs" }],
23 cursor_position: None }),
24 severity: Error }]"#,
25 &diagnostics,
26 );
27}
28
29// FIXME: move this test to hir
30#[test]
31fn test_unresolved_module_diagnostic_no_diag_for_inline_mode() {
32 let (analysis, file_id) = single_file("mod foo {}");
33 let diagnostics = analysis.diagnostics(file_id).unwrap();
34 assert_eq_dbg(r#"[]"#, &diagnostics);
35}
36
37#[test]
38fn test_resolve_parent_module() {
39 let (analysis, pos) = analysis_and_position(
40 "
41 //- /lib.rs
42 mod foo;
43 //- /foo.rs
44 <|>// empty
45 ",
46 );
47 let symbols = analysis.parent_module(pos).unwrap();
48 assert_eq_dbg(
49 r#"[NavigationTarget { file_id: FileId(1), name: "foo", kind: MODULE, range: [4; 7), ptr: None }]"#,
50 &symbols,
51 );
52}
53
54#[test]
55fn test_resolve_parent_module_for_inline() {
56 let (analysis, pos) = analysis_and_position(
57 "
58 //- /lib.rs
59 mod foo {
60 mod bar {
61 mod baz { <|> }
62 }
63 }
64 ",
65 );
66 let symbols = analysis.parent_module(pos).unwrap();
67 assert_eq_dbg(
68 r#"[NavigationTarget { file_id: FileId(1), name: "baz", kind: MODULE, range: [36; 39), ptr: None }]"#,
69 &symbols,
70 );
71}
72
73#[test]
74fn test_resolve_crate_root() {
75 let mock = MockAnalysis::with_files(
76 "
77 //- /bar.rs
78 mod foo;
79 //- /bar/foo.rs
80 // emtpy <|>
81 ",
82 );
83 let root_file = mock.id_of("/bar.rs");
84 let mod_file = mock.id_of("/bar/foo.rs");
85 let mut host = mock.analysis_host();
86 assert!(host.analysis().crate_for(mod_file).unwrap().is_empty());
87
88 let mut crate_graph = CrateGraph::default();
89 let crate_id = crate_graph.add_crate_root(root_file);
90 let mut change = AnalysisChange::new();
91 change.set_crate_graph(crate_graph);
92 host.apply_change(change);
93
94 assert_eq!(host.analysis().crate_for(mod_file).unwrap(), vec![crate_id]);
95}
96
97fn get_all_refs(text: &str) -> Vec<(FileId, TextRange)> {
98 let (analysis, position) = single_file_with_position(text);
99 analysis.find_all_refs(position).unwrap()
100}
101
102#[test]
103fn test_find_all_refs_for_local() {
104 let code = r#"
105 fn main() {
106 let mut i = 1;
107 let j = 1;
108 i = i<|> + j;
109
110 {
111 i = 0;
112 }
113
114 i = 5;
115 }"#;
116
117 let refs = get_all_refs(code);
118 assert_eq!(refs.len(), 5);
119}
120
121#[test]
122fn test_find_all_refs_for_param_inside() {
123 let code = r#"
124 fn foo(i : u32) -> u32 {
125 i<|>
126 }"#;
127
128 let refs = get_all_refs(code);
129 assert_eq!(refs.len(), 2);
130}
131
132#[test]
133fn test_find_all_refs_for_fn_param() {
134 let code = r#"
135 fn foo(i<|> : u32) -> u32 {
136 i
137 }"#;
138
139 let refs = get_all_refs(code);
140 assert_eq!(refs.len(), 2);
141}
142#[test]
143fn test_rename_for_local() {
144 test_rename(
145 r#"
146 fn main() {
147 let mut i = 1;
148 let j = 1;
149 i = i<|> + j;
150
151 {
152 i = 0;
153 }
154
155 i = 5;
156 }"#,
157 "k",
158 r#"
159 fn main() {
160 let mut k = 1;
161 let j = 1;
162 k = k + j;
163
164 {
165 k = 0;
166 }
167
168 k = 5;
169 }"#,
170 );
171}
172
173#[test]
174fn test_rename_for_param_inside() {
175 test_rename(
176 r#"
177 fn foo(i : u32) -> u32 {
178 i<|>
179 }"#,
180 "j",
181 r#"
182 fn foo(j : u32) -> u32 {
183 j
184 }"#,
185 );
186}
187
188#[test]
189fn test_rename_refs_for_fn_param() {
190 test_rename(
191 r#"
192 fn foo(i<|> : u32) -> u32 {
193 i
194 }"#,
195 "new_name",
196 r#"
197 fn foo(new_name : u32) -> u32 {
198 new_name
199 }"#,
200 );
201}
202
203#[test]
204fn test_rename_for_mut_param() {
205 test_rename(
206 r#"
207 fn foo(mut i<|> : u32) -> u32 {
208 i
209 }"#,
210 "new_name",
211 r#"
212 fn foo(mut new_name : u32) -> u32 {
213 new_name
214 }"#,
215 );
216}
217
218fn test_rename(text: &str, new_name: &str, expected: &str) {
219 let (analysis, position) = single_file_with_position(text);
220 let edits = analysis.rename(position, new_name).unwrap();
221 let mut text_edit_bulder = ra_text_edit::TextEditBuilder::default();
222 let mut file_id: Option<FileId> = None;
223 for edit in edits {
224 file_id = Some(edit.file_id);
225 for atom in edit.edit.as_atoms() {
226 text_edit_bulder.replace(atom.delete, atom.insert.clone());
227 }
228 }
229 let result = text_edit_bulder
230 .finish()
231 .apply(&*analysis.file_text(file_id.unwrap()));
232 assert_eq_text!(expected, &*result);
233}
234
235#[test]
236fn world_symbols_include_stuff_from_macros() {
237 let (analysis, _) = single_file(
238 "
239salsa::query_group! {
240pub trait HirDatabase: SyntaxDatabase {}
241}
242 ",
243 );
244
245 let mut symbols = analysis.symbol_search(Query::new("Hir".into())).unwrap();
246 let s = symbols.pop().unwrap();
247 assert_eq!(s.name(), "HirDatabase");
248 assert_eq!(s.range(), TextRange::from_to(33.into(), 44.into()));
249}