Modernise item completion tests

This commit is contained in:
Aleksey Kladov 2020-07-04 23:22:07 +02:00
parent 4cfe0070dd
commit 453167b492

View file

@ -227,330 +227,264 @@ fn make_const_compl_syntax(const_: &ast::ConstDef) -> String {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use insta::assert_debug_snapshot; use expect::{expect, Expect};
use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind}; use crate::completion::{
test_utils::{check_edit, completion_list},
CompletionKind,
};
fn complete(code: &str) -> Vec<CompletionItem> { fn check(ra_fixture: &str, expect: Expect) {
do_completion(code, CompletionKind::Magic) let actual = completion_list(ra_fixture, CompletionKind::Magic);
expect.assert_eq(&actual)
} }
#[test] #[test]
fn name_ref_function_type_const() { fn name_ref_function_type_const() {
let completions = complete( check(
r" r#"
trait Test { trait Test {
type TestType; type TestType;
const TEST_CONST: u16; const TEST_CONST: u16;
fn test(); fn test();
} }
struct T;
struct T1; impl Test for T {
t<|>
impl Test for T1 { }
t<|> "#,
} expect![["
", ct const TEST_CONST: u16 = \n\
fn fn test()
ta type TestType = \n\
"]],
); );
assert_debug_snapshot!(completions, @r###"
[
CompletionItem {
label: "const TEST_CONST: u16 = ",
source_range: 112..113,
delete: 112..113,
insert: "const TEST_CONST: u16 = ",
kind: Const,
lookup: "TEST_CONST",
},
CompletionItem {
label: "fn test()",
source_range: 112..113,
delete: 112..113,
insert: "fn test() {\n $0\n}",
kind: Function,
lookup: "test",
},
CompletionItem {
label: "type TestType = ",
source_range: 112..113,
delete: 112..113,
insert: "type TestType = ",
kind: TypeAlias,
lookup: "TestType",
},
]
"###);
} }
#[test] #[test]
fn no_nested_fn_completions() { fn no_nested_fn_completions() {
let completions = complete( check(
r" r"
trait Test { trait Test {
fn test(); fn test();
fn test2(); fn test2();
} }
struct T;
struct T1; impl Test for T {
fn test() {
impl Test for T1 { t<|>
fn test() { }
t<|> }
} ",
} expect![[""]],
",
); );
assert_debug_snapshot!(completions, @r###"[]"###);
} }
#[test] #[test]
fn name_ref_single_function() { fn name_ref_single_function() {
let completions = complete( check_edit(
r" "test",
trait Test { r#"
fn test(); trait Test {
} fn test();
}
struct T;
struct T1; impl Test for T {
t<|>
}
"#,
r#"
trait Test {
fn test();
}
struct T;
impl Test for T1 { impl Test for T {
t<|> fn test() {
} $0
", }
}
"#,
); );
assert_debug_snapshot!(completions, @r###"
[
CompletionItem {
label: "fn test()",
source_range: 66..67,
delete: 66..67,
insert: "fn test() {\n $0\n}",
kind: Function,
lookup: "test",
},
]
"###);
} }
#[test] #[test]
fn single_function() { fn single_function() {
let completions = complete( check_edit(
r" "test",
trait Test { r#"
fn foo(); trait Test {
} fn test();
}
struct T;
struct T1; impl Test for T {
fn t<|>
}
"#,
r#"
trait Test {
fn test();
}
struct T;
impl Test for T1 { impl Test for T {
fn f<|> fn test() {
} $0
", }
}
"#,
); );
assert_debug_snapshot!(completions, @r###"
[
CompletionItem {
label: "fn foo()",
source_range: 68..69,
delete: 65..69,
insert: "fn foo() {\n $0\n}",
kind: Function,
lookup: "foo",
},
]
"###);
} }
#[test] #[test]
fn hide_implemented_fn() { fn hide_implemented_fn() {
let completions = complete( check(
r" r#"
trait Test { trait Test {
fn foo(); fn foo();
fn foo_bar(); fn foo_bar();
} }
struct T;
struct T1; impl Test for T {
fn foo() {}
impl Test for T1 { fn f<|>
fn foo() {} }
"#,
fn f<|> expect![[r#"
} fn fn foo_bar()
", "#]],
); );
assert_debug_snapshot!(completions, @r###"
[
CompletionItem {
label: "fn foo_bar()",
source_range: 103..104,
delete: 100..104,
insert: "fn foo_bar() {\n $0\n}",
kind: Function,
lookup: "foo_bar",
},
]
"###);
}
#[test]
fn completes_only_on_top_level() {
let completions = complete(
r"
trait Test {
fn foo();
fn foo_bar();
}
struct T1;
impl Test for T1 {
fn foo() {
<|>
}
}
",
);
assert_debug_snapshot!(completions, @r###"[]"###);
} }
#[test] #[test]
fn generic_fn() { fn generic_fn() {
let completions = complete( check_edit(
r" "foo",
trait Test { r#"
fn foo<T>(); trait Test {
} fn foo<T>();
}
struct T;
struct T1; impl Test for T {
fn f<|>
}
"#,
r#"
trait Test {
fn foo<T>();
}
struct T;
impl Test for T1 { impl Test for T {
fn f<|> fn foo<T>() {
} $0
", }
}
"#,
); );
assert_debug_snapshot!(completions, @r###" check_edit(
[ "foo",
CompletionItem { r#"
label: "fn foo()", trait Test {
source_range: 71..72, fn foo<T>() where T: Into<String>;
delete: 68..72, }
insert: "fn foo<T>() {\n $0\n}", struct T;
kind: Function,
lookup: "foo",
},
]
"###);
}
#[test] impl Test for T {
fn generic_constrait_fn() { fn f<|>
let completions = complete( }
r" "#,
trait Test { r#"
fn foo<T>() where T: Into<String>; trait Test {
} fn foo<T>() where T: Into<String>;
}
struct T;
struct T1; impl Test for T {
fn foo<T>()
impl Test for T1 { where T: Into<String> {
fn f<|> $0
} }
", }
"#,
); );
assert_debug_snapshot!(completions, @r###"
[
CompletionItem {
label: "fn foo()",
source_range: 93..94,
delete: 90..94,
insert: "fn foo<T>()\nwhere T: Into<String> {\n $0\n}",
kind: Function,
lookup: "foo",
},
]
"###);
} }
#[test] #[test]
fn associated_type() { fn associated_type() {
let completions = complete( check_edit(
r" "SomeType",
trait Test { r#"
type SomeType; trait Test {
} type SomeType;
}
impl Test for () { impl Test for () {
type S<|> type S<|>
} }
", "#,
"
trait Test {
type SomeType;
}
impl Test for () {
type SomeType = \n\
}
",
); );
assert_debug_snapshot!(completions, @r###"
[
CompletionItem {
label: "type SomeType = ",
source_range: 63..64,
delete: 58..64,
insert: "type SomeType = ",
kind: TypeAlias,
lookup: "SomeType",
},
]
"###);
} }
#[test] #[test]
fn associated_const() { fn associated_const() {
let completions = complete( check_edit(
r" "SOME_CONST",
trait Test { r#"
const SOME_CONST: u16; trait Test {
} const SOME_CONST: u16;
}
impl Test for () { impl Test for () {
const S<|> const S<|>
} }
", "#,
"
trait Test {
const SOME_CONST: u16;
}
impl Test for () {
const SOME_CONST: u16 = \n\
}
",
); );
assert_debug_snapshot!(completions, @r###"
[
CompletionItem {
label: "const SOME_CONST: u16 = ",
source_range: 72..73,
delete: 66..73,
insert: "const SOME_CONST: u16 = ",
kind: Const,
lookup: "SOME_CONST",
},
]
"###);
}
#[test] check_edit(
fn associated_const_with_default() { "SOME_CONST",
let completions = complete( r#"
r" trait Test {
trait Test { const SOME_CONST: u16 = 92;
const SOME_CONST: u16 = 42; }
}
impl Test for () { impl Test for () {
const S<|> const S<|>
} }
", "#,
"
trait Test {
const SOME_CONST: u16 = 92;
}
impl Test for () {
const SOME_CONST: u16 = \n\
}
",
); );
assert_debug_snapshot!(completions, @r###"
[
CompletionItem {
label: "const SOME_CONST: u16 = ",
source_range: 77..78,
delete: 71..78,
insert: "const SOME_CONST: u16 = ",
kind: Const,
lookup: "SOME_CONST",
},
]
"###);
} }
} }