mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-02 06:41:48 +00:00
Move out record completion tests
This commit is contained in:
parent
444679f202
commit
2d696b9c9e
3 changed files with 233 additions and 322 deletions
|
@ -44,325 +44,3 @@ pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) ->
|
||||||
|
|
||||||
Some(())
|
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<T> { a: T }
|
|
||||||
|
|
||||||
fn foo() {
|
|
||||||
let _: A<u32> = 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
|
|
||||||
"#]],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -10,6 +10,7 @@ mod item_list;
|
||||||
mod item;
|
mod item;
|
||||||
mod pattern;
|
mod pattern;
|
||||||
mod predicate;
|
mod predicate;
|
||||||
|
mod record;
|
||||||
mod sourcegen;
|
mod sourcegen;
|
||||||
mod type_pos;
|
mod type_pos;
|
||||||
mod use_tree;
|
mod use_tree;
|
||||||
|
|
232
crates/ide_completion/src/tests/record.rs
Normal file
232
crates/ide_completion/src/tests/record.rs
Normal file
|
@ -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()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue