From ed25345d6986aca6d115f86ff1ee4cb904340979 Mon Sep 17 00:00:00 2001 From: Daniel Silverstone Date: Fri, 1 Apr 2022 19:28:02 +0100 Subject: (subplotlib): Make ScenarioStep hold a full step text rather than just a function name Signed-off-by: Daniel Silverstone --- subplotlib/src/scenario.rs | 8 ++++---- subplotlib/src/step.rs | 18 ++++++++++-------- 2 files changed, 14 insertions(+), 12 deletions(-) (limited to 'subplotlib') diff --git a/subplotlib/src/scenario.rs b/subplotlib/src/scenario.rs index 1200a99..7cebb31 100644 --- a/subplotlib/src/scenario.rs +++ b/subplotlib/src/scenario.rs @@ -235,7 +235,7 @@ impl ScenarioContext { /// let run_step = subplotlib::steplibrary::runcmd::run::Builder::default() /// .argv0("true") /// .args("") -/// .build(); +/// .build("when I run true".to_string()); /// scenario.add_step(run_step, None); /// /// ``` @@ -314,10 +314,10 @@ impl Scenario { if ret.is_ok() { let mut highest = None; for (i, step) in self.steps.iter().map(|(step, _)| step).enumerate() { - println!(" step: {}", step.name()); + println!(" step: {}", step.step_text()); let mut highest_prep = None; for (i, prep) in self.contexts.hooks.borrow().iter().enumerate() { - let res = prep.step_starts(&self.contexts, step.name()); + let res = prep.step_starts(&self.contexts, step.step_text()); if res.is_err() { ret = res; break; @@ -345,7 +345,7 @@ impl Scenario { if let Some(n) = highest { for stepn in (0..=n).rev() { if let (_, Some(cleanup)) = &self.steps[stepn] { - println!(" cleanup: {}", cleanup.name()); + println!(" cleanup: {}", cleanup.step_text()); let res = cleanup.call(&self.contexts, true); if res.is_err() { println!("*** Cleanup returned failure",); diff --git a/subplotlib/src/step.rs b/subplotlib/src/step.rs index b9c2a86..bbea249 100644 --- a/subplotlib/src/step.rs +++ b/subplotlib/src/step.rs @@ -22,10 +22,12 @@ use crate::types::StepResult; /// ``` /// # use subplotlib::prelude::*; /// -/// let step = ScenarioStep::new("step name", |ctx, ok| Ok(()), |scen| ()); +/// let step = ScenarioStep::new( +/// "when everything works".to_string(), |ctx, ok| Ok(()), |scen| () +/// ); /// ``` pub struct ScenarioStep { - name: String, + step_text: String, func: Box StepResult>, reg: Box, } @@ -36,13 +38,13 @@ impl ScenarioStep { /// This is used to construct a scenario step from a function which /// takes the scenario context container. This will generally be /// called from the generated build method for the step. - pub fn new(name: &str, func: F, reg: R) -> Self + pub fn new(step_text: String, func: F, reg: R) -> Self where F: Fn(&ScenarioContext, bool) -> StepResult + 'static, R: Fn(&Scenario) + 'static, { Self { - name: name.to_string(), + step_text, func: Box::new(func), reg: Box::new(reg), } @@ -69,12 +71,12 @@ impl ScenarioStep { // subsequent step calls may not be sound. There's not a lot we can // do to ensure things are good except try. let func = AssertUnwindSafe(|| (*self.func)(context, defuse_poison)); - catch_unwind(func).map_err(|e| Self::render_panic(self.name(), e))? + catch_unwind(func).map_err(|e| Self::render_panic(self.step_text(), e))? } - /// Return the name of this step - pub fn name(&self) -> &str { - &self.name + /// Return the full text of this step + pub fn step_text(&self) -> &str { + &self.step_text } /// Register any context types needed by this step -- cgit v1.2.1