aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/tests/test
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide_api/tests/test')
-rw-r--r--crates/ra_ide_api/tests/test/main.rs249
-rw-r--r--crates/ra_ide_api/tests/test/runnables.rs109
2 files changed, 358 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}
diff --git a/crates/ra_ide_api/tests/test/runnables.rs b/crates/ra_ide_api/tests/test/runnables.rs
new file mode 100644
index 000000000..da8d5e0d5
--- /dev/null
+++ b/crates/ra_ide_api/tests/test/runnables.rs
@@ -0,0 +1,109 @@
1use test_utils::assert_eq_dbg;
2
3use ra_ide_api::mock_analysis::analysis_and_position;
4
5#[test]
6fn test_runnables() {
7 let (analysis, pos) = analysis_and_position(
8 r#"
9 //- /lib.rs
10 <|> //empty
11 fn main() {}
12
13 #[test]
14 fn test_foo() {}
15
16 #[test]
17 #[ignore]
18 fn test_foo() {}
19 "#,
20 );
21 let runnables = analysis.runnables(pos.file_id).unwrap();
22 assert_eq_dbg(
23 r#"[Runnable { range: [1; 21), kind: Bin },
24 Runnable { range: [22; 46), kind: Test { name: "test_foo" } },
25 Runnable { range: [47; 81), kind: Test { name: "test_foo" } }]"#,
26 &runnables,
27 )
28}
29
30#[test]
31fn test_runnables_module() {
32 let (analysis, pos) = analysis_and_position(
33 r#"
34 //- /lib.rs
35 <|> //empty
36 mod test_mod {
37 #[test]
38 fn test_foo1() {}
39 }
40 "#,
41 );
42 let runnables = analysis.runnables(pos.file_id).unwrap();
43 assert_eq_dbg(
44 r#"[Runnable { range: [1; 59), kind: TestMod { path: "test_mod" } },
45 Runnable { range: [28; 57), kind: Test { name: "test_foo1" } }]"#,
46 &runnables,
47 )
48}
49
50#[test]
51fn test_runnables_one_depth_layer_module() {
52 let (analysis, pos) = analysis_and_position(
53 r#"
54 //- /lib.rs
55 <|> //empty
56 mod foo {
57 mod test_mod {
58 #[test]
59 fn test_foo1() {}
60 }
61 }
62 "#,
63 );
64 let runnables = analysis.runnables(pos.file_id).unwrap();
65 assert_eq_dbg(
66 r#"[Runnable { range: [23; 85), kind: TestMod { path: "foo::test_mod" } },
67 Runnable { range: [46; 79), kind: Test { name: "test_foo1" } }]"#,
68 &runnables,
69 )
70}
71
72#[test]
73fn test_runnables_multiple_depth_module() {
74 let (analysis, pos) = analysis_and_position(
75 r#"
76 //- /lib.rs
77 <|> //empty
78 mod foo {
79 mod bar {
80 mod test_mod {
81 #[test]
82 fn test_foo1() {}
83 }
84 }
85 }
86 "#,
87 );
88 let runnables = analysis.runnables(pos.file_id).unwrap();
89 assert_eq_dbg(
90 r#"[Runnable { range: [41; 115), kind: TestMod { path: "foo::bar::test_mod" } },
91 Runnable { range: [68; 105), kind: Test { name: "test_foo1" } }]"#,
92 &runnables,
93 )
94}
95
96#[test]
97fn test_runnables_no_test_function_in_module() {
98 let (analysis, pos) = analysis_and_position(
99 r#"
100 //- /lib.rs
101 <|> //empty
102 mod test_mod {
103 fn foo1() {}
104 }
105 "#,
106 );
107 let runnables = analysis.runnables(pos.file_id).unwrap();
108 assert_eq_dbg(r#"[]"#, &runnables)
109}