From f835279b3ae41644e9568187b4468cd9d9e84eca Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 21 Jun 2021 13:48:25 +0200 Subject: Move out completion pattern tests --- crates/ide_completion/src/tests/pattern.rs | 348 +++++++++++++++++++++++++++++ 1 file changed, 348 insertions(+) create mode 100644 crates/ide_completion/src/tests/pattern.rs (limited to 'crates/ide_completion/src/tests') diff --git a/crates/ide_completion/src/tests/pattern.rs b/crates/ide_completion/src/tests/pattern.rs new file mode 100644 index 000000000..1ad5ccd97 --- /dev/null +++ b/crates/ide_completion/src/tests/pattern.rs @@ -0,0 +1,348 @@ +//! Completions tests for pattern position. +use expect_test::{expect, Expect}; + +use crate::tests::completion_list; + +fn check(ra_fixture: &str, expect: Expect) { + let actual = completion_list(ra_fixture); + expect.assert_eq(&actual) +} + +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 {} + +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)); + expect.assert_eq(&actual) +} + +#[test] +fn ident_rebind_pat() { + check( + r#" +fn quux() { + let en$0 @ x +} +"#, + expect![[r#" + kw mut + "#]], + ); +} + +#[test] +fn ident_ref_pat() { + check( + r#" +fn quux() { + let ref en$0 +} +"#, + expect![[r#" + kw mut + "#]], + ); + check( + r#" +fn quux() { + let ref en$0 @ x +} +"#, + expect![[r#" + kw mut + "#]], + ); +} + +#[test] +fn ident_ref_mut_pat() { + // FIXME mut is already here, don't complete it again + check( + r#" +fn quux() { + let ref mut en$0 +} +"#, + expect![[r#" + kw mut + "#]], + ); + check( + r#" +fn quux() { + let ref mut en$0 @ x +} +"#, + expect![[r#" + kw mut + "#]], + ); +} + +#[test] +fn ref_pat() { + check( + r#" +fn quux() { + let &en$0 +} +"#, + expect![[r#" + kw mut + "#]], + ); + // FIXME mut is already here, don't complete it again + check( + r#" +fn quux() { + let &mut en$0 +} +"#, + expect![[r#" + kw mut + "#]], + ); +} + +#[test] +fn refutable() { + check_with( + r#" +fn foo() { + if let a$0 +} +"#, + expect![[r#" + kw mut + bn Record Record { field$1 }$0 + st Record + en Enum + bn Tuple Tuple($1)$0 + st Tuple + md module + bn TupleV TupleV($1)$0 + ev TupleV + st Unit + ct CONST + ma makro!(…) macro_rules! makro + "#]], + ); +} + +#[test] +fn irrefutable() { + check_with( + r#" +fn foo() { + let a$0 +} +"#, + expect![[r#" + kw mut + bn Record Record { field$1 }$0 + st Record + bn Tuple Tuple($1)$0 + st Tuple + st Unit + ma makro!(…) macro_rules! makro + "#]], + ); +} + +#[test] +fn in_param() { + check_with( + r#" +fn foo(a$0) { +} +"#, + expect![[r#" + kw mut + bn Record Record { field$1 }: Record$0 + st Record + bn Tuple Tuple($1): Tuple$0 + st Tuple + st Unit + ma makro!(…) macro_rules! makro + "#]], + ); +} + +#[test] +fn only_fn_like_macros() { + check( + r#" +macro_rules! m { ($e:expr) => { $e } } + +#[rustc_builtin_macro] +macro Clone {} + +fn foo() { + let x$0 +} +"#, + expect![[r#" + kw mut + ma m!(…) macro_rules! m + "#]], + ); +} + +#[test] +fn in_simple_macro_call() { + check( + r#" +macro_rules! m { ($e:expr) => { $e } } +enum E { X } + +fn foo() { + m!(match E::X { a$0 }) +} +"#, + expect![[r#" + kw mut + ev E::X () + en E + ma m!(…) macro_rules! m + "#]], + ); +} + +#[test] +fn omits_private_fields_pat() { + check( + r#" +mod foo { + pub struct Record { pub field: i32, _field: i32 } + pub struct Tuple(pub u32, u32); + pub struct Invisible(u32, u32); +} +use foo::*; + +fn outer() { + if let a$0 +} +"#, + expect![[r#" + kw mut + bn Record Record { field$1, .. }$0 + st Record + bn Tuple Tuple($1, ..)$0 + st Tuple + st Invisible + md foo + "#]], + ) +} + +// #[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( + r#" +struct Foo(i32); +impl Foo { + fn foo() { + match Foo(0) { + a$0 + } + } +} + "#, + expect![[r#" + kw mut + bn Self Self($1)$0 + sp Self + bn Foo Foo($1)$0 + st Foo + "#]], + ) +} + +#[test] +fn completes_qualified_variant() { + check( + r#" +enum Foo { + Bar { baz: i32 } +} +impl Foo { + fn foo() { + match {Foo::Bar { baz: 0 }} { + B$0 + } + } +} + "#, + 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 + "#]], + ) +} + +#[test] +fn completes_in_record_field_pat() { + check( + r#" +struct Foo { bar: Bar } +struct Bar(u32); +fn outer(Foo { bar: $0 }: Foo) {} +"#, + expect![[r#" + kw mut + bn Foo Foo { bar$1 }$0 + st Foo + bn Bar Bar($1)$0 + st Bar + "#]], + ) +} + +#[test] +fn skips_in_record_field_pat_name() { + check( + r#" +struct Foo { bar: Bar } +struct Bar(u32); +fn outer(Foo { bar$0 }: Foo) {} +"#, + expect![[r#""#]], + ) +} -- cgit v1.2.3 From b9d85f55b7a0a2159971b42bb5dae71efbfeada4 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 21 Jun 2021 14:59:49 +0200 Subject: Move out completion type position tests --- crates/ide_completion/src/tests/items.rs | 32 ----- crates/ide_completion/src/tests/type_pos.rs | 185 ++++++++++++++++++++++++++++ 2 files changed, 185 insertions(+), 32 deletions(-) create mode 100644 crates/ide_completion/src/tests/type_pos.rs (limited to 'crates/ide_completion/src/tests') diff --git a/crates/ide_completion/src/tests/items.rs b/crates/ide_completion/src/tests/items.rs index 8dfb8221b..b98baffd6 100644 --- a/crates/ide_completion/src/tests/items.rs +++ b/crates/ide_completion/src/tests/items.rs @@ -35,22 +35,6 @@ impl Tra$0 ma foo!(…) #[macro_export] macro_rules! foo ma foo!(…) #[macro_export] macro_rules! foo bt u32 - bt bool - bt u8 - bt isize - bt u16 - bt u64 - bt u128 - bt f32 - bt i128 - bt i16 - bt str - bt i64 - bt char - bt f64 - bt i32 - bt i8 - bt usize "##]], ) } @@ -69,22 +53,6 @@ impl Trait for Str$0 ma foo!(…) #[macro_export] macro_rules! foo ma foo!(…) #[macro_export] macro_rules! foo bt u32 - bt bool - bt u8 - bt isize - bt u16 - bt u64 - bt u128 - bt f32 - bt i128 - bt i16 - bt str - bt i64 - bt char - bt f64 - bt i32 - bt i8 - bt usize "##]], ) } diff --git a/crates/ide_completion/src/tests/type_pos.rs b/crates/ide_completion/src/tests/type_pos.rs new file mode 100644 index 000000000..2bfecdd08 --- /dev/null +++ b/crates/ide_completion/src/tests/type_pos.rs @@ -0,0 +1,185 @@ +//! Completions tests for type position. +use expect_test::{expect, Expect}; + +use crate::tests::completion_list; + +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)); + expect.assert_eq(&actual) +} + +#[test] +fn record_field_ty() { + // FIXME: pub shouldnt show up here + check_with( + r#" +struct Foo<'lt, T, const C: usize> { + f: $0 +} +"#, + expect![[r#" + kw pub(crate) + kw pub + sp Self + tp T + tt Trait + en Enum + st Record + st Tuple + md module + st Foo<…> + st Unit + ma makro!(…) macro_rules! makro + bt u32 + "#]], + ) +} + +#[test] +fn tuple_struct_field() { + // FIXME: pub should show up here + check_with( + r#" +struct Foo<'lt, T, const C: usize>(f$0); +"#, + expect![[r#" + sp Self + tp T + tt Trait + en Enum + st Record + st Tuple + md module + st Foo<…> + st Unit + ma makro!(…) macro_rules! makro + bt u32 + "#]], + ) +} + +#[test] +fn fn_return_type() { + // FIXME: return shouldnt show up here + check_with( + r#" +fn x<'lt, T, const C: usize>() -> $0 +"#, + expect![[r#" + kw return + tp T + tt Trait + en Enum + st Record + st Tuple + md module + st Unit + ma makro!(…) macro_rules! makro + bt u32 + "#]], + ); +} + +#[test] +fn body_type_pos() { + // FIXME: return shouldnt show up here + check_with( + r#" +fn foo<'lt, T, const C: usize>() { + let local = (); + let _: $0; +} +"#, + expect![[r#" + kw return + tp T + tt Trait + en Enum + st Record + st Tuple + md module + st Unit + ma makro!(…) macro_rules! makro + bt u32 + "#]], + ); + check_with( + r#" +fn foo<'lt, T, const C: usize>() { + let local = (); + let _: self::$0; +} +"#, + expect![[r#" + tt Trait + en Enum + st Record + st Tuple + md module + st Unit + "#]], + ); +} + +#[test] +fn completes_types_and_const_in_arg_list() { + // FIXME: return shouldnt show up here + // FIXME: we should complete the lifetime here for now + check_with( + r#" +trait Trait2 { + type Foo; +} + +fn foo<'lt, T: Trait2<$0>, const CONST_PARAM: usize>(_: T) {} +"#, + expect![[r#" + kw return + ta Foo = type Foo; + tp T + cp CONST_PARAM + tt Trait + en Enum + st Record + st Tuple + tt Trait2 + md module + st Unit + ct CONST + ma makro!(…) macro_rules! makro + bt u32 + "#]], + ); + check_with( + r#" +trait Trait2 { + type Foo; +} + +fn foo<'lt, T: Trait2, const CONST_PARAM: usize>(_: T) {} + "#, + expect![[r#" + tt Trait + en Enum + st Record + st Tuple + tt Trait2 + md module + st Unit + ct CONST + "#]], + ); +} -- cgit v1.2.3 From 0729913525a55cad3ffe9876c1eb05f7b880d22d Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 21 Jun 2021 15:14:28 +0200 Subject: Various keyword completion fixes --- crates/ide_completion/src/tests/type_pos.rs | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) (limited to 'crates/ide_completion/src/tests') diff --git a/crates/ide_completion/src/tests/type_pos.rs b/crates/ide_completion/src/tests/type_pos.rs index 2bfecdd08..1ab47b27e 100644 --- a/crates/ide_completion/src/tests/type_pos.rs +++ b/crates/ide_completion/src/tests/type_pos.rs @@ -23,7 +23,6 @@ macro_rules! makro {} #[test] fn record_field_ty() { - // FIXME: pub shouldnt show up here check_with( r#" struct Foo<'lt, T, const C: usize> { @@ -31,8 +30,6 @@ struct Foo<'lt, T, const C: usize> { } "#, expect![[r#" - kw pub(crate) - kw pub sp Self tp T tt Trait @@ -42,7 +39,7 @@ struct Foo<'lt, T, const C: usize> { md module st Foo<…> st Unit - ma makro!(…) macro_rules! makro + ma makro!(…) macro_rules! makro bt u32 "#]], ) @@ -50,12 +47,13 @@ struct Foo<'lt, T, const C: usize> { #[test] fn tuple_struct_field() { - // FIXME: pub should show up here check_with( r#" struct Foo<'lt, T, const C: usize>(f$0); "#, expect![[r#" + kw pub(crate) + kw pub sp Self tp T tt Trait @@ -65,7 +63,7 @@ struct Foo<'lt, T, const C: usize>(f$0); md module st Foo<…> st Unit - ma makro!(…) macro_rules! makro + ma makro!(…) macro_rules! makro bt u32 "#]], ) @@ -73,13 +71,11 @@ struct Foo<'lt, T, const C: usize>(f$0); #[test] fn fn_return_type() { - // FIXME: return shouldnt show up here check_with( r#" fn x<'lt, T, const C: usize>() -> $0 "#, expect![[r#" - kw return tp T tt Trait en Enum @@ -95,7 +91,6 @@ fn x<'lt, T, const C: usize>() -> $0 #[test] fn body_type_pos() { - // FIXME: return shouldnt show up here check_with( r#" fn foo<'lt, T, const C: usize>() { @@ -104,7 +99,6 @@ fn foo<'lt, T, const C: usize>() { } "#, expect![[r#" - kw return tp T tt Trait en Enum @@ -136,7 +130,6 @@ fn foo<'lt, T, const C: usize>() { #[test] fn completes_types_and_const_in_arg_list() { - // FIXME: return shouldnt show up here // FIXME: we should complete the lifetime here for now check_with( r#" @@ -147,7 +140,6 @@ trait Trait2 { fn foo<'lt, T: Trait2<$0>, const CONST_PARAM: usize>(_: T) {} "#, expect![[r#" - kw return ta Foo = type Foo; tp T cp CONST_PARAM -- cgit v1.2.3