summaryrefslogtreecommitdiff
path: root/src/steps.rs
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers@digital-scurf.org>2023-08-12 10:36:10 +0100
committerDaniel Silverstone <dsilvers@digital-scurf.org>2023-08-12 10:36:10 +0100
commit1646f8cbc8191d7e7a68abb0a99090b55b069097 (patch)
treeca4aa2e555a7cb6604d637d6717efdf20b298af3 /src/steps.rs
parentf6093a207f7f46eb547d90f2bb20113a9b009028 (diff)
downloadsubplot-1646f8cbc8191d7e7a68abb0a99090b55b069097.tar.gz
steps: Pass location information into scenarios and scenario steps for error messages
Signed-off-by: Daniel Silverstone <dsilvers@digital-scurf.org>
Diffstat (limited to 'src/steps.rs')
-rw-r--r--src/steps.rs35
1 files changed, 25 insertions, 10 deletions
diff --git a/src/steps.rs b/src/steps.rs
index 1005ad8..43e66e2 100644
--- a/src/steps.rs
+++ b/src/steps.rs
@@ -1,4 +1,4 @@
-use crate::SubplotError;
+use crate::{html::Location, SubplotError};
use serde::{Deserialize, Serialize};
use std::fmt;
@@ -16,15 +16,17 @@ pub struct ScenarioStep {
kind: StepKind,
keyword: String,
text: String,
+ origin: Location,
}
impl ScenarioStep {
/// Construct a new step.
- pub fn new(kind: StepKind, keyword: &str, text: &str) -> ScenarioStep {
+ pub fn new(kind: StepKind, keyword: &str, text: &str, origin: Location) -> ScenarioStep {
ScenarioStep {
kind,
keyword: keyword.to_owned(),
text: text.to_owned(),
+ origin,
}
}
@@ -50,6 +52,7 @@ impl ScenarioStep {
pub fn new_from_str(
text: &str,
default: Option<StepKind>,
+ origin: Location,
) -> Result<ScenarioStep, SubplotError> {
if text.trim_start() != text {
return Err(SubplotError::NotAtBoln(text.into()));
@@ -79,7 +82,7 @@ impl ScenarioStep {
if joined.len() > 1 {
joined.pop();
}
- Ok(ScenarioStep::new(kind, keyword, &joined))
+ Ok(ScenarioStep::new(kind, keyword, &joined, origin))
}
}
@@ -119,47 +122,59 @@ impl fmt::Display for StepKind {
#[cfg(test)]
mod test {
+ use crate::html::Location;
+
use super::{ScenarioStep, StepKind, SubplotError};
#[test]
fn parses_given() {
- let step = ScenarioStep::new_from_str("GIVEN I am Tomjon", None).unwrap();
+ let step =
+ ScenarioStep::new_from_str("GIVEN I am Tomjon", None, Location::Unknown).unwrap();
assert_eq!(step.kind(), StepKind::Given);
assert_eq!(step.text(), "I am Tomjon");
}
#[test]
fn parses_given_with_extra_spaces() {
- let step = ScenarioStep::new_from_str("given I am Tomjon ", None).unwrap();
+ let step =
+ ScenarioStep::new_from_str("given I am Tomjon ", None, Location::Unknown)
+ .unwrap();
assert_eq!(step.kind(), StepKind::Given);
assert_eq!(step.text(), "I am Tomjon");
}
#[test]
fn parses_when() {
- let step = ScenarioStep::new_from_str("when I declare myself king", None).unwrap();
+ let step =
+ ScenarioStep::new_from_str("when I declare myself king", None, Location::Unknown)
+ .unwrap();
assert_eq!(step.kind(), StepKind::When);
assert_eq!(step.text(), "I declare myself king");
}
#[test]
fn parses_then() {
- let step = ScenarioStep::new_from_str("thEN everyone accepts it", None).unwrap();
+ let step = ScenarioStep::new_from_str("thEN everyone accepts it", None, Location::Unknown)
+ .unwrap();
assert_eq!(step.kind(), StepKind::Then);
assert_eq!(step.text(), "everyone accepts it");
}
#[test]
fn parses_and() {
- let step =
- ScenarioStep::new_from_str("and everyone accepts it", Some(StepKind::Then)).unwrap();
+ let step = ScenarioStep::new_from_str(
+ "and everyone accepts it",
+ Some(StepKind::Then),
+ Location::Unknown,
+ )
+ .unwrap();
assert_eq!(step.kind(), StepKind::Then);
assert_eq!(step.text(), "everyone accepts it");
}
#[test]
fn fails_to_parse_and() {
- let step = ScenarioStep::new_from_str("and everyone accepts it", None);
+ let step = ScenarioStep::new_from_str("and everyone accepts it", None, Location::Unknown);
assert!(step.is_err());
match step.err() {
None => unreachable!(),