aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_ssr/src/tests.rs164
1 files changed, 97 insertions, 67 deletions
diff --git a/crates/ra_ssr/src/tests.rs b/crates/ra_ssr/src/tests.rs
index 9628dcbac..f20ae2cdf 100644
--- a/crates/ra_ssr/src/tests.rs
+++ b/crates/ra_ssr/src/tests.rs
@@ -148,8 +148,8 @@ fn assert_match_failure_reason(pattern: &str, code: &str, snippet: &str, expecte
148fn ssr_function_to_method() { 148fn ssr_function_to_method() {
149 assert_ssr_transform( 149 assert_ssr_transform(
150 "my_function($a, $b) ==>> ($a).my_method($b)", 150 "my_function($a, $b) ==>> ($a).my_method($b)",
151 "loop { my_function( other_func(x, y), z + w) }", 151 "fn my_function() {} fn main() { loop { my_function( other_func(x, y), z + w) } }",
152 "loop { (other_func(x, y)).my_method(z + w) }", 152 "fn my_function() {} fn main() { loop { (other_func(x, y)).my_method(z + w) } }",
153 ) 153 )
154} 154}
155 155
@@ -157,8 +157,8 @@ fn ssr_function_to_method() {
157fn ssr_nested_function() { 157fn ssr_nested_function() {
158 assert_ssr_transform( 158 assert_ssr_transform(
159 "foo($a, $b, $c) ==>> bar($c, baz($a, $b))", 159 "foo($a, $b, $c) ==>> bar($c, baz($a, $b))",
160 "fn main { foo (x + value.method(b), x+y-z, true && false) }", 160 "fn foo() {} fn main { foo (x + value.method(b), x+y-z, true && false) }",
161 "fn main { bar(true && false, baz(x + value.method(b), x+y-z)) }", 161 "fn foo() {} fn main { bar(true && false, baz(x + value.method(b), x+y-z)) }",
162 ) 162 )
163} 163}
164 164
@@ -166,8 +166,8 @@ fn ssr_nested_function() {
166fn ssr_expected_spacing() { 166fn ssr_expected_spacing() {
167 assert_ssr_transform( 167 assert_ssr_transform(
168 "foo($x) + bar() ==>> bar($x)", 168 "foo($x) + bar() ==>> bar($x)",
169 "fn main() { foo(5) + bar() }", 169 "fn foo() {} fn bar() {} fn main() { foo(5) + bar() }",
170 "fn main() { bar(5) }", 170 "fn foo() {} fn bar() {} fn main() { bar(5) }",
171 ); 171 );
172} 172}
173 173
@@ -175,8 +175,8 @@ fn ssr_expected_spacing() {
175fn ssr_with_extra_space() { 175fn ssr_with_extra_space() {
176 assert_ssr_transform( 176 assert_ssr_transform(
177 "foo($x ) + bar() ==>> bar($x)", 177 "foo($x ) + bar() ==>> bar($x)",
178 "fn main() { foo( 5 ) +bar( ) }", 178 "fn foo() {} fn bar() {} fn main() { foo( 5 ) +bar( ) }",
179 "fn main() { bar(5) }", 179 "fn foo() {} fn bar() {} fn main() { bar(5) }",
180 ); 180 );
181} 181}
182 182
@@ -184,8 +184,8 @@ fn ssr_with_extra_space() {
184fn ssr_keeps_nested_comment() { 184fn ssr_keeps_nested_comment() {
185 assert_ssr_transform( 185 assert_ssr_transform(
186 "foo($x) ==>> bar($x)", 186 "foo($x) ==>> bar($x)",
187 "fn main() { foo(other(5 /* using 5 */)) }", 187 "fn foo() {} fn main() { foo(other(5 /* using 5 */)) }",
188 "fn main() { bar(other(5 /* using 5 */)) }", 188 "fn foo() {} fn main() { bar(other(5 /* using 5 */)) }",
189 ) 189 )
190} 190}
191 191
@@ -193,8 +193,8 @@ fn ssr_keeps_nested_comment() {
193fn ssr_keeps_comment() { 193fn ssr_keeps_comment() {
194 assert_ssr_transform( 194 assert_ssr_transform(
195 "foo($x) ==>> bar($x)", 195 "foo($x) ==>> bar($x)",
196 "fn main() { foo(5 /* using 5 */) }", 196 "fn foo() {} fn main() { foo(5 /* using 5 */) }",
197 "fn main() { bar(5)/* using 5 */ }", 197 "fn foo() {} fn main() { bar(5)/* using 5 */ }",
198 ) 198 )
199} 199}
200 200
@@ -202,8 +202,8 @@ fn ssr_keeps_comment() {
202fn ssr_struct_lit() { 202fn ssr_struct_lit() {
203 assert_ssr_transform( 203 assert_ssr_transform(
204 "foo{a: $a, b: $b} ==>> foo::new($a, $b)", 204 "foo{a: $a, b: $b} ==>> foo::new($a, $b)",
205 "fn main() { foo{b:2, a:1} }", 205 "fn foo() {} fn main() { foo{b:2, a:1} }",
206 "fn main() { foo::new(1, 2) }", 206 "fn foo() {} fn main() { foo::new(1, 2) }",
207 ) 207 )
208} 208}
209 209
@@ -225,16 +225,18 @@ fn match_fn_definition() {
225 225
226#[test] 226#[test]
227fn match_struct_definition() { 227fn match_struct_definition() {
228 assert_matches( 228 let code = r#"
229 "struct $n {$f: Option<String>}", 229 struct Option<T> {}
230 "struct Bar {} struct Foo {name: Option<String>}", 230 struct Bar {}
231 &["struct Foo {name: Option<String>}"], 231 struct Foo {name: Option<String>}"#;
232 ); 232 assert_matches("struct $n {$f: Option<String>}", code, &["struct Foo {name: Option<String>}"]);
233} 233}
234 234
235#[test] 235#[test]
236fn match_expr() { 236fn match_expr() {
237 let code = "fn f() -> i32 {foo(40 + 2, 42)}"; 237 let code = r#"
238 fn foo() {}
239 fn f() -> i32 {foo(40 + 2, 42)}"#;
238 assert_matches("foo($a, $b)", code, &["foo(40 + 2, 42)"]); 240 assert_matches("foo($a, $b)", code, &["foo(40 + 2, 42)"]);
239 assert_no_match("foo($a, $b, $c)", code); 241 assert_no_match("foo($a, $b, $c)", code);
240 assert_no_match("foo($a)", code); 242 assert_no_match("foo($a)", code);
@@ -263,7 +265,9 @@ fn match_nested_method_calls_with_macro_call() {
263 265
264#[test] 266#[test]
265fn match_complex_expr() { 267fn match_complex_expr() {
266 let code = "fn f() -> i32 {foo(bar(40, 2), 42)}"; 268 let code = r#"
269 fn foo() {} fn bar() {}
270 fn f() -> i32 {foo(bar(40, 2), 42)}"#;
267 assert_matches("foo($a, $b)", code, &["foo(bar(40, 2), 42)"]); 271 assert_matches("foo($a, $b)", code, &["foo(bar(40, 2), 42)"]);
268 assert_no_match("foo($a, $b, $c)", code); 272 assert_no_match("foo($a, $b, $c)", code);
269 assert_no_match("foo($a)", code); 273 assert_no_match("foo($a)", code);
@@ -274,53 +278,62 @@ fn match_complex_expr() {
274#[test] 278#[test]
275fn match_with_trailing_commas() { 279fn match_with_trailing_commas() {
276 // Code has comma, pattern doesn't. 280 // Code has comma, pattern doesn't.
277 assert_matches("foo($a, $b)", "fn f() {foo(1, 2,);}", &["foo(1, 2,)"]); 281 assert_matches("foo($a, $b)", "fn foo() {} fn f() {foo(1, 2,);}", &["foo(1, 2,)"]);
278 assert_matches("Foo{$a, $b}", "fn f() {Foo{1, 2,};}", &["Foo{1, 2,}"]); 282 assert_matches("Foo{$a, $b}", "struct Foo {} fn f() {Foo{1, 2,};}", &["Foo{1, 2,}"]);
279 283
280 // Pattern has comma, code doesn't. 284 // Pattern has comma, code doesn't.
281 assert_matches("foo($a, $b,)", "fn f() {foo(1, 2);}", &["foo(1, 2)"]); 285 assert_matches("foo($a, $b,)", "fn foo() {} fn f() {foo(1, 2);}", &["foo(1, 2)"]);
282 assert_matches("Foo{$a, $b,}", "fn f() {Foo{1, 2};}", &["Foo{1, 2}"]); 286 assert_matches("Foo{$a, $b,}", "struct Foo {} fn f() {Foo{1, 2};}", &["Foo{1, 2}"]);
283} 287}
284 288
285#[test] 289#[test]
286fn match_type() { 290fn match_type() {
287 assert_matches("i32", "fn f() -> i32 {1 + 2}", &["i32"]); 291 assert_matches("i32", "fn f() -> i32 {1 + 2}", &["i32"]);
288 assert_matches("Option<$a>", "fn f() -> Option<i32> {42}", &["Option<i32>"]); 292 assert_matches(
289 assert_no_match("Option<$a>", "fn f() -> Result<i32, ()> {42}"); 293 "Option<$a>",
294 "struct Option<T> {} fn f() -> Option<i32> {42}",
295 &["Option<i32>"],
296 );
297 assert_no_match(
298 "Option<$a>",
299 "struct Option<T> {} struct Result<T, E> {} fn f() -> Result<i32, ()> {42}",
300 );
290} 301}
291 302
292#[test] 303#[test]
293fn match_struct_instantiation() { 304fn match_struct_instantiation() {
294 assert_matches( 305 let code = r#"
295 "Foo {bar: 1, baz: 2}", 306 struct Foo {bar: i32, baz: i32}
296 "fn f() {Foo {bar: 1, baz: 2}}", 307 fn f() {Foo {bar: 1, baz: 2}}"#;
297 &["Foo {bar: 1, baz: 2}"], 308 assert_matches("Foo {bar: 1, baz: 2}", code, &["Foo {bar: 1, baz: 2}"]);
298 );
299 // Now with placeholders for all parts of the struct. 309 // Now with placeholders for all parts of the struct.
300 assert_matches( 310 assert_matches("Foo {$a: $b, $c: $d}", code, &["Foo {bar: 1, baz: 2}"]);
301 "Foo {$a: $b, $c: $d}", 311 assert_matches("Foo {}", "struct Foo {} fn f() {Foo {}}", &["Foo {}"]);
302 "fn f() {Foo {bar: 1, baz: 2}}",
303 &["Foo {bar: 1, baz: 2}"],
304 );
305 assert_matches("Foo {}", "fn f() {Foo {}}", &["Foo {}"]);
306} 312}
307 313
308#[test] 314#[test]
309fn match_path() { 315fn match_path() {
310 assert_matches("foo::bar", "fn f() {foo::bar(42)}", &["foo::bar"]); 316 let code = r#"
311 assert_matches("$a::bar", "fn f() {foo::bar(42)}", &["foo::bar"]); 317 mod foo {
312 assert_matches("foo::$b", "fn f() {foo::bar(42)}", &["foo::bar"]); 318 fn bar() {}
319 }
320 fn f() {foo::bar(42)}"#;
321 assert_matches("foo::bar", code, &["foo::bar"]);
322 assert_matches("$a::bar", code, &["foo::bar"]);
323 assert_matches("foo::$b", code, &["foo::bar"]);
313} 324}
314 325
315#[test] 326#[test]
316fn match_pattern() { 327fn match_pattern() {
317 assert_matches("Some($a)", "fn f() {if let Some(x) = foo() {}}", &["Some(x)"]); 328 assert_matches("Some($a)", "struct Some(); fn f() {if let Some(x) = foo() {}}", &["Some(x)"]);
318} 329}
319 330
320#[test] 331#[test]
321fn literal_constraint() { 332fn literal_constraint() {
322 mark::check!(literal_constraint); 333 mark::check!(literal_constraint);
323 let code = r#" 334 let code = r#"
335 enum Option<T> { Some(T), None }
336 use Option::Some;
324 fn f1() { 337 fn f1() {
325 let x1 = Some(42); 338 let x1 = Some(42);
326 let x2 = Some("foo"); 339 let x2 = Some("foo");
@@ -337,24 +350,36 @@ fn literal_constraint() {
337fn match_reordered_struct_instantiation() { 350fn match_reordered_struct_instantiation() {
338 assert_matches( 351 assert_matches(
339 "Foo {aa: 1, b: 2, ccc: 3}", 352 "Foo {aa: 1, b: 2, ccc: 3}",
340 "fn f() {Foo {b: 2, ccc: 3, aa: 1}}", 353 "struct Foo {} fn f() {Foo {b: 2, ccc: 3, aa: 1}}",
341 &["Foo {b: 2, ccc: 3, aa: 1}"], 354 &["Foo {b: 2, ccc: 3, aa: 1}"],
342 ); 355 );
343 assert_no_match("Foo {a: 1}", "fn f() {Foo {b: 1}}"); 356 assert_no_match("Foo {a: 1}", "struct Foo {} fn f() {Foo {b: 1}}");
344 assert_no_match("Foo {a: 1}", "fn f() {Foo {a: 2}}"); 357 assert_no_match("Foo {a: 1}", "struct Foo {} fn f() {Foo {a: 2}}");
345 assert_no_match("Foo {a: 1, b: 2}", "fn f() {Foo {a: 1}}"); 358 assert_no_match("Foo {a: 1, b: 2}", "struct Foo {} fn f() {Foo {a: 1}}");
346 assert_no_match("Foo {a: 1, b: 2}", "fn f() {Foo {b: 2}}"); 359 assert_no_match("Foo {a: 1, b: 2}", "struct Foo {} fn f() {Foo {b: 2}}");
347 assert_no_match("Foo {a: 1, }", "fn f() {Foo {a: 1, b: 2}}"); 360 assert_no_match("Foo {a: 1, }", "struct Foo {} fn f() {Foo {a: 1, b: 2}}");
348 assert_no_match("Foo {a: 1, z: 9}", "fn f() {Foo {a: 1}}"); 361 assert_no_match("Foo {a: 1, z: 9}", "struct Foo {} fn f() {Foo {a: 1}}");
349} 362}
350 363
351#[test] 364#[test]
352fn match_macro_invocation() { 365fn match_macro_invocation() {
353 assert_matches("foo!($a)", "fn() {foo(foo!(foo()))}", &["foo!(foo())"]); 366 assert_matches(
354 assert_matches("foo!(41, $a, 43)", "fn() {foo!(41, 42, 43)}", &["foo!(41, 42, 43)"]); 367 "foo!($a)",
355 assert_no_match("foo!(50, $a, 43)", "fn() {foo!(41, 42, 43}"); 368 "macro_rules! foo {() => {}} fn() {foo(foo!(foo()))}",
356 assert_no_match("foo!(41, $a, 50)", "fn() {foo!(41, 42, 43}"); 369 &["foo!(foo())"],
357 assert_matches("foo!($a())", "fn() {foo!(bar())}", &["foo!(bar())"]); 370 );
371 assert_matches(
372 "foo!(41, $a, 43)",
373 "macro_rules! foo {() => {}} fn() {foo!(41, 42, 43)}",
374 &["foo!(41, 42, 43)"],
375 );
376 assert_no_match("foo!(50, $a, 43)", "macro_rules! foo {() => {}} fn() {foo!(41, 42, 43}");
377 assert_no_match("foo!(41, $a, 50)", "macro_rules! foo {() => {}} fn() {foo!(41, 42, 43}");
378 assert_matches(
379 "foo!($a())",
380 "macro_rules! foo {() => {}} fn() {foo!(bar())}",
381 &["foo!(bar())"],
382 );
358} 383}
359 384
360// When matching within a macro expansion, we only allow matches of nodes that originated from 385// When matching within a macro expansion, we only allow matches of nodes that originated from
@@ -389,15 +414,19 @@ fn no_match_split_expression() {
389 414
390#[test] 415#[test]
391fn replace_function_call() { 416fn replace_function_call() {
392 assert_ssr_transform("foo() ==>> bar()", "fn f1() {foo(); foo();}", "fn f1() {bar(); bar();}"); 417 assert_ssr_transform(
418 "foo() ==>> bar()",
419 "fn foo() {} fn f1() {foo(); foo();}",
420 "fn foo() {} fn f1() {bar(); bar();}",
421 );
393} 422}
394 423
395#[test] 424#[test]
396fn replace_function_call_with_placeholders() { 425fn replace_function_call_with_placeholders() {
397 assert_ssr_transform( 426 assert_ssr_transform(
398 "foo($a, $b) ==>> bar($b, $a)", 427 "foo($a, $b) ==>> bar($b, $a)",
399 "fn f1() {foo(5, 42)}", 428 "fn foo() {} fn f1() {foo(5, 42)}",
400 "fn f1() {bar(42, 5)}", 429 "fn foo() {} fn f1() {bar(42, 5)}",
401 ); 430 );
402} 431}
403 432
@@ -405,8 +434,8 @@ fn replace_function_call_with_placeholders() {
405fn replace_nested_function_calls() { 434fn replace_nested_function_calls() {
406 assert_ssr_transform( 435 assert_ssr_transform(
407 "foo($a) ==>> bar($a)", 436 "foo($a) ==>> bar($a)",
408 "fn f1() {foo(foo(42))}", 437 "fn foo() {} fn f1() {foo(foo(42))}",
409 "fn f1() {bar(bar(42))}", 438 "fn foo() {} fn f1() {bar(bar(42))}",
410 ); 439 );
411} 440}
412 441
@@ -414,8 +443,8 @@ fn replace_nested_function_calls() {
414fn replace_type() { 443fn replace_type() {
415 assert_ssr_transform( 444 assert_ssr_transform(
416 "Result<(), $a> ==>> Option<$a>", 445 "Result<(), $a> ==>> Option<$a>",
417 "fn f1() -> Result<(), Vec<Error>> {foo()}", 446 "struct Result<T, E> {} fn f1() -> Result<(), Vec<Error>> {foo()}",
418 "fn f1() -> Option<Vec<Error>> {foo()}", 447 "struct Result<T, E> {} fn f1() -> Option<Vec<Error>> {foo()}",
419 ); 448 );
420} 449}
421 450
@@ -423,8 +452,8 @@ fn replace_type() {
423fn replace_struct_init() { 452fn replace_struct_init() {
424 assert_ssr_transform( 453 assert_ssr_transform(
425 "Foo {a: $a, b: $b} ==>> Foo::new($a, $b)", 454 "Foo {a: $a, b: $b} ==>> Foo::new($a, $b)",
426 "fn f1() {Foo{b: 1, a: 2}}", 455 "struct Foo {} fn f1() {Foo{b: 1, a: 2}}",
427 "fn f1() {Foo::new(2, 1)}", 456 "struct Foo {} fn f1() {Foo::new(2, 1)}",
428 ); 457 );
429} 458}
430 459
@@ -432,13 +461,13 @@ fn replace_struct_init() {
432fn replace_macro_invocations() { 461fn replace_macro_invocations() {
433 assert_ssr_transform( 462 assert_ssr_transform(
434 "try!($a) ==>> $a?", 463 "try!($a) ==>> $a?",
435 "fn f1() -> Result<(), E> {bar(try!(foo()));}", 464 "macro_rules! try {() => {}} fn f1() -> Result<(), E> {bar(try!(foo()));}",
436 "fn f1() -> Result<(), E> {bar(foo()?);}", 465 "macro_rules! try {() => {}} fn f1() -> Result<(), E> {bar(foo()?);}",
437 ); 466 );
438 assert_ssr_transform( 467 assert_ssr_transform(
439 "foo!($a($b)) ==>> foo($b, $a)", 468 "foo!($a($b)) ==>> foo($b, $a)",
440 "fn f1() {foo!(abc(def() + 2));}", 469 "macro_rules! foo {() => {}} fn f1() {foo!(abc(def() + 2));}",
441 "fn f1() {foo(def() + 2, abc);}", 470 "macro_rules! foo {() => {}} fn f1() {foo(def() + 2, abc);}",
442 ); 471 );
443} 472}
444 473
@@ -527,6 +556,7 @@ fn preserves_whitespace_within_macro_expansion() {
527#[test] 556#[test]
528fn match_failure_reasons() { 557fn match_failure_reasons() {
529 let code = r#" 558 let code = r#"
559 fn bar() {}
530 macro_rules! foo { 560 macro_rules! foo {
531 ($a:expr) => { 561 ($a:expr) => {
532 1 + $a + 2 562 1 + $a + 2