Refactor keyword completion tests

This commit is contained in:
Aleksey Kladov 2020-07-03 12:51:18 +02:00
parent 81bb3d9c1a
commit e2b04621f9

View file

@ -174,46 +174,48 @@ fn complete_return(
#[cfg(test)]
mod tests {
use crate::completion::{test_utils::completion_list, CompletionKind};
use insta::assert_snapshot;
use expect::{expect, Expect};
fn get_keyword_completions(code: &str) -> String {
completion_list(code, CompletionKind::Keyword)
use crate::completion::{test_utils::completion_list, CompletionKind};
fn check(ra_fixture: &str, expect: Expect) {
let actual = completion_list(ra_fixture, CompletionKind::Keyword);
expect.assert_eq(&actual)
}
#[test]
fn test_keywords_in_use_stmt() {
assert_snapshot!(
get_keyword_completions(r"use <|>"),
@r###"
check(
r"use <|>",
expect![[r#"
kw crate::
kw self
kw super::
"###
"#]],
);
assert_snapshot!(
get_keyword_completions(r"use a::<|>"),
@r###"
check(
r"use a::<|>",
expect![[r#"
kw self
kw super::
"###
"#]],
);
assert_snapshot!(
get_keyword_completions(r"use a::{b, <|>}"),
@r###"
check(
r"use a::{b, <|>}",
expect![[r#"
kw self
kw super::
"###
"#]],
);
}
#[test]
fn test_keywords_at_source_file_level() {
assert_snapshot!(
get_keyword_completions(r"m<|>"),
@r###"
check(
r"m<|>",
expect![[r#"
kw const
kw enum
kw extern
@ -228,15 +230,15 @@ mod tests {
kw union
kw unsafe
kw use
"###
"#]],
);
}
#[test]
fn test_keywords_in_function() {
assert_snapshot!(
get_keyword_completions(r"fn quux() { <|> }"),
@r###"
check(
r"fn quux() { <|> }",
expect![[r#"
kw const
kw extern
kw fn
@ -254,15 +256,15 @@ mod tests {
kw unsafe
kw use
kw while
"###
"#]],
);
}
#[test]
fn test_keywords_inside_block() {
assert_snapshot!(
get_keyword_completions(r"fn quux() { if true { <|> } }"),
@r###"
check(
r"fn quux() { if true { <|> } }",
expect![[r#"
kw const
kw extern
kw fn
@ -280,23 +282,15 @@ mod tests {
kw unsafe
kw use
kw while
"###
"#]],
);
}
#[test]
fn test_keywords_after_if() {
assert_snapshot!(
get_keyword_completions(
r"
fn quux() {
if true {
()
} <|>
}
",
),
@r###"
check(
r#"fn quux() { if true { () } <|> }"#,
expect![[r#"
kw const
kw else
kw else if
@ -316,65 +310,63 @@ mod tests {
kw unsafe
kw use
kw while
"###
"#]],
);
}
#[test]
fn test_keywords_in_match_arm() {
assert_snapshot!(
get_keyword_completions(
r"
fn quux() -> i32 {
check(
r#"
fn quux() -> i32 {
match () {
() => <|>
}
}
",
),
@r###"
}
"#,
expect![[r#"
kw if
kw if let
kw loop
kw match
kw return
kw unsafe
"###
"#]],
);
}
#[test]
fn test_keywords_in_trait_def() {
assert_snapshot!(
get_keyword_completions(r"trait My { <|> }"),
@r###"
check(
r"trait My { <|> }",
expect![[r#"
kw const
kw fn
kw type
kw unsafe
"###
"#]],
);
}
#[test]
fn test_keywords_in_impl_def() {
assert_snapshot!(
get_keyword_completions(r"impl My { <|> }"),
@r###"
check(
r"impl My { <|> }",
expect![[r#"
kw const
kw fn
kw pub
kw type
kw unsafe
"###
"#]],
);
}
#[test]
fn test_keywords_in_loop() {
assert_snapshot!(
get_keyword_completions(r"fn my() { loop { <|> } }"),
@r###"
check(
r"fn my() { loop { <|> } }",
expect![[r#"
kw break
kw const
kw continue
@ -394,69 +386,69 @@ mod tests {
kw unsafe
kw use
kw while
"###
"#]],
);
}
#[test]
fn test_keywords_after_unsafe_in_item_list() {
assert_snapshot!(
get_keyword_completions(r"unsafe <|>"),
@r###"
check(
r"unsafe <|>",
expect![[r#"
kw fn
kw impl
kw trait
"###
"#]],
);
}
#[test]
fn test_keywords_after_unsafe_in_block_expr() {
assert_snapshot!(
get_keyword_completions(r"fn my_fn() { unsafe <|> }"),
@r###"
check(
r"fn my_fn() { unsafe <|> }",
expect![[r#"
kw fn
kw impl
kw trait
"###
"#]],
);
}
#[test]
fn test_mut_in_ref_and_in_fn_parameters_list() {
assert_snapshot!(
get_keyword_completions(r"fn my_fn(&<|>) {}"),
@r###"
check(
r"fn my_fn(&<|>) {}",
expect![[r#"
kw mut
"###
"#]],
);
assert_snapshot!(
get_keyword_completions(r"fn my_fn(<|>) {}"),
@r###"
check(
r"fn my_fn(<|>) {}",
expect![[r#"
kw mut
"###
"#]],
);
assert_snapshot!(
get_keyword_completions(r"fn my_fn() { let &<|> }"),
@r###"
check(
r"fn my_fn() { let &<|> }",
expect![[r#"
kw mut
"###
"#]],
);
}
#[test]
fn test_where_keyword() {
assert_snapshot!(
get_keyword_completions(r"trait A <|>"),
@r###"
check(
r"trait A <|>",
expect![[r#"
kw where
"###
"#]],
);
assert_snapshot!(
get_keyword_completions(r"impl A <|>"),
@r###"
check(
r"impl A <|>",
expect![[r#"
kw where
"###
"#]],
);
}
}