3555: Introduce completion test utils r=matklad a=matklad



bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-03-11 10:10:49 +00:00 committed by GitHub
commit 7b323b45a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 93 additions and 40 deletions

View file

@ -16,11 +16,11 @@ mod complete_scope;
mod complete_postfix;
mod complete_macro_in_item_position;
mod complete_trait_impl;
#[cfg(test)]
mod test_utils;
use ra_ide_db::RootDatabase;
#[cfg(test)]
use crate::completion::completion_item::do_completion;
use crate::{
completion::{
completion_context::CompletionContext,

View file

@ -70,7 +70,7 @@ fn complete_methods(acc: &mut Completions, ctx: &CompletionContext, receiver: &T
#[cfg(test)]
mod tests {
use crate::completion::{do_completion, CompletionItem, CompletionKind};
use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind};
use insta::assert_debug_snapshot;
fn do_ref_completion(code: &str) -> Vec<CompletionItem> {

View file

@ -52,7 +52,7 @@ pub(super) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext)
#[cfg(test)]
mod tests {
use crate::completion::{do_completion, CompletionItem, CompletionKind};
use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind};
use insta::assert_debug_snapshot;
fn do_magic_completion(code: &str) -> Vec<CompletionItem> {

View file

@ -117,7 +117,7 @@ fn complete_return(
#[cfg(test)]
mod tests {
use crate::completion::{do_completion, CompletionItem, CompletionKind};
use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind};
use insta::assert_debug_snapshot;
fn do_keyword_completion(code: &str) -> Vec<CompletionItem> {

View file

@ -15,9 +15,10 @@ pub(super) fn complete_macro_in_item_position(acc: &mut Completions, ctx: &Compl
#[cfg(test)]
mod tests {
use crate::completion::{do_completion, CompletionItem, CompletionKind};
use insta::assert_debug_snapshot;
use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind};
fn do_reference_completion(code: &str) -> Vec<CompletionItem> {
do_completion(code, CompletionKind::Reference)
}

View file

@ -103,7 +103,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
mod tests {
use test_utils::covers;
use crate::completion::{do_completion, CompletionItem, CompletionKind};
use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind};
use insta::assert_debug_snapshot;
fn do_reference_completion(code: &str) -> Vec<CompletionItem> {

View file

@ -27,7 +27,7 @@ pub(super) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) {
#[cfg(test)]
mod tests {
use crate::completion::{do_completion, CompletionItem, CompletionKind};
use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind};
use insta::assert_debug_snapshot;
fn complete(code: &str) -> Vec<CompletionItem> {

View file

@ -81,7 +81,7 @@ fn postfix_snippet(ctx: &CompletionContext, label: &str, detail: &str, snippet:
mod tests {
use insta::assert_debug_snapshot;
use crate::completion::{do_completion, CompletionItem, CompletionKind};
use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind};
fn do_postfix_completion(code: &str) -> Vec<CompletionItem> {
do_completion(code, CompletionKind::Postfix)

View file

@ -18,7 +18,7 @@ pub(super) fn complete_record_literal(acc: &mut Completions, ctx: &CompletionCon
#[cfg(test)]
mod tests {
use crate::completion::{do_completion, CompletionItem, CompletionKind};
use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind};
use insta::assert_debug_snapshot;
fn complete(code: &str) -> Vec<CompletionItem> {

View file

@ -17,7 +17,7 @@ pub(super) fn complete_record_pattern(acc: &mut Completions, ctx: &CompletionCon
#[cfg(test)]
mod tests {
use crate::completion::{do_completion, CompletionItem, CompletionKind};
use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind};
use insta::assert_debug_snapshot;
fn complete(code: &str) -> Vec<CompletionItem> {

View file

@ -14,10 +14,10 @@ pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) {
mod tests {
use insta::assert_debug_snapshot;
use crate::completion::{do_completion, CompletionItem, CompletionKind};
use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind};
fn do_reference_completion(code: &str) -> Vec<CompletionItem> {
do_completion(code, CompletionKind::Reference)
fn do_reference_completion(ra_fixture: &str) -> Vec<CompletionItem> {
do_completion(ra_fixture, CompletionKind::Reference)
}
#[test]

View file

@ -42,7 +42,7 @@ fn ${1:feature}() {
#[cfg(test)]
mod tests {
use crate::completion::{do_completion, CompletionItem, CompletionKind};
use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind};
use insta::assert_debug_snapshot;
fn do_snippet_completion(code: &str) -> Vec<CompletionItem> {

View file

@ -217,7 +217,7 @@ fn make_const_compl_syntax(const_: &ast::ConstDef) -> String {
#[cfg(test)]
mod tests {
use crate::completion::{do_completion, CompletionItem, CompletionKind};
use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind};
use insta::assert_debug_snapshot;
fn complete(code: &str) -> Vec<CompletionItem> {

View file

@ -13,7 +13,7 @@ pub struct CompletionItem {
/// Used only internally in tests, to check only specific kind of
/// completion (postfix, keyword, reference, etc).
#[allow(unused)]
completion_kind: CompletionKind,
pub(crate) completion_kind: CompletionKind,
/// Label in the completion pop up which identifies completion.
label: String,
/// Range of identifier that is being completed.
@ -318,24 +318,3 @@ impl Into<Vec<CompletionItem>> for Completions {
self.buf
}
}
#[cfg(test)]
pub(crate) fn do_completion(code: &str, kind: CompletionKind) -> Vec<CompletionItem> {
use crate::{
completion::{completions, CompletionOptions},
mock_analysis::{analysis_and_position, single_file_with_position},
};
let (analysis, position) = if code.contains("//-") {
analysis_and_position(code)
} else {
single_file_with_position(code)
};
let options = CompletionOptions::default();
let completions = completions(&analysis.db, position, &options).unwrap();
let completion_items: Vec<CompletionItem> = completions.into();
let mut kind_completions: Vec<CompletionItem> =
completion_items.into_iter().filter(|c| c.completion_kind == kind).collect();
kind_completions.sort_by_key(|c| c.label.clone());
kind_completions
}

View file

@ -307,12 +307,22 @@ mod tests {
use insta::assert_debug_snapshot;
use test_utils::covers;
use crate::completion::{do_completion, CompletionItem, CompletionKind};
use crate::completion::{
test_utils::{do_completion, do_completion_with_options},
CompletionItem, CompletionKind, CompletionOptions,
};
fn do_reference_completion(ra_fixture: &str) -> Vec<CompletionItem> {
do_completion(ra_fixture, CompletionKind::Reference)
}
fn do_reference_completion_with_options(
ra_fixture: &str,
options: CompletionOptions,
) -> Vec<CompletionItem> {
do_completion_with_options(ra_fixture, CompletionKind::Reference, &options)
}
#[test]
fn enum_detail_includes_names_for_record() {
assert_debug_snapshot!(
@ -533,7 +543,7 @@ mod tests {
}
#[test]
fn parens_for_method_call() {
fn arg_snippets_for_method_call() {
assert_debug_snapshot!(
do_reference_completion(
r"
@ -562,6 +572,40 @@ mod tests {
)
}
#[test]
fn no_arg_snippets_for_method_call() {
assert_debug_snapshot!(
do_reference_completion_with_options(
r"
struct S {}
impl S {
fn foo(&self, x: i32) {}
}
fn bar(s: &S) {
s.f<|>
}
",
CompletionOptions {
add_call_argument_snippets: false,
.. Default::default()
}
),
@r###"
[
CompletionItem {
label: "foo(…)",
source_range: [171; 172),
delete: [171; 172),
insert: "foo($0)",
kind: Method,
lookup: "foo",
detail: "fn foo(&self, x: i32)",
},
]
"###
)
}
#[test]
fn dont_render_function_parens_in_use_item() {
assert_debug_snapshot!(

View file

@ -0,0 +1,29 @@
//! Runs completion for testing purposes.
use crate::{
completion::{completion_item::CompletionKind, CompletionOptions},
mock_analysis::{analysis_and_position, single_file_with_position},
CompletionItem,
};
pub(crate) fn do_completion(code: &str, kind: CompletionKind) -> Vec<CompletionItem> {
do_completion_with_options(code, kind, &CompletionOptions::default())
}
pub(crate) fn do_completion_with_options(
code: &str,
kind: CompletionKind,
options: &CompletionOptions,
) -> Vec<CompletionItem> {
let (analysis, position) = if code.contains("//-") {
analysis_and_position(code)
} else {
single_file_with_position(code)
};
let completions = analysis.completions(position, options).unwrap().unwrap();
let completion_items: Vec<CompletionItem> = completions.into();
let mut kind_completions: Vec<CompletionItem> =
completion_items.into_iter().filter(|c| c.completion_kind == kind).collect();
kind_completions.sort_by_key(|c| c.label().to_owned());
kind_completions
}