From d338a803941c2b0ac83decfcdfac33c09dfaa971 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 16 Jun 2021 17:37:23 +0200 Subject: Start refactoring ide_completion tests --- crates/ide_completion/src/lib.rs | 125 +++------------------------------------ 1 file changed, 7 insertions(+), 118 deletions(-) (limited to 'crates/ide_completion/src/lib.rs') diff --git a/crates/ide_completion/src/lib.rs b/crates/ide_completion/src/lib.rs index 18983aa01..be6442426 100644 --- a/crates/ide_completion/src/lib.rs +++ b/crates/ide_completion/src/lib.rs @@ -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> = 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" }, - ); - } -} -- cgit v1.2.3