More concise completion tests

This commit is contained in:
Aleksey Kladov 2020-06-13 13:57:18 +02:00
parent ef70076f1d
commit b99b4953c9
2 changed files with 146 additions and 182 deletions

View file

@ -170,131 +170,119 @@ fn complete_return(
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::completion::{test_utils::get_completions, CompletionKind}; use crate::completion::{test_utils::completion_list, CompletionKind};
use insta::assert_debug_snapshot; use insta::assert_snapshot;
fn get_keyword_completions(code: &str) -> Vec<String> { fn get_keyword_completions(code: &str) -> String {
get_completions(code, CompletionKind::Keyword) completion_list(code, CompletionKind::Keyword)
} }
#[test] #[test]
fn test_keywords_in_use_stmt() { fn test_keywords_in_use_stmt() {
assert_debug_snapshot!( assert_snapshot!(
get_keyword_completions(r"use <|>"), get_keyword_completions(r"use <|>"),
@r###" @r###"
[ kw crate
"kw crate", kw self
"kw self", kw super
"kw super",
]
"### "###
); );
assert_debug_snapshot!( assert_snapshot!(
get_keyword_completions(r"use a::<|>"), get_keyword_completions(r"use a::<|>"),
@r###" @r###"
[ kw self
"kw self", kw super
"kw super",
]
"### "###
); );
assert_debug_snapshot!( assert_snapshot!(
get_keyword_completions(r"use a::{b, <|>}"), get_keyword_completions(r"use a::{b, <|>}"),
@r###" @r###"
[ kw self
"kw self", kw super
"kw super",
]
"### "###
); );
} }
#[test] #[test]
fn test_keywords_at_source_file_level() { fn test_keywords_at_source_file_level() {
assert_debug_snapshot!( assert_snapshot!(
get_keyword_completions(r"m<|>"), get_keyword_completions(r"m<|>"),
@r###" @r###"
[ kw const
"kw const", kw enum
"kw enum", kw extern
"kw extern", kw fn
"kw fn", kw impl
"kw impl", kw mod
"kw mod", kw pub
"kw pub", kw static
"kw static", kw struct
"kw struct", kw trait
"kw trait", kw type
"kw type", kw union
"kw union", kw unsafe
"kw unsafe", kw use
"kw use",
]
"### "###
); );
} }
#[test] #[test]
fn test_keywords_in_function() { fn test_keywords_in_function() {
assert_debug_snapshot!( assert_snapshot!(
get_keyword_completions(r"fn quux() { <|> }"), get_keyword_completions(r"fn quux() { <|> }"),
@r###" @r###"
[ kw const
"kw const", kw extern
"kw extern", kw fn
"kw fn", kw if
"kw if", kw if let
"kw if let", kw impl
"kw impl", kw let
"kw let", kw loop
"kw loop", kw match
"kw match", kw mod
"kw mod", kw return
"kw return", kw static
"kw static", kw trait
"kw trait", kw type
"kw type", kw unsafe
"kw unsafe", kw use
"kw use", kw while
"kw while",
]
"### "###
); );
} }
#[test] #[test]
fn test_keywords_inside_block() { fn test_keywords_inside_block() {
assert_debug_snapshot!( assert_snapshot!(
get_keyword_completions(r"fn quux() { if true { <|> } }"), get_keyword_completions(r"fn quux() { if true { <|> } }"),
@r###" @r###"
[ kw const
"kw const", kw extern
"kw extern", kw fn
"kw fn", kw if
"kw if", kw if let
"kw if let", kw impl
"kw impl", kw let
"kw let", kw loop
"kw loop", kw match
"kw match", kw mod
"kw mod", kw return
"kw return", kw static
"kw static", kw trait
"kw trait", kw type
"kw type", kw unsafe
"kw unsafe", kw use
"kw use", kw while
"kw while",
]
"### "###
); );
} }
#[test] #[test]
fn test_keywords_after_if() { fn test_keywords_after_if() {
assert_debug_snapshot!( assert_snapshot!(
get_keyword_completions( get_keyword_completions(
r" r"
fn quux() { fn quux() {
@ -305,34 +293,32 @@ mod tests {
", ",
), ),
@r###" @r###"
[ kw const
"kw const", kw else
"kw else", kw else if
"kw else if", kw extern
"kw extern", kw fn
"kw fn", kw if
"kw if", kw if let
"kw if let", kw impl
"kw impl", kw let
"kw let", kw loop
"kw loop", kw match
"kw match", kw mod
"kw mod", kw return
"kw return", kw static
"kw static", kw trait
"kw trait", kw type
"kw type", kw unsafe
"kw unsafe", kw use
"kw use", kw while
"kw while",
]
"### "###
); );
} }
#[test] #[test]
fn test_keywords_in_match_arm() { fn test_keywords_in_match_arm() {
assert_debug_snapshot!( assert_snapshot!(
get_keyword_completions( get_keyword_completions(
r" r"
fn quux() -> i32 { fn quux() -> i32 {
@ -343,151 +329,129 @@ mod tests {
", ",
), ),
@r###" @r###"
[ kw if
"kw if", kw if let
"kw if let", kw loop
"kw loop", kw match
"kw match", kw return
"kw return", kw unsafe
"kw unsafe",
]
"### "###
); );
} }
#[test] #[test]
fn test_keywords_in_trait_def() { fn test_keywords_in_trait_def() {
assert_debug_snapshot!( assert_snapshot!(
get_keyword_completions(r"trait My { <|> }"), get_keyword_completions(r"trait My { <|> }"),
@r###" @r###"
[ kw const
"kw const", kw fn
"kw fn", kw type
"kw type", kw unsafe
"kw unsafe",
]
"### "###
); );
} }
#[test] #[test]
fn test_keywords_in_impl_def() { fn test_keywords_in_impl_def() {
assert_debug_snapshot!( assert_snapshot!(
get_keyword_completions(r"impl My { <|> }"), get_keyword_completions(r"impl My { <|> }"),
@r###" @r###"
[ kw const
"kw const", kw fn
"kw fn", kw pub
"kw pub", kw type
"kw type", kw unsafe
"kw unsafe",
]
"### "###
); );
} }
#[test] #[test]
fn test_keywords_in_loop() { fn test_keywords_in_loop() {
assert_debug_snapshot!( assert_snapshot!(
get_keyword_completions(r"fn my() { loop { <|> } }"), get_keyword_completions(r"fn my() { loop { <|> } }"),
@r###" @r###"
[ kw break
"kw break", kw const
"kw const", kw continue
"kw continue", kw extern
"kw extern", kw fn
"kw fn", kw if
"kw if", kw if let
"kw if let", kw impl
"kw impl", kw let
"kw let", kw loop
"kw loop", kw match
"kw match", kw mod
"kw mod", kw return
"kw return", kw static
"kw static", kw trait
"kw trait", kw type
"kw type", kw unsafe
"kw unsafe", kw use
"kw use", kw while
"kw while",
]
"### "###
); );
} }
#[test] #[test]
fn test_keywords_after_unsafe_in_item_list() { fn test_keywords_after_unsafe_in_item_list() {
assert_debug_snapshot!( assert_snapshot!(
get_keyword_completions(r"unsafe <|>"), get_keyword_completions(r"unsafe <|>"),
@r###" @r###"
[ kw fn
"kw fn", kw impl
"kw impl", kw trait
"kw trait",
]
"### "###
); );
} }
#[test] #[test]
fn test_keywords_after_unsafe_in_block_expr() { fn test_keywords_after_unsafe_in_block_expr() {
assert_debug_snapshot!( assert_snapshot!(
get_keyword_completions(r"fn my_fn() { unsafe <|> }"), get_keyword_completions(r"fn my_fn() { unsafe <|> }"),
@r###" @r###"
[ kw fn
"kw fn", kw impl
"kw impl", kw trait
"kw trait",
]
"### "###
); );
} }
#[test] #[test]
fn test_mut_in_ref_and_in_fn_parameters_list() { fn test_mut_in_ref_and_in_fn_parameters_list() {
assert_debug_snapshot!( assert_snapshot!(
get_keyword_completions(r"fn my_fn(&<|>) {}"), get_keyword_completions(r"fn my_fn(&<|>) {}"),
@r###" @r###"
[ kw mut
"kw mut",
]
"### "###
); );
assert_debug_snapshot!( assert_snapshot!(
get_keyword_completions(r"fn my_fn(<|>) {}"), get_keyword_completions(r"fn my_fn(<|>) {}"),
@r###" @r###"
[ kw mut
"kw mut",
]
"### "###
); );
assert_debug_snapshot!( assert_snapshot!(
get_keyword_completions(r"fn my_fn() { let &<|> }"), get_keyword_completions(r"fn my_fn() { let &<|> }"),
@r###" @r###"
[ kw mut
"kw mut",
]
"### "###
); );
} }
#[test] #[test]
fn test_where_keyword() { fn test_where_keyword() {
assert_debug_snapshot!( assert_snapshot!(
get_keyword_completions(r"trait A <|>"), get_keyword_completions(r"trait A <|>"),
@r###" @r###"
[ kw where
"kw where",
]
"### "###
); );
assert_debug_snapshot!( assert_snapshot!(
get_keyword_completions(r"impl A <|>"), get_keyword_completions(r"impl A <|>"),
@r###" @r###"
[ kw where
"kw where",
]
"### "###
); );
} }

View file

@ -12,8 +12,8 @@ pub(crate) fn do_completion(code: &str, kind: CompletionKind) -> Vec<CompletionI
do_completion_with_options(code, kind, &CompletionConfig::default()) do_completion_with_options(code, kind, &CompletionConfig::default())
} }
pub(crate) fn get_completions(code: &str, kind: CompletionKind) -> Vec<String> { pub(crate) fn completion_list(code: &str, kind: CompletionKind) -> String {
get_completions_with_options(code, kind, &CompletionConfig::default()) completion_list_with_options(code, kind, &CompletionConfig::default())
} }
pub(crate) fn do_completion_with_options( pub(crate) fn do_completion_with_options(
@ -38,11 +38,11 @@ fn get_all_completion_items(code: &str, options: &CompletionConfig) -> Vec<Compl
analysis.completions(options, position).unwrap().unwrap().into() analysis.completions(options, position).unwrap().unwrap().into()
} }
pub(crate) fn get_completions_with_options( pub(crate) fn completion_list_with_options(
code: &str, code: &str,
kind: CompletionKind, kind: CompletionKind,
options: &CompletionConfig, options: &CompletionConfig,
) -> Vec<String> { ) -> String {
let mut kind_completions: Vec<CompletionItem> = get_all_completion_items(code, options) let mut kind_completions: Vec<CompletionItem> = get_all_completion_items(code, options)
.into_iter() .into_iter()
.filter(|c| c.completion_kind == kind) .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.sort_by_key(|c| c.label().to_owned());
kind_completions kind_completions
.into_iter() .into_iter()
.map(|it| format!("{} {}", it.kind().unwrap().tag(), it.label())) .map(|it| format!("{} {}\n", it.kind().unwrap().tag(), it.label()))
.collect() .collect()
} }