From 599ecbd81dc3ae86ab532f6d38823cfbb398a7a7 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 14 Feb 2024 15:16:37 -0600 Subject: [PATCH 1/2] fix(snap): Allow multiple values map to a variable --- crates/snapbox/src/substitutions.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/crates/snapbox/src/substitutions.rs b/crates/snapbox/src/substitutions.rs index d0057436..0066d892 100644 --- a/crates/snapbox/src/substitutions.rs +++ b/crates/snapbox/src/substitutions.rs @@ -7,7 +7,7 @@ use std::borrow::Cow; /// - `[..]`: match multiple characters within a line #[derive(Default, Clone, Debug, PartialEq, Eq)] pub struct Substitutions { - vars: std::collections::BTreeMap<&'static str, Cow<'static, str>>, + vars: std::collections::BTreeMap<&'static str, std::collections::BTreeSet>>, unused: std::collections::BTreeSet<&'static str>, } @@ -43,7 +43,9 @@ impl Substitutions { self.unused.insert(key); } else { self.vars - .insert(key, crate::utils::normalize_text(value.as_ref()).into()); + .entry(key) + .or_default() + .insert(crate::utils::normalize_text(value.as_ref()).into()); } Ok(()) } @@ -79,8 +81,10 @@ impl Substitutions { fn substitute<'v>(&self, value: &'v str) -> Cow<'v, str> { let mut value = Cow::Borrowed(value); for (var, replace) in self.vars.iter() { - debug_assert!(!replace.is_empty()); - value = Cow::Owned(value.replace(replace.as_ref(), var)); + for replace in replace { + debug_assert!(!replace.is_empty()); + value = Cow::Owned(value.replace(replace.as_ref(), var)); + } } value } From 5fa98461367d73af7a804f58a04e027119b7e350 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 14 Feb 2024 15:21:29 -0600 Subject: [PATCH 2/2] feat: Allow clearing an existing variable --- crates/snapbox/src/substitutions.rs | 6 ++++++ src/cases.rs | 10 +++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/crates/snapbox/src/substitutions.rs b/crates/snapbox/src/substitutions.rs index 0066d892..914e9f55 100644 --- a/crates/snapbox/src/substitutions.rs +++ b/crates/snapbox/src/substitutions.rs @@ -63,6 +63,12 @@ impl Substitutions { Ok(()) } + pub fn remove(&mut self, key: &'static str) -> Result<(), crate::Error> { + let key = validate_key(key)?; + self.vars.remove(key); + Ok(()) + } + /// Apply match pattern to `input` /// /// If `pattern` matches `input`, then `pattern` is returned. diff --git a/src/cases.rs b/src/cases.rs index 37200859..405c425a 100644 --- a/src/cases.rs +++ b/src/cases.rs @@ -143,7 +143,7 @@ impl TestCases { /// Batch add variables for normalizing output /// - /// See `insert_var`. + /// See [`TestCases::insert_var`]. pub fn extend_vars( &self, vars: impl IntoIterator>)>, @@ -152,6 +152,14 @@ impl TestCases { Ok(self) } + /// Remove an existing var values + /// + /// See [`TestCases::insert_var`]. + pub fn clear_var(&self, var: &'static str) -> Result<&Self, crate::Error> { + self.substitutions.borrow_mut().remove(var)?; + Ok(self) + } + /// Run tests /// /// This will happen on `drop` if not done explicitly