diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_ssr/Cargo.toml | 3 | ||||
-rw-r--r-- | crates/ra_ssr/src/tests.rs | 71 |
2 files changed, 37 insertions, 37 deletions
diff --git a/crates/ra_ssr/Cargo.toml b/crates/ra_ssr/Cargo.toml index fe098aaee..84e4b171e 100644 --- a/crates/ra_ssr/Cargo.toml +++ b/crates/ra_ssr/Cargo.toml | |||
@@ -18,3 +18,6 @@ ra_ide_db = { path = "../ra_ide_db" } | |||
18 | hir = { path = "../ra_hir", package = "ra_hir" } | 18 | hir = { path = "../ra_hir", package = "ra_hir" } |
19 | rustc-hash = "1.1.0" | 19 | rustc-hash = "1.1.0" |
20 | test_utils = { path = "../test_utils" } | 20 | test_utils = { path = "../test_utils" } |
21 | |||
22 | [dev-dependencies] | ||
23 | expect = { path = "../expect" } | ||
diff --git a/crates/ra_ssr/src/tests.rs b/crates/ra_ssr/src/tests.rs index f20ae2cdf..9f5306592 100644 --- a/crates/ra_ssr/src/tests.rs +++ b/crates/ra_ssr/src/tests.rs | |||
@@ -1,4 +1,5 @@ | |||
1 | use crate::{MatchFinder, SsrRule}; | 1 | use crate::{MatchFinder, SsrRule}; |
2 | use expect::{expect, Expect}; | ||
2 | use ra_db::{FileId, SourceDatabaseExt}; | 3 | use ra_db::{FileId, SourceDatabaseExt}; |
3 | use test_utils::mark; | 4 | use test_utils::mark; |
4 | 5 | ||
@@ -61,16 +62,11 @@ fn single_file(code: &str) -> (ra_ide_db::RootDatabase, FileId) { | |||
61 | ra_ide_db::RootDatabase::with_single_file(code) | 62 | ra_ide_db::RootDatabase::with_single_file(code) |
62 | } | 63 | } |
63 | 64 | ||
64 | fn assert_ssr_transform(rule: &str, input: &str, result: &str) { | 65 | fn assert_ssr_transform(rule: &str, input: &str, expected: Expect) { |
65 | assert_ssr_transforms(&[rule], input, result); | 66 | assert_ssr_transforms(&[rule], input, expected); |
66 | } | 67 | } |
67 | 68 | ||
68 | fn normalize_code(code: &str) -> String { | 69 | fn assert_ssr_transforms(rules: &[&str], input: &str, expected: Expect) { |
69 | let (db, file_id) = single_file(code); | ||
70 | db.file_text(file_id).to_string() | ||
71 | } | ||
72 | |||
73 | fn assert_ssr_transforms(rules: &[&str], input: &str, result: &str) { | ||
74 | let (db, file_id) = single_file(input); | 70 | let (db, file_id) = single_file(input); |
75 | let mut match_finder = MatchFinder::new(&db); | 71 | let mut match_finder = MatchFinder::new(&db); |
76 | for rule in rules { | 72 | for rule in rules { |
@@ -80,12 +76,9 @@ fn assert_ssr_transforms(rules: &[&str], input: &str, result: &str) { | |||
80 | if let Some(edits) = match_finder.edits_for_file(file_id) { | 76 | if let Some(edits) = match_finder.edits_for_file(file_id) { |
81 | // Note, db.file_text is not necessarily the same as `input`, since fixture parsing alters | 77 | // Note, db.file_text is not necessarily the same as `input`, since fixture parsing alters |
82 | // stuff. | 78 | // stuff. |
83 | let mut after = db.file_text(file_id).to_string(); | 79 | let mut actual = db.file_text(file_id).to_string(); |
84 | edits.apply(&mut after); | 80 | edits.apply(&mut actual); |
85 | // Likewise, we need to make sure that whatever transformations fixture parsing applies, | 81 | expected.assert_eq(&actual); |
86 | // also get applied to our expected result. | ||
87 | let result = normalize_code(result); | ||
88 | assert_eq!(after, result); | ||
89 | } else { | 82 | } else { |
90 | panic!("No edits were made"); | 83 | panic!("No edits were made"); |
91 | } | 84 | } |
@@ -149,7 +142,7 @@ fn ssr_function_to_method() { | |||
149 | assert_ssr_transform( | 142 | assert_ssr_transform( |
150 | "my_function($a, $b) ==>> ($a).my_method($b)", | 143 | "my_function($a, $b) ==>> ($a).my_method($b)", |
151 | "fn my_function() {} fn main() { loop { my_function( other_func(x, y), z + w) } }", | 144 | "fn my_function() {} fn main() { loop { my_function( other_func(x, y), z + w) } }", |
152 | "fn my_function() {} fn main() { loop { (other_func(x, y)).my_method(z + w) } }", | 145 | expect![["fn my_function() {} fn main() { loop { (other_func(x, y)).my_method(z + w) } }"]], |
153 | ) | 146 | ) |
154 | } | 147 | } |
155 | 148 | ||
@@ -158,7 +151,7 @@ fn ssr_nested_function() { | |||
158 | assert_ssr_transform( | 151 | assert_ssr_transform( |
159 | "foo($a, $b, $c) ==>> bar($c, baz($a, $b))", | 152 | "foo($a, $b, $c) ==>> bar($c, baz($a, $b))", |
160 | "fn foo() {} fn main { foo (x + value.method(b), x+y-z, true && false) }", | 153 | "fn foo() {} fn main { foo (x + value.method(b), x+y-z, true && false) }", |
161 | "fn foo() {} fn main { bar(true && false, baz(x + value.method(b), x+y-z)) }", | 154 | expect![["fn foo() {} fn main { bar(true && false, baz(x + value.method(b), x+y-z)) }"]], |
162 | ) | 155 | ) |
163 | } | 156 | } |
164 | 157 | ||
@@ -167,7 +160,7 @@ fn ssr_expected_spacing() { | |||
167 | assert_ssr_transform( | 160 | assert_ssr_transform( |
168 | "foo($x) + bar() ==>> bar($x)", | 161 | "foo($x) + bar() ==>> bar($x)", |
169 | "fn foo() {} fn bar() {} fn main() { foo(5) + bar() }", | 162 | "fn foo() {} fn bar() {} fn main() { foo(5) + bar() }", |
170 | "fn foo() {} fn bar() {} fn main() { bar(5) }", | 163 | expect![["fn foo() {} fn bar() {} fn main() { bar(5) }"]], |
171 | ); | 164 | ); |
172 | } | 165 | } |
173 | 166 | ||
@@ -176,7 +169,7 @@ fn ssr_with_extra_space() { | |||
176 | assert_ssr_transform( | 169 | assert_ssr_transform( |
177 | "foo($x ) + bar() ==>> bar($x)", | 170 | "foo($x ) + bar() ==>> bar($x)", |
178 | "fn foo() {} fn bar() {} fn main() { foo( 5 ) +bar( ) }", | 171 | "fn foo() {} fn bar() {} fn main() { foo( 5 ) +bar( ) }", |
179 | "fn foo() {} fn bar() {} fn main() { bar(5) }", | 172 | expect![["fn foo() {} fn bar() {} fn main() { bar(5) }"]], |
180 | ); | 173 | ); |
181 | } | 174 | } |
182 | 175 | ||
@@ -185,7 +178,7 @@ fn ssr_keeps_nested_comment() { | |||
185 | assert_ssr_transform( | 178 | assert_ssr_transform( |
186 | "foo($x) ==>> bar($x)", | 179 | "foo($x) ==>> bar($x)", |
187 | "fn foo() {} fn main() { foo(other(5 /* using 5 */)) }", | 180 | "fn foo() {} fn main() { foo(other(5 /* using 5 */)) }", |
188 | "fn foo() {} fn main() { bar(other(5 /* using 5 */)) }", | 181 | expect![["fn foo() {} fn main() { bar(other(5 /* using 5 */)) }"]], |
189 | ) | 182 | ) |
190 | } | 183 | } |
191 | 184 | ||
@@ -194,7 +187,7 @@ fn ssr_keeps_comment() { | |||
194 | assert_ssr_transform( | 187 | assert_ssr_transform( |
195 | "foo($x) ==>> bar($x)", | 188 | "foo($x) ==>> bar($x)", |
196 | "fn foo() {} fn main() { foo(5 /* using 5 */) }", | 189 | "fn foo() {} fn main() { foo(5 /* using 5 */) }", |
197 | "fn foo() {} fn main() { bar(5)/* using 5 */ }", | 190 | expect![["fn foo() {} fn main() { bar(5)/* using 5 */ }"]], |
198 | ) | 191 | ) |
199 | } | 192 | } |
200 | 193 | ||
@@ -203,7 +196,7 @@ fn ssr_struct_lit() { | |||
203 | assert_ssr_transform( | 196 | assert_ssr_transform( |
204 | "foo{a: $a, b: $b} ==>> foo::new($a, $b)", | 197 | "foo{a: $a, b: $b} ==>> foo::new($a, $b)", |
205 | "fn foo() {} fn main() { foo{b:2, a:1} }", | 198 | "fn foo() {} fn main() { foo{b:2, a:1} }", |
206 | "fn foo() {} fn main() { foo::new(1, 2) }", | 199 | expect![["fn foo() {} fn main() { foo::new(1, 2) }"]], |
207 | ) | 200 | ) |
208 | } | 201 | } |
209 | 202 | ||
@@ -417,7 +410,7 @@ fn replace_function_call() { | |||
417 | assert_ssr_transform( | 410 | assert_ssr_transform( |
418 | "foo() ==>> bar()", | 411 | "foo() ==>> bar()", |
419 | "fn foo() {} fn f1() {foo(); foo();}", | 412 | "fn foo() {} fn f1() {foo(); foo();}", |
420 | "fn foo() {} fn f1() {bar(); bar();}", | 413 | expect![["fn foo() {} fn f1() {bar(); bar();}"]], |
421 | ); | 414 | ); |
422 | } | 415 | } |
423 | 416 | ||
@@ -426,7 +419,7 @@ fn replace_function_call_with_placeholders() { | |||
426 | assert_ssr_transform( | 419 | assert_ssr_transform( |
427 | "foo($a, $b) ==>> bar($b, $a)", | 420 | "foo($a, $b) ==>> bar($b, $a)", |
428 | "fn foo() {} fn f1() {foo(5, 42)}", | 421 | "fn foo() {} fn f1() {foo(5, 42)}", |
429 | "fn foo() {} fn f1() {bar(42, 5)}", | 422 | expect![["fn foo() {} fn f1() {bar(42, 5)}"]], |
430 | ); | 423 | ); |
431 | } | 424 | } |
432 | 425 | ||
@@ -435,7 +428,7 @@ fn replace_nested_function_calls() { | |||
435 | assert_ssr_transform( | 428 | assert_ssr_transform( |
436 | "foo($a) ==>> bar($a)", | 429 | "foo($a) ==>> bar($a)", |
437 | "fn foo() {} fn f1() {foo(foo(42))}", | 430 | "fn foo() {} fn f1() {foo(foo(42))}", |
438 | "fn foo() {} fn f1() {bar(bar(42))}", | 431 | expect![["fn foo() {} fn f1() {bar(bar(42))}"]], |
439 | ); | 432 | ); |
440 | } | 433 | } |
441 | 434 | ||
@@ -444,7 +437,7 @@ fn replace_type() { | |||
444 | assert_ssr_transform( | 437 | assert_ssr_transform( |
445 | "Result<(), $a> ==>> Option<$a>", | 438 | "Result<(), $a> ==>> Option<$a>", |
446 | "struct Result<T, E> {} fn f1() -> Result<(), Vec<Error>> {foo()}", | 439 | "struct Result<T, E> {} fn f1() -> Result<(), Vec<Error>> {foo()}", |
447 | "struct Result<T, E> {} fn f1() -> Option<Vec<Error>> {foo()}", | 440 | expect![["struct Result<T, E> {} fn f1() -> Option<Vec<Error>> {foo()}"]], |
448 | ); | 441 | ); |
449 | } | 442 | } |
450 | 443 | ||
@@ -453,7 +446,7 @@ fn replace_struct_init() { | |||
453 | assert_ssr_transform( | 446 | assert_ssr_transform( |
454 | "Foo {a: $a, b: $b} ==>> Foo::new($a, $b)", | 447 | "Foo {a: $a, b: $b} ==>> Foo::new($a, $b)", |
455 | "struct Foo {} fn f1() {Foo{b: 1, a: 2}}", | 448 | "struct Foo {} fn f1() {Foo{b: 1, a: 2}}", |
456 | "struct Foo {} fn f1() {Foo::new(2, 1)}", | 449 | expect![["struct Foo {} fn f1() {Foo::new(2, 1)}"]], |
457 | ); | 450 | ); |
458 | } | 451 | } |
459 | 452 | ||
@@ -462,12 +455,12 @@ fn replace_macro_invocations() { | |||
462 | assert_ssr_transform( | 455 | assert_ssr_transform( |
463 | "try!($a) ==>> $a?", | 456 | "try!($a) ==>> $a?", |
464 | "macro_rules! try {() => {}} fn f1() -> Result<(), E> {bar(try!(foo()));}", | 457 | "macro_rules! try {() => {}} fn f1() -> Result<(), E> {bar(try!(foo()));}", |
465 | "macro_rules! try {() => {}} fn f1() -> Result<(), E> {bar(foo()?);}", | 458 | expect![["macro_rules! try {() => {}} fn f1() -> Result<(), E> {bar(foo()?);}"]], |
466 | ); | 459 | ); |
467 | assert_ssr_transform( | 460 | assert_ssr_transform( |
468 | "foo!($a($b)) ==>> foo($b, $a)", | 461 | "foo!($a($b)) ==>> foo($b, $a)", |
469 | "macro_rules! foo {() => {}} fn f1() {foo!(abc(def() + 2));}", | 462 | "macro_rules! foo {() => {}} fn f1() {foo!(abc(def() + 2));}", |
470 | "macro_rules! foo {() => {}} fn f1() {foo(def() + 2, abc);}", | 463 | expect![["macro_rules! foo {() => {}} fn f1() {foo(def() + 2, abc);}"]], |
471 | ); | 464 | ); |
472 | } | 465 | } |
473 | 466 | ||
@@ -476,12 +469,12 @@ fn replace_binary_op() { | |||
476 | assert_ssr_transform( | 469 | assert_ssr_transform( |
477 | "$a + $b ==>> $b + $a", | 470 | "$a + $b ==>> $b + $a", |
478 | "fn f() {2 * 3 + 4 * 5}", | 471 | "fn f() {2 * 3 + 4 * 5}", |
479 | "fn f() {4 * 5 + 2 * 3}", | 472 | expect![["fn f() {4 * 5 + 2 * 3}"]], |
480 | ); | 473 | ); |
481 | assert_ssr_transform( | 474 | assert_ssr_transform( |
482 | "$a + $b ==>> $b + $a", | 475 | "$a + $b ==>> $b + $a", |
483 | "fn f() {1 + 2 + 3 + 4}", | 476 | "fn f() {1 + 2 + 3 + 4}", |
484 | "fn f() {4 + 3 + 2 + 1}", | 477 | expect![["fn f() {4 + 3 + 2 + 1}"]], |
485 | ); | 478 | ); |
486 | } | 479 | } |
487 | 480 | ||
@@ -495,7 +488,7 @@ fn multiple_rules() { | |||
495 | assert_ssr_transforms( | 488 | assert_ssr_transforms( |
496 | &["$a + 1 ==>> add_one($a)", "$a + $b ==>> add($a, $b)"], | 489 | &["$a + 1 ==>> add_one($a)", "$a + $b ==>> add($a, $b)"], |
497 | "fn f() -> i32 {3 + 2 + 1}", | 490 | "fn f() -> i32 {3 + 2 + 1}", |
498 | "fn f() -> i32 {add_one(add(3, 2))}", | 491 | expect![["fn f() -> i32 {add_one(add(3, 2))}"]], |
499 | ) | 492 | ) |
500 | } | 493 | } |
501 | 494 | ||
@@ -527,12 +520,14 @@ fn replace_within_macro_expansion() { | |||
527 | macro_rules! macro1 { | 520 | macro_rules! macro1 { |
528 | ($a:expr) => {$a} | 521 | ($a:expr) => {$a} |
529 | } | 522 | } |
530 | fn f() {macro1!(5.x().foo().o2())}"#, | 523 | fn f() {macro1!(5.x().foo().o2())} |
531 | r#" | 524 | "#, |
525 | expect![[r#" | ||
532 | macro_rules! macro1 { | 526 | macro_rules! macro1 { |
533 | ($a:expr) => {$a} | 527 | ($a:expr) => {$a} |
534 | } | 528 | } |
535 | fn f() {macro1!(bar(5.x()).o2())}"#, | 529 | fn f() {macro1!(bar(5.x()).o2())} |
530 | "#]], | ||
536 | ) | 531 | ) |
537 | } | 532 | } |
538 | 533 | ||
@@ -544,12 +539,14 @@ fn preserves_whitespace_within_macro_expansion() { | |||
544 | macro_rules! macro1 { | 539 | macro_rules! macro1 { |
545 | ($a:expr) => {$a} | 540 | ($a:expr) => {$a} |
546 | } | 541 | } |
547 | fn f() {macro1!(1 * 2 + 3 + 4}"#, | 542 | fn f() {macro1!(1 * 2 + 3 + 4} |
548 | r#" | 543 | "#, |
544 | expect![[r#" | ||
549 | macro_rules! macro1 { | 545 | macro_rules! macro1 { |
550 | ($a:expr) => {$a} | 546 | ($a:expr) => {$a} |
551 | } | 547 | } |
552 | fn f() {macro1!(4 - 3 - 1 * 2}"#, | 548 | fn f() {macro1!(4 - 3 - 1 * 2} |
549 | "#]], | ||
553 | ) | 550 | ) |
554 | } | 551 | } |
555 | 552 | ||