From 1a78991df69630b581b4210083c9e94157bab0e1 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Mon, 27 Jan 2020 00:16:18 +0200 Subject: Adjust the tests --- crates/ra_assists/src/assists/auto_import.rs | 144 +++++++++++++++++---------- 1 file changed, 89 insertions(+), 55 deletions(-) (limited to 'crates/ra_assists/src/assists') diff --git a/crates/ra_assists/src/assists/auto_import.rs b/crates/ra_assists/src/assists/auto_import.rs index d196f6c5c..295fdf2e2 100644 --- a/crates/ra_assists/src/assists/auto_import.rs +++ b/crates/ra_assists/src/assists/auto_import.rs @@ -100,88 +100,122 @@ mod tests { use super::*; use crate::helpers::{ check_assist_with_imports_locator, check_assist_with_imports_locator_not_applicable, + TestImportsLocator, }; - use hir::Name; - #[derive(Clone)] - struct TestImportsLocator<'a> { - import_path: &'a [Name], - } + #[test] + fn applicable_when_found_an_import() { + check_assist_with_imports_locator( + auto_import, + TestImportsLocator::new, + r" + PubStruct<|> - impl<'a> TestImportsLocator<'a> { - fn new(import_path: &'a [Name]) -> Self { - TestImportsLocator { import_path } - } - } + pub mod PubMod { + pub struct PubStruct; + } + ", + r" + use PubMod::PubStruct; - impl<'a> ImportsLocator for TestImportsLocator<'a> { - fn find_imports( - &mut self, - _: hir::InFile<&ast::NameRef>, - _: hir::Module, - ) -> Option> { - if self.import_path.is_empty() { - None - } else { - Some(vec![hir::ModPath { - kind: hir::PathKind::Plain, - segments: self.import_path.to_owned(), - }]) + PubStruct<|> + + pub mod PubMod { + pub struct PubStruct; } - } + ", + ); } #[test] - fn applicable_when_found_an_import() { - let import_path = &[hir::name::known::std, hir::name::known::ops, hir::name::known::Debug]; - let mut imports_locator = TestImportsLocator::new(import_path); + fn applicable_when_found_multiple_imports() { check_assist_with_imports_locator( auto_import, - &mut imports_locator, - " - fn main() { + TestImportsLocator::new, + r" + PubStruct<|> + + pub mod PubMod1 { + pub struct PubStruct; + } + pub mod PubMod2 { + pub struct PubStruct; + } + pub mod PubMod3 { + pub struct PubStruct; + } + ", + r" + use PubMod1::PubStruct; + + PubStruct<|> + + pub mod PubMod1 { + pub struct PubStruct; + } + pub mod PubMod2 { + pub struct PubStruct; } + pub mod PubMod3 { + pub struct PubStruct; + } + ", + ); + } + + #[test] + fn not_applicable_for_already_imported_types() { + check_assist_with_imports_locator_not_applicable( + auto_import, + TestImportsLocator::new, + r" + use PubMod::PubStruct; + + PubStruct<|> - Debug<|>", - &format!( - " - use {}; - - fn main() {{ - }} - - Debug<|>", - import_path - .into_iter() - .map(|name| name.to_string()) - .collect::>() - .join("::") - ), + pub mod PubMod { + pub struct PubStruct; + } + ", ); } #[test] - fn not_applicable_when_no_imports_found() { - let mut imports_locator = TestImportsLocator::new(&[]); + fn not_applicable_for_types_with_private_paths() { check_assist_with_imports_locator_not_applicable( auto_import, - &mut imports_locator, - " - fn main() { + TestImportsLocator::new, + r" + PrivateStruct<|> + + pub mod PubMod { + struct PrivateStruct; } + ", + ); + } - Debug<|>", + #[test] + fn not_applicable_when_no_imports_found() { + check_assist_with_imports_locator_not_applicable( + auto_import, + TestImportsLocator::new, + " + PubStruct<|>", ); } #[test] fn not_applicable_in_import_statements() { - let import_path = &[hir::name::known::std, hir::name::known::ops, hir::name::known::Debug]; - let mut imports_locator = TestImportsLocator::new(import_path); check_assist_with_imports_locator_not_applicable( auto_import, - &mut imports_locator, - "use Debug<|>;", + TestImportsLocator::new, + r" + use PubStruct<|>; + + pub mod PubMod { + pub struct PubStruct; + }", ); } } -- cgit v1.2.3