From 25e6bbde01d4a9cd08fa79db5b8b7da6bbf1a623 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 6 May 2020 10:16:55 +0200 Subject: Merge assits::test_helpers and tests --- crates/ra_assists/src/doc_tests.rs | 2 +- crates/ra_assists/src/handlers/add_custom_impl.rs | 2 +- crates/ra_assists/src/handlers/add_derive.rs | 2 +- .../ra_assists/src/handlers/add_explicit_type.rs | 2 +- .../src/handlers/add_from_impl_for_enum.rs | 2 +- crates/ra_assists/src/handlers/add_function.rs | 2 +- crates/ra_assists/src/handlers/add_impl.rs | 2 +- .../src/handlers/add_missing_impl_members.rs | 2 +- crates/ra_assists/src/handlers/add_new.rs | 2 +- crates/ra_assists/src/handlers/apply_demorgan.rs | 2 +- crates/ra_assists/src/handlers/auto_import.rs | 2 +- .../ra_assists/src/handlers/change_visibility.rs | 2 +- crates/ra_assists/src/handlers/early_return.rs | 2 +- crates/ra_assists/src/handlers/fill_match_arms.rs | 2 +- crates/ra_assists/src/handlers/flip_binexpr.rs | 2 +- crates/ra_assists/src/handlers/flip_comma.rs | 2 +- crates/ra_assists/src/handlers/flip_trait_bound.rs | 2 +- .../src/handlers/inline_local_variable.rs | 2 +- .../ra_assists/src/handlers/introduce_variable.rs | 2 +- crates/ra_assists/src/handlers/invert_if.rs | 2 +- crates/ra_assists/src/handlers/merge_imports.rs | 2 +- crates/ra_assists/src/handlers/merge_match_arms.rs | 2 +- crates/ra_assists/src/handlers/move_bounds.rs | 2 +- crates/ra_assists/src/handlers/move_guard.rs | 2 +- crates/ra_assists/src/handlers/raw_string.rs | 2 +- crates/ra_assists/src/handlers/remove_dbg.rs | 2 +- crates/ra_assists/src/handlers/reorder_fields.rs | 2 +- .../src/handlers/replace_if_let_with_match.rs | 2 +- .../src/handlers/replace_let_with_if_let.rs | 2 +- .../handlers/replace_qualified_name_with_use.rs | 2 +- .../src/handlers/replace_unwrap_with_match.rs | 2 +- crates/ra_assists/src/handlers/split_import.rs | 2 +- crates/ra_assists/src/handlers/unwrap_block.rs | 2 +- crates/ra_assists/src/lib.rs | 149 +-------------------- crates/ra_assists/src/tests.rs | 137 +++++++++++++++++++ 35 files changed, 172 insertions(+), 180 deletions(-) create mode 100644 crates/ra_assists/src/tests.rs diff --git a/crates/ra_assists/src/doc_tests.rs b/crates/ra_assists/src/doc_tests.rs index f627f31dc..5eabb33d0 100644 --- a/crates/ra_assists/src/doc_tests.rs +++ b/crates/ra_assists/src/doc_tests.rs @@ -12,7 +12,7 @@ use crate::resolved_assists; fn check(assist_id: &str, before: &str, after: &str) { let (selection, before) = extract_range_or_offset(before); - let (db, file_id) = crate::helpers::with_single_file(&before); + let (db, file_id) = crate::tests::with_single_file(&before); let frange = FileRange { file_id, range: selection.into() }; let assist = resolved_assists(&db, frange) diff --git a/crates/ra_assists/src/handlers/add_custom_impl.rs b/crates/ra_assists/src/handlers/add_custom_impl.rs index 4ea26a550..b72f7aeac 100644 --- a/crates/ra_assists/src/handlers/add_custom_impl.rs +++ b/crates/ra_assists/src/handlers/add_custom_impl.rs @@ -95,7 +95,7 @@ pub(crate) fn add_custom_impl(ctx: AssistCtx) -> Option { #[cfg(test)] mod tests { - use crate::helpers::{check_assist, check_assist_not_applicable}; + use crate::tests::{check_assist, check_assist_not_applicable}; use super::*; diff --git a/crates/ra_assists/src/handlers/add_derive.rs b/crates/ra_assists/src/handlers/add_derive.rs index 6254eb7c4..3629dac6b 100644 --- a/crates/ra_assists/src/handlers/add_derive.rs +++ b/crates/ra_assists/src/handlers/add_derive.rs @@ -58,7 +58,7 @@ fn derive_insertion_offset(nominal: &ast::NominalDef) -> Option { #[cfg(test)] mod tests { use super::*; - use crate::helpers::{check_assist, check_assist_target}; + use crate::tests::{check_assist, check_assist_target}; #[test] fn add_derive_new() { diff --git a/crates/ra_assists/src/handlers/add_explicit_type.rs b/crates/ra_assists/src/handlers/add_explicit_type.rs index bc313782b..e39e1f4f3 100644 --- a/crates/ra_assists/src/handlers/add_explicit_type.rs +++ b/crates/ra_assists/src/handlers/add_explicit_type.rs @@ -77,7 +77,7 @@ pub(crate) fn add_explicit_type(ctx: AssistCtx) -> Option { mod tests { use super::*; - use crate::helpers::{check_assist, check_assist_not_applicable, check_assist_target}; + use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target}; #[test] fn add_explicit_type_target() { diff --git a/crates/ra_assists/src/handlers/add_from_impl_for_enum.rs b/crates/ra_assists/src/handlers/add_from_impl_for_enum.rs index 49deb6701..ee0d5ce98 100644 --- a/crates/ra_assists/src/handlers/add_from_impl_for_enum.rs +++ b/crates/ra_assists/src/handlers/add_from_impl_for_enum.rs @@ -97,7 +97,7 @@ fn existing_from_impl( mod tests { use super::*; - use crate::helpers::{check_assist, check_assist_not_applicable}; + use crate::tests::{check_assist, check_assist_not_applicable}; use test_utils::covers; #[test] diff --git a/crates/ra_assists/src/handlers/add_function.rs b/crates/ra_assists/src/handlers/add_function.rs index 6c7456579..cb2afc863 100644 --- a/crates/ra_assists/src/handlers/add_function.rs +++ b/crates/ra_assists/src/handlers/add_function.rs @@ -283,7 +283,7 @@ fn next_space_for_fn_in_module( #[cfg(test)] mod tests { - use crate::helpers::{check_assist, check_assist_not_applicable}; + use crate::tests::{check_assist, check_assist_not_applicable}; use super::*; diff --git a/crates/ra_assists/src/handlers/add_impl.rs b/crates/ra_assists/src/handlers/add_impl.rs index d26f8b93d..3d390c20b 100644 --- a/crates/ra_assists/src/handlers/add_impl.rs +++ b/crates/ra_assists/src/handlers/add_impl.rs @@ -60,7 +60,7 @@ pub(crate) fn add_impl(ctx: AssistCtx) -> Option { #[cfg(test)] mod tests { use super::*; - use crate::helpers::{check_assist, check_assist_target}; + use crate::tests::{check_assist, check_assist_target}; #[test] fn test_add_impl() { diff --git a/crates/ra_assists/src/handlers/add_missing_impl_members.rs b/crates/ra_assists/src/handlers/add_missing_impl_members.rs index e47feda71..f7a101503 100644 --- a/crates/ra_assists/src/handlers/add_missing_impl_members.rs +++ b/crates/ra_assists/src/handlers/add_missing_impl_members.rs @@ -181,7 +181,7 @@ fn add_body(fn_def: ast::FnDef) -> ast::FnDef { #[cfg(test)] mod tests { - use crate::helpers::{check_assist, check_assist_not_applicable}; + use crate::tests::{check_assist, check_assist_not_applicable}; use super::*; diff --git a/crates/ra_assists/src/handlers/add_new.rs b/crates/ra_assists/src/handlers/add_new.rs index e8a36c7de..1b5d604d1 100644 --- a/crates/ra_assists/src/handlers/add_new.rs +++ b/crates/ra_assists/src/handlers/add_new.rs @@ -178,7 +178,7 @@ fn has_new_fn(imp: &ast::ImplDef) -> bool { #[cfg(test)] mod tests { - use crate::helpers::{check_assist, check_assist_not_applicable, check_assist_target}; + use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target}; use super::*; diff --git a/crates/ra_assists/src/handlers/apply_demorgan.rs b/crates/ra_assists/src/handlers/apply_demorgan.rs index 260b9e073..a0c48d872 100644 --- a/crates/ra_assists/src/handlers/apply_demorgan.rs +++ b/crates/ra_assists/src/handlers/apply_demorgan.rs @@ -60,7 +60,7 @@ fn opposite_logic_op(kind: ast::BinOp) -> Option<&'static str> { mod tests { use super::*; - use crate::helpers::{check_assist, check_assist_not_applicable}; + use crate::tests::{check_assist, check_assist_not_applicable}; #[test] fn demorgan_turns_and_into_or() { diff --git a/crates/ra_assists/src/handlers/auto_import.rs b/crates/ra_assists/src/handlers/auto_import.rs index db6c4d2fa..9e4171ccd 100644 --- a/crates/ra_assists/src/handlers/auto_import.rs +++ b/crates/ra_assists/src/handlers/auto_import.rs @@ -277,7 +277,7 @@ impl ImportCandidate { #[cfg(test)] mod tests { use super::*; - use crate::helpers::{check_assist, check_assist_not_applicable, check_assist_target}; + use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target}; #[test] fn applicable_when_found_an_import() { diff --git a/crates/ra_assists/src/handlers/change_visibility.rs b/crates/ra_assists/src/handlers/change_visibility.rs index 1cd532e80..6ac1f8e69 100644 --- a/crates/ra_assists/src/handlers/change_visibility.rs +++ b/crates/ra_assists/src/handlers/change_visibility.rs @@ -110,7 +110,7 @@ fn change_vis(ctx: AssistCtx, vis: ast::Visibility) -> Option { mod tests { use test_utils::covers; - use crate::helpers::{check_assist, check_assist_not_applicable, check_assist_target}; + use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target}; use super::*; diff --git a/crates/ra_assists/src/handlers/early_return.rs b/crates/ra_assists/src/handlers/early_return.rs index eede2fe91..55ccc37b0 100644 --- a/crates/ra_assists/src/handlers/early_return.rs +++ b/crates/ra_assists/src/handlers/early_return.rs @@ -182,7 +182,7 @@ pub(crate) fn convert_to_guarded_return(ctx: AssistCtx) -> Option { #[cfg(test)] mod tests { - use crate::helpers::{check_assist, check_assist_not_applicable}; + use crate::tests::{check_assist, check_assist_not_applicable}; use super::*; diff --git a/crates/ra_assists/src/handlers/fill_match_arms.rs b/crates/ra_assists/src/handlers/fill_match_arms.rs index 8d1af9933..1f9cd5585 100644 --- a/crates/ra_assists/src/handlers/fill_match_arms.rs +++ b/crates/ra_assists/src/handlers/fill_match_arms.rs @@ -168,7 +168,7 @@ fn build_pat(db: &RootDatabase, module: hir::Module, var: hir::EnumVariant) -> O #[cfg(test)] mod tests { - use crate::helpers::{check_assist, check_assist_not_applicable, check_assist_target}; + use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target}; use super::fill_match_arms; diff --git a/crates/ra_assists/src/handlers/flip_binexpr.rs b/crates/ra_assists/src/handlers/flip_binexpr.rs index 8030efb35..41db963dc 100644 --- a/crates/ra_assists/src/handlers/flip_binexpr.rs +++ b/crates/ra_assists/src/handlers/flip_binexpr.rs @@ -69,7 +69,7 @@ impl From for FlipAction { mod tests { use super::*; - use crate::helpers::{check_assist, check_assist_not_applicable, check_assist_target}; + use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target}; #[test] fn flip_binexpr_target_is_the_op() { diff --git a/crates/ra_assists/src/handlers/flip_comma.rs b/crates/ra_assists/src/handlers/flip_comma.rs index 1dacf29f8..e65c9a41d 100644 --- a/crates/ra_assists/src/handlers/flip_comma.rs +++ b/crates/ra_assists/src/handlers/flip_comma.rs @@ -39,7 +39,7 @@ pub(crate) fn flip_comma(ctx: AssistCtx) -> Option { mod tests { use super::*; - use crate::helpers::{check_assist, check_assist_target}; + use crate::tests::{check_assist, check_assist_target}; #[test] fn flip_comma_works_for_function_parameters() { diff --git a/crates/ra_assists/src/handlers/flip_trait_bound.rs b/crates/ra_assists/src/handlers/flip_trait_bound.rs index f56769624..f186da585 100644 --- a/crates/ra_assists/src/handlers/flip_trait_bound.rs +++ b/crates/ra_assists/src/handlers/flip_trait_bound.rs @@ -43,7 +43,7 @@ pub(crate) fn flip_trait_bound(ctx: AssistCtx) -> Option { mod tests { use super::*; - use crate::helpers::{check_assist, check_assist_not_applicable, check_assist_target}; + use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target}; #[test] fn flip_trait_bound_assist_available() { diff --git a/crates/ra_assists/src/handlers/inline_local_variable.rs b/crates/ra_assists/src/handlers/inline_local_variable.rs index 60ec536a7..5f3b8dfd1 100644 --- a/crates/ra_assists/src/handlers/inline_local_variable.rs +++ b/crates/ra_assists/src/handlers/inline_local_variable.rs @@ -125,7 +125,7 @@ pub(crate) fn inline_local_variable(ctx: AssistCtx) -> Option { mod tests { use test_utils::covers; - use crate::helpers::{check_assist, check_assist_not_applicable}; + use crate::tests::{check_assist, check_assist_not_applicable}; use super::*; diff --git a/crates/ra_assists/src/handlers/introduce_variable.rs b/crates/ra_assists/src/handlers/introduce_variable.rs index 39c656305..9c2c20b22 100644 --- a/crates/ra_assists/src/handlers/introduce_variable.rs +++ b/crates/ra_assists/src/handlers/introduce_variable.rs @@ -136,7 +136,7 @@ fn anchor_stmt(expr: ast::Expr) -> Option<(SyntaxNode, bool)> { mod tests { use test_utils::covers; - use crate::helpers::{check_assist, check_assist_not_applicable, check_assist_target}; + use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target}; use super::*; diff --git a/crates/ra_assists/src/handlers/invert_if.rs b/crates/ra_assists/src/handlers/invert_if.rs index 682e08512..303c1806d 100644 --- a/crates/ra_assists/src/handlers/invert_if.rs +++ b/crates/ra_assists/src/handlers/invert_if.rs @@ -62,7 +62,7 @@ pub(crate) fn invert_if(ctx: AssistCtx) -> Option { mod tests { use super::*; - use crate::helpers::{check_assist, check_assist_not_applicable}; + use crate::tests::{check_assist, check_assist_not_applicable}; #[test] fn invert_if_remove_inequality() { diff --git a/crates/ra_assists/src/handlers/merge_imports.rs b/crates/ra_assists/src/handlers/merge_imports.rs index 4be1238f1..9a2083609 100644 --- a/crates/ra_assists/src/handlers/merge_imports.rs +++ b/crates/ra_assists/src/handlers/merge_imports.rs @@ -125,7 +125,7 @@ fn first_path(path: &ast::Path) -> ast::Path { #[cfg(test)] mod tests { - use crate::helpers::check_assist; + use crate::tests::check_assist; use super::*; diff --git a/crates/ra_assists/src/handlers/merge_match_arms.rs b/crates/ra_assists/src/handlers/merge_match_arms.rs index 5a77d3dbc..9ae099b41 100644 --- a/crates/ra_assists/src/handlers/merge_match_arms.rs +++ b/crates/ra_assists/src/handlers/merge_match_arms.rs @@ -105,7 +105,7 @@ fn contains_placeholder(a: &ast::MatchArm) -> bool { #[cfg(test)] mod tests { - use crate::helpers::{check_assist, check_assist_not_applicable}; + use crate::tests::{check_assist, check_assist_not_applicable}; use super::*; diff --git a/crates/ra_assists/src/handlers/move_bounds.rs b/crates/ra_assists/src/handlers/move_bounds.rs index 0f26884dc..89956aea9 100644 --- a/crates/ra_assists/src/handlers/move_bounds.rs +++ b/crates/ra_assists/src/handlers/move_bounds.rs @@ -89,7 +89,7 @@ fn build_predicate(param: ast::TypeParam) -> Option { mod tests { use super::*; - use crate::helpers::check_assist; + use crate::tests::check_assist; #[test] fn move_bounds_to_where_clause_fn() { diff --git a/crates/ra_assists/src/handlers/move_guard.rs b/crates/ra_assists/src/handlers/move_guard.rs index b084dd9ee..f2aa7e594 100644 --- a/crates/ra_assists/src/handlers/move_guard.rs +++ b/crates/ra_assists/src/handlers/move_guard.rs @@ -132,7 +132,7 @@ pub(crate) fn move_arm_cond_to_match_guard(ctx: AssistCtx) -> Option { mod tests { use super::*; - use crate::helpers::{check_assist, check_assist_not_applicable, check_assist_target}; + use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target}; #[test] fn move_guard_to_arm_body_target() { diff --git a/crates/ra_assists/src/handlers/raw_string.rs b/crates/ra_assists/src/handlers/raw_string.rs index 567400b9c..542f7a637 100644 --- a/crates/ra_assists/src/handlers/raw_string.rs +++ b/crates/ra_assists/src/handlers/raw_string.rs @@ -138,7 +138,7 @@ fn count_hashes(s: &str) -> usize { #[cfg(test)] mod test { use super::*; - use crate::helpers::{check_assist, check_assist_not_applicable, check_assist_target}; + use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target}; #[test] fn make_raw_string_target() { diff --git a/crates/ra_assists/src/handlers/remove_dbg.rs b/crates/ra_assists/src/handlers/remove_dbg.rs index 4e5eb4350..ddfb21a7e 100644 --- a/crates/ra_assists/src/handlers/remove_dbg.rs +++ b/crates/ra_assists/src/handlers/remove_dbg.rs @@ -90,7 +90,7 @@ fn is_valid_macrocall(macro_call: &ast::MacroCall, macro_name: &str) -> Option Option Option { mod tests { use super::*; - use crate::helpers::{check_assist, check_assist_target}; + use crate::tests::{check_assist, check_assist_target}; #[test] fn test_replace_if_let_with_match_unwraps_simple_expressions() { diff --git a/crates/ra_assists/src/handlers/replace_let_with_if_let.rs b/crates/ra_assists/src/handlers/replace_let_with_if_let.rs index 0cf23b754..dc4d16055 100644 --- a/crates/ra_assists/src/handlers/replace_let_with_if_let.rs +++ b/crates/ra_assists/src/handlers/replace_let_with_if_let.rs @@ -74,7 +74,7 @@ pub(crate) fn replace_let_with_if_let(ctx: AssistCtx) -> Option { #[cfg(test)] mod tests { - use crate::helpers::check_assist; + use crate::tests::check_assist; use super::*; diff --git a/crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs b/crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs index ff2463c77..624178924 100644 --- a/crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs +++ b/crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs @@ -74,7 +74,7 @@ fn collect_hir_path_segments(path: &hir::Path) -> Option> { #[cfg(test)] mod tests { - use crate::helpers::{check_assist, check_assist_not_applicable}; + use crate::tests::{check_assist, check_assist_not_applicable}; use super::*; diff --git a/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs b/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs index 62d4ea522..dcb471edb 100644 --- a/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs +++ b/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs @@ -63,7 +63,7 @@ pub(crate) fn replace_unwrap_with_match(ctx: AssistCtx) -> Option { #[cfg(test)] mod tests { use super::*; - use crate::helpers::{check_assist, check_assist_target}; + use crate::tests::{check_assist, check_assist_target}; #[test] fn test_replace_result_unwrap_with_match() { diff --git a/crates/ra_assists/src/handlers/split_import.rs b/crates/ra_assists/src/handlers/split_import.rs index f25826796..a59f2f76f 100644 --- a/crates/ra_assists/src/handlers/split_import.rs +++ b/crates/ra_assists/src/handlers/split_import.rs @@ -37,7 +37,7 @@ pub(crate) fn split_import(ctx: AssistCtx) -> Option { #[cfg(test)] mod tests { - use crate::helpers::{check_assist, check_assist_not_applicable, check_assist_target}; + use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target}; use super::*; diff --git a/crates/ra_assists/src/handlers/unwrap_block.rs b/crates/ra_assists/src/handlers/unwrap_block.rs index 859c70ad8..89992117d 100644 --- a/crates/ra_assists/src/handlers/unwrap_block.rs +++ b/crates/ra_assists/src/handlers/unwrap_block.rs @@ -89,7 +89,7 @@ fn extract_expr(cursor_range: TextRange, block: ast::BlockExpr) -> Option (RootDatabase, FileId) { - let (mut db, file_id) = RootDatabase::with_single_file(text); - // FIXME: ideally, this should be done by the above `RootDatabase::with_single_file`, - // but it looks like this might need specialization? :( - db.set_local_roots(Arc::new(vec![db.file_source_root(file_id)])); - (db, file_id) - } - - pub(crate) fn check_assist(assist: Handler, ra_fixture_before: &str, ra_fixture_after: &str) { - check(assist, ra_fixture_before, ExpectedResult::After(ra_fixture_after)); - } - - // FIXME: instead of having a separate function here, maybe use - // `extract_ranges` and mark the target as ` ` in the - // fixuture? - pub(crate) fn check_assist_target(assist: Handler, ra_fixture: &str, target: &str) { - check(assist, ra_fixture, ExpectedResult::Target(target)); - } - - pub(crate) fn check_assist_not_applicable(assist: Handler, ra_fixture: &str) { - check(assist, ra_fixture, ExpectedResult::NotApplicable); - } - - enum ExpectedResult<'a> { - NotApplicable, - After(&'a str), - Target(&'a str), - } - - fn check(assist: Handler, before: &str, expected: ExpectedResult) { - let (text_without_caret, file_with_caret_id, range_or_offset, db) = - if before.contains("//-") { - let (mut db, position) = RootDatabase::with_position(before); - db.set_local_roots(Arc::new(vec![db.file_source_root(position.file_id)])); - ( - db.file_text(position.file_id).as_ref().to_owned(), - position.file_id, - RangeOrOffset::Offset(position.offset), - db, - ) - } else { - let (range_or_offset, text_without_caret) = extract_range_or_offset(before); - let (db, file_id) = with_single_file(&text_without_caret); - (text_without_caret, file_id, range_or_offset, db) - }; - - let frange = FileRange { file_id: file_with_caret_id, range: range_or_offset.into() }; - - let sema = Semantics::new(&db); - let assist_ctx = AssistCtx::new(&sema, frange, true); - - match (assist(assist_ctx), expected) { - (Some(assist), ExpectedResult::After(after)) => { - let action = assist.0[0].action.clone().unwrap(); - - let mut actual = if let AssistFile::TargetFile(file_id) = action.file { - db.file_text(file_id).as_ref().to_owned() - } else { - text_without_caret - }; - action.edit.apply(&mut actual); - - match action.cursor_position { - None => { - if let RangeOrOffset::Offset(before_cursor_pos) = range_or_offset { - let off = action - .edit - .apply_to_offset(before_cursor_pos) - .expect("cursor position is affected by the edit"); - actual = add_cursor(&actual, off) - } - } - Some(off) => actual = add_cursor(&actual, off), - }; - - assert_eq_text!(after, &actual); - } - (Some(assist), ExpectedResult::Target(target)) => { - let action = assist.0[0].action.clone().unwrap(); - let range = action.target.expect("expected target on action"); - assert_eq_text!(&text_without_caret[range], target); - } - (Some(_), ExpectedResult::NotApplicable) => panic!("assist should not be applicable!"), - (None, ExpectedResult::After(_)) | (None, ExpectedResult::Target(_)) => { - panic!("code action is not applicable") - } - (None, ExpectedResult::NotApplicable) => (), - }; - } -} - -#[cfg(test)] -mod tests { - use ra_db::FileRange; - use ra_syntax::TextRange; - use test_utils::{extract_offset, extract_range}; - - use crate::{helpers, resolved_assists}; - - #[test] - fn assist_order_field_struct() { - let before = "struct Foo { <|>bar: u32 }"; - let (before_cursor_pos, before) = extract_offset(before); - let (db, file_id) = helpers::with_single_file(&before); - let frange = FileRange { file_id, range: TextRange::empty(before_cursor_pos) }; - let assists = resolved_assists(&db, frange); - let mut assists = assists.iter(); - - assert_eq!( - assists.next().expect("expected assist").label.label, - "Change visibility to pub(crate)" - ); - assert_eq!(assists.next().expect("expected assist").label.label, "Add `#[derive]`"); - } - - #[test] - fn assist_order_if_expr() { - let before = " - pub fn test_some_range(a: int) -> bool { - if let 2..6 = <|>5<|> { - true - } else { - false - } - }"; - let (range, before) = extract_range(before); - let (db, file_id) = helpers::with_single_file(&before); - let frange = FileRange { file_id, range }; - let assists = resolved_assists(&db, frange); - let mut assists = assists.iter(); - - assert_eq!(assists.next().expect("expected assist").label.label, "Extract into variable"); - assert_eq!(assists.next().expect("expected assist").label.label, "Replace with match"); - } -} diff --git a/crates/ra_assists/src/tests.rs b/crates/ra_assists/src/tests.rs new file mode 100644 index 000000000..ca3096a43 --- /dev/null +++ b/crates/ra_assists/src/tests.rs @@ -0,0 +1,137 @@ +use std::sync::Arc; + +use hir::Semantics; +use ra_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt}; +use ra_ide_db::{symbol_index::SymbolsDatabase, RootDatabase}; +use ra_syntax::TextRange; +use test_utils::{ + add_cursor, assert_eq_text, extract_offset, extract_range, extract_range_or_offset, + RangeOrOffset, +}; + +use crate::{handlers::Handler, resolved_assists, AssistCtx, AssistFile}; + +pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) { + let (mut db, file_id) = RootDatabase::with_single_file(text); + // FIXME: ideally, this should be done by the above `RootDatabase::with_single_file`, + // but it looks like this might need specialization? :( + db.set_local_roots(Arc::new(vec![db.file_source_root(file_id)])); + (db, file_id) +} + +pub(crate) fn check_assist(assist: Handler, ra_fixture_before: &str, ra_fixture_after: &str) { + check(assist, ra_fixture_before, ExpectedResult::After(ra_fixture_after)); +} + +// FIXME: instead of having a separate function here, maybe use +// `extract_ranges` and mark the target as ` ` in the +// fixuture? +pub(crate) fn check_assist_target(assist: Handler, ra_fixture: &str, target: &str) { + check(assist, ra_fixture, ExpectedResult::Target(target)); +} + +pub(crate) fn check_assist_not_applicable(assist: Handler, ra_fixture: &str) { + check(assist, ra_fixture, ExpectedResult::NotApplicable); +} + +enum ExpectedResult<'a> { + NotApplicable, + After(&'a str), + Target(&'a str), +} + +fn check(assist: Handler, before: &str, expected: ExpectedResult) { + let (text_without_caret, file_with_caret_id, range_or_offset, db) = if before.contains("//-") { + let (mut db, position) = RootDatabase::with_position(before); + db.set_local_roots(Arc::new(vec![db.file_source_root(position.file_id)])); + ( + db.file_text(position.file_id).as_ref().to_owned(), + position.file_id, + RangeOrOffset::Offset(position.offset), + db, + ) + } else { + let (range_or_offset, text_without_caret) = extract_range_or_offset(before); + let (db, file_id) = with_single_file(&text_without_caret); + (text_without_caret, file_id, range_or_offset, db) + }; + + let frange = FileRange { file_id: file_with_caret_id, range: range_or_offset.into() }; + + let sema = Semantics::new(&db); + let assist_ctx = AssistCtx::new(&sema, frange, true); + + match (assist(assist_ctx), expected) { + (Some(assist), ExpectedResult::After(after)) => { + let action = assist.0[0].action.clone().unwrap(); + + let mut actual = if let AssistFile::TargetFile(file_id) = action.file { + db.file_text(file_id).as_ref().to_owned() + } else { + text_without_caret + }; + action.edit.apply(&mut actual); + + match action.cursor_position { + None => { + if let RangeOrOffset::Offset(before_cursor_pos) = range_or_offset { + let off = action + .edit + .apply_to_offset(before_cursor_pos) + .expect("cursor position is affected by the edit"); + actual = add_cursor(&actual, off) + } + } + Some(off) => actual = add_cursor(&actual, off), + }; + + assert_eq_text!(after, &actual); + } + (Some(assist), ExpectedResult::Target(target)) => { + let action = assist.0[0].action.clone().unwrap(); + let range = action.target.expect("expected target on action"); + assert_eq_text!(&text_without_caret[range], target); + } + (Some(_), ExpectedResult::NotApplicable) => panic!("assist should not be applicable!"), + (None, ExpectedResult::After(_)) | (None, ExpectedResult::Target(_)) => { + panic!("code action is not applicable") + } + (None, ExpectedResult::NotApplicable) => (), + }; +} + +#[test] +fn assist_order_field_struct() { + let before = "struct Foo { <|>bar: u32 }"; + let (before_cursor_pos, before) = extract_offset(before); + let (db, file_id) = with_single_file(&before); + let frange = FileRange { file_id, range: TextRange::empty(before_cursor_pos) }; + let assists = resolved_assists(&db, frange); + let mut assists = assists.iter(); + + assert_eq!( + assists.next().expect("expected assist").label.label, + "Change visibility to pub(crate)" + ); + assert_eq!(assists.next().expect("expected assist").label.label, "Add `#[derive]`"); +} + +#[test] +fn assist_order_if_expr() { + let before = " + pub fn test_some_range(a: int) -> bool { + if let 2..6 = <|>5<|> { + true + } else { + false + } + }"; + let (range, before) = extract_range(before); + let (db, file_id) = with_single_file(&before); + let frange = FileRange { file_id, range }; + let assists = resolved_assists(&db, frange); + let mut assists = assists.iter(); + + assert_eq!(assists.next().expect("expected assist").label.label, "Extract into variable"); + assert_eq!(assists.next().expect("expected assist").label.label, "Replace with match"); +} -- cgit v1.2.3 From d308ff910b18719fabaeb9df7bc5fc0afbd0a194 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 6 May 2020 10:18:12 +0200 Subject: Merge doc_tests and tests --- crates/ra_assists/src/doc_tests.rs | 39 -- crates/ra_assists/src/doc_tests/generated.rs | 749 --------------------------- crates/ra_assists/src/lib.rs | 2 - crates/ra_assists/src/tests.rs | 28 + crates/ra_assists/src/tests/generated.rs | 749 +++++++++++++++++++++++++++ 5 files changed, 777 insertions(+), 790 deletions(-) delete mode 100644 crates/ra_assists/src/doc_tests.rs delete mode 100644 crates/ra_assists/src/doc_tests/generated.rs create mode 100644 crates/ra_assists/src/tests/generated.rs diff --git a/crates/ra_assists/src/doc_tests.rs b/crates/ra_assists/src/doc_tests.rs deleted file mode 100644 index 5eabb33d0..000000000 --- a/crates/ra_assists/src/doc_tests.rs +++ /dev/null @@ -1,39 +0,0 @@ -//! Each assist definition has a special comment, which specifies docs and -//! example. -//! -//! We collect all the example and write the as tests in this module. - -mod generated; - -use ra_db::FileRange; -use test_utils::{assert_eq_text, extract_range_or_offset}; - -use crate::resolved_assists; - -fn check(assist_id: &str, before: &str, after: &str) { - let (selection, before) = extract_range_or_offset(before); - let (db, file_id) = crate::tests::with_single_file(&before); - let frange = FileRange { file_id, range: selection.into() }; - - let assist = resolved_assists(&db, frange) - .into_iter() - .find(|assist| assist.label.id.0 == assist_id) - .unwrap_or_else(|| { - panic!( - "\n\nAssist is not applicable: {}\nAvailable assists: {}", - assist_id, - resolved_assists(&db, frange) - .into_iter() - .map(|assist| assist.label.id.0) - .collect::>() - .join(", ") - ) - }); - - let actual = { - let mut actual = before.clone(); - assist.action.edit.apply(&mut actual); - actual - }; - assert_eq_text!(after, &actual); -} diff --git a/crates/ra_assists/src/doc_tests/generated.rs b/crates/ra_assists/src/doc_tests/generated.rs deleted file mode 100644 index 6696cc832..000000000 --- a/crates/ra_assists/src/doc_tests/generated.rs +++ /dev/null @@ -1,749 +0,0 @@ -//! Generated file, do not edit by hand, see `xtask/src/codegen` - -use super::check; - -#[test] -fn doctest_add_custom_impl() { - check( - "add_custom_impl", - r#####" -#[derive(Deb<|>ug, Display)] -struct S; -"#####, - r#####" -#[derive(Display)] -struct S; - -impl Debug for S { - -} -"#####, - ) -} - -#[test] -fn doctest_add_derive() { - check( - "add_derive", - r#####" -struct Point { - x: u32, - y: u32,<|> -} -"#####, - r#####" -#[derive()] -struct Point { - x: u32, - y: u32, -} -"#####, - ) -} - -#[test] -fn doctest_add_explicit_type() { - check( - "add_explicit_type", - r#####" -fn main() { - let x<|> = 92; -} -"#####, - r#####" -fn main() { - let x: i32 = 92; -} -"#####, - ) -} - -#[test] -fn doctest_add_function() { - check( - "add_function", - r#####" -struct Baz; -fn baz() -> Baz { Baz } -fn foo() { - bar<|>("", baz()); -} - -"#####, - r#####" -struct Baz; -fn baz() -> Baz { Baz } -fn foo() { - bar("", baz()); -} - -fn bar(arg: &str, baz: Baz) { - todo!() -} - -"#####, - ) -} - -#[test] -fn doctest_add_hash() { - check( - "add_hash", - r#####" -fn main() { - r#"Hello,<|> World!"#; -} -"#####, - r#####" -fn main() { - r##"Hello, World!"##; -} -"#####, - ) -} - -#[test] -fn doctest_add_impl() { - check( - "add_impl", - r#####" -struct Ctx { - data: T,<|> -} -"#####, - r#####" -struct Ctx { - data: T, -} - -impl Ctx { - -} -"#####, - ) -} - -#[test] -fn doctest_add_impl_default_members() { - check( - "add_impl_default_members", - r#####" -trait Trait { - Type X; - fn foo(&self); - fn bar(&self) {} -} - -impl Trait for () { - Type X = (); - fn foo(&self) {}<|> - -} -"#####, - r#####" -trait Trait { - Type X; - fn foo(&self); - fn bar(&self) {} -} - -impl Trait for () { - Type X = (); - fn foo(&self) {} - fn bar(&self) {} - -} -"#####, - ) -} - -#[test] -fn doctest_add_impl_missing_members() { - check( - "add_impl_missing_members", - r#####" -trait Trait { - Type X; - fn foo(&self) -> T; - fn bar(&self) {} -} - -impl Trait for () {<|> - -} -"#####, - r#####" -trait Trait { - Type X; - fn foo(&self) -> T; - fn bar(&self) {} -} - -impl Trait for () { - fn foo(&self) -> u32 { - todo!() - } - -} -"#####, - ) -} - -#[test] -fn doctest_add_new() { - check( - "add_new", - r#####" -struct Ctx { - data: T,<|> -} -"#####, - r#####" -struct Ctx { - data: T, -} - -impl Ctx { - fn new(data: T) -> Self { Self { data } } -} - -"#####, - ) -} - -#[test] -fn doctest_apply_demorgan() { - check( - "apply_demorgan", - r#####" -fn main() { - if x != 4 ||<|> !y {} -} -"#####, - r#####" -fn main() { - if !(x == 4 && y) {} -} -"#####, - ) -} - -#[test] -fn doctest_auto_import() { - check( - "auto_import", - r#####" -fn main() { - let map = HashMap<|>::new(); -} -pub mod std { pub mod collections { pub struct HashMap { } } } -"#####, - r#####" -use std::collections::HashMap; - -fn main() { - let map = HashMap::new(); -} -pub mod std { pub mod collections { pub struct HashMap { } } } -"#####, - ) -} - -#[test] -fn doctest_change_visibility() { - check( - "change_visibility", - r#####" -<|>fn frobnicate() {} -"#####, - r#####" -pub(crate) fn frobnicate() {} -"#####, - ) -} - -#[test] -fn doctest_convert_to_guarded_return() { - check( - "convert_to_guarded_return", - r#####" -fn main() { - <|>if cond { - foo(); - bar(); - } -} -"#####, - r#####" -fn main() { - if !cond { - return; - } - foo(); - bar(); -} -"#####, - ) -} - -#[test] -fn doctest_fill_match_arms() { - check( - "fill_match_arms", - r#####" -enum Action { Move { distance: u32 }, Stop } - -fn handle(action: Action) { - match action { - <|> - } -} -"#####, - r#####" -enum Action { Move { distance: u32 }, Stop } - -fn handle(action: Action) { - match action { - Action::Move { distance } => {} - Action::Stop => {} - } -} -"#####, - ) -} - -#[test] -fn doctest_flip_binexpr() { - check( - "flip_binexpr", - r#####" -fn main() { - let _ = 90 +<|> 2; -} -"#####, - r#####" -fn main() { - let _ = 2 + 90; -} -"#####, - ) -} - -#[test] -fn doctest_flip_comma() { - check( - "flip_comma", - r#####" -fn main() { - ((1, 2),<|> (3, 4)); -} -"#####, - r#####" -fn main() { - ((3, 4), (1, 2)); -} -"#####, - ) -} - -#[test] -fn doctest_flip_trait_bound() { - check( - "flip_trait_bound", - r#####" -fn foo Copy>() { } -"#####, - r#####" -fn foo() { } -"#####, - ) -} - -#[test] -fn doctest_inline_local_variable() { - check( - "inline_local_variable", - r#####" -fn main() { - let x<|> = 1 + 2; - x * 4; -} -"#####, - r#####" -fn main() { - (1 + 2) * 4; -} -"#####, - ) -} - -#[test] -fn doctest_introduce_variable() { - check( - "introduce_variable", - r#####" -fn main() { - <|>(1 + 2)<|> * 4; -} -"#####, - r#####" -fn main() { - let var_name = (1 + 2); - var_name * 4; -} -"#####, - ) -} - -#[test] -fn doctest_invert_if() { - check( - "invert_if", - r#####" -fn main() { - if<|> !y { A } else { B } -} -"#####, - r#####" -fn main() { - if y { B } else { A } -} -"#####, - ) -} - -#[test] -fn doctest_make_raw_string() { - check( - "make_raw_string", - r#####" -fn main() { - "Hello,<|> World!"; -} -"#####, - r#####" -fn main() { - r#"Hello, World!"#; -} -"#####, - ) -} - -#[test] -fn doctest_make_usual_string() { - check( - "make_usual_string", - r#####" -fn main() { - r#"Hello,<|> "World!""#; -} -"#####, - r#####" -fn main() { - "Hello, \"World!\""; -} -"#####, - ) -} - -#[test] -fn doctest_merge_imports() { - check( - "merge_imports", - r#####" -use std::<|>fmt::Formatter; -use std::io; -"#####, - r#####" -use std::{fmt::Formatter, io}; -"#####, - ) -} - -#[test] -fn doctest_merge_match_arms() { - check( - "merge_match_arms", - r#####" -enum Action { Move { distance: u32 }, Stop } - -fn handle(action: Action) { - match action { - <|>Action::Move(..) => foo(), - Action::Stop => foo(), - } -} -"#####, - r#####" -enum Action { Move { distance: u32 }, Stop } - -fn handle(action: Action) { - match action { - Action::Move(..) | Action::Stop => foo(), - } -} -"#####, - ) -} - -#[test] -fn doctest_move_arm_cond_to_match_guard() { - check( - "move_arm_cond_to_match_guard", - r#####" -enum Action { Move { distance: u32 }, Stop } - -fn handle(action: Action) { - match action { - Action::Move { distance } => <|>if distance > 10 { foo() }, - _ => (), - } -} -"#####, - r#####" -enum Action { Move { distance: u32 }, Stop } - -fn handle(action: Action) { - match action { - Action::Move { distance } if distance > 10 => foo(), - _ => (), - } -} -"#####, - ) -} - -#[test] -fn doctest_move_bounds_to_where_clause() { - check( - "move_bounds_to_where_clause", - r#####" -fn applyF: FnOnce(T) -> U>(f: F, x: T) -> U { - f(x) -} -"#####, - r#####" -fn apply(f: F, x: T) -> U where F: FnOnce(T) -> U { - f(x) -} -"#####, - ) -} - -#[test] -fn doctest_move_guard_to_arm_body() { - check( - "move_guard_to_arm_body", - r#####" -enum Action { Move { distance: u32 }, Stop } - -fn handle(action: Action) { - match action { - Action::Move { distance } <|>if distance > 10 => foo(), - _ => (), - } -} -"#####, - r#####" -enum Action { Move { distance: u32 }, Stop } - -fn handle(action: Action) { - match action { - Action::Move { distance } => if distance > 10 { foo() }, - _ => (), - } -} -"#####, - ) -} - -#[test] -fn doctest_remove_dbg() { - check( - "remove_dbg", - r#####" -fn main() { - <|>dbg!(92); -} -"#####, - r#####" -fn main() { - 92; -} -"#####, - ) -} - -#[test] -fn doctest_remove_hash() { - check( - "remove_hash", - r#####" -fn main() { - r#"Hello,<|> World!"#; -} -"#####, - r#####" -fn main() { - r"Hello, World!"; -} -"#####, - ) -} - -#[test] -fn doctest_remove_mut() { - check( - "remove_mut", - r#####" -impl Walrus { - fn feed(&mut<|> self, amount: u32) {} -} -"#####, - r#####" -impl Walrus { - fn feed(&self, amount: u32) {} -} -"#####, - ) -} - -#[test] -fn doctest_reorder_fields() { - check( - "reorder_fields", - r#####" -struct Foo {foo: i32, bar: i32}; -const test: Foo = <|>Foo {bar: 0, foo: 1} -"#####, - r#####" -struct Foo {foo: i32, bar: i32}; -const test: Foo = Foo {foo: 1, bar: 0} -"#####, - ) -} - -#[test] -fn doctest_replace_if_let_with_match() { - check( - "replace_if_let_with_match", - r#####" -enum Action { Move { distance: u32 }, Stop } - -fn handle(action: Action) { - <|>if let Action::Move { distance } = action { - foo(distance) - } else { - bar() - } -} -"#####, - r#####" -enum Action { Move { distance: u32 }, Stop } - -fn handle(action: Action) { - match action { - Action::Move { distance } => foo(distance), - _ => bar(), - } -} -"#####, - ) -} - -#[test] -fn doctest_replace_let_with_if_let() { - check( - "replace_let_with_if_let", - r#####" -enum Option { Some(T), None } - -fn main(action: Action) { - <|>let x = compute(); -} - -fn compute() -> Option { None } -"#####, - r#####" -enum Option { Some(T), None } - -fn main(action: Action) { - if let Some(x) = compute() { - } -} - -fn compute() -> Option { None } -"#####, - ) -} - -#[test] -fn doctest_replace_qualified_name_with_use() { - check( - "replace_qualified_name_with_use", - r#####" -fn process(map: std::collections::<|>HashMap) {} -"#####, - r#####" -use std::collections::HashMap; - -fn process(map: HashMap) {} -"#####, - ) -} - -#[test] -fn doctest_replace_unwrap_with_match() { - check( - "replace_unwrap_with_match", - r#####" -enum Result { Ok(T), Err(E) } -fn main() { - let x: Result = Result::Ok(92); - let y = x.<|>unwrap(); -} -"#####, - r#####" -enum Result { Ok(T), Err(E) } -fn main() { - let x: Result = Result::Ok(92); - let y = match x { - Ok(a) => a, - _ => unreachable!(), - }; -} -"#####, - ) -} - -#[test] -fn doctest_split_import() { - check( - "split_import", - r#####" -use std::<|>collections::HashMap; -"#####, - r#####" -use std::{collections::HashMap}; -"#####, - ) -} - -#[test] -fn doctest_unwrap_block() { - check( - "unwrap_block", - r#####" -fn foo() { - if true {<|> - println!("foo"); - } -} -"#####, - r#####" -fn foo() { - println!("foo"); -} -"#####, - ) -} diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index b84d60c77..b794b021d 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -13,8 +13,6 @@ macro_rules! eprintln { mod assist_ctx; mod marks; #[cfg(test)] -mod doc_tests; -#[cfg(test)] mod tests; pub mod utils; pub mod ast_transform; diff --git a/crates/ra_assists/src/tests.rs b/crates/ra_assists/src/tests.rs index ca3096a43..97744904a 100644 --- a/crates/ra_assists/src/tests.rs +++ b/crates/ra_assists/src/tests.rs @@ -34,6 +34,34 @@ pub(crate) fn check_assist_not_applicable(assist: Handler, ra_fixture: &str) { check(assist, ra_fixture, ExpectedResult::NotApplicable); } +fn check_doc_test(assist_id: &str, before: &str, after: &str) { + let (selection, before) = extract_range_or_offset(before); + let (db, file_id) = crate::tests::with_single_file(&before); + let frange = FileRange { file_id, range: selection.into() }; + + let assist = resolved_assists(&db, frange) + .into_iter() + .find(|assist| assist.label.id.0 == assist_id) + .unwrap_or_else(|| { + panic!( + "\n\nAssist is not applicable: {}\nAvailable assists: {}", + assist_id, + resolved_assists(&db, frange) + .into_iter() + .map(|assist| assist.label.id.0) + .collect::>() + .join(", ") + ) + }); + + let actual = { + let mut actual = before.clone(); + assist.action.edit.apply(&mut actual); + actual + }; + assert_eq_text!(after, &actual); +} + enum ExpectedResult<'a> { NotApplicable, After(&'a str), diff --git a/crates/ra_assists/src/tests/generated.rs b/crates/ra_assists/src/tests/generated.rs new file mode 100644 index 000000000..6696cc832 --- /dev/null +++ b/crates/ra_assists/src/tests/generated.rs @@ -0,0 +1,749 @@ +//! Generated file, do not edit by hand, see `xtask/src/codegen` + +use super::check; + +#[test] +fn doctest_add_custom_impl() { + check( + "add_custom_impl", + r#####" +#[derive(Deb<|>ug, Display)] +struct S; +"#####, + r#####" +#[derive(Display)] +struct S; + +impl Debug for S { + +} +"#####, + ) +} + +#[test] +fn doctest_add_derive() { + check( + "add_derive", + r#####" +struct Point { + x: u32, + y: u32,<|> +} +"#####, + r#####" +#[derive()] +struct Point { + x: u32, + y: u32, +} +"#####, + ) +} + +#[test] +fn doctest_add_explicit_type() { + check( + "add_explicit_type", + r#####" +fn main() { + let x<|> = 92; +} +"#####, + r#####" +fn main() { + let x: i32 = 92; +} +"#####, + ) +} + +#[test] +fn doctest_add_function() { + check( + "add_function", + r#####" +struct Baz; +fn baz() -> Baz { Baz } +fn foo() { + bar<|>("", baz()); +} + +"#####, + r#####" +struct Baz; +fn baz() -> Baz { Baz } +fn foo() { + bar("", baz()); +} + +fn bar(arg: &str, baz: Baz) { + todo!() +} + +"#####, + ) +} + +#[test] +fn doctest_add_hash() { + check( + "add_hash", + r#####" +fn main() { + r#"Hello,<|> World!"#; +} +"#####, + r#####" +fn main() { + r##"Hello, World!"##; +} +"#####, + ) +} + +#[test] +fn doctest_add_impl() { + check( + "add_impl", + r#####" +struct Ctx { + data: T,<|> +} +"#####, + r#####" +struct Ctx { + data: T, +} + +impl Ctx { + +} +"#####, + ) +} + +#[test] +fn doctest_add_impl_default_members() { + check( + "add_impl_default_members", + r#####" +trait Trait { + Type X; + fn foo(&self); + fn bar(&self) {} +} + +impl Trait for () { + Type X = (); + fn foo(&self) {}<|> + +} +"#####, + r#####" +trait Trait { + Type X; + fn foo(&self); + fn bar(&self) {} +} + +impl Trait for () { + Type X = (); + fn foo(&self) {} + fn bar(&self) {} + +} +"#####, + ) +} + +#[test] +fn doctest_add_impl_missing_members() { + check( + "add_impl_missing_members", + r#####" +trait Trait { + Type X; + fn foo(&self) -> T; + fn bar(&self) {} +} + +impl Trait for () {<|> + +} +"#####, + r#####" +trait Trait { + Type X; + fn foo(&self) -> T; + fn bar(&self) {} +} + +impl Trait for () { + fn foo(&self) -> u32 { + todo!() + } + +} +"#####, + ) +} + +#[test] +fn doctest_add_new() { + check( + "add_new", + r#####" +struct Ctx { + data: T,<|> +} +"#####, + r#####" +struct Ctx { + data: T, +} + +impl Ctx { + fn new(data: T) -> Self { Self { data } } +} + +"#####, + ) +} + +#[test] +fn doctest_apply_demorgan() { + check( + "apply_demorgan", + r#####" +fn main() { + if x != 4 ||<|> !y {} +} +"#####, + r#####" +fn main() { + if !(x == 4 && y) {} +} +"#####, + ) +} + +#[test] +fn doctest_auto_import() { + check( + "auto_import", + r#####" +fn main() { + let map = HashMap<|>::new(); +} +pub mod std { pub mod collections { pub struct HashMap { } } } +"#####, + r#####" +use std::collections::HashMap; + +fn main() { + let map = HashMap::new(); +} +pub mod std { pub mod collections { pub struct HashMap { } } } +"#####, + ) +} + +#[test] +fn doctest_change_visibility() { + check( + "change_visibility", + r#####" +<|>fn frobnicate() {} +"#####, + r#####" +pub(crate) fn frobnicate() {} +"#####, + ) +} + +#[test] +fn doctest_convert_to_guarded_return() { + check( + "convert_to_guarded_return", + r#####" +fn main() { + <|>if cond { + foo(); + bar(); + } +} +"#####, + r#####" +fn main() { + if !cond { + return; + } + foo(); + bar(); +} +"#####, + ) +} + +#[test] +fn doctest_fill_match_arms() { + check( + "fill_match_arms", + r#####" +enum Action { Move { distance: u32 }, Stop } + +fn handle(action: Action) { + match action { + <|> + } +} +"#####, + r#####" +enum Action { Move { distance: u32 }, Stop } + +fn handle(action: Action) { + match action { + Action::Move { distance } => {} + Action::Stop => {} + } +} +"#####, + ) +} + +#[test] +fn doctest_flip_binexpr() { + check( + "flip_binexpr", + r#####" +fn main() { + let _ = 90 +<|> 2; +} +"#####, + r#####" +fn main() { + let _ = 2 + 90; +} +"#####, + ) +} + +#[test] +fn doctest_flip_comma() { + check( + "flip_comma", + r#####" +fn main() { + ((1, 2),<|> (3, 4)); +} +"#####, + r#####" +fn main() { + ((3, 4), (1, 2)); +} +"#####, + ) +} + +#[test] +fn doctest_flip_trait_bound() { + check( + "flip_trait_bound", + r#####" +fn foo Copy>() { } +"#####, + r#####" +fn foo() { } +"#####, + ) +} + +#[test] +fn doctest_inline_local_variable() { + check( + "inline_local_variable", + r#####" +fn main() { + let x<|> = 1 + 2; + x * 4; +} +"#####, + r#####" +fn main() { + (1 + 2) * 4; +} +"#####, + ) +} + +#[test] +fn doctest_introduce_variable() { + check( + "introduce_variable", + r#####" +fn main() { + <|>(1 + 2)<|> * 4; +} +"#####, + r#####" +fn main() { + let var_name = (1 + 2); + var_name * 4; +} +"#####, + ) +} + +#[test] +fn doctest_invert_if() { + check( + "invert_if", + r#####" +fn main() { + if<|> !y { A } else { B } +} +"#####, + r#####" +fn main() { + if y { B } else { A } +} +"#####, + ) +} + +#[test] +fn doctest_make_raw_string() { + check( + "make_raw_string", + r#####" +fn main() { + "Hello,<|> World!"; +} +"#####, + r#####" +fn main() { + r#"Hello, World!"#; +} +"#####, + ) +} + +#[test] +fn doctest_make_usual_string() { + check( + "make_usual_string", + r#####" +fn main() { + r#"Hello,<|> "World!""#; +} +"#####, + r#####" +fn main() { + "Hello, \"World!\""; +} +"#####, + ) +} + +#[test] +fn doctest_merge_imports() { + check( + "merge_imports", + r#####" +use std::<|>fmt::Formatter; +use std::io; +"#####, + r#####" +use std::{fmt::Formatter, io}; +"#####, + ) +} + +#[test] +fn doctest_merge_match_arms() { + check( + "merge_match_arms", + r#####" +enum Action { Move { distance: u32 }, Stop } + +fn handle(action: Action) { + match action { + <|>Action::Move(..) => foo(), + Action::Stop => foo(), + } +} +"#####, + r#####" +enum Action { Move { distance: u32 }, Stop } + +fn handle(action: Action) { + match action { + Action::Move(..) | Action::Stop => foo(), + } +} +"#####, + ) +} + +#[test] +fn doctest_move_arm_cond_to_match_guard() { + check( + "move_arm_cond_to_match_guard", + r#####" +enum Action { Move { distance: u32 }, Stop } + +fn handle(action: Action) { + match action { + Action::Move { distance } => <|>if distance > 10 { foo() }, + _ => (), + } +} +"#####, + r#####" +enum Action { Move { distance: u32 }, Stop } + +fn handle(action: Action) { + match action { + Action::Move { distance } if distance > 10 => foo(), + _ => (), + } +} +"#####, + ) +} + +#[test] +fn doctest_move_bounds_to_where_clause() { + check( + "move_bounds_to_where_clause", + r#####" +fn applyF: FnOnce(T) -> U>(f: F, x: T) -> U { + f(x) +} +"#####, + r#####" +fn apply(f: F, x: T) -> U where F: FnOnce(T) -> U { + f(x) +} +"#####, + ) +} + +#[test] +fn doctest_move_guard_to_arm_body() { + check( + "move_guard_to_arm_body", + r#####" +enum Action { Move { distance: u32 }, Stop } + +fn handle(action: Action) { + match action { + Action::Move { distance } <|>if distance > 10 => foo(), + _ => (), + } +} +"#####, + r#####" +enum Action { Move { distance: u32 }, Stop } + +fn handle(action: Action) { + match action { + Action::Move { distance } => if distance > 10 { foo() }, + _ => (), + } +} +"#####, + ) +} + +#[test] +fn doctest_remove_dbg() { + check( + "remove_dbg", + r#####" +fn main() { + <|>dbg!(92); +} +"#####, + r#####" +fn main() { + 92; +} +"#####, + ) +} + +#[test] +fn doctest_remove_hash() { + check( + "remove_hash", + r#####" +fn main() { + r#"Hello,<|> World!"#; +} +"#####, + r#####" +fn main() { + r"Hello, World!"; +} +"#####, + ) +} + +#[test] +fn doctest_remove_mut() { + check( + "remove_mut", + r#####" +impl Walrus { + fn feed(&mut<|> self, amount: u32) {} +} +"#####, + r#####" +impl Walrus { + fn feed(&self, amount: u32) {} +} +"#####, + ) +} + +#[test] +fn doctest_reorder_fields() { + check( + "reorder_fields", + r#####" +struct Foo {foo: i32, bar: i32}; +const test: Foo = <|>Foo {bar: 0, foo: 1} +"#####, + r#####" +struct Foo {foo: i32, bar: i32}; +const test: Foo = Foo {foo: 1, bar: 0} +"#####, + ) +} + +#[test] +fn doctest_replace_if_let_with_match() { + check( + "replace_if_let_with_match", + r#####" +enum Action { Move { distance: u32 }, Stop } + +fn handle(action: Action) { + <|>if let Action::Move { distance } = action { + foo(distance) + } else { + bar() + } +} +"#####, + r#####" +enum Action { Move { distance: u32 }, Stop } + +fn handle(action: Action) { + match action { + Action::Move { distance } => foo(distance), + _ => bar(), + } +} +"#####, + ) +} + +#[test] +fn doctest_replace_let_with_if_let() { + check( + "replace_let_with_if_let", + r#####" +enum Option { Some(T), None } + +fn main(action: Action) { + <|>let x = compute(); +} + +fn compute() -> Option { None } +"#####, + r#####" +enum Option { Some(T), None } + +fn main(action: Action) { + if let Some(x) = compute() { + } +} + +fn compute() -> Option { None } +"#####, + ) +} + +#[test] +fn doctest_replace_qualified_name_with_use() { + check( + "replace_qualified_name_with_use", + r#####" +fn process(map: std::collections::<|>HashMap) {} +"#####, + r#####" +use std::collections::HashMap; + +fn process(map: HashMap) {} +"#####, + ) +} + +#[test] +fn doctest_replace_unwrap_with_match() { + check( + "replace_unwrap_with_match", + r#####" +enum Result { Ok(T), Err(E) } +fn main() { + let x: Result = Result::Ok(92); + let y = x.<|>unwrap(); +} +"#####, + r#####" +enum Result { Ok(T), Err(E) } +fn main() { + let x: Result = Result::Ok(92); + let y = match x { + Ok(a) => a, + _ => unreachable!(), + }; +} +"#####, + ) +} + +#[test] +fn doctest_split_import() { + check( + "split_import", + r#####" +use std::<|>collections::HashMap; +"#####, + r#####" +use std::{collections::HashMap}; +"#####, + ) +} + +#[test] +fn doctest_unwrap_block() { + check( + "unwrap_block", + r#####" +fn foo() { + if true {<|> + println!("foo"); + } +} +"#####, + r#####" +fn foo() { + println!("foo"); +} +"#####, + ) +} -- cgit v1.2.3 From eea431180eb5267928c8e0d4b8099ab1042e01d1 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 6 May 2020 10:21:35 +0200 Subject: Regenerate --- crates/ra_assists/src/tests.rs | 2 + crates/ra_assists/src/tests/generated.rs | 76 ++++++++++++++++---------------- xtask/src/codegen.rs | 2 +- xtask/src/codegen/gen_assists_docs.rs | 4 +- xtask/tests/tidy-tests/main.rs | 2 +- 5 files changed, 44 insertions(+), 42 deletions(-) diff --git a/crates/ra_assists/src/tests.rs b/crates/ra_assists/src/tests.rs index 97744904a..483e11931 100644 --- a/crates/ra_assists/src/tests.rs +++ b/crates/ra_assists/src/tests.rs @@ -1,3 +1,5 @@ +mod generated; + use std::sync::Arc; use hir::Semantics; diff --git a/crates/ra_assists/src/tests/generated.rs b/crates/ra_assists/src/tests/generated.rs index 6696cc832..7d35fa284 100644 --- a/crates/ra_assists/src/tests/generated.rs +++ b/crates/ra_assists/src/tests/generated.rs @@ -1,10 +1,10 @@ //! Generated file, do not edit by hand, see `xtask/src/codegen` -use super::check; +use super::check_doc_test; #[test] fn doctest_add_custom_impl() { - check( + check_doc_test( "add_custom_impl", r#####" #[derive(Deb<|>ug, Display)] @@ -23,7 +23,7 @@ impl Debug for S { #[test] fn doctest_add_derive() { - check( + check_doc_test( "add_derive", r#####" struct Point { @@ -43,7 +43,7 @@ struct Point { #[test] fn doctest_add_explicit_type() { - check( + check_doc_test( "add_explicit_type", r#####" fn main() { @@ -60,7 +60,7 @@ fn main() { #[test] fn doctest_add_function() { - check( + check_doc_test( "add_function", r#####" struct Baz; @@ -87,7 +87,7 @@ fn bar(arg: &str, baz: Baz) { #[test] fn doctest_add_hash() { - check( + check_doc_test( "add_hash", r#####" fn main() { @@ -104,7 +104,7 @@ fn main() { #[test] fn doctest_add_impl() { - check( + check_doc_test( "add_impl", r#####" struct Ctx { @@ -125,7 +125,7 @@ impl Ctx { #[test] fn doctest_add_impl_default_members() { - check( + check_doc_test( "add_impl_default_members", r#####" trait Trait { @@ -159,7 +159,7 @@ impl Trait for () { #[test] fn doctest_add_impl_missing_members() { - check( + check_doc_test( "add_impl_missing_members", r#####" trait Trait { @@ -191,7 +191,7 @@ impl Trait for () { #[test] fn doctest_add_new() { - check( + check_doc_test( "add_new", r#####" struct Ctx { @@ -213,7 +213,7 @@ impl Ctx { #[test] fn doctest_apply_demorgan() { - check( + check_doc_test( "apply_demorgan", r#####" fn main() { @@ -230,7 +230,7 @@ fn main() { #[test] fn doctest_auto_import() { - check( + check_doc_test( "auto_import", r#####" fn main() { @@ -251,7 +251,7 @@ pub mod std { pub mod collections { pub struct HashMap { } } } #[test] fn doctest_change_visibility() { - check( + check_doc_test( "change_visibility", r#####" <|>fn frobnicate() {} @@ -264,7 +264,7 @@ pub(crate) fn frobnicate() {} #[test] fn doctest_convert_to_guarded_return() { - check( + check_doc_test( "convert_to_guarded_return", r#####" fn main() { @@ -288,7 +288,7 @@ fn main() { #[test] fn doctest_fill_match_arms() { - check( + check_doc_test( "fill_match_arms", r#####" enum Action { Move { distance: u32 }, Stop } @@ -314,7 +314,7 @@ fn handle(action: Action) { #[test] fn doctest_flip_binexpr() { - check( + check_doc_test( "flip_binexpr", r#####" fn main() { @@ -331,7 +331,7 @@ fn main() { #[test] fn doctest_flip_comma() { - check( + check_doc_test( "flip_comma", r#####" fn main() { @@ -348,7 +348,7 @@ fn main() { #[test] fn doctest_flip_trait_bound() { - check( + check_doc_test( "flip_trait_bound", r#####" fn foo Copy>() { } @@ -361,7 +361,7 @@ fn foo() { } #[test] fn doctest_inline_local_variable() { - check( + check_doc_test( "inline_local_variable", r#####" fn main() { @@ -379,7 +379,7 @@ fn main() { #[test] fn doctest_introduce_variable() { - check( + check_doc_test( "introduce_variable", r#####" fn main() { @@ -397,7 +397,7 @@ fn main() { #[test] fn doctest_invert_if() { - check( + check_doc_test( "invert_if", r#####" fn main() { @@ -414,7 +414,7 @@ fn main() { #[test] fn doctest_make_raw_string() { - check( + check_doc_test( "make_raw_string", r#####" fn main() { @@ -431,7 +431,7 @@ fn main() { #[test] fn doctest_make_usual_string() { - check( + check_doc_test( "make_usual_string", r#####" fn main() { @@ -448,7 +448,7 @@ fn main() { #[test] fn doctest_merge_imports() { - check( + check_doc_test( "merge_imports", r#####" use std::<|>fmt::Formatter; @@ -462,7 +462,7 @@ use std::{fmt::Formatter, io}; #[test] fn doctest_merge_match_arms() { - check( + check_doc_test( "merge_match_arms", r#####" enum Action { Move { distance: u32 }, Stop } @@ -488,7 +488,7 @@ fn handle(action: Action) { #[test] fn doctest_move_arm_cond_to_match_guard() { - check( + check_doc_test( "move_arm_cond_to_match_guard", r#####" enum Action { Move { distance: u32 }, Stop } @@ -515,7 +515,7 @@ fn handle(action: Action) { #[test] fn doctest_move_bounds_to_where_clause() { - check( + check_doc_test( "move_bounds_to_where_clause", r#####" fn applyF: FnOnce(T) -> U>(f: F, x: T) -> U { @@ -532,7 +532,7 @@ fn apply(f: F, x: T) -> U where F: FnOnce(T) -> U { #[test] fn doctest_move_guard_to_arm_body() { - check( + check_doc_test( "move_guard_to_arm_body", r#####" enum Action { Move { distance: u32 }, Stop } @@ -559,7 +559,7 @@ fn handle(action: Action) { #[test] fn doctest_remove_dbg() { - check( + check_doc_test( "remove_dbg", r#####" fn main() { @@ -576,7 +576,7 @@ fn main() { #[test] fn doctest_remove_hash() { - check( + check_doc_test( "remove_hash", r#####" fn main() { @@ -593,7 +593,7 @@ fn main() { #[test] fn doctest_remove_mut() { - check( + check_doc_test( "remove_mut", r#####" impl Walrus { @@ -610,7 +610,7 @@ impl Walrus { #[test] fn doctest_reorder_fields() { - check( + check_doc_test( "reorder_fields", r#####" struct Foo {foo: i32, bar: i32}; @@ -625,7 +625,7 @@ const test: Foo = Foo {foo: 1, bar: 0} #[test] fn doctest_replace_if_let_with_match() { - check( + check_doc_test( "replace_if_let_with_match", r#####" enum Action { Move { distance: u32 }, Stop } @@ -653,7 +653,7 @@ fn handle(action: Action) { #[test] fn doctest_replace_let_with_if_let() { - check( + check_doc_test( "replace_let_with_if_let", r#####" enum Option { Some(T), None } @@ -679,7 +679,7 @@ fn compute() -> Option { None } #[test] fn doctest_replace_qualified_name_with_use() { - check( + check_doc_test( "replace_qualified_name_with_use", r#####" fn process(map: std::collections::<|>HashMap) {} @@ -694,7 +694,7 @@ fn process(map: HashMap) {} #[test] fn doctest_replace_unwrap_with_match() { - check( + check_doc_test( "replace_unwrap_with_match", r#####" enum Result { Ok(T), Err(E) } @@ -718,7 +718,7 @@ fn main() { #[test] fn doctest_split_import() { - check( + check_doc_test( "split_import", r#####" use std::<|>collections::HashMap; @@ -731,7 +731,7 @@ use std::{collections::HashMap}; #[test] fn doctest_unwrap_block() { - check( + check_doc_test( "unwrap_block", r#####" fn foo() { diff --git a/xtask/src/codegen.rs b/xtask/src/codegen.rs index 0e4dcb95a..b4907f4b2 100644 --- a/xtask/src/codegen.rs +++ b/xtask/src/codegen.rs @@ -27,7 +27,7 @@ const AST_NODES: &str = "crates/ra_syntax/src/ast/generated/nodes.rs"; const AST_TOKENS: &str = "crates/ra_syntax/src/ast/generated/tokens.rs"; const ASSISTS_DIR: &str = "crates/ra_assists/src/handlers"; -const ASSISTS_TESTS: &str = "crates/ra_assists/src/doc_tests/generated.rs"; +const ASSISTS_TESTS: &str = "crates/ra_assists/src/tests/generated.rs"; const ASSISTS_DOCS: &str = "docs/user/assists.md"; #[derive(Debug, PartialEq, Eq, Clone, Copy)] diff --git a/xtask/src/codegen/gen_assists_docs.rs b/xtask/src/codegen/gen_assists_docs.rs index 31d606535..4bd6b5f0c 100644 --- a/xtask/src/codegen/gen_assists_docs.rs +++ b/xtask/src/codegen/gen_assists_docs.rs @@ -101,14 +101,14 @@ fn collect_assists() -> Result> { } fn generate_tests(assists: &[Assist], mode: Mode) -> Result<()> { - let mut buf = String::from("use super::check;\n"); + let mut buf = String::from("use super::check_doc_test;\n"); for assist in assists.iter() { let test = format!( r######" #[test] fn doctest_{}() {{ - check( + check_doc_test( "{}", r#####" {}"#####, r#####" diff --git a/xtask/tests/tidy-tests/main.rs b/xtask/tests/tidy-tests/main.rs index 3213c4dfa..a3c2e37d1 100644 --- a/xtask/tests/tidy-tests/main.rs +++ b/xtask/tests/tidy-tests/main.rs @@ -24,7 +24,7 @@ fn check_todo(path: &Path, text: &str) { // This file itself is whitelisted since this test itself contains matches. "tests/cli.rs", // Some of our assists generate `todo!()` so those files are whitelisted. - "doc_tests/generated.rs", + "tests/generated.rs", "handlers/add_missing_impl_members.rs", "handlers/add_function.rs", // To support generating `todo!()` in assists, we have `expr_todo()` in ast::make. -- cgit v1.2.3