diff options
author | Daniel Silverstone <dsilvers@digital-scurf.org> | 2021-08-14 10:48:26 +0100 |
---|---|---|
committer | Daniel Silverstone <dsilvers@digital-scurf.org> | 2021-09-07 17:32:20 +0100 |
commit | d5c69db746b5c4ba938248a7d1c285256c6e6412 (patch) | |
tree | c0945c1eba87cc2702ea574a94c5cfa5ae61762c /src/bindings.rs | |
parent | 4436b7ee28b0318a96d98833d85d712c8a18850d (diff) | |
download | subplot-d5c69db746b5c4ba938248a7d1c285256c6e6412.tar.gz |
subplot: Properly support polyglot bindings
Signed-off-by: Daniel Silverstone <dsilvers@digital-scurf.org>
Diffstat (limited to 'src/bindings.rs')
-rw-r--r-- | src/bindings.rs | 39 |
1 files changed, 22 insertions, 17 deletions
diff --git a/src/bindings.rs b/src/bindings.rs index 9282155..91a2c8d 100644 --- a/src/bindings.rs +++ b/src/bindings.rs @@ -207,8 +207,8 @@ impl Binding { } /// Retrieve a particular implementation by name - pub fn step_impl(&self, _template: &str) -> Option<Arc<BindingImpl>> { - self.impls.values().next().cloned() + pub fn step_impl(&self, template: &str) -> Option<Arc<BindingImpl>> { + self.impls.get(template).cloned() } /// Return the compiled regular expression for the pattern of the @@ -219,8 +219,13 @@ impl Binding { &self.regex } + /// Return the type bindings for this binding. + pub fn types(&self) -> impl Iterator<Item = (&str, CaptureType)> { + self.types.iter().map(|(s, c)| (s.as_str(), *c)) + } + /// Try to match defined binding against a parsed scenario step. - pub fn match_with_step(&self, step: &ScenarioStep) -> Option<MatchedStep> { + pub fn match_with_step(&self, template: &str, step: &ScenarioStep) -> Option<MatchedStep> { if self.kind() != step.kind() { return None; } @@ -229,7 +234,7 @@ impl Binding { let caps = self.regex.captures(step_text)?; // If there is only one capture, it's the whole string. - let mut m = MatchedStep::new(self, &self.types); + let mut m = MatchedStep::new(self, template); if caps.len() == 1 { m.append_part(PartialStep::uncaptured(step_text)); return Some(m); @@ -336,21 +341,21 @@ mod test_binding { fn does_not_match_with_wrong_kind() { let step = ScenarioStep::new(StepKind::Given, "given", "yo"); let b = Binding::new(StepKind::When, "yo", false, HashMap::new()).unwrap(); - assert!(b.match_with_step(&step).is_none()); + assert!(b.match_with_step("", &step).is_none()); } #[test] fn does_not_match_with_wrong_text() { let step = ScenarioStep::new(StepKind::Given, "given", "foo"); let b = Binding::new(StepKind::Given, "bar", false, HashMap::new()).unwrap(); - assert!(b.match_with_step(&step).is_none()); + assert!(b.match_with_step("", &step).is_none()); } #[test] fn match_with_fixed_pattern() { let step = ScenarioStep::new(StepKind::Given, "given", "foo"); let b = Binding::new(StepKind::Given, "foo", false, HashMap::new()).unwrap(); - let m = b.match_with_step(&step).unwrap(); + let m = b.match_with_step("", &step).unwrap(); assert_eq!(m.kind(), StepKind::Given); let mut parts = m.parts(); let p = parts.next().unwrap(); @@ -368,7 +373,7 @@ mod test_binding { HashMap::new(), ) .unwrap(); - let m = b.match_with_step(&step).unwrap(); + let m = b.match_with_step("", &step).unwrap(); assert_eq!(m.kind(), StepKind::Given); let mut parts = m.parts(); assert_eq!(parts.next().unwrap(), &PartialStep::uncaptured("I am ")); @@ -381,9 +386,9 @@ mod test_binding { fn case_sensitive_mismatch() { let step = ScenarioStep::new(StepKind::Given, "given", "I am Tomjon"); let b = Binding::new(StepKind::Given, r"i am tomjon", false, HashMap::new()).unwrap(); - assert!(b.match_with_step(&step).is_some()); + assert!(b.match_with_step("", &step).is_some()); let b = Binding::new(StepKind::Given, r"i am tomjon", true, HashMap::new()).unwrap(); - assert!(b.match_with_step(&step).is_none()); + assert!(b.match_with_step("", &step).is_none()); } } @@ -479,11 +484,11 @@ impl Bindings { /// Find the binding matching a given scenario step, if there is /// exactly one. - pub fn find(&self, step: &ScenarioStep) -> Result<MatchedStep> { + pub fn find(&self, template: &str, step: &ScenarioStep) -> Result<MatchedStep> { let mut matches: Vec<MatchedStep> = self .bindings() .iter() - .filter_map(|b| b.match_with_step(step)) + .filter_map(|b| b.match_with_step(template, step)) .collect(); if matches.len() > 1 { // Too many matching bindings. @@ -688,7 +693,7 @@ mod test_bindings { let mut bindings = Bindings::new(); bindings.add(binding); assert!(matches!( - bindings.find(&step), + bindings.find("", &step), Err(SubplotError::BindingUnknown(_)) )); } @@ -706,7 +711,7 @@ mod test_bindings { let mut bindings = Bindings::new(); bindings.add(binding); assert!(matches!( - bindings.find(&step), + bindings.find("", &step), Err(SubplotError::BindingUnknown(_)) )); } @@ -727,7 +732,7 @@ mod test_bindings { .unwrap(), ); assert!(matches!( - bindings.find(&step), + bindings.find("", &step), Err(SubplotError::BindingNotUnique(_, _)) )); } @@ -738,7 +743,7 @@ mod test_bindings { let binding = Binding::new(StepKind::Given, r"I am Tomjon", false, HashMap::new()).unwrap(); let mut bindings = Bindings::new(); bindings.add(binding); - let m = bindings.find(&step).unwrap(); + let m = bindings.find("", &step).unwrap(); assert_eq!(m.kind(), StepKind::Given); let mut parts = m.parts(); let p = parts.next().unwrap(); @@ -761,7 +766,7 @@ mod test_bindings { .unwrap(); let mut bindings = Bindings::new(); bindings.add(binding); - let m = bindings.find(&step).unwrap(); + let m = bindings.find("", &step).unwrap(); assert_eq!(m.kind(), StepKind::Given); let mut parts = m.parts(); let p = parts.next().unwrap(); |