Start refactoring ide_completion tests

This commit is contained in:
Lukas Wirth 2021-06-16 17:37:23 +02:00
parent f38770cd26
commit d338a80394
9 changed files with 306 additions and 251 deletions

View file

@ -1,14 +1,16 @@
//! `completions` crate provides utilities for generating completions of user input.
mod completions;
mod config;
mod item;
mod context;
mod item;
mod patterns;
#[cfg(test)]
mod test_utils;
mod render;
mod completions;
#[cfg(test)]
mod tests;
#[cfg(test)]
mod test_utils;
use completions::flyimport::position_for_import;
use ide_db::{
@ -141,6 +143,7 @@ pub fn completions(
let ctx = CompletionContext::new(db, position, config)?;
if ctx.no_completion_required() {
cov_mark::hit!(no_completion_required);
// No work required here.
return None;
}
@ -200,117 +203,3 @@ pub fn resolve_completion_edits(
ImportEdit { import, scope }.to_text_edit(config.insert_use).map(|edit| vec![edit])
}
#[cfg(test)]
mod tests {
use crate::test_utils::{self, TEST_CONFIG};
struct DetailAndDocumentation<'a> {
detail: &'a str,
documentation: &'a str,
}
fn check_detail_and_documentation(ra_fixture: &str, expected: DetailAndDocumentation) {
let (db, position) = test_utils::position(ra_fixture);
let config = TEST_CONFIG;
let completions: Vec<_> = crate::completions(&db, &config, position).unwrap().into();
for item in completions {
if item.detail() == Some(expected.detail) {
let opt = item.documentation();
let doc = opt.as_ref().map(|it| it.as_str());
assert_eq!(doc, Some(expected.documentation));
return;
}
}
panic!("completion detail not found: {}", expected.detail)
}
fn check_no_completion(ra_fixture: &str) {
let (db, position) = test_utils::position(ra_fixture);
let config = TEST_CONFIG;
let completions: Option<Vec<String>> = crate::completions(&db, &config, position)
.and_then(|completions| {
let completions: Vec<_> = completions.into();
if completions.is_empty() {
None
} else {
Some(completions)
}
})
.map(|completions| {
completions.into_iter().map(|completion| format!("{:?}", completion)).collect()
});
// `assert_eq` instead of `assert!(completions.is_none())` to get the list of completions if test will panic.
assert_eq!(completions, None, "Completions were generated, but weren't expected");
}
#[test]
fn test_completion_detail_from_macro_generated_struct_fn_doc_attr() {
check_detail_and_documentation(
r#"
macro_rules! bar {
() => {
struct Bar;
impl Bar {
#[doc = "Do the foo"]
fn foo(&self) {}
}
}
}
bar!();
fn foo() {
let bar = Bar;
bar.fo$0;
}
"#,
DetailAndDocumentation { detail: "fn(&self)", documentation: "Do the foo" },
);
}
#[test]
fn test_completion_detail_from_macro_generated_struct_fn_doc_comment() {
check_detail_and_documentation(
r#"
macro_rules! bar {
() => {
struct Bar;
impl Bar {
/// Do the foo
fn foo(&self) {}
}
}
}
bar!();
fn foo() {
let bar = Bar;
bar.fo$0;
}
"#,
DetailAndDocumentation { detail: "fn(&self)", documentation: "Do the foo" },
);
}
#[test]
fn test_no_completions_required() {
// There must be no hint for 'in' keyword.
check_no_completion(r#"fn foo() { for i i$0 }"#);
// After 'in' keyword hints may be spawned.
check_detail_and_documentation(
r#"
/// Do the foo
fn foo() -> &'static str { "foo" }
fn bar() {
for c in fo$0
}
"#,
DetailAndDocumentation { detail: "fn() -> &str", documentation: "Do the foo" },
);
}
}