Skip to content

Commit 547a12e

Browse files
authored
Merge pull request rust-lang#18887 from vishruth-thimmaiah/refactor_completions_tests
refactor test helpers within ide-completions
2 parents fbc1d51 + 8d1ae29 commit 547a12e

22 files changed

+322
-448
lines changed

src/tools/rust-analyzer/crates/ide-completion/src/completions/dot.rs

Lines changed: 48 additions & 60 deletions
Large diffs are not rendered by default.

src/tools/rust-analyzer/crates/ide-completion/src/completions/extern_abi.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,13 @@ pub(crate) fn complete_extern_abi(
6565

6666
#[cfg(test)]
6767
mod tests {
68-
use expect_test::{expect, Expect};
68+
use expect_test::expect;
6969

70-
use crate::tests::{check_edit, completion_list_no_kw};
71-
72-
fn check(ra_fixture: &str, expect: Expect) {
73-
let actual = completion_list_no_kw(ra_fixture);
74-
expect.assert_eq(&actual);
75-
}
70+
use crate::tests::{check_edit, check_no_kw};
7671

7772
#[test]
7873
fn only_completes_in_string_literals() {
79-
check(
74+
check_no_kw(
8075
r#"
8176
$0 fn foo {}
8277
"#,
@@ -86,7 +81,7 @@ $0 fn foo {}
8681

8782
#[test]
8883
fn requires_extern_prefix() {
89-
check(
84+
check_no_kw(
9085
r#"
9186
"$0" fn foo {}
9287
"#,
@@ -96,7 +91,7 @@ $0 fn foo {}
9691

9792
#[test]
9893
fn works() {
99-
check(
94+
check_no_kw(
10095
r#"
10196
extern "$0" fn foo {}
10297
"#,

src/tools/rust-analyzer/crates/ide-completion/src/completions/format_string.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,13 @@ pub(crate) fn format_string(
6161

6262
#[cfg(test)]
6363
mod tests {
64-
use expect_test::{expect, Expect};
64+
use expect_test::expect;
6565

66-
use crate::tests::{check_edit, completion_list_no_kw};
67-
68-
fn check(ra_fixture: &str, expect: Expect) {
69-
let actual = completion_list_no_kw(ra_fixture);
70-
expect.assert_eq(&actual);
71-
}
66+
use crate::tests::{check_edit, check_no_kw};
7267

7368
#[test]
7469
fn works_when_wrapped() {
75-
check(
70+
check_no_kw(
7671
r#"
7772
//- minicore: fmt
7873
macro_rules! print {
@@ -89,7 +84,7 @@ fn main() {
8984

9085
#[test]
9186
fn no_completion_without_brace() {
92-
check(
87+
check_no_kw(
9388
r#"
9489
//- minicore: fmt
9590
fn main() {

src/tools/rust-analyzer/crates/ide-completion/src/completions/item_list/trait_impl.rs

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -514,18 +514,13 @@ fn function_declaration(
514514

515515
#[cfg(test)]
516516
mod tests {
517-
use expect_test::{expect, Expect};
517+
use expect_test::expect;
518518

519-
use crate::tests::{check_edit, completion_list_no_kw};
520-
521-
fn check(ra_fixture: &str, expect: Expect) {
522-
let actual = completion_list_no_kw(ra_fixture);
523-
expect.assert_eq(&actual)
524-
}
519+
use crate::tests::{check_edit, check_no_kw};
525520

526521
#[test]
527522
fn no_completion_inside_fn() {
528-
check(
523+
check_no_kw(
529524
r"
530525
trait Test { fn test(); fn test2(); }
531526
struct T;
@@ -544,7 +539,7 @@ impl Test for T {
544539
"#]],
545540
);
546541

547-
check(
542+
check_no_kw(
548543
r"
549544
trait Test { fn test(); fn test2(); }
550545
struct T;
@@ -558,7 +553,7 @@ impl Test for T {
558553
expect![[""]],
559554
);
560555

561-
check(
556+
check_no_kw(
562557
r"
563558
trait Test { fn test(); fn test2(); }
564559
struct T;
@@ -573,7 +568,7 @@ impl Test for T {
573568
);
574569

575570
// https://github.com/rust-lang/rust-analyzer/pull/5976#issuecomment-692332191
576-
check(
571+
check_no_kw(
577572
r"
578573
trait Test { fn test(); fn test2(); }
579574
struct T;
@@ -587,7 +582,7 @@ impl Test for T {
587582
expect![[r#""#]],
588583
);
589584

590-
check(
585+
check_no_kw(
591586
r"
592587
trait Test { fn test(_: i32); fn test2(); }
593588
struct T;
@@ -606,7 +601,7 @@ impl Test for T {
606601
"#]],
607602
);
608603

609-
check(
604+
check_no_kw(
610605
r"
611606
trait Test { fn test(_: fn()); fn test2(); }
612607
struct T;
@@ -624,7 +619,7 @@ impl Test for T {
624619

625620
#[test]
626621
fn no_completion_inside_const() {
627-
check(
622+
check_no_kw(
628623
r"
629624
trait Test { const TEST: fn(); const TEST2: u32; type Test; fn test(); }
630625
struct T;
@@ -636,7 +631,7 @@ impl Test for T {
636631
expect![[r#""#]],
637632
);
638633

639-
check(
634+
check_no_kw(
640635
r"
641636
trait Test { const TEST: u32; const TEST2: u32; type Test; fn test(); }
642637
struct T;
@@ -653,7 +648,7 @@ impl Test for T {
653648
"#]],
654649
);
655650

656-
check(
651+
check_no_kw(
657652
r"
658653
trait Test { const TEST: u32; const TEST2: u32; type Test; fn test(); }
659654
struct T;
@@ -670,7 +665,7 @@ impl Test for T {
670665
"#]],
671666
);
672667

673-
check(
668+
check_no_kw(
674669
r"
675670
trait Test { const TEST: u32; const TEST2: u32; type Test; fn test(); }
676671
struct T;
@@ -689,7 +684,7 @@ impl Test for T {
689684
"#]],
690685
);
691686

692-
check(
687+
check_no_kw(
693688
r"
694689
trait Test { const TEST: u32; const TEST2: u32; type Test; fn test(); }
695690
struct T;
@@ -703,7 +698,7 @@ impl Test for T {
703698
expect![[""]],
704699
);
705700

706-
check(
701+
check_no_kw(
707702
r"
708703
trait Test { const TEST: u32; const TEST2: u32; type Test; fn test(); }
709704
struct T;
@@ -720,7 +715,7 @@ impl Test for T {
720715

721716
#[test]
722717
fn no_completion_inside_type() {
723-
check(
718+
check_no_kw(
724719
r"
725720
trait Test { type Test; type Test2; fn test(); }
726721
struct T;
@@ -737,7 +732,7 @@ impl Test for T {
737732
"#]],
738733
);
739734

740-
check(
735+
check_no_kw(
741736
r"
742737
trait Test { type Test; type Test2; fn test(); }
743738
struct T;
@@ -1263,7 +1258,7 @@ impl Foo<u32> for Bar {
12631258

12641259
#[test]
12651260
fn works_directly_in_impl() {
1266-
check(
1261+
check_no_kw(
12671262
r#"
12681263
trait Tr {
12691264
fn required();
@@ -1277,7 +1272,7 @@ impl Tr for () {
12771272
fn fn required()
12781273
"#]],
12791274
);
1280-
check(
1275+
check_no_kw(
12811276
r#"
12821277
trait Tr {
12831278
fn provided() {}

src/tools/rust-analyzer/crates/ide-completion/src/completions/keyword.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,9 @@ pub(crate) fn complete_for_and_where(
3232

3333
#[cfg(test)]
3434
mod tests {
35-
use expect_test::{expect, Expect};
35+
use expect_test::expect;
3636

37-
use crate::tests::{check_edit, completion_list};
38-
39-
fn check(ra_fixture: &str, expect: Expect) {
40-
let actual = completion_list(ra_fixture);
41-
expect.assert_eq(&actual)
42-
}
37+
use crate::tests::{check, check_edit};
4338

4439
#[test]
4540
fn test_else_edit_after_if() {

src/tools/rust-analyzer/crates/ide-completion/src/completions/lifetime.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,9 @@ pub(crate) fn complete_label(
5959

6060
#[cfg(test)]
6161
mod tests {
62-
use expect_test::{expect, Expect};
62+
use expect_test::expect;
6363

64-
use crate::tests::{check_edit, completion_list};
65-
66-
fn check(ra_fixture: &str, expect: Expect) {
67-
let actual = completion_list(ra_fixture);
68-
expect.assert_eq(&actual);
69-
}
64+
use crate::tests::{check, check_edit};
7065

7166
#[test]
7267
fn check_lifetime_edit() {

src/tools/rust-analyzer/crates/ide-completion/src/completions/mod_.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,9 @@ fn module_chain_to_containing_module_file(
159159

160160
#[cfg(test)]
161161
mod tests {
162-
use expect_test::{expect, Expect};
162+
use expect_test::expect;
163163

164-
use crate::tests::completion_list;
165-
166-
fn check(ra_fixture: &str, expect: Expect) {
167-
let actual = completion_list(ra_fixture);
168-
expect.assert_eq(&actual);
169-
}
164+
use crate::tests::check;
170165

171166
#[test]
172167
fn lib_module_completion() {

src/tools/rust-analyzer/crates/ide-completion/src/completions/postfix.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -401,18 +401,13 @@ fn add_custom_postfix_completions(
401401

402402
#[cfg(test)]
403403
mod tests {
404-
use expect_test::{expect, Expect};
404+
use expect_test::expect;
405405

406406
use crate::{
407-
tests::{check_edit, check_edit_with_config, completion_list, TEST_CONFIG},
407+
tests::{check, check_edit, check_edit_with_config, TEST_CONFIG},
408408
CompletionConfig, Snippet,
409409
};
410410

411-
fn check(ra_fixture: &str, expect: Expect) {
412-
let actual = completion_list(ra_fixture);
413-
expect.assert_eq(&actual)
414-
}
415-
416411
#[test]
417412
fn postfix_completion_works_for_trivial_path_expression() {
418413
check(

src/tools/rust-analyzer/crates/ide-completion/src/tests.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,34 @@ pub(crate) fn check_edit_with_config(
253253
assert_eq_text!(&ra_fixture_after, &actual)
254254
}
255255

256-
fn check_empty(ra_fixture: &str, expect: Expect) {
256+
pub(crate) fn check(ra_fixture: &str, expect: Expect) {
257257
let actual = completion_list(ra_fixture);
258258
expect.assert_eq(&actual);
259259
}
260260

261+
pub(crate) fn check_with_base_items(ra_fixture: &str, expect: Expect) {
262+
check(&format!("{BASE_ITEMS_FIXTURE}{ra_fixture}"), expect)
263+
}
264+
265+
pub(crate) fn check_no_kw(ra_fixture: &str, expect: Expect) {
266+
let actual = completion_list_no_kw(ra_fixture);
267+
expect.assert_eq(&actual)
268+
}
269+
270+
pub(crate) fn check_with_private_editable(ra_fixture: &str, expect: Expect) {
271+
let actual = completion_list_no_kw_with_private_editable(ra_fixture);
272+
expect.assert_eq(&actual);
273+
}
274+
275+
pub(crate) fn check_with_trigger_character(
276+
ra_fixture: &str,
277+
trigger_character: Option<char>,
278+
expect: Expect,
279+
) {
280+
let actual = completion_list_with_trigger_character(ra_fixture, trigger_character);
281+
expect.assert_eq(&actual)
282+
}
283+
261284
pub(crate) fn get_all_items(
262285
config: CompletionConfig<'_>,
263286
code: &str,

0 commit comments

Comments
 (0)