From fe52f8f0281c0f56955871863a6e0c14732540f9 Mon Sep 17 00:00:00 2001 From: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com> Date: Tue, 5 May 2020 17:43:28 +0200 Subject: add doctest support #4317 Signed-off-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com> --- crates/ra_ide/src/runnables.rs | 43 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'crates/ra_ide/src/runnables.rs') diff --git a/crates/ra_ide/src/runnables.rs b/crates/ra_ide/src/runnables.rs index 38637c19c..fa8a9d92c 100644 --- a/crates/ra_ide/src/runnables.rs +++ b/crates/ra_ide/src/runnables.rs @@ -9,6 +9,7 @@ use ra_syntax::{ }; use crate::FileId; +use ast::DocCommentsOwner; use std::fmt::Display; #[derive(Debug)] @@ -37,6 +38,7 @@ pub enum RunnableKind { Test { test_id: TestId, attr: TestAttr }, TestMod { path: String }, Bench { test_id: TestId }, + DocTest { test_id: TestId }, Bin, } @@ -81,6 +83,8 @@ fn runnable_fn(sema: &Semantics, fn_def: ast::FnDef) -> Option bool { .any(|attribute_text| attribute_text.contains("test")) } +fn has_doc_test(fn_def: &ast::FnDef) -> bool { + fn_def.doc_comment_text().map_or(false, |comment| comment.contains("```")) +} + fn runnable_mod(sema: &Semantics, module: ast::Module) -> Option { let has_test_function = module .item_list()? @@ -194,6 +202,41 @@ mod tests { ); } + #[test] + fn test_runnables_doc_test() { + let (analysis, pos) = analysis_and_position( + r#" + //- /lib.rs + <|> //empty + fn main() {} + + /// ``` + /// let x = 5; + /// ``` + fn foo() {} + "#, + ); + let runnables = analysis.runnables(pos.file_id).unwrap(); + assert_debug_snapshot!(&runnables, + @r###" + [ + Runnable { + range: 1..21, + kind: Bin, + }, + Runnable { + range: 22..64, + kind: DocTest { + test_id: Path( + "foo", + ), + }, + }, + ] + "### + ); + } + #[test] fn test_runnables_module() { let (analysis, pos) = analysis_and_position( -- cgit v1.2.3