diff options
Diffstat (limited to 'src/bindings.rs')
-rw-r--r-- | src/bindings.rs | 51 |
1 files changed, 38 insertions, 13 deletions
diff --git a/src/bindings.rs b/src/bindings.rs index b629992..5fe5887 100644 --- a/src/bindings.rs +++ b/src/bindings.rs @@ -166,6 +166,7 @@ pub struct Binding { regex: Regex, impls: HashMap<String, Arc<BindingImpl>>, types: HashMap<String, CaptureType>, + doc: Option<String>, } impl Binding { @@ -175,6 +176,7 @@ impl Binding { pattern: &str, case_sensitive: bool, mut types: HashMap<String, CaptureType>, + doc: Option<String>, ) -> Result<Binding, SubplotError> { let regex = RegexBuilder::new(&format!("^{pattern}$")) .case_insensitive(!case_sensitive) @@ -193,6 +195,7 @@ impl Binding { regex, impls: HashMap::new(), types, + doc, }) } @@ -214,6 +217,11 @@ impl Binding { &self.pattern } + /// Return documentation string for binding, if any. + pub fn doc(&self) -> Option<&str> { + self.doc.as_deref() + } + /// Retrieve a particular implementation by name pub fn step_impl(&self, template: &str) -> Option<Arc<BindingImpl>> { self.impls.get(template).cloned() @@ -319,7 +327,7 @@ mod test_binding { #[test] fn creates_new() { - let b = Binding::new(StepKind::Given, "I am Tomjon", false, HashMap::new()).unwrap(); + let b = Binding::new(StepKind::Given, "I am Tomjon", false, HashMap::new(), None).unwrap(); assert_eq!(b.kind(), StepKind::Given); assert!(b.regex().is_match("I am Tomjon")); assert!(!b.regex().is_match("I am Tomjon of Lancre")); @@ -328,19 +336,20 @@ mod test_binding { #[test] fn equal() { - let a = Binding::new(StepKind::Given, "I am Tomjon", false, HashMap::new()).unwrap(); - let b = Binding::new(StepKind::Given, "I am Tomjon", false, HashMap::new()).unwrap(); + let a = Binding::new(StepKind::Given, "I am Tomjon", false, HashMap::new(), None).unwrap(); + let b = Binding::new(StepKind::Given, "I am Tomjon", false, HashMap::new(), None).unwrap(); assert_eq!(a, b); } #[test] fn not_equal() { - let a = Binding::new(StepKind::Given, "I am Tomjon", false, HashMap::new()).unwrap(); + let a = Binding::new(StepKind::Given, "I am Tomjon", false, HashMap::new(), None).unwrap(); let b = Binding::new( StepKind::Given, "I am Tomjon of Lancre", false, HashMap::new(), + None, ) .unwrap(); assert_ne!(a, b); @@ -349,21 +358,21 @@ mod test_binding { #[test] fn does_not_match_with_wrong_kind() { let step = ScenarioStep::new(StepKind::Given, "given", "yo", Location::Unknown); - let b = Binding::new(StepKind::When, "yo", false, HashMap::new()).unwrap(); + let b = Binding::new(StepKind::When, "yo", false, HashMap::new(), None).unwrap(); assert!(b.match_with_step("", &step).is_none()); } #[test] fn does_not_match_with_wrong_text() { let step = ScenarioStep::new(StepKind::Given, "given", "foo", Location::Unknown); - let b = Binding::new(StepKind::Given, "bar", false, HashMap::new()).unwrap(); + let b = Binding::new(StepKind::Given, "bar", false, HashMap::new(), None).unwrap(); assert!(b.match_with_step("", &step).is_none()); } #[test] fn match_with_fixed_pattern() { let step = ScenarioStep::new(StepKind::Given, "given", "foo", Location::Unknown); - let b = Binding::new(StepKind::Given, "foo", false, HashMap::new()).unwrap(); + let b = Binding::new(StepKind::Given, "foo", false, HashMap::new(), None).unwrap(); let m = b.match_with_step("", &step).unwrap(); assert_eq!(m.kind(), StepKind::Given); let mut parts = m.parts(); @@ -385,6 +394,7 @@ mod test_binding { r"I am (?P<who>\S+), I am", false, HashMap::new(), + None, ) .unwrap(); let m = b.match_with_step("", &step).unwrap(); @@ -399,9 +409,9 @@ mod test_binding { #[test] fn case_sensitive_mismatch() { let step = ScenarioStep::new(StepKind::Given, "given", "I am Tomjon", Location::Unknown); - let b = Binding::new(StepKind::Given, r"i am tomjon", false, HashMap::new()).unwrap(); + let b = Binding::new(StepKind::Given, r"i am tomjon", false, HashMap::new(), None).unwrap(); assert!(b.match_with_step("", &step).is_some()); - let b = Binding::new(StepKind::Given, r"i am tomjon", true, HashMap::new()).unwrap(); + let b = Binding::new(StepKind::Given, r"i am tomjon", true, HashMap::new(), None).unwrap(); assert!(b.match_with_step("", &step).is_none()); } } @@ -445,6 +455,7 @@ struct ParsedBinding { case_sensitive: bool, #[serde(default)] types: HashMap<String, CaptureType>, + doc: Option<String>, } #[derive(Debug, Deserialize)] @@ -578,7 +589,13 @@ fn from_hashmap(parsed: &ParsedBinding) -> Result<Binding, SubplotError> { trace!("Successfully acquired binding"); - let mut ret = Binding::new(kind, &pattern, parsed.case_sensitive, types)?; + let mut ret = Binding::new( + kind, + &pattern, + parsed.case_sensitive, + types, + parsed.doc.clone(), + )?; trace!("Binding parsed OK"); for (template, pimpl) in &parsed.impls { ret.add_impl(template, &pimpl.function, pimpl.cleanup.as_deref()); @@ -612,6 +629,7 @@ mod test_bindings { r"I am (?P<name>\S+)", false, HashMap::new(), + None, ) .unwrap(); let mut bindings = Bindings::new(); @@ -694,7 +712,8 @@ mod test_bindings { #[test] fn does_not_find_match_for_unmatching_kind() { let step = ScenarioStep::new(StepKind::Given, "given", "I am Tomjon", Location::Unknown); - let binding = Binding::new(StepKind::When, r"I am Tomjon", false, HashMap::new()).unwrap(); + let binding = + Binding::new(StepKind::When, r"I am Tomjon", false, HashMap::new(), None).unwrap(); let mut bindings = Bindings::new(); bindings.add(binding); assert!(matches!( @@ -711,6 +730,7 @@ mod test_bindings { r"I am Tomjon of Lancre", false, HashMap::new(), + None, ) .unwrap(); let mut bindings = Bindings::new(); @@ -725,7 +745,9 @@ mod test_bindings { fn two_matching_bindings() { let step = ScenarioStep::new(StepKind::Given, "given", "I am Tomjon", Location::Unknown); let mut bindings = Bindings::default(); - bindings.add(Binding::new(StepKind::Given, r"I am Tomjon", false, HashMap::new()).unwrap()); + bindings.add( + Binding::new(StepKind::Given, r"I am Tomjon", false, HashMap::new(), None).unwrap(), + ); bindings.add( Binding::new( StepKind::Given, @@ -733,6 +755,7 @@ mod test_bindings { .unwrap(), false, HashMap::new(), + None, ) .unwrap(), ); @@ -745,7 +768,8 @@ mod test_bindings { #[test] fn finds_match_for_fixed_string_pattern() { let step = ScenarioStep::new(StepKind::Given, "given", "I am Tomjon", Location::Unknown); - let binding = Binding::new(StepKind::Given, r"I am Tomjon", false, HashMap::new()).unwrap(); + let binding = + Binding::new(StepKind::Given, r"I am Tomjon", false, HashMap::new(), None).unwrap(); let mut bindings = Bindings::new(); bindings.add(binding); let m = bindings.find("", &step).unwrap(); @@ -767,6 +791,7 @@ mod test_bindings { r"I am (?P<name>\S+)", false, HashMap::new(), + None, ) .unwrap(); let mut bindings = Bindings::new(); |