Skip to content

Commit c5079a8

Browse files
SSR: Update tests so that all paths in patterns can be resolved
1 parent 026cefa commit c5079a8

File tree

1 file changed

+97
-67
lines changed

1 file changed

+97
-67
lines changed

crates/ra_ssr/src/tests.rs

+97-67
Original file line numberDiff line numberDiff line change
@@ -148,62 +148,62 @@ fn assert_match_failure_reason(pattern: &str, code: &str, snippet: &str, expecte
148148
fn ssr_function_to_method() {
149149
assert_ssr_transform(
150150
"my_function($a, $b) ==>> ($a).my_method($b)",
151-
"loop { my_function( other_func(x, y), z + w) }",
152-
"loop { (other_func(x, y)).my_method(z + w) }",
151+
"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) } }",
153153
)
154154
}
155155

156156
#[test]
157157
fn ssr_nested_function() {
158158
assert_ssr_transform(
159159
"foo($a, $b, $c) ==>> bar($c, baz($a, $b))",
160-
"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)) }",
160+
"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)) }",
162162
)
163163
}
164164

165165
#[test]
166166
fn ssr_expected_spacing() {
167167
assert_ssr_transform(
168168
"foo($x) + bar() ==>> bar($x)",
169-
"fn main() { foo(5) + bar() }",
170-
"fn main() { bar(5) }",
169+
"fn foo() {} fn bar() {} fn main() { foo(5) + bar() }",
170+
"fn foo() {} fn bar() {} fn main() { bar(5) }",
171171
);
172172
}
173173

174174
#[test]
175175
fn ssr_with_extra_space() {
176176
assert_ssr_transform(
177177
"foo($x ) + bar() ==>> bar($x)",
178-
"fn main() { foo( 5 ) +bar( ) }",
179-
"fn main() { bar(5) }",
178+
"fn foo() {} fn bar() {} fn main() { foo( 5 ) +bar( ) }",
179+
"fn foo() {} fn bar() {} fn main() { bar(5) }",
180180
);
181181
}
182182

183183
#[test]
184184
fn ssr_keeps_nested_comment() {
185185
assert_ssr_transform(
186186
"foo($x) ==>> bar($x)",
187-
"fn main() { foo(other(5 /* using 5 */)) }",
188-
"fn main() { bar(other(5 /* using 5 */)) }",
187+
"fn foo() {} fn main() { foo(other(5 /* using 5 */)) }",
188+
"fn foo() {} fn main() { bar(other(5 /* using 5 */)) }",
189189
)
190190
}
191191

192192
#[test]
193193
fn ssr_keeps_comment() {
194194
assert_ssr_transform(
195195
"foo($x) ==>> bar($x)",
196-
"fn main() { foo(5 /* using 5 */) }",
197-
"fn main() { bar(5)/* using 5 */ }",
196+
"fn foo() {} fn main() { foo(5 /* using 5 */) }",
197+
"fn foo() {} fn main() { bar(5)/* using 5 */ }",
198198
)
199199
}
200200

201201
#[test]
202202
fn ssr_struct_lit() {
203203
assert_ssr_transform(
204204
"foo{a: $a, b: $b} ==>> foo::new($a, $b)",
205-
"fn main() { foo{b:2, a:1} }",
206-
"fn main() { foo::new(1, 2) }",
205+
"fn foo() {} fn main() { foo{b:2, a:1} }",
206+
"fn foo() {} fn main() { foo::new(1, 2) }",
207207
)
208208
}
209209

@@ -225,16 +225,18 @@ fn match_fn_definition() {
225225

226226
#[test]
227227
fn match_struct_definition() {
228-
assert_matches(
229-
"struct $n {$f: Option<String>}",
230-
"struct Bar {} struct Foo {name: Option<String>}",
231-
&["struct Foo {name: Option<String>}"],
232-
);
228+
let code = r#"
229+
struct Option<T> {}
230+
struct Bar {}
231+
struct Foo {name: Option<String>}"#;
232+
assert_matches("struct $n {$f: Option<String>}", code, &["struct Foo {name: Option<String>}"]);
233233
}
234234

235235
#[test]
236236
fn 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)}"#;
238240
assert_matches("foo($a, $b)", code, &["foo(40 + 2, 42)"]);
239241
assert_no_match("foo($a, $b, $c)", code);
240242
assert_no_match("foo($a)", code);
@@ -263,7 +265,9 @@ fn match_nested_method_calls_with_macro_call() {
263265

264266
#[test]
265267
fn 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)}"#;
267271
assert_matches("foo($a, $b)", code, &["foo(bar(40, 2), 42)"]);
268272
assert_no_match("foo($a, $b, $c)", code);
269273
assert_no_match("foo($a)", code);
@@ -274,53 +278,62 @@ fn match_complex_expr() {
274278
#[test]
275279
fn match_with_trailing_commas() {
276280
// Code has comma, pattern doesn't.
277-
assert_matches("foo($a, $b)", "fn f() {foo(1, 2,);}", &["foo(1, 2,)"]);
278-
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,)"]);
282+
assert_matches("Foo{$a, $b}", "struct Foo {} fn f() {Foo{1, 2,};}", &["Foo{1, 2,}"]);
279283

280284
// Pattern has comma, code doesn't.
281-
assert_matches("foo($a, $b,)", "fn f() {foo(1, 2);}", &["foo(1, 2)"]);
282-
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)"]);
286+
assert_matches("Foo{$a, $b,}", "struct Foo {} fn f() {Foo{1, 2};}", &["Foo{1, 2}"]);
283287
}
284288

285289
#[test]
286290
fn match_type() {
287291
assert_matches("i32", "fn f() -> i32 {1 + 2}", &["i32"]);
288-
assert_matches("Option<$a>", "fn f() -> Option<i32> {42}", &["Option<i32>"]);
289-
assert_no_match("Option<$a>", "fn f() -> Result<i32, ()> {42}");
292+
assert_matches(
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+
);
290301
}
291302

292303
#[test]
293304
fn match_struct_instantiation() {
294-
assert_matches(
295-
"Foo {bar: 1, baz: 2}",
296-
"fn f() {Foo {bar: 1, baz: 2}}",
297-
&["Foo {bar: 1, baz: 2}"],
298-
);
305+
let code = r#"
306+
struct Foo {bar: i32, baz: i32}
307+
fn f() {Foo {bar: 1, baz: 2}}"#;
308+
assert_matches("Foo {bar: 1, baz: 2}", code, &["Foo {bar: 1, baz: 2}"]);
299309
// Now with placeholders for all parts of the struct.
300-
assert_matches(
301-
"Foo {$a: $b, $c: $d}",
302-
"fn f() {Foo {bar: 1, baz: 2}}",
303-
&["Foo {bar: 1, baz: 2}"],
304-
);
305-
assert_matches("Foo {}", "fn f() {Foo {}}", &["Foo {}"]);
310+
assert_matches("Foo {$a: $b, $c: $d}", code, &["Foo {bar: 1, baz: 2}"]);
311+
assert_matches("Foo {}", "struct Foo {} fn f() {Foo {}}", &["Foo {}"]);
306312
}
307313

308314
#[test]
309315
fn match_path() {
310-
assert_matches("foo::bar", "fn f() {foo::bar(42)}", &["foo::bar"]);
311-
assert_matches("$a::bar", "fn f() {foo::bar(42)}", &["foo::bar"]);
312-
assert_matches("foo::$b", "fn f() {foo::bar(42)}", &["foo::bar"]);
316+
let code = r#"
317+
mod foo {
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"]);
313324
}
314325

315326
#[test]
316327
fn 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)"]);
318329
}
319330

320331
#[test]
321332
fn literal_constraint() {
322333
mark::check!(literal_constraint);
323334
let code = r#"
335+
enum Option<T> { Some(T), None }
336+
use Option::Some;
324337
fn f1() {
325338
let x1 = Some(42);
326339
let x2 = Some("foo");
@@ -337,24 +350,36 @@ fn literal_constraint() {
337350
fn match_reordered_struct_instantiation() {
338351
assert_matches(
339352
"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}}",
341354
&["Foo {b: 2, ccc: 3, aa: 1}"],
342355
);
343-
assert_no_match("Foo {a: 1}", "fn f() {Foo {b: 1}}");
344-
assert_no_match("Foo {a: 1}", "fn f() {Foo {a: 2}}");
345-
assert_no_match("Foo {a: 1, b: 2}", "fn f() {Foo {a: 1}}");
346-
assert_no_match("Foo {a: 1, b: 2}", "fn f() {Foo {b: 2}}");
347-
assert_no_match("Foo {a: 1, }", "fn f() {Foo {a: 1, b: 2}}");
348-
assert_no_match("Foo {a: 1, z: 9}", "fn f() {Foo {a: 1}}");
356+
assert_no_match("Foo {a: 1}", "struct Foo {} fn f() {Foo {b: 1}}");
357+
assert_no_match("Foo {a: 1}", "struct Foo {} fn f() {Foo {a: 2}}");
358+
assert_no_match("Foo {a: 1, b: 2}", "struct Foo {} fn f() {Foo {a: 1}}");
359+
assert_no_match("Foo {a: 1, b: 2}", "struct Foo {} fn f() {Foo {b: 2}}");
360+
assert_no_match("Foo {a: 1, }", "struct Foo {} fn f() {Foo {a: 1, b: 2}}");
361+
assert_no_match("Foo {a: 1, z: 9}", "struct Foo {} fn f() {Foo {a: 1}}");
349362
}
350363

351364
#[test]
352365
fn match_macro_invocation() {
353-
assert_matches("foo!($a)", "fn() {foo(foo!(foo()))}", &["foo!(foo())"]);
354-
assert_matches("foo!(41, $a, 43)", "fn() {foo!(41, 42, 43)}", &["foo!(41, 42, 43)"]);
355-
assert_no_match("foo!(50, $a, 43)", "fn() {foo!(41, 42, 43}");
356-
assert_no_match("foo!(41, $a, 50)", "fn() {foo!(41, 42, 43}");
357-
assert_matches("foo!($a())", "fn() {foo!(bar())}", &["foo!(bar())"]);
366+
assert_matches(
367+
"foo!($a)",
368+
"macro_rules! foo {() => {}} fn() {foo(foo!(foo()))}",
369+
&["foo!(foo())"],
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+
);
358383
}
359384

360385
// When matching within a macro expansion, we only allow matches of nodes that originated from
@@ -389,56 +414,60 @@ fn no_match_split_expression() {
389414

390415
#[test]
391416
fn 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+
);
393422
}
394423

395424
#[test]
396425
fn replace_function_call_with_placeholders() {
397426
assert_ssr_transform(
398427
"foo($a, $b) ==>> bar($b, $a)",
399-
"fn f1() {foo(5, 42)}",
400-
"fn f1() {bar(42, 5)}",
428+
"fn foo() {} fn f1() {foo(5, 42)}",
429+
"fn foo() {} fn f1() {bar(42, 5)}",
401430
);
402431
}
403432

404433
#[test]
405434
fn replace_nested_function_calls() {
406435
assert_ssr_transform(
407436
"foo($a) ==>> bar($a)",
408-
"fn f1() {foo(foo(42))}",
409-
"fn f1() {bar(bar(42))}",
437+
"fn foo() {} fn f1() {foo(foo(42))}",
438+
"fn foo() {} fn f1() {bar(bar(42))}",
410439
);
411440
}
412441

413442
#[test]
414443
fn replace_type() {
415444
assert_ssr_transform(
416445
"Result<(), $a> ==>> Option<$a>",
417-
"fn f1() -> Result<(), Vec<Error>> {foo()}",
418-
"fn f1() -> Option<Vec<Error>> {foo()}",
446+
"struct Result<T, E> {} fn f1() -> Result<(), Vec<Error>> {foo()}",
447+
"struct Result<T, E> {} fn f1() -> Option<Vec<Error>> {foo()}",
419448
);
420449
}
421450

422451
#[test]
423452
fn replace_struct_init() {
424453
assert_ssr_transform(
425454
"Foo {a: $a, b: $b} ==>> Foo::new($a, $b)",
426-
"fn f1() {Foo{b: 1, a: 2}}",
427-
"fn f1() {Foo::new(2, 1)}",
455+
"struct Foo {} fn f1() {Foo{b: 1, a: 2}}",
456+
"struct Foo {} fn f1() {Foo::new(2, 1)}",
428457
);
429458
}
430459

431460
#[test]
432461
fn replace_macro_invocations() {
433462
assert_ssr_transform(
434463
"try!($a) ==>> $a?",
435-
"fn f1() -> Result<(), E> {bar(try!(foo()));}",
436-
"fn f1() -> Result<(), E> {bar(foo()?);}",
464+
"macro_rules! try {() => {}} fn f1() -> Result<(), E> {bar(try!(foo()));}",
465+
"macro_rules! try {() => {}} fn f1() -> Result<(), E> {bar(foo()?);}",
437466
);
438467
assert_ssr_transform(
439468
"foo!($a($b)) ==>> foo($b, $a)",
440-
"fn f1() {foo!(abc(def() + 2));}",
441-
"fn f1() {foo(def() + 2, abc);}",
469+
"macro_rules! foo {() => {}} fn f1() {foo!(abc(def() + 2));}",
470+
"macro_rules! foo {() => {}} fn f1() {foo(def() + 2, abc);}",
442471
);
443472
}
444473

@@ -527,6 +556,7 @@ fn preserves_whitespace_within_macro_expansion() {
527556
#[test]
528557
fn match_failure_reasons() {
529558
let code = r#"
559+
fn bar() {}
530560
macro_rules! foo {
531561
($a:expr) => {
532562
1 + $a + 2

0 commit comments

Comments
 (0)