From d5c96672aac6fd4ac25474814879af2c04bd798b Mon Sep 17 00:00:00 2001 From: Dawer <7803845+iDawer@users.noreply.github.com> Date: Fri, 21 May 2021 11:27:41 +0500 Subject: Test `fill_match_arms` for lazy computation. This also adds `ide_assists::tests::check_assist_unresolved` function. --- crates/ide_assists/src/handlers/fill_match_arms.rs | 20 +++++++++++++++++++- crates/ide_assists/src/tests.rs | 21 +++++++++++++++++++-- 2 files changed, 38 insertions(+), 3 deletions(-) (limited to 'crates') diff --git a/crates/ide_assists/src/handlers/fill_match_arms.rs b/crates/ide_assists/src/handlers/fill_match_arms.rs index 3532759bf..97435f021 100644 --- a/crates/ide_assists/src/handlers/fill_match_arms.rs +++ b/crates/ide_assists/src/handlers/fill_match_arms.rs @@ -105,6 +105,7 @@ pub(crate) fn fill_match_arms(acc: &mut Assists, ctx: &AssistContext) -> Option< let missing_pats = variants_of_enums .into_iter() .multi_cartesian_product() + .inspect(|_| cov_mark::hit!(fill_match_arms_lazy_computation)) .map(|variants| { let patterns = variants.into_iter().filter_map(|variant| build_pat(ctx.db(), module, variant)); @@ -279,7 +280,9 @@ fn build_pat(db: &RootDatabase, module: hir::Module, var: ExtendedVariant) -> Op mod tests { use ide_db::helpers::FamousDefs; - use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target}; + use crate::tests::{ + check_assist, check_assist_not_applicable, check_assist_target, check_assist_unresolved, + }; use super::fill_match_arms; @@ -1058,4 +1061,19 @@ fn foo(t: Test) { }"#, ); } + + #[test] + fn lazy_computation() { + // Computing a single missing arm is enough to determine applicability of the assist. + cov_mark::check_count!(fill_match_arms_lazy_computation, 1); + check_assist_unresolved( + fill_match_arms, + r#" +enum A { One, Two, } +fn foo(tuple: (A, A)) { + match $0tuple {}; +} +"#, + ); + } } diff --git a/crates/ide_assists/src/tests.rs b/crates/ide_assists/src/tests.rs index 0d3969c36..b091ab91a 100644 --- a/crates/ide_assists/src/tests.rs +++ b/crates/ide_assists/src/tests.rs @@ -61,6 +61,12 @@ pub(crate) fn check_assist_not_applicable(assist: Handler, ra_fixture: &str) { check(assist, ra_fixture, ExpectedResult::NotApplicable, None); } +/// Check assist in unresolved state. Useful to check assists for lazy computation. +#[track_caller] +pub(crate) fn check_assist_unresolved(assist: Handler, ra_fixture: &str) { + check(assist, ra_fixture, ExpectedResult::Unresolved, None); +} + #[track_caller] fn check_doc_test(assist_id: &str, before: &str, after: &str) { let after = trim_indent(after); @@ -97,6 +103,7 @@ fn check_doc_test(assist_id: &str, before: &str, after: &str) { enum ExpectedResult<'a> { NotApplicable, + Unresolved, After(&'a str), Target(&'a str), } @@ -111,7 +118,11 @@ fn check(handler: Handler, before: &str, expected: ExpectedResult, assist_label: let sema = Semantics::new(&db); let config = TEST_CONFIG; let ctx = AssistContext::new(sema, &config, frange); - let mut acc = Assists::new(&ctx, AssistResolveStrategy::All); + let resolve = match expected { + ExpectedResult::Unresolved => AssistResolveStrategy::None, + _ => AssistResolveStrategy::All, + }; + let mut acc = Assists::new(&ctx, resolve); handler(&mut acc, &ctx); let mut res = acc.finish(); @@ -159,8 +170,14 @@ fn check(handler: Handler, before: &str, expected: ExpectedResult, assist_label: let range = assist.target; assert_eq_text!(&text_without_caret[range], target); } + (Some(assist), ExpectedResult::Unresolved) => assert!( + assist.source_change.is_none(), + "unresolved assist should not contain source changes" + ), (Some(_), ExpectedResult::NotApplicable) => panic!("assist should not be applicable!"), - (None, ExpectedResult::After(_)) | (None, ExpectedResult::Target(_)) => { + (None, ExpectedResult::After(_)) + | (None, ExpectedResult::Target(_)) + | (None, ExpectedResult::Unresolved) => { panic!("code action is not applicable") } (None, ExpectedResult::NotApplicable) => (), -- cgit v1.2.3