From 9f9b38bdb47f6301fc01287fbe8dc1256c44fe08 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 3 Jul 2020 23:13:22 +0200 Subject: Cleanup presentation tests --- crates/ra_ide/src/completion/presentation.rs | 193 +++++++-------------------- crates/ra_ide/src/completion/test_utils.rs | 12 +- 2 files changed, 59 insertions(+), 146 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/completion/presentation.rs b/crates/ra_ide/src/completion/presentation.rs index bd274bd74..f472b9529 100644 --- a/crates/ra_ide/src/completion/presentation.rs +++ b/crates/ra_ide/src/completion/presentation.rs @@ -460,7 +460,7 @@ mod tests { use test_utils::mark; use crate::completion::{ - test_utils::{do_completion, do_completion_with_options}, + test_utils::{check_edit, do_completion, do_completion_with_options}, CompletionConfig, CompletionItem, CompletionKind, }; @@ -636,150 +636,59 @@ fn foo() { #[test] fn inserts_parens_for_function_calls() { mark::check!(inserts_parens_for_function_calls); - assert_debug_snapshot!( - do_reference_completion( - r" - fn no_args() {} - fn main() { no_<|> } - " - ), - @r###" - [ - CompletionItem { - label: "main()", - source_range: 28..31, - delete: 28..31, - insert: "main()$0", - kind: Function, - lookup: "main", - detail: "fn main()", - }, - CompletionItem { - label: "no_args()", - source_range: 28..31, - delete: 28..31, - insert: "no_args()$0", - kind: Function, - lookup: "no_args", - detail: "fn no_args()", - }, - ] - "### - ); - assert_debug_snapshot!( - do_reference_completion( - r" - fn with_args(x: i32, y: String) {} - fn main() { with_<|> } - " - ), - @r###" - [ - CompletionItem { - label: "main()", - source_range: 47..52, - delete: 47..52, - insert: "main()$0", - kind: Function, - lookup: "main", - detail: "fn main()", - }, - CompletionItem { - label: "with_args(…)", - source_range: 47..52, - delete: 47..52, - insert: "with_args(${1:x}, ${2:y})$0", - kind: Function, - lookup: "with_args", - detail: "fn with_args(x: i32, y: String)", - trigger_call_info: true, - }, - ] - "### + check_edit( + "no_args", + r#" +fn no_args() {} +fn main() { no_<|> } +"#, + r#" +fn no_args() {} +fn main() { no_args()$0 } +"#, ); - assert_debug_snapshot!( - do_reference_completion( - r" - fn with_ignored_args(_foo: i32, ___bar: bool, ho_ge_: String) {} - fn main() { with_<|> } - " - ), - @r###" - [ - CompletionItem { - label: "main()", - source_range: 77..82, - delete: 77..82, - insert: "main()$0", - kind: Function, - lookup: "main", - detail: "fn main()", - }, - CompletionItem { - label: "with_ignored_args(…)", - source_range: 77..82, - delete: 77..82, - insert: "with_ignored_args(${1:foo}, ${2:bar}, ${3:ho_ge_})$0", - kind: Function, - lookup: "with_ignored_args", - detail: "fn with_ignored_args(_foo: i32, ___bar: bool, ho_ge_: String)", - trigger_call_info: true, - }, - ] - "### + check_edit( + "with_args", + r#" +fn with_args(x: i32, y: String) {} +fn main() { with_<|> } +"#, + r#" +fn with_args(x: i32, y: String) {} +fn main() { with_args(${1:x}, ${2:y})$0 } +"#, ); - assert_debug_snapshot!( - do_reference_completion( - r" - struct S {} - impl S { - fn foo(&self) {} - } - fn bar(s: &S) { - s.f<|> - } - " - ), - @r###" - [ - CompletionItem { - label: "foo()", - source_range: 66..67, - delete: 66..67, - insert: "foo()$0", - kind: Method, - lookup: "foo", - detail: "fn foo(&self)", - }, - ] - "### + check_edit( + "foo", + r#" +struct S; +impl S { + fn foo(&self) {} +} +fn bar(s: &S) { s.f<|> } +"#, + r#" +struct S; +impl S { + fn foo(&self) {} +} +fn bar(s: &S) { s.foo()$0 } +"#, ); - assert_debug_snapshot!( - do_reference_completion( - r" - struct S {} - impl S { - fn foo_ignored_args(&self, _a: bool, b: i32) {} - } - fn bar(s: &S) { - s.f<|> - } - " - ), - @r###" - [ - CompletionItem { - label: "foo_ignored_args(…)", - source_range: 97..98, - delete: 97..98, - insert: "foo_ignored_args(${1:a}, ${2:b})$0", - kind: Method, - lookup: "foo_ignored_args", - detail: "fn foo_ignored_args(&self, _a: bool, b: i32)", - trigger_call_info: true, - }, - ] - "### + } + + #[test] + fn strips_underscores_from_args() { + check_edit( + "foo", + r#" +fn foo(_foo: i32, ___bar: bool, ho_ge_: String) {} +fn main() { f<|> } +"#, + r#" +fn foo(_foo: i32, ___bar: bool, ho_ge_: String) {} +fn main() { foo(${1:foo}, ${2:bar}, ${3:ho_ge_})$0 } +"#, ); } diff --git a/crates/ra_ide/src/completion/test_utils.rs b/crates/ra_ide/src/completion/test_utils.rs index 9c036eac7..f25190241 100644 --- a/crates/ra_ide/src/completion/test_utils.rs +++ b/crates/ra_ide/src/completion/test_utils.rs @@ -3,7 +3,7 @@ use hir::Semantics; use itertools::Itertools; use ra_syntax::{AstNode, NodeOrToken, SyntaxElement}; -use stdx::format_to; +use stdx::{format_to, trim_indent}; use test_utils::assert_eq_text; use crate::{ @@ -57,14 +57,18 @@ pub(crate) fn completion_list_with_options( } pub(crate) fn check_edit(what: &str, ra_fixture_before: &str, ra_fixture_after: &str) { + let ra_fixture_after = trim_indent(ra_fixture_after); let (analysis, position) = analysis_and_position(ra_fixture_before); let completions: Vec = analysis.completions(&CompletionConfig::default(), position).unwrap().unwrap().into(); - let (completion,) = - completions.into_iter().filter(|it| it.label() == what).collect_tuple().unwrap(); + let (completion,) = completions + .iter() + .filter(|it| it.lookup() == what) + .collect_tuple() + .unwrap_or_else(|| panic!("can't find {:?} completion in {:#?}", what, completions)); let mut actual = analysis.file_text(position.file_id).unwrap().to_string(); completion.text_edit().apply(&mut actual); - assert_eq_text!(ra_fixture_after, &actual) + assert_eq_text!(&ra_fixture_after, &actual) } pub(crate) fn check_pattern_is_applicable(code: &str, check: fn(SyntaxElement) -> bool) { -- cgit v1.2.3