Skip to content

Commit

Permalink
Merge pull request assert-rs#273 from epage/wild
Browse files Browse the repository at this point in the history
refactor(snap): Clean up json wildcards
  • Loading branch information
epage authored Apr 18, 2024
2 parents cbda979 + ab3d141 commit c1c0b01
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions crates/snapbox/src/data/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,18 @@ fn normalize_value_matches(
substitutions: &crate::Substitutions,
) {
use serde_json::Value::*;

const VALUE_WILDCARD: &str = "{...}";

match (actual, expected) {
// "{...}" is a wildcard
(act, String(exp)) if exp == "{...}" => {
*act = serde_json::json!("{...}");
(act, String(exp)) if exp == VALUE_WILDCARD => {
*act = serde_json::json!(VALUE_WILDCARD);
}
(String(act), String(exp)) => {
*act = substitutions.normalize(act, exp);
}
(Array(act), Array(exp)) => {
let wildcard = String("{...}".to_string());
let mut sections = exp.split(|e| e == &wildcard).peekable();
let mut sections = exp.split(|e| e == VALUE_WILDCARD).peekable();
let mut processed = 0;
while let Some(expected_subset) = sections.next() {
// Process all values in the current section
Expand All @@ -162,14 +163,14 @@ fn normalize_value_matches(
// If the next section has nothing in it, replace from processed to end with
// a single "{...}"
if next_section.is_empty() {
act.splice(processed.., vec![wildcard.clone()]);
act.splice(processed.., vec![String(VALUE_WILDCARD.to_owned())]);
processed += 1;
} else {
let first = next_section.first().unwrap();
// Replace everything up until the value we are looking for with
// a single "{...}".
if let Some(index) = act.iter().position(|v| v == first) {
act.splice(processed..index, vec![wildcard.clone()]);
act.splice(processed..index, vec![String(VALUE_WILDCARD.to_owned())]);
processed += 1;
} else {
// If we cannot find the value we are looking for return early
Expand Down

0 comments on commit c1c0b01

Please sign in to comment.