From 453167b492872643286dfcb9d540fce6f4b36209 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 4 Jul 2020 23:22:07 +0200 Subject: [PATCH] Modernise item completion tests --- .../src/completion/complete_trait_impl.rs | 464 ++++++++---------- 1 file changed, 199 insertions(+), 265 deletions(-) diff --git a/crates/ra_ide/src/completion/complete_trait_impl.rs b/crates/ra_ide/src/completion/complete_trait_impl.rs index 23e42928dd..a610fd6d16 100644 --- a/crates/ra_ide/src/completion/complete_trait_impl.rs +++ b/crates/ra_ide/src/completion/complete_trait_impl.rs @@ -227,330 +227,264 @@ fn make_const_compl_syntax(const_: &ast::ConstDef) -> String { #[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 { - 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(); - } + check( + r#" +trait Test { + type TestType; + const TEST_CONST: u16; + fn test(); +} +struct T; - struct T1; - - 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(); - } +trait Test { + fn test(); + fn test2(); +} +struct T; - struct T1; - - 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(); - } + check( + r#" +trait Test { + fn foo(); + fn foo_bar(); +} +struct T; - struct T1; - - impl Test for T1 { - fn foo() {} - - fn f<|> - } - ", +impl Test for T { + 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] fn generic_fn() { - let completions = complete( - r" - trait Test { - fn foo(); - } + check_edit( + "foo", + r#" +trait Test { + fn foo(); +} +struct T; - struct T1; +impl Test for T { + fn f<|> +} +"#, + r#" +trait Test { + fn foo(); +} +struct T; - impl Test for T1 { - fn f<|> - } - ", +impl Test for T { + fn foo() { + $0 +} +} +"#, ); - assert_debug_snapshot!(completions, @r###" - [ - CompletionItem { - label: "fn foo()", - source_range: 71..72, - delete: 68..72, - insert: "fn foo() {\n $0\n}", - kind: Function, - lookup: "foo", - }, - ] - "###); - } + check_edit( + "foo", + r#" +trait Test { + fn foo() where T: Into; +} +struct T; - #[test] - fn generic_constrait_fn() { - let completions = complete( - r" - trait Test { - fn foo() where T: Into; - } +impl Test for T { + fn f<|> +} +"#, + r#" +trait Test { + fn foo() where T: Into; +} +struct T; - struct T1; - - impl Test for T1 { - fn f<|> - } - ", +impl Test for T { + fn foo() +where T: Into { + $0 +} +} +"#, ); - assert_debug_snapshot!(completions, @r###" - [ - CompletionItem { - label: "fn foo()", - source_range: 93..94, - delete: 90..94, - insert: "fn foo()\nwhere T: Into {\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", - }, - ] - "###); } }