diff --git a/crates/snapbox/src/filter/mod.rs b/crates/snapbox/src/filter/mod.rs index d52cfe99..28411d7b 100644 --- a/crates/snapbox/src/filter/mod.rs +++ b/crates/snapbox/src/filter/mod.rs @@ -35,13 +35,13 @@ impl Filter for FilterNewlines { #[cfg(feature = "json")] DataInner::Json(value) => { let mut value = value; - normalize_json_string(&mut value, normalize_lines); + normalize_json_string(&mut value, &normalize_lines); DataInner::Json(value) } #[cfg(feature = "json")] DataInner::JsonLines(value) => { let mut value = value; - normalize_json_string(&mut value, normalize_lines); + normalize_json_string(&mut value, &normalize_lines); DataInner::JsonLines(value) } #[cfg(feature = "term-svg")] @@ -82,13 +82,13 @@ impl Filter for FilterPaths { #[cfg(feature = "json")] DataInner::Json(value) => { let mut value = value; - normalize_json_string(&mut value, normalize_paths); + normalize_json_string(&mut value, &normalize_paths); DataInner::Json(value) } #[cfg(feature = "json")] DataInner::JsonLines(value) => { let mut value = value; - normalize_json_string(&mut value, normalize_paths); + normalize_json_string(&mut value, &normalize_paths); DataInner::JsonLines(value) } #[cfg(feature = "term-svg")] @@ -119,8 +119,48 @@ fn normalize_paths_chars(data: impl Iterator) -> impl Iterator { + redactions: &'r Redactions, +} +impl Filter for NormalizeRedactions<'_> { + fn filter(&self, data: Data) -> Data { + let source = data.source; + let filters = data.filters; + let inner = match data.inner { + DataInner::Error(err) => DataInner::Error(err), + DataInner::Binary(bin) => DataInner::Binary(bin), + DataInner::Text(text) => { + let lines = self.redactions.redact(&text); + DataInner::Text(lines) + } + #[cfg(feature = "json")] + DataInner::Json(value) => { + let mut value = value; + normalize_json_string(&mut value, &|s| self.redactions.redact(s)); + DataInner::Json(value) + } + #[cfg(feature = "json")] + DataInner::JsonLines(value) => { + let mut value = value; + normalize_json_string(&mut value, &|s| self.redactions.redact(s)); + DataInner::JsonLines(value) + } + #[cfg(feature = "term-svg")] + DataInner::TermSvg(text) => { + let lines = normalize_lines(&text); + DataInner::TermSvg(lines) + } + }; + Data { + inner, + source, + filters, + } + } +} + #[cfg(feature = "structured-data")] -fn normalize_json_string(value: &mut serde_json::Value, op: fn(&str) -> String) { +fn normalize_json_string(value: &mut serde_json::Value, op: &dyn Fn(&str) -> String) { match value { serde_json::Value::String(str) => { *str = op(str); diff --git a/crates/snapbox/src/filter/pattern.rs b/crates/snapbox/src/filter/pattern.rs index 857bab20..c8fe33fd 100644 --- a/crates/snapbox/src/filter/pattern.rs +++ b/crates/snapbox/src/filter/pattern.rs @@ -1,4 +1,4 @@ -use super::Redactions; +use super::{Filter, NormalizeRedactions, Redactions}; use crate::data::DataInner; use crate::Data; @@ -43,6 +43,10 @@ impl<'a> NormalizeToExpected<'a> { let Some(substitutions) = self.substitutions else { return actual; }; + let actual = NormalizeRedactions { + redactions: substitutions, + } + .filter(actual); normalize_data_to_redactions(actual, expected, substitutions) } } @@ -160,7 +164,6 @@ fn normalize_value_to_redactions( let has_key_wildcard = exp.get(KEY_WILDCARD).and_then(|v| v.as_str()) == Some(VALUE_WILDCARD); for (actual_key, mut actual_value) in std::mem::replace(act, serde_json::Map::new()) { - let actual_key = substitutions.redact(&actual_key); if let Some(expected_value) = exp.get(&actual_key) { normalize_value_to_redactions(&mut actual_value, expected_value, substitutions) } else if has_key_wildcard { @@ -181,11 +184,9 @@ fn normalize_str_to_redactions(input: &str, pattern: &str, redactions: &Redactio return input.to_owned(); } - let input = redactions.redact(input); - let mut normalized: Vec<&str> = Vec::new(); let mut input_index = 0; - let input_lines: Vec<_> = crate::utils::LinesWithTerminator::new(&input).collect(); + let input_lines: Vec<_> = crate::utils::LinesWithTerminator::new(input).collect(); let mut pattern_lines = crate::utils::LinesWithTerminator::new(pattern).peekable(); 'outer: while let Some(pattern_line) = pattern_lines.next() { if is_line_elide(pattern_line) {