aboutsummaryrefslogtreecommitdiff
path: root/crates/ssr/src/tests.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ssr/src/tests.rs')
-rw-r--r--crates/ssr/src/tests.rs113
1 files changed, 102 insertions, 11 deletions
diff --git a/crates/ssr/src/tests.rs b/crates/ssr/src/tests.rs
index 63131f6ca..d6918c22d 100644
--- a/crates/ssr/src/tests.rs
+++ b/crates/ssr/src/tests.rs
@@ -59,7 +59,7 @@ fn parser_undefined_placeholder_in_replacement() {
59 ); 59 );
60} 60}
61 61
62/// `code` may optionally contain a cursor marker `<|>`. If it doesn't, then the position will be 62/// `code` may optionally contain a cursor marker `$0`. If it doesn't, then the position will be
63/// the start of the file. If there's a second cursor marker, then we'll return a single range. 63/// the start of the file. If there's a second cursor marker, then we'll return a single range.
64pub(crate) fn single_file(code: &str) -> (ide_db::RootDatabase, FilePosition, Vec<FileRange>) { 64pub(crate) fn single_file(code: &str) -> (ide_db::RootDatabase, FilePosition, Vec<FileRange>) {
65 use ide_db::base_db::fixture::WithFixture; 65 use ide_db::base_db::fixture::WithFixture;
@@ -160,6 +160,97 @@ fn assert_match_failure_reason(pattern: &str, code: &str, snippet: &str, expecte
160} 160}
161 161
162#[test] 162#[test]
163fn ssr_let_stmt_in_macro_match() {
164 assert_matches(
165 "let a = 0",
166 r#"
167 macro_rules! m1 { ($a:stmt) => {$a}; }
168 fn f() {m1!{ let a = 0 };}"#,
169 // FIXME: Whitespace is not part of the matched block
170 &["leta=0"],
171 );
172}
173
174#[test]
175fn ssr_let_stmt_in_fn_match() {
176 assert_matches("let $a = 10;", "fn main() { let x = 10; x }", &["let x = 10;"]);
177 assert_matches("let $a = $b;", "fn main() { let x = 10; x }", &["let x = 10;"]);
178}
179
180#[test]
181fn ssr_block_expr_match() {
182 assert_matches("{ let $a = $b; }", "fn main() { let x = 10; }", &["{ let x = 10; }"]);
183 assert_matches("{ let $a = $b; $c }", "fn main() { let x = 10; x }", &["{ let x = 10; x }"]);
184}
185
186#[test]
187fn ssr_let_stmt_replace() {
188 // Pattern and template with trailing semicolon
189 assert_ssr_transform(
190 "let $a = $b; ==>> let $a = 11;",
191 "fn main() { let x = 10; x }",
192 expect![["fn main() { let x = 11; x }"]],
193 );
194}
195
196#[test]
197fn ssr_let_stmt_replace_expr() {
198 // Trailing semicolon should be dropped from the new expression
199 assert_ssr_transform(
200 "let $a = $b; ==>> $b",
201 "fn main() { let x = 10; }",
202 expect![["fn main() { 10 }"]],
203 );
204}
205
206#[test]
207fn ssr_blockexpr_replace_stmt_with_stmt() {
208 assert_ssr_transform(
209 "if $a() {$b;} ==>> $b;",
210 "{
211 if foo() {
212 bar();
213 }
214 Ok(())
215}",
216 expect![[r#"{
217 bar();
218 Ok(())
219}"#]],
220 );
221}
222
223#[test]
224fn ssr_blockexpr_match_trailing_expr() {
225 assert_matches(
226 "if $a() {$b;}",
227 "{
228 if foo() {
229 bar();
230 }
231}",
232 &["if foo() {
233 bar();
234 }"],
235 );
236}
237
238#[test]
239fn ssr_blockexpr_replace_trailing_expr_with_stmt() {
240 assert_ssr_transform(
241 "if $a() {$b;} ==>> $b;",
242 "{
243 if foo() {
244 bar();
245 }
246}",
247 expect![["{
248 bar();
249}"]],
250 );
251}
252
253#[test]
163fn ssr_function_to_method() { 254fn ssr_function_to_method() {
164 assert_ssr_transform( 255 assert_ssr_transform(
165 "my_function($a, $b) ==>> ($a).my_method($b)", 256 "my_function($a, $b) ==>> ($a).my_method($b)",
@@ -505,7 +596,7 @@ fn replace_function_call() {
505 // This test also makes sure that we ignore empty-ranges. 596 // This test also makes sure that we ignore empty-ranges.
506 assert_ssr_transform( 597 assert_ssr_transform(
507 "foo() ==>> bar()", 598 "foo() ==>> bar()",
508 "fn foo() {<|><|>} fn bar() {} fn f1() {foo(); foo();}", 599 "fn foo() {$0$0} fn bar() {} fn f1() {foo(); foo();}",
509 expect![["fn foo() {} fn bar() {} fn f1() {bar(); bar();}"]], 600 expect![["fn foo() {} fn bar() {} fn f1() {bar(); bar();}"]],
510 ); 601 );
511} 602}
@@ -615,7 +706,7 @@ fn replace_associated_trait_constant() {
615 706
616#[test] 707#[test]
617fn replace_path_in_different_contexts() { 708fn replace_path_in_different_contexts() {
618 // Note the <|> inside module a::b which marks the point where the rule is interpreted. We 709 // Note the $0 inside module a::b which marks the point where the rule is interpreted. We
619 // replace foo with bar, but both need different path qualifiers in different contexts. In f4, 710 // replace foo with bar, but both need different path qualifiers in different contexts. In f4,
620 // foo is unqualified because of a use statement, however the replacement needs to be fully 711 // foo is unqualified because of a use statement, however the replacement needs to be fully
621 // qualified. 712 // qualified.
@@ -623,7 +714,7 @@ fn replace_path_in_different_contexts() {
623 "c::foo() ==>> c::bar()", 714 "c::foo() ==>> c::bar()",
624 r#" 715 r#"
625 mod a { 716 mod a {
626 pub mod b {<|> 717 pub mod b {$0
627 pub mod c { 718 pub mod c {
628 pub fn foo() {} 719 pub fn foo() {}
629 pub fn bar() {} 720 pub fn bar() {}
@@ -1005,7 +1096,7 @@ fn pattern_is_a_single_segment_path() {
1005 fn f1() -> i32 { 1096 fn f1() -> i32 {
1006 let foo = 1; 1097 let foo = 1;
1007 let bar = 2; 1098 let bar = 2;
1008 foo<|> 1099 foo$0
1009 } 1100 }
1010 "#, 1101 "#,
1011 expect![[r#" 1102 expect![[r#"
@@ -1037,7 +1128,7 @@ fn replace_local_variable_reference() {
1037 let foo = 5; 1128 let foo = 5;
1038 res += foo + 1; 1129 res += foo + 1;
1039 let foo = 10; 1130 let foo = 10;
1040 res += foo + 2;<|> 1131 res += foo + 2;$0
1041 res += foo + 3; 1132 res += foo + 3;
1042 let foo = 15; 1133 let foo = 15;
1043 res += foo + 4; 1134 res += foo + 4;
@@ -1069,9 +1160,9 @@ fn replace_path_within_selection() {
1069 let foo = 41; 1160 let foo = 41;
1070 let bar = 42; 1161 let bar = 42;
1071 do_stuff(foo); 1162 do_stuff(foo);
1072 do_stuff(foo);<|> 1163 do_stuff(foo);$0
1073 do_stuff(foo); 1164 do_stuff(foo);
1074 do_stuff(foo);<|> 1165 do_stuff(foo);$0
1075 do_stuff(foo); 1166 do_stuff(foo);
1076 }"#, 1167 }"#,
1077 expect![[r#" 1168 expect![[r#"
@@ -1094,9 +1185,9 @@ fn replace_nonpath_within_selection() {
1094 "$a + $b ==>> $b * $a", 1185 "$a + $b ==>> $b * $a",
1095 r#" 1186 r#"
1096 fn main() { 1187 fn main() {
1097 let v = 1 + 2;<|> 1188 let v = 1 + 2;$0
1098 let v2 = 3 + 3; 1189 let v2 = 3 + 3;
1099 let v3 = 4 + 5;<|> 1190 let v3 = 4 + 5;$0
1100 let v4 = 6 + 7; 1191 let v4 = 6 + 7;
1101 }"#, 1192 }"#,
1102 expect![[r#" 1193 expect![[r#"
@@ -1121,7 +1212,7 @@ fn replace_self() {
1121 fn bar(_: &S1) {} 1212 fn bar(_: &S1) {}
1122 impl S1 { 1213 impl S1 {
1123 fn f1(&self) { 1214 fn f1(&self) {
1124 foo(self)<|> 1215 foo(self)$0
1125 } 1216 }
1126 fn f2(&self) { 1217 fn f2(&self) {
1127 foo(self) 1218 foo(self)