From 0dd35ff2b2ceffdb926953fdacc7d30e1968047d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 25 Oct 2019 14:16:46 +0300 Subject: auto-generate assists docs and tests --- crates/ra_assists/src/assists/early_return.rs | 43 +++++++++++++-------------- crates/ra_assists/src/doc_tests.rs | 23 ++++++++++++++ crates/ra_assists/src/doc_tests/generated.rs | 27 +++++++++++++++++ crates/ra_assists/src/lib.rs | 4 ++- 4 files changed, 73 insertions(+), 24 deletions(-) create mode 100644 crates/ra_assists/src/doc_tests.rs create mode 100644 crates/ra_assists/src/doc_tests/generated.rs (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/assists/early_return.rs b/crates/ra_assists/src/assists/early_return.rs index f7d7e12e7..b3d025340 100644 --- a/crates/ra_assists/src/assists/early_return.rs +++ b/crates/ra_assists/src/assists/early_return.rs @@ -1,26 +1,3 @@ -//! Assist: `convert_to_guarded_return` -//! -//! Replace a large conditional with a guarded return. -//! -//! ```text -//! fn <|>main() { -//! if cond { -//! foo(); -//! bar(); -//! } -//! } -//! ``` -//! -> -//! ```text -//! fn main() { -//! if !cond { -//! return; -//! } -//! foo(); -//! bar(); -//! } -//! ``` - use std::ops::RangeInclusive; use hir::db::HirDatabase; @@ -36,6 +13,26 @@ use crate::{ AssistId, }; +// Assist: convert_to_guarded_return +// Replace a large conditional with a guarded return. +// ``` +// fn main() { +// <|>if cond { +// foo(); +// bar(); +// } +// } +// ``` +// -> +// ``` +// fn main() { +// if !cond { +// return; +// } +// foo(); +// bar(); +// } +// ``` pub(crate) fn convert_to_guarded_return(mut ctx: AssistCtx) -> Option { let if_expr: ast::IfExpr = ctx.node_at_offset()?; let expr = if_expr.condition()?.expr()?; diff --git a/crates/ra_assists/src/doc_tests.rs b/crates/ra_assists/src/doc_tests.rs new file mode 100644 index 000000000..88e901517 --- /dev/null +++ b/crates/ra_assists/src/doc_tests.rs @@ -0,0 +1,23 @@ +//! 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 hir::mock::MockDatabase; +use ra_db::FileRange; +use ra_syntax::TextRange; +use test_utils::{assert_eq_text, extract_offset}; + +fn check(assist_id: &str, before: &str, after: &str) { + let (before_cursor_pos, before) = extract_offset(before); + let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); + let frange = FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; + + let (_assist_id, action) = + crate::assists(&db, frange).into_iter().find(|(id, _)| id.id.0 == assist_id).unwrap(); + + let actual = action.edit.apply(&before); + 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 new file mode 100644 index 000000000..e5f6910f1 --- /dev/null +++ b/crates/ra_assists/src/doc_tests/generated.rs @@ -0,0 +1,27 @@ +//! Generated file, do not edit by hand, see `crate/ra_tools/src/codegen` + +use super::check; + +#[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(); +} +"#####, + ) +} diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index ab77b46a9..de576324f 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -7,6 +7,8 @@ mod assist_ctx; mod marks; +#[cfg(test)] +mod doc_tests; use hir::db::HirDatabase; use itertools::Itertools; @@ -36,7 +38,7 @@ pub struct AssistAction { pub target: Option, } -/// Return all the assists eapplicable at the given position. +/// Return all the assists applicable at the given position. /// /// Assists are returned in the "unresolved" state, that is only labels are /// returned, without actual edits. -- cgit v1.2.3