From 2d696b9c9eb206f69556d7f933c5a7baf5ae3c77 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Thu, 22 Jul 2021 19:38:49 +0200 Subject: [PATCH 1/2] Move out record completion tests --- .../ide_completion/src/completions/record.rs | 322 ------------------ crates/ide_completion/src/tests.rs | 1 + crates/ide_completion/src/tests/record.rs | 232 +++++++++++++ 3 files changed, 233 insertions(+), 322 deletions(-) create mode 100644 crates/ide_completion/src/tests/record.rs diff --git a/crates/ide_completion/src/completions/record.rs b/crates/ide_completion/src/completions/record.rs index e876337f12..c7b8ba3d14 100644 --- a/crates/ide_completion/src/completions/record.rs +++ b/crates/ide_completion/src/completions/record.rs @@ -44,325 +44,3 @@ pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) -> Some(()) } - -#[cfg(test)] -mod tests { - use expect_test::{expect, Expect}; - - use crate::{ - tests::{check_edit, filtered_completion_list}, - CompletionKind, - }; - - fn check(ra_fixture: &str, expect: Expect) { - let actual = filtered_completion_list(ra_fixture, CompletionKind::Reference); - expect.assert_eq(&actual); - } - - fn check_snippet(ra_fixture: &str, expect: Expect) { - let actual = filtered_completion_list(ra_fixture, CompletionKind::Snippet); - expect.assert_eq(&actual); - } - - #[test] - fn test_record_literal_field_default() { - let test_code = r#" -//- minicore: default -struct S { foo: u32, bar: usize } - -impl Default for S { - fn default() -> Self { - S { - foo: 0, - bar: 0, - } - } -} - -fn process(f: S) { - let other = S { - foo: 5, - .$0 - }; -} -"#; - check( - test_code, - expect![[r#" - fd bar usize - "#]], - ); - - check_snippet( - test_code, - expect![[r#" - fd ..Default::default() - "#]], - ); - } - - #[test] - fn test_record_literal_field_default_completion() { - check_edit( - "..Default::default()", - r#" -//- minicore: default -struct S { foo: u32, bar: usize } - -impl Default for S { - fn default() -> Self { - S { - foo: 0, - bar: 0, - } - } -} - -fn process(f: S) { - let other = S { - foo: 5, - .$0 - }; -} -"#, - r#" -struct S { foo: u32, bar: usize } - -impl Default for S { - fn default() -> Self { - S { - foo: 0, - bar: 0, - } - } -} - -fn process(f: S) { - let other = S { - foo: 5, - ..Default::default() - }; -} -"#, - ); - } - - #[test] - fn test_record_literal_field_without_default() { - let test_code = r#" -struct S { foo: u32, bar: usize } - -fn process(f: S) { - let other = S { - foo: 5, - .$0 - }; -} -"#; - check( - test_code, - expect![[r#" - fd bar usize - "#]], - ); - - check_snippet(test_code, expect![[r#""#]]); - } - - #[test] - fn test_record_pattern_field() { - check( - r#" -struct S { foo: u32 } - -fn process(f: S) { - match f { - S { f$0: 92 } => (), - } -} -"#, - expect![[r#" - fd foo u32 - "#]], - ); - } - - #[test] - fn test_record_pattern_enum_variant() { - check( - r#" -enum E { S { foo: u32, bar: () } } - -fn process(e: E) { - match e { - E::S { $0 } => (), - } -} -"#, - expect![[r#" - fd foo u32 - fd bar () - "#]], - ); - } - - #[test] - fn test_record_pattern_field_in_simple_macro() { - check( - r" -macro_rules! m { ($e:expr) => { $e } } -struct S { foo: u32 } - -fn process(f: S) { - m!(match f { - S { f$0: 92 } => (), - }) -} -", - expect![[r#" - fd foo u32 - "#]], - ); - } - - #[test] - fn only_missing_fields_are_completed_in_destruct_pats() { - check( - r#" -struct S { - foo1: u32, foo2: u32, - bar: u32, baz: u32, -} - -fn main() { - let s = S { - foo1: 1, foo2: 2, - bar: 3, baz: 4, - }; - if let S { foo1, foo2: a, $0 } = s {} -} -"#, - expect![[r#" - fd bar u32 - fd baz u32 - "#]], - ); - } - - #[test] - fn test_record_literal_field() { - check( - r#" -struct A { the_field: u32 } -fn foo() { - A { the$0 } -} -"#, - expect![[r#" - fd the_field u32 - "#]], - ); - } - - #[test] - fn test_record_literal_enum_variant() { - check( - r#" -enum E { A { a: u32 } } -fn foo() { - let _ = E::A { $0 } -} -"#, - expect![[r#" - fd a u32 - "#]], - ); - } - - #[test] - fn test_record_literal_two_structs() { - check( - r#" -struct A { a: u32 } -struct B { b: u32 } - -fn foo() { - let _: A = B { $0 } -} -"#, - expect![[r#" - fd b u32 - "#]], - ); - } - - #[test] - fn test_record_literal_generic_struct() { - check( - r#" -struct A { a: T } - -fn foo() { - let _: A = A { $0 } -} -"#, - expect![[r#" - fd a u32 - "#]], - ); - } - - #[test] - fn test_record_literal_field_in_simple_macro() { - check( - r#" -macro_rules! m { ($e:expr) => { $e } } -struct A { the_field: u32 } -fn foo() { - m!(A { the$0 }) -} -"#, - expect![[r#" - fd the_field u32 - "#]], - ); - } - - #[test] - fn only_missing_fields_are_completed() { - check( - r#" -struct S { - foo1: u32, foo2: u32, - bar: u32, baz: u32, -} - -fn main() { - let foo1 = 1; - let s = S { foo1, foo2: 5, $0 } -} -"#, - expect![[r#" - fd bar u32 - fd baz u32 - "#]], - ); - } - - #[test] - fn completes_functional_update() { - check( - r#" -struct S { foo1: u32, foo2: u32 } - -fn main() { - let foo1 = 1; - let s = S { foo1, $0 .. loop {} } -} -"#, - expect![[r#" - fd foo2 u32 - "#]], - ); - } -} diff --git a/crates/ide_completion/src/tests.rs b/crates/ide_completion/src/tests.rs index 0b0c828617..1eec2c113e 100644 --- a/crates/ide_completion/src/tests.rs +++ b/crates/ide_completion/src/tests.rs @@ -10,6 +10,7 @@ mod item_list; mod item; mod pattern; mod predicate; +mod record; mod sourcegen; mod type_pos; mod use_tree; diff --git a/crates/ide_completion/src/tests/record.rs b/crates/ide_completion/src/tests/record.rs new file mode 100644 index 0000000000..fa5f3fe46e --- /dev/null +++ b/crates/ide_completion/src/tests/record.rs @@ -0,0 +1,232 @@ +use expect_test::{expect, Expect}; + +use crate::tests::{check_edit, completion_list}; + +fn check(ra_fixture: &str, expect: Expect) { + let actual = completion_list(ra_fixture); + expect.assert_eq(&actual); +} + +#[test] +fn with_default_impl() { + check( + r#" +//- minicore: default +struct Struct { foo: u32, bar: usize } + +impl Default for Struct { + fn default() -> Self { + Struct { + foo: 0, + bar: 0, + } + } +} + +fn foo() { + let other = Struct { + foo: 5, + $0 + }; +} +"#, + expect![[r#" + fd ..Default::default() + fd bar usize + "#]], + ); +} + +#[test] +fn without_default_impl() { + check( + r#" +struct Struct { foo: u32, bar: usize } + +fn foo() { + let other = Struct { + foo: 5, + $0 + }; +} +"#, + expect![[r#" + fd bar usize + "#]], + ); +} + +#[test] +fn record_pattern_field() { + check( + r#" +struct Struct { foo: u32, bar: u32 } + +fn foo(s: Struct) { + match s { + Struct { foo, $0: 92 } => (), + } +} +"#, + expect![[r#" + fd bar u32 + "#]], + ); +} + +#[test] +fn pattern_enum_variant() { + check( + r#" +enum Enum { Variant { foo: u32, bar: u32 } } +fn foo(e: Enum) { + match e { + Enum::Variant { foo, $0 } => (), + } +} +"#, + expect![[r#" + fd bar u32 + "#]], + ); +} + +#[test] +fn record_literal_field_in_macro() { + check( + r#" +macro_rules! m { ($e:expr) => { $e } } +struct Struct { field: u32 } +fn foo() { + m!(Struct { fie$0 }) +} +"#, + expect![[r#" + fd field u32 + "#]], + ); +} + +#[test] +fn record_pattern_field_in_macro() { + check( + r" +macro_rules! m { ($e:expr) => { $e } } +struct Struct { field: u32 } + +fn foo(f: Struct) { + m!(match f { + Struct { f$0: 92 } => (), + }) +} +", + expect![[r#" + fd field u32 + "#]], + ); +} + +#[test] +fn functional_update() { + // FIXME: This should filter out all completions that do not have the type `Foo` + check( + r#" +struct Foo { foo1: u32, foo2: u32 } + +fn main() { + let thing = 1; + let foo = Foo { foo1: 0, foo2: 0 }; + let foo2 = Foo { thing, ..$0 } +} +"#, + expect![[r#" + kw unsafe + kw match + kw while + kw while let + kw loop + kw if + kw if let + kw for + kw true + kw false + kw return + kw self + kw super + kw crate + lc foo Foo + lc thing i32 + st Foo + fn main() fn() + bt u32 + "#]], + ); +} + +#[test] +fn default_completion_edit() { + check_edit( + "..Default::default()", + r#" +//- minicore: default +struct Struct { foo: u32, bar: usize } + +impl Default for Struct { + fn default() -> Self {} +} + +fn foo() { + let other = Struct { + foo: 5, + .$0 + }; +} +"#, + r#" +struct Struct { foo: u32, bar: usize } + +impl Default for Struct { + fn default() -> Self {} +} + +fn foo() { + let other = Struct { + foo: 5, + ..Default::default() + }; +} +"#, + ); + check_edit( + "..Default::default()", + r#" +//- minicore: default +struct Struct { foo: u32, bar: usize } + +impl Default for Struct { + fn default() -> Self {} +} + +fn foo() { + let other = Struct { + foo: 5, + $0 + }; +} +"#, + r#" +struct Struct { foo: u32, bar: usize } + +impl Default for Struct { + fn default() -> Self {} +} + +fn foo() { + let other = Struct { + foo: 5, + ..Default::default() + }; +} +"#, + ); +} From d5947d9d487d15eea928ee687286651f6a98a4bf Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Thu, 22 Jul 2021 19:59:01 +0200 Subject: [PATCH 2/2] Clarify what the outline test module is for --- .../ide_completion/src/completions/record.rs | 74 +++++++++++++++++++ .../ide_completion/src/completions/snippet.rs | 6 -- .../src/completions/trait_impl.rs | 44 ----------- crates/ide_completion/src/tests.rs | 3 + crates/ide_completion/src/tests/item_list.rs | 37 ++++++++++ crates/ide_completion/src/tests/record.rs | 70 +----------------- 6 files changed, 115 insertions(+), 119 deletions(-) diff --git a/crates/ide_completion/src/completions/record.rs b/crates/ide_completion/src/completions/record.rs index c7b8ba3d14..c7cf5e3e4b 100644 --- a/crates/ide_completion/src/completions/record.rs +++ b/crates/ide_completion/src/completions/record.rs @@ -44,3 +44,77 @@ pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) -> Some(()) } + +#[cfg(test)] +mod tests { + + use crate::tests::check_edit; + + #[test] + fn default_completion_edit() { + check_edit( + "..Default::default()", + r#" +//- minicore: default +struct Struct { foo: u32, bar: usize } + +impl Default for Struct { + fn default() -> Self {} +} + +fn foo() { + let other = Struct { + foo: 5, + .$0 + }; +} +"#, + r#" +struct Struct { foo: u32, bar: usize } + +impl Default for Struct { + fn default() -> Self {} +} + +fn foo() { + let other = Struct { + foo: 5, + ..Default::default() + }; +} +"#, + ); + check_edit( + "..Default::default()", + r#" +//- minicore: default +struct Struct { foo: u32, bar: usize } + +impl Default for Struct { + fn default() -> Self {} +} + +fn foo() { + let other = Struct { + foo: 5, + $0 + }; +} +"#, + r#" +struct Struct { foo: u32, bar: usize } + +impl Default for Struct { + fn default() -> Self {} +} + +fn foo() { + let other = Struct { + foo: 5, + ..Default::default() + }; +} +"#, + ); + } +} diff --git a/crates/ide_completion/src/completions/snippet.rs b/crates/ide_completion/src/completions/snippet.rs index cbc20cc2c3..6237a6ef1e 100644 --- a/crates/ide_completion/src/completions/snippet.rs +++ b/crates/ide_completion/src/completions/snippet.rs @@ -108,10 +108,4 @@ mod tests { "#]], ); } - - #[test] - fn should_not_complete_snippets_in_path() { - check(r#"fn foo(x: i32) { ::foo$0 }"#, expect![[""]]); - check(r#"fn foo(x: i32) { ::$0 }"#, expect![[""]]); - } } diff --git a/crates/ide_completion/src/completions/trait_impl.rs b/crates/ide_completion/src/completions/trait_impl.rs index 65f0f38430..3713be0a17 100644 --- a/crates/ide_completion/src/completions/trait_impl.rs +++ b/crates/ide_completion/src/completions/trait_impl.rs @@ -299,29 +299,6 @@ mod tests { expect.assert_eq(&actual) } - #[test] - fn name_ref_function_type_const() { - check( - r#" -trait Test { - type TestType; - const TEST_CONST: u16; - fn test(); -} -struct T; - -impl Test for T { - t$0 -} -"#, - expect![[" -ta type TestType = \n\ -ct const TEST_CONST: u16 = \n\ -fn fn test() -"]], - ); - } - #[test] fn no_completion_inside_fn() { check( @@ -572,27 +549,6 @@ impl Test for T { ); } - #[test] - fn hide_implemented_fn() { - check( - r#" -trait Test { - fn foo(); - fn foo_bar(); -} -struct T; - -impl Test for T { - fn foo() {} - fn f$0 -} -"#, - expect![[r#" - fn fn foo_bar() - "#]], - ); - } - #[test] fn generic_fn() { check_edit( diff --git a/crates/ide_completion/src/tests.rs b/crates/ide_completion/src/tests.rs index 1eec2c113e..cd812b617e 100644 --- a/crates/ide_completion/src/tests.rs +++ b/crates/ide_completion/src/tests.rs @@ -3,6 +3,9 @@ //! Most tests live in this module or its submodules unless for very specific completions like //! `attributes` or `lifetimes` where the completed concept is a distinct thing. //! Notable examples for completions that are being tested in this module's submodule are paths. +//! Another exception are `check_edit` tests which usually live in the completion modules themselves, +//! as the main purpose of this test module here is to give the developer an overview of whats being +//! completed where, not how. mod attribute; mod fn_param; diff --git a/crates/ide_completion/src/tests/item_list.rs b/crates/ide_completion/src/tests/item_list.rs index a170616f55..1c1915ffbd 100644 --- a/crates/ide_completion/src/tests/item_list.rs +++ b/crates/ide_completion/src/tests/item_list.rs @@ -210,3 +210,40 @@ fn in_trait_assoc_item_list() { "##]], ); } + +#[test] +fn in_trait_impl_assoc_item_list() { + check( + r#" +trait Test { + type Type0; + type Type1; + const CONST0: (); + const CONST1: (); + fn function0(); + fn function1(); +} + +impl Test for () { + type Type0 = (); + const CONST0: () = (); + fn function0() {} + $0 +} +"#, + expect![[r##" + kw pub(crate) + kw pub + kw unsafe + kw fn + kw const + kw type + kw self + kw super + kw crate + md module + ma makro!(…) #[macro_export] macro_rules! makro + ma makro!(…) #[macro_export] macro_rules! makro + "##]], + ); +} diff --git a/crates/ide_completion/src/tests/record.rs b/crates/ide_completion/src/tests/record.rs index fa5f3fe46e..db3e315307 100644 --- a/crates/ide_completion/src/tests/record.rs +++ b/crates/ide_completion/src/tests/record.rs @@ -1,6 +1,6 @@ use expect_test::{expect, Expect}; -use crate::tests::{check_edit, completion_list}; +use crate::tests::completion_list; fn check(ra_fixture: &str, expect: Expect) { let actual = completion_list(ra_fixture); @@ -162,71 +162,3 @@ fn main() { "#]], ); } - -#[test] -fn default_completion_edit() { - check_edit( - "..Default::default()", - r#" -//- minicore: default -struct Struct { foo: u32, bar: usize } - -impl Default for Struct { - fn default() -> Self {} -} - -fn foo() { - let other = Struct { - foo: 5, - .$0 - }; -} -"#, - r#" -struct Struct { foo: u32, bar: usize } - -impl Default for Struct { - fn default() -> Self {} -} - -fn foo() { - let other = Struct { - foo: 5, - ..Default::default() - }; -} -"#, - ); - check_edit( - "..Default::default()", - r#" -//- minicore: default -struct Struct { foo: u32, bar: usize } - -impl Default for Struct { - fn default() -> Self {} -} - -fn foo() { - let other = Struct { - foo: 5, - $0 - }; -} -"#, - r#" -struct Struct { foo: u32, bar: usize } - -impl Default for Struct { - fn default() -> Self {} -} - -fn foo() { - let other = Struct { - foo: 5, - ..Default::default() - }; -} -"#, - ); -}