From ea72a5136cc9d2bf3b83425569723e9e37bab403 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 23 Jul 2021 16:09:30 +0200 Subject: [PATCH 1/3] Add functions to base items completion test fixture --- .../src/completions/unqualified_path.rs | 55 ---------------- crates/ide_completion/src/tests.rs | 3 +- crates/ide_completion/src/tests/item.rs | 8 +-- crates/ide_completion/src/tests/item_list.rs | 10 +-- crates/ide_completion/src/tests/pattern.rs | 4 +- crates/ide_completion/src/tests/predicate.rs | 6 +- crates/ide_completion/src/tests/type_pos.rs | 66 +++++++++---------- 7 files changed, 46 insertions(+), 106 deletions(-) diff --git a/crates/ide_completion/src/completions/unqualified_path.rs b/crates/ide_completion/src/completions/unqualified_path.rs index eaf117d678..62ec0df799 100644 --- a/crates/ide_completion/src/completions/unqualified_path.rs +++ b/crates/ide_completion/src/completions/unqualified_path.rs @@ -135,61 +135,6 @@ mod tests { expect.assert_eq(&actual) } - #[test] - fn completes_bindings_from_let() { - check( - r#" -fn quux(x: i32) { - let y = 92; - 1 + $0; - let z = (); -} -"#, - expect![[r#" - lc y i32 - lc x i32 - fn quux(…) fn(i32) - "#]], - ); - } - - #[test] - fn completes_bindings_from_if_let() { - check( - r#" -fn quux() { - if let Some(x) = foo() { - let y = 92; - }; - if let Some(a) = bar() { - let b = 62; - 1 + $0 - } -} -"#, - expect![[r#" - lc b i32 - lc a - fn quux() fn() - "#]], - ); - } - - #[test] - fn completes_bindings_from_for() { - check( - r#" -fn quux() { - for x in &[1, 2, 3] { $0 } -} -"#, - expect![[r#" - lc x - fn quux() fn() - "#]], - ); - } - #[test] fn completes_if_prefix_is_keyword() { cov_mark::check!(completes_if_prefix_is_keyword); diff --git a/crates/ide_completion/src/tests.rs b/crates/ide_completion/src/tests.rs index cd812b617e..b7ba0a893a 100644 --- a/crates/ide_completion/src/tests.rs +++ b/crates/ide_completion/src/tests.rs @@ -38,7 +38,7 @@ use test_utils::assert_eq_text; use crate::{item::CompletionKind, CompletionConfig, CompletionItem}; /// Lots of basic item definitions -const BASE_FIXTURE: &str = r#" +const BASE_ITEMS_FIXTURE: &str = r#" enum Enum { TupleV(u32), RecordV { field: u32 }, UnitV } use self::Enum::TupleV; mod module {} @@ -53,6 +53,7 @@ struct Unit; macro_rules! makro {} #[rustc_builtin_macro] pub macro Clone {} +fn function() {} "#; pub(crate) const TEST_CONFIG: CompletionConfig = CompletionConfig { diff --git a/crates/ide_completion/src/tests/item.rs b/crates/ide_completion/src/tests/item.rs index f4e1301832..45d653cfd2 100644 --- a/crates/ide_completion/src/tests/item.rs +++ b/crates/ide_completion/src/tests/item.rs @@ -4,10 +4,10 @@ //! in [crate::completions::mod_]. use expect_test::{expect, Expect}; -use crate::tests::{completion_list, BASE_FIXTURE}; +use crate::tests::{completion_list, BASE_ITEMS_FIXTURE}; fn check(ra_fixture: &str, expect: Expect) { - let actual = completion_list(&format!("{}{}", BASE_FIXTURE, ra_fixture)); + let actual = completion_list(&format!("{}{}", BASE_ITEMS_FIXTURE, ra_fixture)); expect.assert_eq(&actual) } @@ -25,10 +25,10 @@ impl Tra$0 en Enum st Record st Tuple - ma makro!(…) #[macro_export] macro_rules! makro md module st Unit ma makro!(…) #[macro_export] macro_rules! makro + ma makro!(…) #[macro_export] macro_rules! makro bt u32 "##]], ) @@ -48,10 +48,10 @@ impl Trait for Str$0 en Enum st Record st Tuple - ma makro!(…) #[macro_export] macro_rules! makro md module st Unit ma makro!(…) #[macro_export] macro_rules! makro + ma makro!(…) #[macro_export] macro_rules! makro bt u32 "##]], ) diff --git a/crates/ide_completion/src/tests/item_list.rs b/crates/ide_completion/src/tests/item_list.rs index 1c1915ffbd..bd8df49e41 100644 --- a/crates/ide_completion/src/tests/item_list.rs +++ b/crates/ide_completion/src/tests/item_list.rs @@ -1,10 +1,10 @@ //! Completion tests for item list position. use expect_test::{expect, Expect}; -use crate::tests::{completion_list, BASE_FIXTURE}; +use crate::tests::{completion_list, BASE_ITEMS_FIXTURE}; fn check(ra_fixture: &str, expect: Expect) { - let actual = completion_list(&format!("{}{}", BASE_FIXTURE, ra_fixture)); + let actual = completion_list(&format!("{}{}", BASE_ITEMS_FIXTURE, ra_fixture)); expect.assert_eq(&actual) } @@ -65,9 +65,9 @@ fn in_source_file_item_list() { kw self kw super kw crate - ma makro!(…) #[macro_export] macro_rules! makro md module ma makro!(…) #[macro_export] macro_rules! makro + ma makro!(…) #[macro_export] macro_rules! makro "##]], ) } @@ -105,8 +105,8 @@ fn in_qualified_path() { check( r#"crate::$0"#, expect![[r##" - ma makro!(…) #[macro_export] macro_rules! makro md module + ma makro!(…) #[macro_export] macro_rules! makro "##]], ) } @@ -170,9 +170,9 @@ fn in_impl_assoc_item_list() { kw self kw super kw crate - ma makro!(…) #[macro_export] macro_rules! makro md module ma makro!(…) #[macro_export] macro_rules! makro + ma makro!(…) #[macro_export] macro_rules! makro "##]], ) } diff --git a/crates/ide_completion/src/tests/pattern.rs b/crates/ide_completion/src/tests/pattern.rs index b5c2fef85e..7b868fd854 100644 --- a/crates/ide_completion/src/tests/pattern.rs +++ b/crates/ide_completion/src/tests/pattern.rs @@ -1,7 +1,7 @@ //! Completion tests for pattern position. use expect_test::{expect, Expect}; -use crate::tests::{completion_list, BASE_FIXTURE}; +use crate::tests::{completion_list, BASE_ITEMS_FIXTURE}; fn check(ra_fixture: &str, expect: Expect) { let actual = completion_list(ra_fixture); @@ -9,7 +9,7 @@ fn check(ra_fixture: &str, expect: Expect) { } fn check_with(ra_fixture: &str, expect: Expect) { - let actual = completion_list(&format!("{}\n{}", BASE_FIXTURE, ra_fixture)); + let actual = completion_list(&format!("{}\n{}", BASE_ITEMS_FIXTURE, ra_fixture)); expect.assert_eq(&actual) } diff --git a/crates/ide_completion/src/tests/predicate.rs b/crates/ide_completion/src/tests/predicate.rs index b65a716ed5..d47b2ecf50 100644 --- a/crates/ide_completion/src/tests/predicate.rs +++ b/crates/ide_completion/src/tests/predicate.rs @@ -1,10 +1,10 @@ //! Completion tests for predicates and bounds. use expect_test::{expect, Expect}; -use crate::tests::{completion_list, BASE_FIXTURE}; +use crate::tests::{completion_list, BASE_ITEMS_FIXTURE}; fn check(ra_fixture: &str, expect: Expect) { - let actual = completion_list(&format!("{}\n{}", BASE_FIXTURE, ra_fixture)); + let actual = completion_list(&format!("{}\n{}", BASE_ITEMS_FIXTURE, ra_fixture)); expect.assert_eq(&actual) } @@ -130,10 +130,10 @@ impl Record { en Enum st Record st Tuple - ma makro!(…) #[macro_export] macro_rules! makro md module st Unit ma makro!(…) #[macro_export] macro_rules! makro + ma makro!(…) #[macro_export] macro_rules! makro bt u32 "##]], ); diff --git a/crates/ide_completion/src/tests/type_pos.rs b/crates/ide_completion/src/tests/type_pos.rs index efffa4ee6e..eb81aa81e4 100644 --- a/crates/ide_completion/src/tests/type_pos.rs +++ b/crates/ide_completion/src/tests/type_pos.rs @@ -1,23 +1,10 @@ //! Completion tests for type position. use expect_test::{expect, Expect}; -use crate::tests::completion_list; +use crate::tests::{completion_list, BASE_ITEMS_FIXTURE}; fn check_with(ra_fixture: &str, expect: Expect) { - let base = r#" -enum Enum { TupleV(u32), RecordV { field: u32 }, UnitV } -use self::Enum::TupleV; -mod module {} - -trait Trait {} -static STATIC: Unit = Unit; -const CONST: Unit = Unit; -struct Record { field: u32 } -struct Tuple(u32); -struct Unit -macro_rules! makro {} -"#; - let actual = completion_list(&format!("{}\n{}", base, ra_fixture)); + let actual = completion_list(&format!("{}\n{}", BASE_ITEMS_FIXTURE, ra_fixture)); expect.assert_eq(&actual) } @@ -29,7 +16,7 @@ struct Foo<'lt, T, const C: usize> { f: $0 } "#, - expect![[r#" + expect![[r##" kw self kw super kw crate @@ -42,9 +29,10 @@ struct Foo<'lt, T, const C: usize> { md module st Foo<…> st Unit - ma makro!(…) macro_rules! makro + ma makro!(…) #[macro_export] macro_rules! makro + ma makro!(…) #[macro_export] macro_rules! makro bt u32 - "#]], + "##]], ) } @@ -54,7 +42,7 @@ fn tuple_struct_field() { r#" struct Foo<'lt, T, const C: usize>(f$0); "#, - expect![[r#" + expect![[r##" kw pub(crate) kw pub kw self @@ -69,9 +57,10 @@ struct Foo<'lt, T, const C: usize>(f$0); md module st Foo<…> st Unit - ma makro!(…) macro_rules! makro + ma makro!(…) #[macro_export] macro_rules! makro + ma makro!(…) #[macro_export] macro_rules! makro bt u32 - "#]], + "##]], ) } @@ -81,7 +70,7 @@ fn fn_return_type() { r#" fn x<'lt, T, const C: usize>() -> $0 "#, - expect![[r#" + expect![[r##" kw self kw super kw crate @@ -92,9 +81,10 @@ fn x<'lt, T, const C: usize>() -> $0 st Tuple md module st Unit - ma makro!(…) macro_rules! makro + ma makro!(…) #[macro_export] macro_rules! makro + ma makro!(…) #[macro_export] macro_rules! makro bt u32 - "#]], + "##]], ); } @@ -107,7 +97,7 @@ fn foo<'lt, T, const C: usize>() { let _: $0; } "#, - expect![[r#" + expect![[r##" kw self kw super kw crate @@ -118,9 +108,10 @@ fn foo<'lt, T, const C: usize>() { st Tuple md module st Unit - ma makro!(…) macro_rules! makro + ma makro!(…) #[macro_export] macro_rules! makro + ma makro!(…) #[macro_export] macro_rules! makro bt u32 - "#]], + "##]], ); check_with( r#" @@ -129,14 +120,15 @@ fn foo<'lt, T, const C: usize>() { let _: self::$0; } "#, - expect![[r#" + expect![[r##" tt Trait en Enum st Record st Tuple md module st Unit - "#]], + ma makro!(…) #[macro_export] macro_rules! makro + "##]], ); } @@ -150,7 +142,7 @@ trait Trait2 { fn foo<'lt, T: Trait2<$0>, const CONST_PARAM: usize>(_: T) {} "#, - expect![[r#" + expect![[r##" kw self kw super kw crate @@ -161,13 +153,14 @@ fn foo<'lt, T: Trait2<$0>, const CONST_PARAM: usize>(_: T) {} en Enum st Record st Tuple - tt Trait2 md module st Unit + ma makro!(…) #[macro_export] macro_rules! makro + tt Trait2 ct CONST - ma makro!(…) macro_rules! makro + ma makro!(…) #[macro_export] macro_rules! makro bt u32 - "#]], + "##]], ); check_with( r#" @@ -177,15 +170,16 @@ trait Trait2 { fn foo<'lt, T: Trait2, const CONST_PARAM: usize>(_: T) {} "#, - expect![[r#" + expect![[r##" tt Trait en Enum st Record st Tuple - tt Trait2 md module st Unit + ma makro!(…) #[macro_export] macro_rules! makro + tt Trait2 ct CONST - "#]], + "##]], ); } From 8de3f7ee538b0fee7783c18b03042dc3f12ca84b Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 23 Jul 2021 16:37:19 +0200 Subject: [PATCH 2/3] Move out unqualified_path completion tests --- .../ide_completion/src/completions/record.rs | 1 - .../ide_completion/src/completions/snippet.rs | 23 -- .../src/completions/unqualified_path.rs | 301 +----------------- crates/ide_completion/src/tests.rs | 1 + crates/ide_completion/src/tests/expression.rs | 249 +++++++++++++++ 5 files changed, 251 insertions(+), 324 deletions(-) create mode 100644 crates/ide_completion/src/tests/expression.rs diff --git a/crates/ide_completion/src/completions/record.rs b/crates/ide_completion/src/completions/record.rs index c7cf5e3e4b..151297042b 100644 --- a/crates/ide_completion/src/completions/record.rs +++ b/crates/ide_completion/src/completions/record.rs @@ -47,7 +47,6 @@ pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) -> #[cfg(test)] mod tests { - use crate::tests::check_edit; #[test] diff --git a/crates/ide_completion/src/completions/snippet.rs b/crates/ide_completion/src/completions/snippet.rs index 6237a6ef1e..a896a759ab 100644 --- a/crates/ide_completion/src/completions/snippet.rs +++ b/crates/ide_completion/src/completions/snippet.rs @@ -86,26 +86,3 @@ fn ${1:feature}() { let item = snippet(ctx, cap, "macro_rules", "macro_rules! $1 {\n\t($2) => {\n\t\t$0\n\t};\n}"); item.add_to(acc); } - -#[cfg(test)] -mod tests { - use expect_test::{expect, Expect}; - - use crate::{tests::filtered_completion_list, CompletionKind}; - - fn check(ra_fixture: &str, expect: Expect) { - let actual = filtered_completion_list(ra_fixture, CompletionKind::Snippet); - expect.assert_eq(&actual) - } - - #[test] - fn completes_snippets_in_expressions() { - check( - r#"fn foo(x: i32) { $0 }"#, - expect![[r#" - sn pd - sn ppd - "#]], - ); - } -} diff --git a/crates/ide_completion/src/completions/unqualified_path.rs b/crates/ide_completion/src/completions/unqualified_path.rs index 62ec0df799..8258026182 100644 --- a/crates/ide_completion/src/completions/unqualified_path.rs +++ b/crates/ide_completion/src/completions/unqualified_path.rs @@ -87,7 +87,7 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC if let ScopeDef::GenericParam(hir::GenericParam::LifetimeParam(_)) | ScopeDef::Label(_) = res { - cov_mark::hit!(skip_lifetime_completion); + cov_mark::hit!(unqualified_skip_lifetime_completion); return; } let add_resolution = match res { @@ -155,51 +155,6 @@ fn main() { ) } - #[test] - fn completes_generic_params() { - check( - r#"fn quux() { $0 }"#, - expect![[r#" - tp T - fn quux() fn() - "#]], - ); - check( - r#"fn quux() { $0 }"#, - expect![[r#" - cp C - fn quux() fn() - "#]], - ); - } - - #[test] - fn does_not_complete_lifetimes() { - cov_mark::check!(skip_lifetime_completion); - check( - r#"fn quux<'a>() { $0 }"#, - expect![[r#" - fn quux() fn() - "#]], - ); - } - - #[test] - fn completes_module_items() { - check( - r#" -struct S; -enum E {} -fn quux() { $0 } -"#, - expect![[r#" - st S - fn quux() fn() - en E - "#]], - ); - } - /// Regression test for issue #6091. #[test] fn correctly_completes_module_items_prefixed_with_underscore() { @@ -220,55 +175,6 @@ fn _alpha() {} ) } - #[test] - fn completes_module_items_in_nested_modules() { - check( - r#" -struct Foo; -mod m { - struct Bar; - fn quux() { $0 } -} -"#, - expect![[r#" - fn quux() fn() - st Bar - "#]], - ); - } - - #[test] - fn dont_show_both_completions_for_shadowing() { - check( - r#" -fn foo() { - let bar = 92; - { - let bar = 62; - drop($0) - } -} -"#, - // FIXME: should be only one bar here - expect![[r#" - lc bar i32 - lc bar i32 - fn foo() fn() - "#]], - ); - } - - #[test] - fn completes_self_in_methods() { - check( - r#"impl S { fn foo(&self) { $0 } }"#, - expect![[r#" - lc self &{unknown} - sp Self - "#]], - ); - } - #[test] fn completes_prelude() { check( @@ -318,32 +224,6 @@ mod macros { ); } - #[test] - fn does_not_complete_non_fn_macros() { - check( - r#" -#[rustc_builtin_macro] -pub macro Clone {} - -fn f() {$0} -"#, - expect![[r#" - fn f() fn() - "#]], - ); - check( - r#" -#[rustc_builtin_macro] -pub macro bench {} - -fn f() {$0} -"#, - expect![[r#" - fn f() fn() - "#]], - ); - } - #[test] fn completes_std_prelude_if_core_is_defined() { check( @@ -372,183 +252,4 @@ pub mod prelude { "#]], ); } - - #[test] - fn completes_macros_as_value() { - check( - r#" -macro_rules! foo { () => {} } - -#[macro_use] -mod m1 { - macro_rules! bar { () => {} } -} - -mod m2 { - macro_rules! nope { () => {} } - - #[macro_export] - macro_rules! baz { () => {} } -} - -fn main() { let v = $0 } -"#, - expect![[r##" - md m1 - ma baz!(…) #[macro_export] macro_rules! baz - fn main() fn() - md m2 - ma bar!(…) macro_rules! bar - ma foo!(…) macro_rules! foo - "##]], - ); - } - - #[test] - fn completes_both_macro_and_value() { - check( - r#" -macro_rules! foo { () => {} } -fn foo() { $0 } -"#, - expect![[r#" - fn foo() fn() - ma foo!(…) macro_rules! foo - "#]], - ); - } - - #[test] - fn completes_macros_as_stmt() { - check( - r#" -macro_rules! foo { () => {} } -fn main() { $0 } -"#, - expect![[r#" - fn main() fn() - ma foo!(…) macro_rules! foo - "#]], - ); - } - - #[test] - fn completes_local_item() { - check( - r#" -fn main() { - return f$0; - fn frobnicate() {} -} -"#, - expect![[r#" - fn frobnicate() fn() - fn main() fn() - "#]], - ); - } - - #[test] - fn completes_in_simple_macro_1() { - check( - r#" -macro_rules! m { ($e:expr) => { $e } } -fn quux(x: i32) { - let y = 92; - m!($0); -} -"#, - expect![[r#" - lc y i32 - lc x i32 - fn quux(…) fn(i32) - ma m!(…) macro_rules! m - "#]], - ); - } - - #[test] - fn completes_in_simple_macro_2() { - check( - r" -macro_rules! m { ($e:expr) => { $e } } -fn quux(x: i32) { - let y = 92; - m!(x$0); -} -", - expect![[r#" - lc y i32 - lc x i32 - fn quux(…) fn(i32) - ma m!(…) macro_rules! m - "#]], - ); - } - - #[test] - fn completes_in_simple_macro_without_closing_parens() { - check( - r#" -macro_rules! m { ($e:expr) => { $e } } -fn quux(x: i32) { - let y = 92; - m!(x$0 -} -"#, - expect![[r#" - lc y i32 - lc x i32 - fn quux(…) fn(i32) - ma m!(…) macro_rules! m - "#]], - ); - } - - #[test] - fn completes_unresolved_uses() { - check( - r#" -use spam::Quux; - -fn main() { $0 } -"#, - expect![[r#" - fn main() fn() - ?? Quux - "#]], - ); - } - - #[test] - fn completes_enum_variant_basic_expr() { - check( - r#" -enum Foo { Bar, Baz, Quux } -fn main() { let foo: Foo = Q$0 } -"#, - expect![[r#" - ev Foo::Bar () - ev Foo::Baz () - ev Foo::Quux () - en Foo - fn main() fn() - "#]], - ) - } - - #[test] - fn completes_enum_variant_from_module() { - check( - r#" -mod m { pub enum E { V } } -fn f() -> m::E { V$0 } -"#, - expect![[r#" - ev m::E::V () - md m - fn f() fn() -> E - "#]], - ) - } } diff --git a/crates/ide_completion/src/tests.rs b/crates/ide_completion/src/tests.rs index b7ba0a893a..8575914509 100644 --- a/crates/ide_completion/src/tests.rs +++ b/crates/ide_completion/src/tests.rs @@ -8,6 +8,7 @@ //! completed where, not how. mod attribute; +mod expression; mod fn_param; mod item_list; mod item; diff --git a/crates/ide_completion/src/tests/expression.rs b/crates/ide_completion/src/tests/expression.rs new file mode 100644 index 0000000000..fb9f7a5a3b --- /dev/null +++ b/crates/ide_completion/src/tests/expression.rs @@ -0,0 +1,249 @@ +//! Completion tests for expressions. +use expect_test::{expect, Expect}; + +use crate::tests::{completion_list, BASE_ITEMS_FIXTURE}; + +fn check(ra_fixture: &str, expect: Expect) { + let actual = completion_list(&format!("{}{}", BASE_ITEMS_FIXTURE, ra_fixture)); + expect.assert_eq(&actual) +} + +fn check_empty(ra_fixture: &str, expect: Expect) { + let actual = completion_list(ra_fixture); + expect.assert_eq(&actual); +} + +#[test] +fn completes_various_bindings() { + check_empty( + r#" +fn func(param0 @ (param1, param2): (i32, i32)) { + let letlocal = 92; + if let ifletlocal = 100 { + match 0 { + matcharm => 1 + $0, + otherwise => (), + } + } + let letlocal2 = 44; +} +"#, + 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 matcharm i32 + lc ifletlocal i32 + lc letlocal i32 + lc param0 (i32, i32) + lc param1 i32 + lc param2 i32 + fn func(…) fn((i32, i32)) + bt u32 + "#]], + ); +} + +#[test] +fn completes_all_the_things() { + cov_mark::check!(unqualified_skip_lifetime_completion); + check( + r#" +use non_existant::Unresolved; +mod qualified { pub enum Enum { Variant } } + +impl Unit { + fn foo<'lifetime, TypeParam, const CONST_PARAM: usize>(self) { + fn local_func() {} + $0 + } +} +"#, + expect![[r##" + kw unsafe + kw fn + kw const + kw type + kw impl + kw extern + kw use + kw trait + kw static + kw mod + kw match + kw while + kw while let + kw loop + kw if + kw if let + kw for + kw true + kw false + kw let + kw return + sn pd + sn ppd + kw self + kw super + kw crate + fn local_func() fn() + bt u32 + lc self Unit + tp TypeParam + cp CONST_PARAM + sp Self + tt Trait + en Enum + st Record + st Tuple + md module + st Unit + md qualified + ma makro!(…) #[macro_export] macro_rules! makro + ?? Unresolved + fn function() fn() + sc STATIC + ev TupleV(…) (u32) + ct CONST + ma makro!(…) #[macro_export] macro_rules! makro + me self.foo() fn(self) + "##]], + ); +} + +#[test] +fn shadowing_shows_single_completion() { + check_empty( + r#" +fn foo() { + let bar = 92; + { + let bar = 62; + drop($0) + } +} +"#, + // FIXME: should be only one bar here + 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 bar i32 + lc bar i32 + fn foo() fn() + bt u32 + "#]], + ); +} + +#[test] +fn in_macro_expr_frag() { + check_empty( + r#" +macro_rules! m { ($e:expr) => { $e } } +fn quux(x: i32) { + m!($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 + bt u32 + lc x i32 + fn quux(…) fn(i32) + ma m!(…) macro_rules! m + "#]], + ); + check_empty( + r" +macro_rules! m { ($e:expr) => { $e } } +fn quux(x: i32) { + m!(x$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 + bt u32 + lc x i32 + fn quux(…) fn(i32) + ma m!(…) macro_rules! m + "#]], + ); + check_empty( + r#" +macro_rules! m { ($e:expr) => { $e } } +fn quux(x: i32) { + let y = 92; + m!(x$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 y i32 + bt u32 + lc x i32 + fn quux(…) fn(i32) + ma m!(…) macro_rules! m + "#]], + ); +} From 189440c7b55ce892fe931c42b02c4c8178a6cc0a Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 23 Jul 2021 17:02:39 +0200 Subject: [PATCH 3/3] Add a Union to the base item completion test fixture --- .../src/completions/qualified_path.rs | 69 -------------- crates/ide_completion/src/tests.rs | 14 +-- crates/ide_completion/src/tests/expression.rs | 55 +++++++++++ crates/ide_completion/src/tests/item.rs | 2 + crates/ide_completion/src/tests/pattern.rs | 93 +++++++------------ crates/ide_completion/src/tests/predicate.rs | 3 + crates/ide_completion/src/tests/type_pos.rs | 40 ++++++-- 7 files changed, 134 insertions(+), 142 deletions(-) diff --git a/crates/ide_completion/src/completions/qualified_path.rs b/crates/ide_completion/src/completions/qualified_path.rs index 03b079ed7a..4b2aaa1d7a 100644 --- a/crates/ide_completion/src/completions/qualified_path.rs +++ b/crates/ide_completion/src/completions/qualified_path.rs @@ -255,60 +255,6 @@ mod tests { expect.assert_eq(&actual); } - fn check_builtin(ra_fixture: &str, expect: Expect) { - let actual = filtered_completion_list(ra_fixture, CompletionKind::BuiltinType); - expect.assert_eq(&actual); - } - - #[test] - fn dont_complete_primitive_in_use() { - check_builtin(r#"use self::$0;"#, expect![[""]]); - } - - #[test] - fn dont_complete_primitive_in_module_scope() { - check_builtin(r#"fn foo() { self::$0 }"#, expect![[""]]); - } - - #[test] - fn completes_enum_variant() { - check( - r#" -enum E { Foo, Bar(i32) } -fn foo() { let _ = E::$0 } -"#, - expect![[r#" - ev Foo () - ev Bar(…) (i32) - "#]], - ); - } - - #[test] - fn completes_struct_associated_items() { - check( - r#" -//- /lib.rs -struct S; - -impl S { - fn a() {} - fn b(&self) {} - const C: i32 = 42; - type T = i32; -} - -fn foo() { let _ = S::$0 } -"#, - expect![[r#" - fn a() fn() - me b(…) fn(&self) - ct C const C: i32 = 42; - ta T type T = i32; - "#]], - ); - } - #[test] fn associated_item_visibility() { check( @@ -336,21 +282,6 @@ fn foo() { let _ = S::$0 } ); } - #[test] - fn completes_enum_associated_method() { - check( - r#" -enum E {}; -impl E { fn m() { } } - -fn foo() { let _ = E::$0 } - "#, - expect![[r#" - fn m() fn() - "#]], - ); - } - #[test] fn completes_union_associated_method() { check( diff --git a/crates/ide_completion/src/tests.rs b/crates/ide_completion/src/tests.rs index 8575914509..b8d60dfd2c 100644 --- a/crates/ide_completion/src/tests.rs +++ b/crates/ide_completion/src/tests.rs @@ -1,11 +1,12 @@ //! Tests and test utilities for completions. //! -//! 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. +//! Most tests live in this module or its submodules. The tests in these submodules are "location" +//! oriented, that is they try to check completions for something like type position, param position +//! etc. +//! Tests that are more orientated towards specific completion types like visibility checks of path +//! completions or `check_edit` tests usually live in their respective completion modules instead. +//! This gives this test module and its submodules here the main purpose of giving the developer an +//! overview of whats being completed where, not how. mod attribute; mod expression; @@ -55,6 +56,7 @@ macro_rules! makro {} #[rustc_builtin_macro] pub macro Clone {} fn function() {} +union Union { field: i32 } "#; pub(crate) const TEST_CONFIG: CompletionConfig = CompletionConfig { diff --git a/crates/ide_completion/src/tests/expression.rs b/crates/ide_completion/src/tests/expression.rs index fb9f7a5a3b..cf22fb20ab 100644 --- a/crates/ide_completion/src/tests/expression.rs +++ b/crates/ide_completion/src/tests/expression.rs @@ -70,6 +70,7 @@ impl Unit { } } "#, + // `self` is in here twice, once as the module, once as the local expect![[r##" kw unsafe kw fn @@ -114,12 +115,42 @@ impl Unit { ?? Unresolved fn function() fn() sc STATIC + un Union ev TupleV(…) (u32) ct CONST ma makro!(…) #[macro_export] macro_rules! makro me self.foo() fn(self) "##]], ); + check( + r#" +use non_existant::Unresolved; +mod qualified { pub enum Enum { Variant } } + +impl Unit { + fn foo<'lifetime, TypeParam, const CONST_PARAM: usize>(self) { + fn local_func() {} + self::$0 + } +} +"#, + expect![[r##" + tt Trait + en Enum + st Record + st Tuple + md module + st Unit + md qualified + ma makro!(…) #[macro_export] macro_rules! makro + ?? Unresolved + fn function() fn() + sc STATIC + un Union + ev TupleV(…) (u32) + ct CONST + "##]], + ); } #[test] @@ -247,3 +278,27 @@ fn quux(x: i32) { "#]], ); } + +#[test] +fn enum_qualified() { + check( + r#" +impl Enum { + type AssocType = (); + const ASSOC_CONST: () = (); + fn assoc_fn() {} +} +fn func() { + Enum::$0 +} +"#, + expect![[r#" + ev TupleV(…) (u32) + ev RecordV { field: u32 } + ev UnitV () + ct ASSOC_CONST const ASSOC_CONST: () = (); + fn assoc_fn() fn() + ta AssocType type AssocType = (); + "#]], + ); +} diff --git a/crates/ide_completion/src/tests/item.rs b/crates/ide_completion/src/tests/item.rs index 45d653cfd2..ad87bdc751 100644 --- a/crates/ide_completion/src/tests/item.rs +++ b/crates/ide_completion/src/tests/item.rs @@ -28,6 +28,7 @@ impl Tra$0 md module st Unit ma makro!(…) #[macro_export] macro_rules! makro + un Union ma makro!(…) #[macro_export] macro_rules! makro bt u32 "##]], @@ -51,6 +52,7 @@ impl Trait for Str$0 md module st Unit ma makro!(…) #[macro_export] macro_rules! makro + un Union ma makro!(…) #[macro_export] macro_rules! makro bt u32 "##]], diff --git a/crates/ide_completion/src/tests/pattern.rs b/crates/ide_completion/src/tests/pattern.rs index 7b868fd854..5791921e44 100644 --- a/crates/ide_completion/src/tests/pattern.rs +++ b/crates/ide_completion/src/tests/pattern.rs @@ -3,19 +3,19 @@ use expect_test::{expect, Expect}; use crate::tests::{completion_list, BASE_ITEMS_FIXTURE}; -fn check(ra_fixture: &str, expect: Expect) { +fn check_empty(ra_fixture: &str, expect: Expect) { let actual = completion_list(ra_fixture); expect.assert_eq(&actual) } -fn check_with(ra_fixture: &str, expect: Expect) { +fn check(ra_fixture: &str, expect: Expect) { let actual = completion_list(&format!("{}\n{}", BASE_ITEMS_FIXTURE, ra_fixture)); expect.assert_eq(&actual) } #[test] fn ident_rebind_pat() { - check( + check_empty( r#" fn quux() { let en$0 @ x @@ -29,7 +29,7 @@ fn quux() { #[test] fn ident_ref_pat() { - check( + check_empty( r#" fn quux() { let ref en$0 @@ -39,7 +39,7 @@ fn quux() { kw mut "#]], ); - check( + check_empty( r#" fn quux() { let ref en$0 @ x @@ -54,7 +54,7 @@ fn quux() { #[test] fn ident_ref_mut_pat() { // FIXME mut is already here, don't complete it again - check( + check_empty( r#" fn quux() { let ref mut en$0 @@ -64,7 +64,7 @@ fn quux() { kw mut "#]], ); - check( + check_empty( r#" fn quux() { let ref mut en$0 @ x @@ -78,7 +78,7 @@ fn quux() { #[test] fn ref_pat() { - check( + check_empty( r#" fn quux() { let &en$0 @@ -89,7 +89,7 @@ fn quux() { "#]], ); // FIXME mut is already here, don't complete it again - check( + check_empty( r#" fn quux() { let &mut en$0 @@ -103,7 +103,7 @@ fn quux() { #[test] fn refutable() { - check_with( + check( r#" fn foo() { if let a$0 @@ -129,7 +129,7 @@ fn foo() { #[test] fn irrefutable() { - check_with( + check( r#" fn foo() { let a$0 @@ -150,7 +150,7 @@ fn foo() { #[test] fn in_param() { - check_with( + check( r#" fn foo(a$0) { } @@ -170,7 +170,7 @@ fn foo(a$0) { #[test] fn only_fn_like_macros() { - check( + check_empty( r#" macro_rules! m { ($e:expr) => { $e } } @@ -190,7 +190,7 @@ fn foo() { #[test] fn in_simple_macro_call() { - check( + check_empty( r#" macro_rules! m { ($e:expr) => { $e } } enum E { X } @@ -210,7 +210,7 @@ fn foo() { #[test] fn omits_private_fields_pat() { - check( + check_empty( r#" mod foo { pub struct Record { pub field: i32, _field: i32 } @@ -235,32 +235,9 @@ fn outer() { ) } -// #[test] -// fn only_shows_ident_completion() { -// check_edit( -// "Foo", -// r#" -// struct Foo(i32); -// fn main() { -// match Foo(92) { -// a$0(92) => (), -// } -// } -// "#, -// r#" -// struct Foo(i32); -// fn main() { -// match Foo(92) { -// Foo(92) => (), -// } -// } -// "#, -// ); -// } - #[test] fn completes_self_pats() { - check( + check_empty( r#" struct Foo(i32); impl Foo { @@ -282,35 +259,33 @@ impl Foo { } #[test] -fn completes_qualified_variant() { +fn enum_qualified() { + // FIXME: Don't show functions, they aren't patterns check( r#" -enum Foo { - Bar { baz: i32 } +impl Enum { + type AssocType = (); + const ASSOC_CONST: () = (); + fn assoc_fn() {} } -impl Foo { - fn foo() { - match {Foo::Bar { baz: 0 }} { - B$0 - } - } +fn func() { + if let Enum::$0 = unknown {} } - "#, +"#, expect![[r#" - kw mut - bn Self::Bar Self::Bar { baz$1 }$0 - ev Self::Bar { baz: i32 } - bn Foo::Bar Foo::Bar { baz$1 }$0 - ev Foo::Bar { baz: i32 } - sp Self - en Foo + ev TupleV(…) (u32) + ev RecordV { field: u32 } + ev UnitV () + ct ASSOC_CONST const ASSOC_CONST: () = (); + fn assoc_fn() fn() + ta AssocType type AssocType = (); "#]], - ) + ); } #[test] fn completes_in_record_field_pat() { - check( + check_empty( r#" struct Foo { bar: Bar } struct Bar(u32); @@ -328,7 +303,7 @@ fn outer(Foo { bar: $0 }: Foo) {} #[test] fn skips_in_record_field_pat_name() { - check( + check_empty( r#" struct Foo { bar: Bar } struct Bar(u32); diff --git a/crates/ide_completion/src/tests/predicate.rs b/crates/ide_completion/src/tests/predicate.rs index d47b2ecf50..d43e4b50b1 100644 --- a/crates/ide_completion/src/tests/predicate.rs +++ b/crates/ide_completion/src/tests/predicate.rs @@ -27,6 +27,7 @@ struct Foo<'lt, T, const C: usize> where $0 {} st Foo<…> st Unit ma makro!(…) #[macro_export] macro_rules! makro + un Union ma makro!(…) #[macro_export] macro_rules! makro bt u32 "##]], @@ -107,6 +108,7 @@ struct Foo<'lt, T, const C: usize> where for<'a> $0 {} st Foo<…> st Unit ma makro!(…) #[macro_export] macro_rules! makro + un Union ma makro!(…) #[macro_export] macro_rules! makro bt u32 "##]], @@ -133,6 +135,7 @@ impl Record { md module st Unit ma makro!(…) #[macro_export] macro_rules! makro + un Union ma makro!(…) #[macro_export] macro_rules! makro bt u32 "##]], diff --git a/crates/ide_completion/src/tests/type_pos.rs b/crates/ide_completion/src/tests/type_pos.rs index eb81aa81e4..88146357ca 100644 --- a/crates/ide_completion/src/tests/type_pos.rs +++ b/crates/ide_completion/src/tests/type_pos.rs @@ -3,14 +3,14 @@ use expect_test::{expect, Expect}; use crate::tests::{completion_list, BASE_ITEMS_FIXTURE}; -fn check_with(ra_fixture: &str, expect: Expect) { +fn check(ra_fixture: &str, expect: Expect) { let actual = completion_list(&format!("{}\n{}", BASE_ITEMS_FIXTURE, ra_fixture)); expect.assert_eq(&actual) } #[test] fn record_field_ty() { - check_with( + check( r#" struct Foo<'lt, T, const C: usize> { f: $0 @@ -30,6 +30,7 @@ struct Foo<'lt, T, const C: usize> { st Foo<…> st Unit ma makro!(…) #[macro_export] macro_rules! makro + un Union ma makro!(…) #[macro_export] macro_rules! makro bt u32 "##]], @@ -38,7 +39,7 @@ struct Foo<'lt, T, const C: usize> { #[test] fn tuple_struct_field() { - check_with( + check( r#" struct Foo<'lt, T, const C: usize>(f$0); "#, @@ -58,6 +59,7 @@ struct Foo<'lt, T, const C: usize>(f$0); st Foo<…> st Unit ma makro!(…) #[macro_export] macro_rules! makro + un Union ma makro!(…) #[macro_export] macro_rules! makro bt u32 "##]], @@ -66,7 +68,7 @@ struct Foo<'lt, T, const C: usize>(f$0); #[test] fn fn_return_type() { - check_with( + check( r#" fn x<'lt, T, const C: usize>() -> $0 "#, @@ -82,6 +84,7 @@ fn x<'lt, T, const C: usize>() -> $0 md module st Unit ma makro!(…) #[macro_export] macro_rules! makro + un Union ma makro!(…) #[macro_export] macro_rules! makro bt u32 "##]], @@ -90,7 +93,7 @@ fn x<'lt, T, const C: usize>() -> $0 #[test] fn body_type_pos() { - check_with( + check( r#" fn foo<'lt, T, const C: usize>() { let local = (); @@ -109,11 +112,12 @@ fn foo<'lt, T, const C: usize>() { md module st Unit ma makro!(…) #[macro_export] macro_rules! makro + un Union ma makro!(…) #[macro_export] macro_rules! makro bt u32 "##]], ); - check_with( + check( r#" fn foo<'lt, T, const C: usize>() { let local = (); @@ -128,13 +132,14 @@ fn foo<'lt, T, const C: usize>() { md module st Unit ma makro!(…) #[macro_export] macro_rules! makro + un Union "##]], ); } #[test] fn completes_types_and_const_in_arg_list() { - check_with( + check( r#" trait Trait2 { type Foo; @@ -157,12 +162,13 @@ fn foo<'lt, T: Trait2<$0>, const CONST_PARAM: usize>(_: T) {} st Unit ma makro!(…) #[macro_export] macro_rules! makro tt Trait2 + un Union ct CONST ma makro!(…) #[macro_export] macro_rules! makro bt u32 "##]], ); - check_with( + check( r#" trait Trait2 { type Foo; @@ -179,7 +185,25 @@ fn foo<'lt, T: Trait2, const CONST_PARAM: usize>(_: T) {} st Unit ma makro!(…) #[macro_export] macro_rules! makro tt Trait2 + un Union ct CONST "##]], ); } + +#[test] +fn enum_qualified() { + check( + r#" +impl Enum { + type AssocType = (); + const ASSOC_CONST: () = (); + fn assoc_fn() {} +} +fn func(_: Enum::$0) {} +"#, + expect![[r#" + ta AssocType type AssocType = (); + "#]], + ); +}