diff options
author | Lars Wirzenius <liw@liw.fi> | 2023-12-01 18:01:44 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2023-12-25 10:17:07 +0200 |
commit | 9972ba7f7c798e12741e9310a258df64c9310c05 (patch) | |
tree | 0084856ea6a9c3dbb4c4cedac83d3b24aa48bdc0 /src/steps.rs | |
parent | d6ba90c694fe8905aadf02acbbb0ff7e24f30b4e (diff) | |
download | subplot-9972ba7f7c798e12741e9310a258df64c9310c05.tar.gz |
feat: typeset scenarios by typesetting steps
Typeset each step in a scenario, including captures in the steps.
Previously the scenario was just one <pre> element, now things can be
styled with CSS.
Signed-off-by: Lars Wirzenius <liw@liw.fi>
Sponsored-by: author
Diffstat (limited to 'src/steps.rs')
-rw-r--r-- | src/steps.rs | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/src/steps.rs b/src/steps.rs index d9d1725..7f5e7d4 100644 --- a/src/steps.rs +++ b/src/steps.rs @@ -96,6 +96,85 @@ impl fmt::Display for ScenarioStep { } } +/// Parse a scenario snippet into a vector of steps. +pub(crate) fn parse_scenario_snippet( + text: &str, + loc: &Location, +) -> Result<Vec<ScenarioStep>, SubplotError> { + let mut steps = vec![]; + let mut prevkind = None; + for (idx, line) in text.lines().enumerate() { + let line_loc = match loc.clone() { + Location::Known { + filename, + line, + col, + } => Location::Known { + filename, + line: line + idx, + col, + }, + Location::Unknown => Location::Unknown, + }; + if !line.trim().is_empty() { + let step = ScenarioStep::new_from_str(line, prevkind, line_loc)?; + prevkind = Some(step.kind()); + steps.push(step); + } + } + Ok(steps) +} + +#[cfg(test)] +mod test_steps_parser { + use super::{parse_scenario_snippet, Location, ScenarioStep, StepKind, SubplotError}; + use std::path::Path; + + fn parse(text: &str) -> Result<Vec<ScenarioStep>, SubplotError> { + let loc = Location::new(Path::new("test"), 1, 1); + parse_scenario_snippet(text, &loc) + } + + #[test] + fn empty_string() { + assert_eq!(parse("").unwrap(), vec![]); + } + + #[test] + fn simple() { + assert_eq!( + parse("given foo").unwrap(), + vec![ScenarioStep::new( + StepKind::Given, + "given", + "foo", + Location::new(Path::new("test"), 1, 1), + )] + ); + } + + #[test] + fn two_simple() { + assert_eq!( + parse("given foo\nthen bar\n").unwrap(), + vec![ + ScenarioStep::new( + StepKind::Given, + "given", + "foo", + Location::new(Path::new("test"), 1, 1), + ), + ScenarioStep::new( + StepKind::Then, + "then", + "bar", + Location::new(Path::new("test"), 2, 1), + ) + ] + ); + } +} + /// The kind of scenario step we have: given, when, or then. /// /// This needs to be extended if the Subplot language gets extended with other |