diff options
Diffstat (limited to 'src/matches.rs')
-rw-r--r-- | src/matches.rs | 42 |
1 files changed, 31 insertions, 11 deletions
diff --git a/src/matches.rs b/src/matches.rs index 9130641..18c9832 100644 --- a/src/matches.rs +++ b/src/matches.rs @@ -1,3 +1,4 @@ +use crate::html::{Attribute, Content, Element, ElementTag, Location}; use crate::Binding; use crate::Scenario; use crate::StepKind; @@ -12,6 +13,7 @@ use serde::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize)] pub struct MatchedScenario { title: String, + origin: Location, steps: Vec<MatchedStep>, } @@ -29,6 +31,7 @@ impl MatchedScenario { .collect(); Ok(MatchedScenario { title: scen.title().to_string(), + origin: scen.origin().clone(), steps: steps?, }) } @@ -73,6 +76,7 @@ pub struct MatchedStep { kind: StepKind, pattern: String, text: String, + origin: Location, parts: Vec<PartialStep>, function: Option<String>, cleanup: Option<String>, @@ -81,12 +85,13 @@ pub struct MatchedStep { impl MatchedStep { /// Return a new empty match. Empty means it has no step parts. - pub fn new(binding: &Binding, template: &str) -> MatchedStep { + pub fn new(binding: &Binding, template: &str, origin: Location) -> MatchedStep { let bimpl = binding.step_impl(template); MatchedStep { kind: binding.kind(), pattern: binding.pattern().to_string(), text: "".to_string(), + origin, parts: vec![], function: bimpl.clone().map(|b| b.function().to_owned()), cleanup: bimpl.and_then(|b| b.cleanup().map(String::from)), @@ -136,6 +141,14 @@ impl MatchedStep { pub fn types(&self) -> &HashMap<String, CaptureType> { &self.types } + + /// Render the step as HTML. + pub fn to_html(&self) -> Element { + let mut e = Element::new(ElementTag::Span); + e.push_attribute(Attribute::new("class", "scenario_step")); + e.push_child(Content::Text(self.text().into())); + e + } } /// Part of a scenario step, possibly captured by a pattern. @@ -151,6 +164,8 @@ pub enum PartialStep { name: String, /// Text of the capture. text: String, + /// Type of capture. + kind: CaptureType, }, } @@ -161,10 +176,11 @@ impl PartialStep { } /// Construct a textual captured part of a scenario step. - pub fn text(name: &str, text: &str) -> PartialStep { + pub fn text(name: &str, text: &str, kind: CaptureType) -> PartialStep { PartialStep::CapturedText { name: name.to_string(), text: text.to_string(), + kind, } } @@ -179,6 +195,7 @@ impl PartialStep { #[cfg(test)] mod test_partial_steps { use super::PartialStep; + use crate::bindings::CaptureType; #[test] fn identical_uncaptured_texts_match() { @@ -196,29 +213,29 @@ mod test_partial_steps { #[test] fn identical_captured_texts_match() { - let p1 = PartialStep::text("xxx", "foo"); - let p2 = PartialStep::text("xxx", "foo"); + let p1 = PartialStep::text("xxx", "foo", CaptureType::Text); + let p2 = PartialStep::text("xxx", "foo", CaptureType::Text); assert_eq!(p1, p2); } #[test] fn different_captured_texts_dont_match() { - let p1 = PartialStep::text("xxx", "foo"); - let p2 = PartialStep::text("xxx", "bar"); + let p1 = PartialStep::text("xxx", "foo", CaptureType::Text); + let p2 = PartialStep::text("xxx", "bar", CaptureType::Text); assert_ne!(p1, p2); } #[test] fn differently_named_captured_texts_dont_match() { - let p1 = PartialStep::text("xxx", "foo"); - let p2 = PartialStep::text("yyy", "foo"); + let p1 = PartialStep::text("xxx", "foo", CaptureType::Text); + let p2 = PartialStep::text("yyy", "foo", CaptureType::Text); assert_ne!(p1, p2); } #[test] fn differently_captured_texts_dont_match() { let p1 = PartialStep::uncaptured("foo"); - let p2 = PartialStep::text("xxx", "foo"); + let p2 = PartialStep::text("xxx", "foo", CaptureType::Text); assert_ne!(p1, p2); } } @@ -249,6 +266,8 @@ impl StepSnippet { #[cfg(test)] mod test { + use crate::bindings::CaptureType; + use super::PartialStep; #[test] @@ -262,11 +281,12 @@ mod test { #[test] fn returns_text() { - let p = PartialStep::text("xxx", "foo"); + let p = PartialStep::text("xxx", "foo", crate::bindings::CaptureType::Text); match p { - PartialStep::CapturedText { name, text } => { + PartialStep::CapturedText { name, text, kind } => { assert_eq!(name, "xxx"); assert_eq!(text, "foo"); + assert_eq!(kind, CaptureType::Text); } _ => panic!("expected CapturedText: {:?}", p), } |