Introduce completion test utils

This commit is contained in:
Aleksey Kladov 2020-03-11 10:46:43 +01:00
parent 9b0b59c5d2
commit dad8f1e064
16 changed files with 49 additions and 48 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, do_completion_with_options};
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,32 +318,3 @@ impl Into<Vec<CompletionItem>> for Completions {
self.buf
}
}
#[cfg(test)]
pub(crate) fn do_completion(code: &str, kind: CompletionKind) -> Vec<CompletionItem> {
do_completion_with_options(code, kind, &crate::completion::CompletionOptions::default())
}
#[cfg(test)]
pub(crate) fn do_completion_with_options(
code: &str,
kind: CompletionKind,
options: &crate::completion::CompletionOptions,
) -> Vec<CompletionItem> {
use crate::{
completion::completions,
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 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

@ -308,8 +308,8 @@ mod tests {
use test_utils::covers;
use crate::completion::{
do_completion, do_completion_with_options, CompletionItem, CompletionKind,
CompletionOptions,
test_utils::{do_completion, do_completion_with_options},
CompletionItem, CompletionKind, CompletionOptions,
};
fn do_reference_completion(ra_fixture: &str) -> Vec<CompletionItem> {

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
}