aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_ssr/src/tests.rs102
1 files changed, 76 insertions, 26 deletions
diff --git a/crates/ra_ssr/src/tests.rs b/crates/ra_ssr/src/tests.rs
index c7c37af2f..11512c8cc 100644
--- a/crates/ra_ssr/src/tests.rs
+++ b/crates/ra_ssr/src/tests.rs
@@ -154,8 +154,19 @@ fn ssr_function_to_method() {
154fn ssr_nested_function() { 154fn ssr_nested_function() {
155 assert_ssr_transform( 155 assert_ssr_transform(
156 "foo($a, $b, $c) ==>> bar($c, baz($a, $b))", 156 "foo($a, $b, $c) ==>> bar($c, baz($a, $b))",
157 "fn foo() {} fn main { foo (x + value.method(b), x+y-z, true && false) }", 157 r#"
158 expect![["fn foo() {} fn main { bar(true && false, baz(x + value.method(b), x+y-z)) }"]], 158 //- /lib.rs crate:foo
159 fn foo() {}
160 fn bar() {}
161 fn baz() {}
162 fn main { foo (x + value.method(b), x+y-z, true && false) }
163 "#,
164 expect![[r#"
165 fn foo() {}
166 fn bar() {}
167 fn baz() {}
168 fn main { bar(true && false, baz(x + value.method(b), x+y-z)) }
169 "#]],
159 ) 170 )
160} 171}
161 172
@@ -181,8 +192,8 @@ fn ssr_with_extra_space() {
181fn ssr_keeps_nested_comment() { 192fn ssr_keeps_nested_comment() {
182 assert_ssr_transform( 193 assert_ssr_transform(
183 "foo($x) ==>> bar($x)", 194 "foo($x) ==>> bar($x)",
184 "fn foo() {} fn main() { foo(other(5 /* using 5 */)) }", 195 "fn foo() {} fn bar() {} fn main() { foo(other(5 /* using 5 */)) }",
185 expect![["fn foo() {} fn main() { bar(other(5 /* using 5 */)) }"]], 196 expect![["fn foo() {} fn bar() {} fn main() { bar(other(5 /* using 5 */)) }"]],
186 ) 197 )
187} 198}
188 199
@@ -190,17 +201,25 @@ fn ssr_keeps_nested_comment() {
190fn ssr_keeps_comment() { 201fn ssr_keeps_comment() {
191 assert_ssr_transform( 202 assert_ssr_transform(
192 "foo($x) ==>> bar($x)", 203 "foo($x) ==>> bar($x)",
193 "fn foo() {} fn main() { foo(5 /* using 5 */) }", 204 "fn foo() {} fn bar() {} fn main() { foo(5 /* using 5 */) }",
194 expect![["fn foo() {} fn main() { bar(5)/* using 5 */ }"]], 205 expect![["fn foo() {} fn bar() {} fn main() { bar(5)/* using 5 */ }"]],
195 ) 206 )
196} 207}
197 208
198#[test] 209#[test]
199fn ssr_struct_lit() { 210fn ssr_struct_lit() {
200 assert_ssr_transform( 211 assert_ssr_transform(
201 "foo{a: $a, b: $b} ==>> foo::new($a, $b)", 212 "Foo{a: $a, b: $b} ==>> Foo::new($a, $b)",
202 "fn foo() {} fn main() { foo{b:2, a:1} }", 213 r#"
203 expect![["fn foo() {} fn main() { foo::new(1, 2) }"]], 214 struct Foo() {}
215 impl Foo { fn new() {} }
216 fn main() { Foo{b:2, a:1} }
217 "#,
218 expect![[r#"
219 struct Foo() {}
220 impl Foo { fn new() {} }
221 fn main() { Foo::new(1, 2) }
222 "#]],
204 ) 223 )
205} 224}
206 225
@@ -312,7 +331,7 @@ fn match_struct_instantiation() {
312fn match_path() { 331fn match_path() {
313 let code = r#" 332 let code = r#"
314 mod foo { 333 mod foo {
315 fn bar() {} 334 pub fn bar() {}
316 } 335 }
317 fn f() {foo::bar(42)}"#; 336 fn f() {foo::bar(42)}"#;
318 assert_matches("foo::bar", code, &["foo::bar"]); 337 assert_matches("foo::bar", code, &["foo::bar"]);
@@ -413,8 +432,8 @@ fn no_match_split_expression() {
413fn replace_function_call() { 432fn replace_function_call() {
414 assert_ssr_transform( 433 assert_ssr_transform(
415 "foo() ==>> bar()", 434 "foo() ==>> bar()",
416 "fn foo() {} fn f1() {foo(); foo();}", 435 "fn foo() {} fn bar() {} fn f1() {foo(); foo();}",
417 expect![["fn foo() {} fn f1() {bar(); bar();}"]], 436 expect![["fn foo() {} fn bar() {} fn f1() {bar(); bar();}"]],
418 ); 437 );
419} 438}
420 439
@@ -422,8 +441,8 @@ fn replace_function_call() {
422fn replace_function_call_with_placeholders() { 441fn replace_function_call_with_placeholders() {
423 assert_ssr_transform( 442 assert_ssr_transform(
424 "foo($a, $b) ==>> bar($b, $a)", 443 "foo($a, $b) ==>> bar($b, $a)",
425 "fn foo() {} fn f1() {foo(5, 42)}", 444 "fn foo() {} fn bar() {} fn f1() {foo(5, 42)}",
426 expect![["fn foo() {} fn f1() {bar(42, 5)}"]], 445 expect![["fn foo() {} fn bar() {} fn f1() {bar(42, 5)}"]],
427 ); 446 );
428} 447}
429 448
@@ -431,26 +450,40 @@ fn replace_function_call_with_placeholders() {
431fn replace_nested_function_calls() { 450fn replace_nested_function_calls() {
432 assert_ssr_transform( 451 assert_ssr_transform(
433 "foo($a) ==>> bar($a)", 452 "foo($a) ==>> bar($a)",
434 "fn foo() {} fn f1() {foo(foo(42))}", 453 "fn foo() {} fn bar() {} fn f1() {foo(foo(42))}",
435 expect![["fn foo() {} fn f1() {bar(bar(42))}"]], 454 expect![["fn foo() {} fn bar() {} fn f1() {bar(bar(42))}"]],
436 ); 455 );
437} 456}
438 457
439#[test] 458#[test]
440fn replace_type() { 459fn replace_associated_function_call() {
441 assert_ssr_transform( 460 assert_ssr_transform(
442 "Result<(), $a> ==>> Option<$a>", 461 "Foo::new() ==>> Bar::new()",
443 "struct Result<T, E> {} fn f1() -> Result<(), Vec<Error>> {foo()}", 462 r#"
444 expect![["struct Result<T, E> {} fn f1() -> Option<Vec<Error>> {foo()}"]], 463 struct Foo {}
464 impl Foo { fn new() {} }
465 struct Bar {}
466 impl Bar { fn new() {} }
467 fn f1() {Foo::new();}
468 "#,
469 expect![[r#"
470 struct Foo {}
471 impl Foo { fn new() {} }
472 struct Bar {}
473 impl Bar { fn new() {} }
474 fn f1() {Bar::new();}
475 "#]],
445 ); 476 );
446} 477}
447 478
448#[test] 479#[test]
449fn replace_struct_init() { 480fn replace_type() {
450 assert_ssr_transform( 481 assert_ssr_transform(
451 "Foo {a: $a, b: $b} ==>> Foo::new($a, $b)", 482 "Result<(), $a> ==>> Option<$a>",
452 "struct Foo {} fn f1() {Foo{b: 1, a: 2}}", 483 "struct Result<T, E> {} struct Option<T> {} fn f1() -> Result<(), Vec<Error>> {foo()}",
453 expect![["struct Foo {} fn f1() {Foo::new(2, 1)}"]], 484 expect![[
485 "struct Result<T, E> {} struct Option<T> {} fn f1() -> Option<Vec<Error>> {foo()}"
486 ]],
454 ); 487 );
455} 488}
456 489
@@ -491,8 +524,23 @@ fn match_binary_op() {
491fn multiple_rules() { 524fn multiple_rules() {
492 assert_ssr_transforms( 525 assert_ssr_transforms(
493 &["$a + 1 ==>> add_one($a)", "$a + $b ==>> add($a, $b)"], 526 &["$a + 1 ==>> add_one($a)", "$a + $b ==>> add($a, $b)"],
494 "fn f() -> i32 {3 + 2 + 1}", 527 "fn add() {} fn add_one() {} fn f() -> i32 {3 + 2 + 1}",
495 expect![["fn f() -> i32 {add_one(add(3, 2))}"]], 528 expect![["fn add() {} fn add_one() {} fn f() -> i32 {add_one(add(3, 2))}"]],
529 )
530}
531
532#[test]
533fn multiple_rules_with_nested_matches() {
534 assert_ssr_transforms(
535 &["foo1($a) ==>> bar1($a)", "foo2($a) ==>> bar2($a)"],
536 r#"
537 fn foo1() {} fn foo2() {} fn bar1() {} fn bar2() {}
538 fn f() {foo1(foo2(foo1(foo2(foo1(42)))))}
539 "#,
540 expect![[r#"
541 fn foo1() {} fn foo2() {} fn bar1() {} fn bar2() {}
542 fn f() {bar1(bar2(bar1(bar2(bar1(42)))))}
543 "#]],
496 ) 544 )
497} 545}
498 546
@@ -524,12 +572,14 @@ fn replace_within_macro_expansion() {
524 macro_rules! macro1 { 572 macro_rules! macro1 {
525 ($a:expr) => {$a} 573 ($a:expr) => {$a}
526 } 574 }
575 fn bar() {}
527 fn f() {macro1!(5.x().foo().o2())} 576 fn f() {macro1!(5.x().foo().o2())}
528 "#, 577 "#,
529 expect![[r#" 578 expect![[r#"
530 macro_rules! macro1 { 579 macro_rules! macro1 {
531 ($a:expr) => {$a} 580 ($a:expr) => {$a}
532 } 581 }
582 fn bar() {}
533 fn f() {macro1!(bar(5.x()).o2())} 583 fn f() {macro1!(bar(5.x()).o2())}
534 "#]], 584 "#]],
535 ) 585 )