From b99b4953c9a4791f7f39ab208a1842086ad0f04c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 13 Jun 2020 13:57:18 +0200 Subject: More concise completion tests --- crates/ra_ide/src/completion/complete_keyword.rs | 318 ++++++++++------------- crates/ra_ide/src/completion/test_utils.rs | 10 +- 2 files changed, 146 insertions(+), 182 deletions(-) (limited to 'crates/ra_ide/src/completion') diff --git a/crates/ra_ide/src/completion/complete_keyword.rs b/crates/ra_ide/src/completion/complete_keyword.rs index 0ab330b73..b2f621a11 100644 --- a/crates/ra_ide/src/completion/complete_keyword.rs +++ b/crates/ra_ide/src/completion/complete_keyword.rs @@ -170,131 +170,119 @@ fn complete_return( #[cfg(test)] mod tests { - use crate::completion::{test_utils::get_completions, CompletionKind}; - use insta::assert_debug_snapshot; + use crate::completion::{test_utils::completion_list, CompletionKind}; + use insta::assert_snapshot; - fn get_keyword_completions(code: &str) -> Vec { - get_completions(code, CompletionKind::Keyword) + fn get_keyword_completions(code: &str) -> String { + completion_list(code, CompletionKind::Keyword) } #[test] fn test_keywords_in_use_stmt() { - assert_debug_snapshot!( + assert_snapshot!( get_keyword_completions(r"use <|>"), @r###" - [ - "kw crate", - "kw self", - "kw super", - ] + kw crate + kw self + kw super "### ); - assert_debug_snapshot!( + assert_snapshot!( get_keyword_completions(r"use a::<|>"), @r###" - [ - "kw self", - "kw super", - ] + kw self + kw super "### ); - assert_debug_snapshot!( + assert_snapshot!( get_keyword_completions(r"use a::{b, <|>}"), @r###" - [ - "kw self", - "kw super", - ] + kw self + kw super "### ); } #[test] fn test_keywords_at_source_file_level() { - assert_debug_snapshot!( + assert_snapshot!( get_keyword_completions(r"m<|>"), @r###" - [ - "kw const", - "kw enum", - "kw extern", - "kw fn", - "kw impl", - "kw mod", - "kw pub", - "kw static", - "kw struct", - "kw trait", - "kw type", - "kw union", - "kw unsafe", - "kw use", - ] + kw const + kw enum + kw extern + kw fn + kw impl + kw mod + kw pub + kw static + kw struct + kw trait + kw type + kw union + kw unsafe + kw use "### ); } #[test] fn test_keywords_in_function() { - assert_debug_snapshot!( + assert_snapshot!( get_keyword_completions(r"fn quux() { <|> }"), @r###" - [ - "kw const", - "kw extern", - "kw fn", - "kw if", - "kw if let", - "kw impl", - "kw let", - "kw loop", - "kw match", - "kw mod", - "kw return", - "kw static", - "kw trait", - "kw type", - "kw unsafe", - "kw use", - "kw while", - ] + kw const + kw extern + kw fn + kw if + kw if let + kw impl + kw let + kw loop + kw match + kw mod + kw return + kw static + kw trait + kw type + kw unsafe + kw use + kw while "### ); } #[test] fn test_keywords_inside_block() { - assert_debug_snapshot!( + assert_snapshot!( get_keyword_completions(r"fn quux() { if true { <|> } }"), @r###" - [ - "kw const", - "kw extern", - "kw fn", - "kw if", - "kw if let", - "kw impl", - "kw let", - "kw loop", - "kw match", - "kw mod", - "kw return", - "kw static", - "kw trait", - "kw type", - "kw unsafe", - "kw use", - "kw while", - ] + kw const + kw extern + kw fn + kw if + kw if let + kw impl + kw let + kw loop + kw match + kw mod + kw return + kw static + kw trait + kw type + kw unsafe + kw use + kw while "### ); } #[test] fn test_keywords_after_if() { - assert_debug_snapshot!( + assert_snapshot!( get_keyword_completions( r" fn quux() { @@ -305,34 +293,32 @@ mod tests { ", ), @r###" - [ - "kw const", - "kw else", - "kw else if", - "kw extern", - "kw fn", - "kw if", - "kw if let", - "kw impl", - "kw let", - "kw loop", - "kw match", - "kw mod", - "kw return", - "kw static", - "kw trait", - "kw type", - "kw unsafe", - "kw use", - "kw while", - ] + kw const + kw else + kw else if + kw extern + kw fn + kw if + kw if let + kw impl + kw let + kw loop + kw match + kw mod + kw return + kw static + kw trait + kw type + kw unsafe + kw use + kw while "### ); } #[test] fn test_keywords_in_match_arm() { - assert_debug_snapshot!( + assert_snapshot!( get_keyword_completions( r" fn quux() -> i32 { @@ -343,151 +329,129 @@ mod tests { ", ), @r###" - [ - "kw if", - "kw if let", - "kw loop", - "kw match", - "kw return", - "kw unsafe", - ] + kw if + kw if let + kw loop + kw match + kw return + kw unsafe "### ); } #[test] fn test_keywords_in_trait_def() { - assert_debug_snapshot!( + assert_snapshot!( get_keyword_completions(r"trait My { <|> }"), @r###" - [ - "kw const", - "kw fn", - "kw type", - "kw unsafe", - ] + kw const + kw fn + kw type + kw unsafe "### ); } #[test] fn test_keywords_in_impl_def() { - assert_debug_snapshot!( + assert_snapshot!( get_keyword_completions(r"impl My { <|> }"), @r###" - [ - "kw const", - "kw fn", - "kw pub", - "kw type", - "kw unsafe", - ] + kw const + kw fn + kw pub + kw type + kw unsafe "### ); } #[test] fn test_keywords_in_loop() { - assert_debug_snapshot!( + assert_snapshot!( get_keyword_completions(r"fn my() { loop { <|> } }"), @r###" - [ - "kw break", - "kw const", - "kw continue", - "kw extern", - "kw fn", - "kw if", - "kw if let", - "kw impl", - "kw let", - "kw loop", - "kw match", - "kw mod", - "kw return", - "kw static", - "kw trait", - "kw type", - "kw unsafe", - "kw use", - "kw while", - ] + kw break + kw const + kw continue + kw extern + kw fn + kw if + kw if let + kw impl + kw let + kw loop + kw match + kw mod + kw return + kw static + kw trait + kw type + kw unsafe + kw use + kw while "### ); } #[test] fn test_keywords_after_unsafe_in_item_list() { - assert_debug_snapshot!( + assert_snapshot!( get_keyword_completions(r"unsafe <|>"), @r###" - [ - "kw fn", - "kw impl", - "kw trait", - ] + kw fn + kw impl + kw trait "### ); } #[test] fn test_keywords_after_unsafe_in_block_expr() { - assert_debug_snapshot!( + assert_snapshot!( get_keyword_completions(r"fn my_fn() { unsafe <|> }"), @r###" - [ - "kw fn", - "kw impl", - "kw trait", - ] + kw fn + kw impl + kw trait "### ); } #[test] fn test_mut_in_ref_and_in_fn_parameters_list() { - assert_debug_snapshot!( + assert_snapshot!( get_keyword_completions(r"fn my_fn(&<|>) {}"), @r###" - [ - "kw mut", - ] + kw mut "### ); - assert_debug_snapshot!( + assert_snapshot!( get_keyword_completions(r"fn my_fn(<|>) {}"), @r###" - [ - "kw mut", - ] + kw mut "### ); - assert_debug_snapshot!( + assert_snapshot!( get_keyword_completions(r"fn my_fn() { let &<|> }"), @r###" - [ - "kw mut", - ] + kw mut "### ); } #[test] fn test_where_keyword() { - assert_debug_snapshot!( + assert_snapshot!( get_keyword_completions(r"trait A <|>"), @r###" - [ - "kw where", - ] + kw where "### ); - assert_debug_snapshot!( + assert_snapshot!( get_keyword_completions(r"impl A <|>"), @r###" - [ - "kw where", - ] + kw where "### ); } diff --git a/crates/ra_ide/src/completion/test_utils.rs b/crates/ra_ide/src/completion/test_utils.rs index ba724116d..1e16a43ca 100644 --- a/crates/ra_ide/src/completion/test_utils.rs +++ b/crates/ra_ide/src/completion/test_utils.rs @@ -12,8 +12,8 @@ pub(crate) fn do_completion(code: &str, kind: CompletionKind) -> Vec Vec { - get_completions_with_options(code, kind, &CompletionConfig::default()) +pub(crate) fn completion_list(code: &str, kind: CompletionKind) -> String { + completion_list_with_options(code, kind, &CompletionConfig::default()) } pub(crate) fn do_completion_with_options( @@ -38,11 +38,11 @@ fn get_all_completion_items(code: &str, options: &CompletionConfig) -> Vec Vec { +) -> String { let mut kind_completions: Vec = get_all_completion_items(code, options) .into_iter() .filter(|c| c.completion_kind == kind) @@ -50,7 +50,7 @@ pub(crate) fn get_completions_with_options( kind_completions.sort_by_key(|c| c.label().to_owned()); kind_completions .into_iter() - .map(|it| format!("{} {}", it.kind().unwrap().tag(), it.label())) + .map(|it| format!("{} {}\n", it.kind().unwrap().tag(), it.label())) .collect() } -- cgit v1.2.3