summaryrefslogtreecommitdiff
path: root/src/steps.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/steps.rs')
-rw-r--r--src/steps.rs79
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