From de74c0dcab2efd50d68f70d15de3fced718e8c7a Mon Sep 17 00:00:00 2001 From: vsrs Date: Sat, 6 Jun 2020 12:00:46 +0300 Subject: Preliminary runnables refactoring --- crates/ra_ide/src/runnables.rs | 78 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/runnables.rs b/crates/ra_ide/src/runnables.rs index f32ce0d22..9f7b5edfd 100644 --- a/crates/ra_ide/src/runnables.rs +++ b/crates/ra_ide/src/runnables.rs @@ -42,6 +42,42 @@ pub enum RunnableKind { Bin, } +#[derive(Debug, Eq, PartialEq)] +pub struct RunnableAction { + pub run_title: &'static str, + pub debugee: bool, +} + +const TEST: RunnableAction = RunnableAction { run_title: "▶\u{fe0e} Run Test", debugee: true }; +const DOCTEST: RunnableAction = + RunnableAction { run_title: "▶\u{fe0e} Run Doctest", debugee: false }; +const BENCH: RunnableAction = RunnableAction { run_title: "▶\u{fe0e} Run Bench", debugee: true }; +const BIN: RunnableAction = RunnableAction { run_title: "▶\u{fe0e} Run", debugee: true }; + +impl Runnable { + // test package::module::testname + pub fn label(&self, target: Option) -> String { + match &self.kind { + RunnableKind::Test { test_id, .. } => format!("test {}", test_id), + RunnableKind::TestMod { path } => format!("test-mod {}", path), + RunnableKind::Bench { test_id } => format!("bench {}", test_id), + RunnableKind::DocTest { test_id, .. } => format!("doctest {}", test_id), + RunnableKind::Bin => { + target.map_or_else(|| "run binary".to_string(), |t| format!("run {}", t)) + } + } + } + + pub fn action(&self) -> &'static RunnableAction { + match &self.kind { + RunnableKind::Test { .. } | RunnableKind::TestMod { .. } => &TEST, + RunnableKind::DocTest { .. } => &DOCTEST, + RunnableKind::Bench { .. } => &BENCH, + RunnableKind::Bin => &BIN, + } + } +} + // Feature: Run // // Shows a popup suggesting to run a test/benchmark/binary **at the current cursor @@ -207,6 +243,15 @@ mod tests { use crate::mock_analysis::analysis_and_position; + use super::{Runnable, RunnableAction, BENCH, BIN, DOCTEST, TEST}; + + fn assert_actions(runnables: &[Runnable], actions: &[&RunnableAction]) { + assert_eq!( + actions, + runnables.into_iter().map(|it| it.action()).collect::>().as_slice() + ); + } + #[test] fn test_runnables() { let (analysis, pos) = analysis_and_position( @@ -221,6 +266,9 @@ mod tests { #[test] #[ignore] fn test_foo() {} + + #[bench] + fn bench() {} "#, ); let runnables = analysis.runnables(pos.file_id).unwrap(); @@ -295,9 +343,32 @@ mod tests { }, cfg_exprs: [], }, + Runnable { + nav: NavigationTarget { + file_id: FileId( + 1, + ), + full_range: 82..104, + name: "bench", + kind: FN_DEF, + focus_range: Some( + 94..99, + ), + container_name: None, + description: None, + docs: None, + }, + kind: Bench { + test_id: Path( + "bench", + ), + }, + cfg_exprs: [], + }, ] "### ); + assert_actions(&runnables, &[&BIN, &TEST, &TEST, &BENCH]); } #[test] @@ -361,6 +432,7 @@ mod tests { ] "### ); + assert_actions(&runnables, &[&BIN, &DOCTEST]); } #[test] @@ -427,6 +499,7 @@ mod tests { ] "### ); + assert_actions(&runnables, &[&BIN, &DOCTEST]); } #[test] @@ -493,6 +566,7 @@ mod tests { ] "### ); + assert_actions(&runnables, &[&TEST, &TEST]); } #[test] @@ -561,6 +635,7 @@ mod tests { ] "### ); + assert_actions(&runnables, &[&TEST, &TEST]); } #[test] @@ -631,6 +706,7 @@ mod tests { ] "### ); + assert_actions(&runnables, &[&TEST, &TEST]); } #[test] @@ -681,6 +757,7 @@ mod tests { ] "### ); + assert_actions(&runnables, &[&TEST]); } #[test] @@ -739,6 +816,7 @@ mod tests { ] "### ); + assert_actions(&runnables, &[&TEST]); } #[test] -- cgit v1.2.3