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)]
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<String> {
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
"###
);
}

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())
}
pub(crate) fn get_completions(code: &str, kind: CompletionKind) -> Vec<String> {
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<Compl
analysis.completions(options, position).unwrap().unwrap().into()
}
pub(crate) fn get_completions_with_options(
pub(crate) fn completion_list_with_options(
code: &str,
kind: CompletionKind,
options: &CompletionConfig,
) -> Vec<String> {
) -> String {
let mut kind_completions: Vec<CompletionItem> = 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()
}