From 503de8e229473802b92ecd120430c12f6c8b49eb Mon Sep 17 00:00:00 2001
From: Aleksey Kladov <aleksey.kladov@gmail.com>
Date: Fri, 3 Jul 2020 13:21:14 +0200
Subject: Add function to test completion edit

---
 crates/ra_ide/src/completion/complete_keyword.rs | 10 +++++++++-
 crates/ra_ide/src/completion/test_utils.rs       | 13 +++++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)

(limited to 'crates')

diff --git a/crates/ra_ide/src/completion/complete_keyword.rs b/crates/ra_ide/src/completion/complete_keyword.rs
index c3c6eda33..34d061f5a 100644
--- a/crates/ra_ide/src/completion/complete_keyword.rs
+++ b/crates/ra_ide/src/completion/complete_keyword.rs
@@ -176,7 +176,10 @@ fn complete_return(
 mod tests {
     use expect::{expect, Expect};
 
-    use crate::completion::{test_utils::completion_list, CompletionKind};
+    use crate::completion::{
+        test_utils::{check_edit, completion_list},
+        CompletionKind,
+    };
 
     fn check(ra_fixture: &str, expect: Expect) {
         let actual = completion_list(ra_fixture, CompletionKind::Keyword);
@@ -312,6 +315,11 @@ mod tests {
                 kw while
             "#]],
         );
+        check_edit(
+            "else",
+            r#"fn quux() { if true { () } <|> }"#,
+            r#"fn quux() { if true { () } else {$0} }"#,
+        );
     }
 
     #[test]
diff --git a/crates/ra_ide/src/completion/test_utils.rs b/crates/ra_ide/src/completion/test_utils.rs
index 5938415b3..9c036eac7 100644
--- a/crates/ra_ide/src/completion/test_utils.rs
+++ b/crates/ra_ide/src/completion/test_utils.rs
@@ -1,8 +1,10 @@
 //! Runs completion for testing purposes.
 
 use hir::Semantics;
+use itertools::Itertools;
 use ra_syntax::{AstNode, NodeOrToken, SyntaxElement};
 use stdx::format_to;
+use test_utils::assert_eq_text;
 
 use crate::{
     completion::{completion_item::CompletionKind, CompletionConfig},
@@ -54,6 +56,17 @@ pub(crate) fn completion_list_with_options(
         .collect()
 }
 
+pub(crate) fn check_edit(what: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
+    let (analysis, position) = analysis_and_position(ra_fixture_before);
+    let completions: Vec<CompletionItem> =
+        analysis.completions(&CompletionConfig::default(), position).unwrap().unwrap().into();
+    let (completion,) =
+        completions.into_iter().filter(|it| it.label() == what).collect_tuple().unwrap();
+    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)
+}
+
 pub(crate) fn check_pattern_is_applicable(code: &str, check: fn(SyntaxElement) -> bool) {
     let (analysis, pos) = analysis_and_position(code);
     analysis
-- 
cgit v1.2.3