#[cfg(test)]
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> {
- do_completion(code, CompletionKind::Magic)
+ fn check(ra_fixture: &str, expect: Expect) {
+ let actual = completion_list(ra_fixture, CompletionKind::Magic);
+ expect.assert_eq(&actual)
}
#[test]
fn name_ref_function_type_const() {
- let completions = complete(
- r"
- trait Test {
- type TestType;
- const TEST_CONST: u16;
- fn test();
- }
-
- struct T1;
+ check(
+ r#"
+trait Test {
+ type TestType;
+ const TEST_CONST: u16;
+ fn test();
+}
+struct T;
- impl Test for T1 {
- t<|>
- }
- ",
+impl Test for T {
+ 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]
fn no_nested_fn_completions() {
- let completions = complete(
+ check(
r"
- trait Test {
- fn test();
- fn test2();
- }
-
- struct T1;
+trait Test {
+ fn test();
+ fn test2();
+}
+struct T;
- impl Test for T1 {
- fn test() {
- t<|>
- }
- }
- ",
+impl Test for T {
+ fn test() {
+ t<|>
+ }
+}
+",
+ expect![[""]],
);
- assert_debug_snapshot!(completions, @r###"[]"###);
}
#[test]
fn name_ref_single_function() {
- let completions = complete(
- r"
- trait Test {
- fn test();
- }
+ check_edit(
+ "test",
+ r#"
+trait Test {
+ fn test();
+}
+struct T;
- struct T1;
+impl Test for T {
+ t<|>
+}
+"#,
+ r#"
+trait Test {
+ fn test();
+}
+struct T;
- impl Test for T1 {
- t<|>
- }
- ",
+impl Test for 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]
fn single_function() {
- let completions = complete(
- r"
- trait Test {
- fn foo();
- }
+ check_edit(
+ "test",
+ r#"
+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 {
- fn f<|>
- }
- ",
+impl Test for T {
+ 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]
fn hide_implemented_fn() {
- let completions = complete(
- r"
- trait Test {
- fn foo();
- fn foo_bar();
- }
-
- struct T1;
-
- impl Test for T1 {
- fn foo() {}
-
- fn f<|>
- }
- ",
- );
- 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;
+ check(
+ r#"
+trait Test {
+ fn foo();
+ fn foo_bar();
+}
+struct T;
- impl Test for T1 {
- fn foo() {
- <|>
- }
- }
- ",
+impl Test for T {
+ fn foo() {}
+ fn f<|>
+}
+"#,
+ expect![[r#"
+ fn fn foo_bar()
+ "#]],
);
- assert_debug_snapshot!(completions, @r###"[]"###);
}
#[test]
fn generic_fn() {
- let completions = complete(
- r"
- trait Test {
- fn foo<T>();
- }
+ check_edit(
+ "foo",
+ r#"
+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 {
- fn f<|>
- }
- ",
+impl Test for T {
+ fn foo<T>() {
+ $0
+}
+}
+"#,
);
- assert_debug_snapshot!(completions, @r###"
- [
- CompletionItem {
- label: "fn foo()",
- source_range: 71..72,
- delete: 68..72,
- insert: "fn foo<T>() {\n $0\n}",
- kind: Function,
- lookup: "foo",
- },
- ]
- "###);
- }
-
- #[test]
- fn generic_constrait_fn() {
- let completions = complete(
- r"
- trait Test {
- fn foo<T>() where T: Into<String>;
- }
+ check_edit(
+ "foo",
+ r#"
+trait Test {
+ fn foo<T>() where T: Into<String>;
+}
+struct T;
- struct T1;
+impl Test for T {
+ fn f<|>
+}
+"#,
+ r#"
+trait Test {
+ fn foo<T>() where T: Into<String>;
+}
+struct T;
- impl Test for T1 {
- fn f<|>
- }
- ",
+impl Test for T {
+ fn foo<T>()
+where T: Into<String> {
+ $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]
fn associated_type() {
- let completions = complete(
- r"
- trait Test {
- type SomeType;
- }
+ check_edit(
+ "SomeType",
+ r#"
+trait Test {
+ type SomeType;
+}
- impl Test for () {
- type S<|>
- }
- ",
+impl Test for () {
+ 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]
fn associated_const() {
- let completions = complete(
- r"
- trait Test {
- const SOME_CONST: u16;
- }
+ check_edit(
+ "SOME_CONST",
+ r#"
+trait Test {
+ const SOME_CONST: u16;
+}
- impl Test for () {
- const S<|>
- }
- ",
+impl Test for () {
+ 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]
- fn associated_const_with_default() {
- let completions = complete(
- r"
- trait Test {
- const SOME_CONST: u16 = 42;
- }
+ check_edit(
+ "SOME_CONST",
+ r#"
+trait Test {
+ const SOME_CONST: u16 = 92;
+}
- impl Test for () {
- const S<|>
- }
- ",
+impl Test for () {
+ 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",
- },
- ]
- "###);
}
}