aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api
diff options
context:
space:
mode:
authorgfreezy <[email protected]>2019-01-18 08:29:09 +0000
committerAleksey Kladov <[email protected]>2019-01-19 12:36:58 +0000
commit360167db16a3ae6bb3146cf8e05336288b711921 (patch)
tree7ebb001ab92b37b05aa0c62208ee6915bffec097 /crates/ra_ide_api
parent6ab6d1eaa036f8620542ad9c12764957e868b7e0 (diff)
prefer inline tests
Diffstat (limited to 'crates/ra_ide_api')
-rw-r--r--crates/ra_ide_api/src/rename.rs136
-rw-r--r--crates/ra_ide_api/src/snapshots/tests__rename_mod.snap (renamed from crates/ra_ide_api/tests/test/snapshots/test__rename_mod.snap)4
-rw-r--r--crates/ra_ide_api/src/snapshots/tests__rename_mod_in_dir.snap (renamed from crates/ra_ide_api/tests/test/snapshots/test__rename_mod_in_dir.snap)4
-rw-r--r--crates/ra_ide_api/tests/test/main.rs127
4 files changed, 140 insertions, 131 deletions
diff --git a/crates/ra_ide_api/src/rename.rs b/crates/ra_ide_api/src/rename.rs
index 9ab6f2a77..53dc273c6 100644
--- a/crates/ra_ide_api/src/rename.rs
+++ b/crates/ra_ide_api/src/rename.rs
@@ -132,3 +132,139 @@ fn rename_reference(
132 cursor_position: None, 132 cursor_position: None,
133 }); 133 });
134} 134}
135
136#[cfg(test)]
137mod tests {
138 use insta::assert_debug_snapshot_matches;
139 use test_utils::assert_eq_text;
140 use crate::{
141 mock_analysis::single_file_with_position,
142 mock_analysis::analysis_and_position,
143 FileId
144};
145
146 #[test]
147 fn test_rename_for_local() {
148 test_rename(
149 r#"
150 fn main() {
151 let mut i = 1;
152 let j = 1;
153 i = i<|> + j;
154
155 {
156 i = 0;
157 }
158
159 i = 5;
160 }"#,
161 "k",
162 r#"
163 fn main() {
164 let mut k = 1;
165 let j = 1;
166 k = k + j;
167
168 {
169 k = 0;
170 }
171
172 k = 5;
173 }"#,
174 );
175 }
176
177 #[test]
178 fn test_rename_for_param_inside() {
179 test_rename(
180 r#"
181 fn foo(i : u32) -> u32 {
182 i<|>
183 }"#,
184 "j",
185 r#"
186 fn foo(j : u32) -> u32 {
187 j
188 }"#,
189 );
190 }
191
192 #[test]
193 fn test_rename_refs_for_fn_param() {
194 test_rename(
195 r#"
196 fn foo(i<|> : u32) -> u32 {
197 i
198 }"#,
199 "new_name",
200 r#"
201 fn foo(new_name : u32) -> u32 {
202 new_name
203 }"#,
204 );
205 }
206
207 #[test]
208 fn test_rename_for_mut_param() {
209 test_rename(
210 r#"
211 fn foo(mut i<|> : u32) -> u32 {
212 i
213 }"#,
214 "new_name",
215 r#"
216 fn foo(mut new_name : u32) -> u32 {
217 new_name
218 }"#,
219 );
220 }
221
222 #[test]
223 fn test_rename_mod() {
224 let (analysis, position) = analysis_and_position(
225 "
226 //- /bar.rs
227 mod fo<|>o;
228 //- /bar/foo.rs
229 // emtpy
230 ",
231 );
232 let new_name = "foo2";
233 let source_change = analysis.rename(position, new_name).unwrap();
234 assert_debug_snapshot_matches!("rename_mod", &source_change);
235 }
236
237 #[test]
238 fn test_rename_mod_in_dir() {
239 let (analysis, position) = analysis_and_position(
240 "
241 //- /lib.rs
242 mod fo<|>o;
243 //- /foo/mod.rs
244 // emtpy
245 ",
246 );
247 let new_name = "foo2";
248 let source_change = analysis.rename(position, new_name).unwrap();
249 assert_debug_snapshot_matches!("rename_mod_in_dir", &source_change);
250 }
251
252 fn test_rename(text: &str, new_name: &str, expected: &str) {
253 let (analysis, position) = single_file_with_position(text);
254 let source_change = analysis.rename(position, new_name).unwrap();
255 let mut text_edit_bulder = ra_text_edit::TextEditBuilder::default();
256 let mut file_id: Option<FileId> = None;
257 if let Some(change) = source_change {
258 for edit in change.source_file_edits {
259 file_id = Some(edit.file_id);
260 for atom in edit.edit.as_atoms() {
261 text_edit_bulder.replace(atom.delete, atom.insert.clone());
262 }
263 }
264 }
265 let result = text_edit_bulder
266 .finish()
267 .apply(&*analysis.file_text(file_id.unwrap()));
268 assert_eq_text!(expected, &*result);
269 }
270}
diff --git a/crates/ra_ide_api/tests/test/snapshots/test__rename_mod.snap b/crates/ra_ide_api/src/snapshots/tests__rename_mod.snap
index 54f622b95..dc97b3334 100644
--- a/crates/ra_ide_api/tests/test/snapshots/test__rename_mod.snap
+++ b/crates/ra_ide_api/src/snapshots/tests__rename_mod.snap
@@ -1,6 +1,6 @@
1Created: 2019-01-16T14:12:39.379431+00:00 1Created: 2019-01-18T08:26:43.427092+00:00
2Creator: [email protected] 2Creator: [email protected]
3Source: crates/ra_ide_api/tests/test/main.rs 3Source: crates/ra_ide_api/src/rename.rs
4 4
5Some( 5Some(
6 SourceChange { 6 SourceChange {
diff --git a/crates/ra_ide_api/tests/test/snapshots/test__rename_mod_in_dir.snap b/crates/ra_ide_api/src/snapshots/tests__rename_mod_in_dir.snap
index aac30e89f..7dcd65185 100644
--- a/crates/ra_ide_api/tests/test/snapshots/test__rename_mod_in_dir.snap
+++ b/crates/ra_ide_api/src/snapshots/tests__rename_mod_in_dir.snap
@@ -1,6 +1,6 @@
1Created: 2019-01-16T14:12:39.379358+00:00 1Created: 2019-01-18T08:26:43.427095+00:00
2Creator: [email protected] 2Creator: [email protected]
3Source: crates/ra_ide_api/tests/test/main.rs 3Source: crates/ra_ide_api/src/rename.rs
4 4
5Some( 5Some(
6 SourceChange { 6 SourceChange {
diff --git a/crates/ra_ide_api/tests/test/main.rs b/crates/ra_ide_api/tests/test/main.rs
index 2077a89ce..756075190 100644
--- a/crates/ra_ide_api/tests/test/main.rs
+++ b/crates/ra_ide_api/tests/test/main.rs
@@ -2,9 +2,7 @@ use ra_ide_api::{
2 AnalysisChange, 2 AnalysisChange,
3 CrateGraph, FileId, mock_analysis::{MockAnalysis, single_file, single_file_with_position}, Query, 3 CrateGraph, FileId, mock_analysis::{MockAnalysis, single_file, single_file_with_position}, Query,
4}; 4};
5use ra_ide_api::mock_analysis::analysis_and_position;
6use ra_syntax::TextRange; 5use ra_syntax::TextRange;
7use test_utils::assert_eq_text;
8use insta::assert_debug_snapshot_matches; 6use insta::assert_debug_snapshot_matches;
9 7
10#[test] 8#[test]
@@ -93,131 +91,6 @@ fn test_find_all_refs_for_fn_param() {
93} 91}
94 92
95#[test] 93#[test]
96fn test_rename_for_local() {
97 test_rename(
98 r#"
99 fn main() {
100 let mut i = 1;
101 let j = 1;
102 i = i<|> + j;
103
104 {
105 i = 0;
106 }
107
108 i = 5;
109 }"#,
110 "k",
111 r#"
112 fn main() {
113 let mut k = 1;
114 let j = 1;
115 k = k + j;
116
117 {
118 k = 0;
119 }
120
121 k = 5;
122 }"#,
123 );
124}
125
126#[test]
127fn test_rename_for_param_inside() {
128 test_rename(
129 r#"
130 fn foo(i : u32) -> u32 {
131 i<|>
132 }"#,
133 "j",
134 r#"
135 fn foo(j : u32) -> u32 {
136 j
137 }"#,
138 );
139}
140
141#[test]
142fn test_rename_refs_for_fn_param() {
143 test_rename(
144 r#"
145 fn foo(i<|> : u32) -> u32 {
146 i
147 }"#,
148 "new_name",
149 r#"
150 fn foo(new_name : u32) -> u32 {
151 new_name
152 }"#,
153 );
154}
155
156#[test]
157fn test_rename_for_mut_param() {
158 test_rename(
159 r#"
160 fn foo(mut i<|> : u32) -> u32 {
161 i
162 }"#,
163 "new_name",
164 r#"
165 fn foo(mut new_name : u32) -> u32 {
166 new_name
167 }"#,
168 );
169}
170
171#[test]
172fn test_rename_mod() {
173 let (analysis, position) = analysis_and_position(
174 "
175 //- /bar.rs
176 mod fo<|>o;
177 //- /bar/foo.rs
178 // emtpy
179 ",
180 );
181 let new_name = "foo2";
182 let source_change = analysis.rename(position, new_name).unwrap();
183 assert_debug_snapshot_matches!("rename_mod", &source_change);
184}
185
186#[test]
187fn test_rename_mod_in_dir() {
188 let (analysis, position) = analysis_and_position(
189 "
190 //- /lib.rs
191 mod fo<|>o;
192 //- /foo/mod.rs
193 // emtpy
194 ",
195 );
196 let new_name = "foo2";
197 let source_change = analysis.rename(position, new_name).unwrap();
198 assert_debug_snapshot_matches!("rename_mod_in_dir", &source_change);
199}
200
201fn test_rename(text: &str, new_name: &str, expected: &str) {
202 let (analysis, position) = single_file_with_position(text);
203 let source_change = analysis.rename(position, new_name).unwrap();
204 let mut text_edit_bulder = ra_text_edit::TextEditBuilder::default();
205 let mut file_id: Option<FileId> = None;
206 if let Some(change) = source_change {
207 for edit in change.source_file_edits {
208 file_id = Some(edit.file_id);
209 for atom in edit.edit.as_atoms() {
210 text_edit_bulder.replace(atom.delete, atom.insert.clone());
211 }
212 }
213 }
214 let result = text_edit_bulder
215 .finish()
216 .apply(&*analysis.file_text(file_id.unwrap()));
217 assert_eq_text!(expected, &*result);
218}
219
220#[test]
221fn world_symbols_include_stuff_from_macros() { 94fn world_symbols_include_stuff_from_macros() {
222 let (analysis, _) = single_file( 95 let (analysis, _) = single_file(
223 " 96 "