Skip to content

Commit

Permalink
feat(filter)!: Allow key wildcards
Browse files Browse the repository at this point in the history
Fixes #134
  • Loading branch information
epage committed May 23, 2024
1 parent 9a873fa commit 4c1e2c0
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions crates/snapbox/src/assert/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ impl Assert {
/// - `[..]` is a character-wildcard when inside a line
/// - `[EXE]` matches `.exe` on Windows
/// - `"{...}"` is a JSON value wildcard
/// - `"...": "{...}"` is a JSON key-value wildcard
/// - `\` to `/`
/// - Newlines
///
Expand Down
2 changes: 2 additions & 0 deletions crates/snapbox/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,7 @@ impl OutputAssert {
/// - `[..]` is a character-wildcard when inside a line
/// - `[EXE]` matches `.exe` on Windows
/// - `"{...}"` is a JSON value wildcard
/// - `"...": "{...}"` is a JSON key-value wildcard
/// - `\` to `/`
/// - Newlines
///
Expand Down Expand Up @@ -654,6 +655,7 @@ impl OutputAssert {
/// - `[..]` is a character-wildcard when inside a line
/// - `[EXE]` matches `.exe` on Windows
/// - `"{...}"` is a JSON value wildcard
/// - `"...": "{...}"` is a JSON key-value wildcard
/// - `\` to `/`
/// - Newlines
///
Expand Down
8 changes: 8 additions & 0 deletions crates/snapbox/src/filter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ fn normalize_value_matches(
) {
use serde_json::Value::*;

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

match (actual, expected) {
Expand Down Expand Up @@ -254,13 +255,20 @@ fn normalize_value_matches(
}
}
(Object(act), Object(exp)) => {
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.normalize(&actual_key, "");
if let Some(expected_value) = exp.get(&actual_key) {
normalize_value_matches(&mut actual_value, expected_value, substitutions)
} else if has_key_wildcard {
continue;
}
act.insert(actual_key, actual_value);
}
if has_key_wildcard {
act.insert(KEY_WILDCARD.to_owned(), String(VALUE_WILDCARD.to_owned()));
}
}
(_, _) => {}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/snapbox/src/filter/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ fn json_normalize_wildcard_key() {

let expected_actual = json!({
"a": "value-a",
"b": "value-b",
"c": "value-c",
"...": "{...}",
});
let expected_actual = Data::json(expected_actual);
assert_eq!(actual, expected_actual);
Expand Down
1 change: 1 addition & 0 deletions crates/snapbox/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/// - `[..]` is a character-wildcard when inside a line
/// - `[EXE]` matches `.exe` on Windows
/// - `"{...}"` is a JSON value wildcard
/// - `"...": "{...}"` is a JSON key-value wildcard
/// - `\` to `/`
/// - Newlines
///
Expand Down
1 change: 1 addition & 0 deletions crates/tryfn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ where
/// - `[..]` is a character-wildcard when inside a line
/// - `[EXE]` matches `.exe` on Windows
/// - `"{...}"` is a JSON value wildcard
/// - `"...": "{...}"` is a JSON key-value wildcard
/// - `\` to `/`
/// - Newlines
///
Expand Down

0 comments on commit 4c1e2c0

Please sign in to comment.