From 03ca33406e983e7748deab09c7677c7f4cdaeeb7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 4 Jul 2020 14:53:43 +0200 Subject: Modernize tests --- crates/ra_ide/src/completion/complete_fn_param.rs | 124 +++++++++++----------- 1 file changed, 60 insertions(+), 64 deletions(-) (limited to 'crates') diff --git a/crates/ra_ide/src/completion/complete_fn_param.rs b/crates/ra_ide/src/completion/complete_fn_param.rs index f5573ddf7..9fb5c050e 100644 --- a/crates/ra_ide/src/completion/complete_fn_param.rs +++ b/crates/ra_ide/src/completion/complete_fn_param.rs @@ -54,85 +54,81 @@ pub(super) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext) #[cfg(test)] mod tests { - use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind}; - use insta::assert_debug_snapshot; + use expect::{expect, Expect}; - fn do_magic_completion(code: &str) -> Vec { - do_completion(code, CompletionKind::Magic) + use crate::completion::{test_utils::do_completion, CompletionKind}; + + fn check(ra_fixture: &str, expect: Expect) { + let actual = do_completion(ra_fixture, CompletionKind::Magic); + expect.assert_debug_eq(&actual); } #[test] fn test_param_completion_last_param() { - assert_debug_snapshot!( - do_magic_completion( - r" - fn foo(file_id: FileId) {} - fn bar(file_id: FileId) {} - fn baz(file<|>) {} - ", - ), - @r###" - [ - CompletionItem { - label: "file_id: FileId", - source_range: 61..65, - delete: 61..65, - insert: "file_id: FileId", - lookup: "file_id", - }, - ] - "### + check( + r#" +fn foo(file_id: FileId) {} +fn bar(file_id: FileId) {} +fn baz(file<|>) {} +"#, + expect![[r#" + [ + CompletionItem { + label: "file_id: FileId", + source_range: 61..65, + delete: 61..65, + insert: "file_id: FileId", + lookup: "file_id", + }, + ] + "#]], ); } #[test] fn test_param_completion_nth_param() { - assert_debug_snapshot!( - do_magic_completion( - r" - fn foo(file_id: FileId) {} - fn bar(file_id: FileId) {} - fn baz(file<|>, x: i32) {} - ", - ), - @r###" - [ - CompletionItem { - label: "file_id: FileId", - source_range: 61..65, - delete: 61..65, - insert: "file_id: FileId", - lookup: "file_id", - }, - ] - "### + check( + r#" +fn foo(file_id: FileId) {} +fn bar(file_id: FileId) {} +fn baz(file<|>, x: i32) {} +"#, + expect![[r#" + [ + CompletionItem { + label: "file_id: FileId", + source_range: 61..65, + delete: 61..65, + insert: "file_id: FileId", + lookup: "file_id", + }, + ] + "#]], ); } #[test] fn test_param_completion_trait_param() { - assert_debug_snapshot!( - do_magic_completion( - r" - pub(crate) trait SourceRoot { - pub fn contains(&self, file_id: FileId) -> bool; - pub fn module_map(&self) -> &ModuleMap; - pub fn lines(&self, file_id: FileId) -> &LineIndex; - pub fn syntax(&self, file<|>) - } - ", - ), - @r###" - [ - CompletionItem { - label: "file_id: FileId", - source_range: 208..212, - delete: 208..212, - insert: "file_id: FileId", - lookup: "file_id", - }, - ] - "### + check( + r#" +pub(crate) trait SourceRoot { + pub fn contains(&self, file_id: FileId) -> bool; + pub fn module_map(&self) -> &ModuleMap; + pub fn lines(&self, file_id: FileId) -> &LineIndex; + pub fn syntax(&self, file<|>) +} +"#, + expect![[r#" + [ + CompletionItem { + label: "file_id: FileId", + source_range: 208..212, + delete: 208..212, + insert: "file_id: FileId", + lookup: "file_id", + }, + ] + "#]], ); } } -- cgit v1.2.3 From 216e093f900d39b2e58127f66826f5bb3d33f21b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 4 Jul 2020 15:04:57 +0200 Subject: Macro tests --- .../completion/complete_macro_in_item_position.rs | 135 +++------------------ crates/ra_ide/src/completion/presentation.rs | 54 +++++++++ 2 files changed, 70 insertions(+), 119 deletions(-) (limited to 'crates') diff --git a/crates/ra_ide/src/completion/complete_macro_in_item_position.rs b/crates/ra_ide/src/completion/complete_macro_in_item_position.rs index 4c33f41d4..d6613ed7b 100644 --- a/crates/ra_ide/src/completion/complete_macro_in_item_position.rs +++ b/crates/ra_ide/src/completion/complete_macro_in_item_position.rs @@ -15,130 +15,27 @@ pub(super) fn complete_macro_in_item_position(acc: &mut Completions, ctx: &Compl #[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::completion_list, CompletionKind}; - fn do_reference_completion(code: &str) -> Vec { - do_completion(code, CompletionKind::Reference) + fn check(ra_fixture: &str, expect: Expect) { + let actual = completion_list(ra_fixture, CompletionKind::Reference); + expect.assert_eq(&actual) } #[test] fn completes_macros_as_item() { - assert_debug_snapshot!( - do_reference_completion( - " - //- /main.rs - macro_rules! foo { - () => {} - } - - fn foo() {} - - <|> - " - ), - @r###" - [ - CompletionItem { - label: "foo!(…)", - source_range: 48..48, - delete: 48..48, - insert: "foo!($0)", - kind: Macro, - detail: "macro_rules! foo", - }, - ] - "### - ); - } - - #[test] - fn completes_vec_macros_with_square_brackets() { - assert_debug_snapshot!( - do_reference_completion( - " - //- /main.rs - /// Creates a [`Vec`] containing the arguments. - /// - /// - Create a [`Vec`] containing a given list of elements: - /// - /// ``` - /// let v = vec![1, 2, 3]; - /// assert_eq!(v[0], 1); - /// assert_eq!(v[1], 2); - /// assert_eq!(v[2], 3); - /// ``` - macro_rules! vec { - () => {} - } - - fn foo() {} - - <|> - " - ), - @r###" - [ - CompletionItem { - label: "vec![…]", - source_range: 282..282, - delete: 282..282, - insert: "vec![$0]", - kind: Macro, - detail: "macro_rules! vec", - documentation: Documentation( - "Creates a [`Vec`] containing the arguments.\n\n- Create a [`Vec`] containing a given list of elements:\n\n```\nlet v = vec![1, 2, 3];\nassert_eq!(v[0], 1);\nassert_eq!(v[1], 2);\nassert_eq!(v[2], 3);\n```", - ), - }, - ] - "### - ); - } - - #[test] - fn completes_macros_braces_guessing() { - assert_debug_snapshot!( - do_reference_completion( - " - //- /main.rs - /// Foo - /// - /// Not call `fooo!()` `fooo!()`, or `_foo![]` `_foo![]`. - /// Call as `let _=foo! { hello world };` - macro_rules! foo { - () => {} - } - - fn main() { - <|> - } - " - ), - @r###" - [ - CompletionItem { - label: "foo! {…}", - source_range: 164..164, - delete: 164..164, - insert: "foo! {$0}", - kind: Macro, - detail: "macro_rules! foo", - documentation: Documentation( - "Foo\n\nNot call `fooo!()` `fooo!()`, or `_foo![]` `_foo![]`.\nCall as `let _=foo! { hello world };`", - ), - }, - CompletionItem { - label: "main()", - source_range: 164..164, - delete: 164..164, - insert: "main()$0", - kind: Function, - lookup: "main", - detail: "fn main()", - }, - ] - "### - ); + check( + r#" +macro_rules! foo { () => {} } +fn foo() {} + +<|> +"#, + expect![[r#" + ma foo!(…) macro_rules! foo + "#]], + ) } } diff --git a/crates/ra_ide/src/completion/presentation.rs b/crates/ra_ide/src/completion/presentation.rs index dc391c46b..946bbef7c 100644 --- a/crates/ra_ide/src/completion/presentation.rs +++ b/crates/ra_ide/src/completion/presentation.rs @@ -174,6 +174,7 @@ impl Completions { builder .insert_snippet(cap, format!("{}!{}$0{}", name, bra, ket)) .label(format!("{}!{}…{}", name, bra, ket)) + .lookup_by(format!("{}!", name)) } None if needs_bang => builder.insert_text(format!("{}!", name)), _ => { @@ -1079,4 +1080,57 @@ fn go(world: &WorldSnapshot) { go(w<|>) } "#]], ); } + + #[test] + fn guesses_macro_braces() { + check_edit( + "vec!", + r#" +/// Creates a [`Vec`] containing the arguments. +/// +/// ``` +/// let v = vec![1, 2, 3]; +/// assert_eq!(v[0], 1); +/// assert_eq!(v[1], 2); +/// assert_eq!(v[2], 3); +/// ``` +macro_rules! vec { () => {} } + +fn fn main() { v<|> } +"#, + r#" +/// Creates a [`Vec`] containing the arguments. +/// +/// ``` +/// let v = vec![1, 2, 3]; +/// assert_eq!(v[0], 1); +/// assert_eq!(v[1], 2); +/// assert_eq!(v[2], 3); +/// ``` +macro_rules! vec { () => {} } + +fn fn main() { vec![$0] } +"#, + ); + + check_edit( + "foo!", + r#" +/// Foo +/// +/// Don't call `fooo!()` `fooo!()`, or `_foo![]` `_foo![]`, +/// call as `let _=foo! { hello world };` +macro_rules! foo { () => {} } +fn main() { <|> } +"#, + r#" +/// Foo +/// +/// Don't call `fooo!()` `fooo!()`, or `_foo![]` `_foo![]`, +/// call as `let _=foo! { hello world };` +macro_rules! foo { () => {} } +fn main() { foo! {$0} } +"#, + ) + } } -- cgit v1.2.3 From 9dacd2338cbc7b414fc7a22be108500abfa826f4 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 4 Jul 2020 15:05:33 +0200 Subject: Fix lookup in tests --- crates/ra_ide/src/completion/complete_pattern.rs | 1 + crates/ra_ide/src/completion/complete_qualified_path.rs | 1 + crates/ra_ide/src/completion/complete_unqualified_path.rs | 9 +++++++++ 3 files changed, 11 insertions(+) (limited to 'crates') diff --git a/crates/ra_ide/src/completion/complete_pattern.rs b/crates/ra_ide/src/completion/complete_pattern.rs index 367e2bbce..9b79e7a09 100644 --- a/crates/ra_ide/src/completion/complete_pattern.rs +++ b/crates/ra_ide/src/completion/complete_pattern.rs @@ -130,6 +130,7 @@ mod tests { delete: 90..90, insert: "m!($0)", kind: Macro, + lookup: "m!", detail: "macro_rules! m", }, ] diff --git a/crates/ra_ide/src/completion/complete_qualified_path.rs b/crates/ra_ide/src/completion/complete_qualified_path.rs index a16866cd2..5175c8afe 100644 --- a/crates/ra_ide/src/completion/complete_qualified_path.rs +++ b/crates/ra_ide/src/completion/complete_qualified_path.rs @@ -1189,6 +1189,7 @@ mod tests { delete: 82..82, insert: "foo!($0)", kind: Macro, + lookup: "foo!", detail: "#[macro_export]\nmacro_rules! foo", }, CompletionItem { diff --git a/crates/ra_ide/src/completion/complete_unqualified_path.rs b/crates/ra_ide/src/completion/complete_unqualified_path.rs index a0a04bb58..0d1e9f8ea 100644 --- a/crates/ra_ide/src/completion/complete_unqualified_path.rs +++ b/crates/ra_ide/src/completion/complete_unqualified_path.rs @@ -785,6 +785,7 @@ mod tests { delete: 256..256, insert: "bar!($0)", kind: Macro, + lookup: "bar!", detail: "macro_rules! bar", }, CompletionItem { @@ -793,6 +794,7 @@ mod tests { delete: 256..256, insert: "baz!($0)", kind: Macro, + lookup: "baz!", detail: "#[macro_export]\nmacro_rules! baz", }, CompletionItem { @@ -801,6 +803,7 @@ mod tests { delete: 256..256, insert: "foo!($0)", kind: Macro, + lookup: "foo!", detail: "macro_rules! foo", }, CompletionItem { @@ -854,6 +857,7 @@ mod tests { delete: 50..50, insert: "foo!($0)", kind: Macro, + lookup: "foo!", detail: "macro_rules! foo", }, CompletionItem { @@ -893,6 +897,7 @@ mod tests { delete: 58..58, insert: "foo!($0)", kind: Macro, + lookup: "foo!", detail: "macro_rules! foo", }, CompletionItem { @@ -932,6 +937,7 @@ mod tests { delete: 51..51, insert: "foo!($0)", kind: Macro, + lookup: "foo!", detail: "macro_rules! foo", }, CompletionItem { @@ -1005,6 +1011,7 @@ mod tests { delete: 80..80, insert: "m!($0)", kind: Macro, + lookup: "m!", detail: "macro_rules! m", }, CompletionItem { @@ -1058,6 +1065,7 @@ mod tests { delete: 80..81, insert: "m!($0)", kind: Macro, + lookup: "m!", detail: "macro_rules! m", }, CompletionItem { @@ -1111,6 +1119,7 @@ mod tests { delete: 80..81, insert: "m!($0)", kind: Macro, + lookup: "m!", detail: "macro_rules! m", }, CompletionItem { -- cgit v1.2.3