summaryrefslogtreecommitdiff
path: root/src/bindings.rs
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers@digital-scurf.org>2020-12-15 13:42:59 +0000
committerDaniel Silverstone <dsilvers@digital-scurf.org>2020-12-18 17:50:19 +0000
commit37e9b82bbb7011b4b3d02f47921e5be3f59616da (patch)
tree6debcae2808c469792f246fbe134a1834c1da5d1 /src/bindings.rs
parentd5d02d4296ed6cb07bb3e6f4438752c7b877bcaa (diff)
downloadsubplot-37e9b82bbb7011b4b3d02f47921e5be3f59616da.tar.gz
bindings: Use/amend the typemap when parsing simple patterns
Signed-off-by: Daniel Silverstone <dsilvers@digital-scurf.org>
Diffstat (limited to 'src/bindings.rs')
-rw-r--r--src/bindings.rs64
1 files changed, 48 insertions, 16 deletions
diff --git a/src/bindings.rs b/src/bindings.rs
index 3d321ec..94b7a23 100644
--- a/src/bindings.rs
+++ b/src/bindings.rs
@@ -456,11 +456,13 @@ fn from_hashmap(parsed: &ParsedBinding) -> Result<Binding> {
return Err(SubplotError::BindingWithoutKnownKeyword(msg));
};
+ let mut types = parsed.types.clone();
+
let pattern = if parsed.regex.unwrap_or(false) {
pattern.to_string()
} else {
// if we get here parsed.regex is either None or Some(false)
- regex_from_simple_pattern(pattern, parsed.regex.is_some())?
+ regex_from_simple_pattern(pattern, parsed.regex.is_some(), &mut types)?
};
Ok(Binding::new(
@@ -469,7 +471,7 @@ fn from_hashmap(parsed: &ParsedBinding) -> Result<Binding> {
&parsed.function,
parsed.cleanup.as_deref(),
parsed.case_sensitive,
- parsed.types.clone(),
+ types,
)?)
}
@@ -521,7 +523,7 @@ mod test_bindings {
- then: the total is {total}
function: check_total
types:
- total: number
+ total: word
";
let mut bindings = Bindings::new();
bindings.add_from_yaml(&yaml).unwrap();
@@ -607,7 +609,8 @@ mod test_bindings {
bindings.add(
Binding::new(
StepKind::Given,
- &super::regex_from_simple_pattern(r"I am {name}", false).unwrap(),
+ &super::regex_from_simple_pattern(r"I am {name}", false, &mut HashMap::new())
+ .unwrap(),
"set_foo",
None,
false,
@@ -701,7 +704,11 @@ lazy_static! {
};
}
-fn regex_from_simple_pattern(pattern: &str, explicit_plain: bool) -> Result<String> {
+fn regex_from_simple_pattern(
+ pattern: &str,
+ explicit_plain: bool,
+ types: &mut HashMap<String, String>,
+) -> Result<String> {
let pat = Regex::new(r"\{[^\s\{\}]+\}").unwrap();
let mut r = String::new();
let mut end = 0;
@@ -721,9 +728,33 @@ fn regex_from_simple_pattern(pattern: &str, explicit_plain: bool) -> Result<Stri
let (name, kind) = if let Some(i) = name.find(':') {
let (name, suffix) = name.split_at(i);
assert!(suffix.starts_with(':'));
- (name, &suffix[1..])
+ (name, Some(&suffix[1..]))
} else {
- (name, "word")
+ (name, None)
+ };
+
+ let (name, kind) = match (name, kind, types.contains_key(name)) {
+ (name, Some(kind), false) => {
+ // There is a kind, but it's not in the map
+ types.insert(name.to_string(), kind.to_string());
+ (name, kind)
+ }
+ (name, None, true) => {
+ // There is no kind, but it is present in the map
+ (name, types[name].as_str())
+ }
+ (name, Some(kind), true) => {
+ // There is a kind and it's in the map, they must match
+ if kind != types.get(name).unwrap().as_str() {
+ return Err(SubplotError::SimplePatternKindMismatch(name.to_string()));
+ }
+ (name, kind)
+ }
+ (name, None, false) => {
+ // There is no kind, and it's not in the map, so default to word
+ types.insert(name.to_string(), "word".to_string());
+ (name, "word")
+ }
};
if let Some((regex, _)) = KIND_PATTERNS.get(kind) {
@@ -751,27 +782,28 @@ mod test_regex_from_simple_pattern {
use super::regex_from_simple_pattern;
use crate::SubplotError;
use regex::Regex;
+ use std::collections::HashMap;
#[test]
fn returns_empty_string_as_is() {
- let ret = regex_from_simple_pattern("", false).unwrap();
+ let ret = regex_from_simple_pattern("", false, &mut HashMap::new()).unwrap();
assert_eq!(ret, "");
}
#[test]
fn returns_boring_pattern_as_is() {
- let ret = regex_from_simple_pattern("boring", false).unwrap();
+ let ret = regex_from_simple_pattern("boring", false, &mut HashMap::new()).unwrap();
assert_eq!(ret, "boring");
}
#[test]
fn returns_pattern_with_regexp_chars_escaped() {
- let ret = regex_from_simple_pattern(r".[]*\\", true).unwrap();
+ let ret = regex_from_simple_pattern(r".[]*\\", true, &mut HashMap::new()).unwrap();
assert_eq!(ret, r"\.\[\]\*\\\\");
}
fn matches(pattern: &str, text: &str) {
- let r = regex_from_simple_pattern(pattern, false).unwrap();
+ let r = regex_from_simple_pattern(pattern, false, &mut HashMap::new()).unwrap();
let r = Regex::new(&r).unwrap();
let m = r.find(text);
assert!(m.is_some());
@@ -781,7 +813,7 @@ mod test_regex_from_simple_pattern {
}
fn doesnt_match(pattern: &str, text: &str) {
- let r = regex_from_simple_pattern(pattern, false).unwrap();
+ let r = regex_from_simple_pattern(pattern, false, &mut HashMap::new()).unwrap();
let r = Regex::new(&r).unwrap();
if let Some(m) = r.find(text) {
assert!(m.start() > 0 || m.end() < text.len());
@@ -858,7 +890,7 @@ mod test_regex_from_simple_pattern {
#[test]
fn returns_error_for_stray_opening_brace() {
- match regex_from_simple_pattern("{", false) {
+ match regex_from_simple_pattern("{", false, &mut HashMap::new()) {
Err(SubplotError::StrayBraceInSimplePattern(_)) => (),
Err(e) => panic!("unexpected error: {}", e),
_ => unreachable!(),
@@ -867,7 +899,7 @@ mod test_regex_from_simple_pattern {
#[test]
fn returns_error_for_stray_closing_brace() {
- match regex_from_simple_pattern("}", false) {
+ match regex_from_simple_pattern("}", false, &mut HashMap::new()) {
Err(SubplotError::StrayBraceInSimplePattern(_)) => (),
Err(e) => panic!("unexpected error: {}", e),
_ => unreachable!(),
@@ -876,7 +908,7 @@ mod test_regex_from_simple_pattern {
#[test]
fn returns_error_for_stray_opening_brace_before_capture() {
- match regex_from_simple_pattern("{{foo}", false) {
+ match regex_from_simple_pattern("{{foo}", false, &mut HashMap::new()) {
Err(SubplotError::StrayBraceInSimplePattern(_)) => (),
Err(e) => panic!("unexpected error: {}", e),
_ => unreachable!(),
@@ -885,7 +917,7 @@ mod test_regex_from_simple_pattern {
#[test]
fn returns_error_for_stray_closing_brace_before_capture() {
- match regex_from_simple_pattern("}{foo}", false) {
+ match regex_from_simple_pattern("}{foo}", false, &mut HashMap::new()) {
Err(SubplotError::StrayBraceInSimplePattern(_)) => (),
Err(e) => panic!("unexpected error: {}", e),
_ => unreachable!(),