Skip to content

Commit

Permalink
feat(filter): Unordered match support
Browse files Browse the repository at this point in the history
Fixes #151
  • Loading branch information
epage committed May 23, 2024
1 parent c44c6f4 commit 4134e1a
Show file tree
Hide file tree
Showing 7 changed files with 1,203 additions and 12 deletions.
10 changes: 7 additions & 3 deletions crates/snapbox/src/assert/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,15 @@ impl Assert {
if expected.filters.is_newlines_set() {
actual = FilterNewlines.filter(actual);
}

let mut normalize = NormalizeToExpected::new();
if expected.filters.is_redaction_set() {
actual = NormalizeToExpected::new()
.redact_with(&self.substitutions)
.normalize(actual, &expected);
normalize = normalize.redact_with(&self.substitutions);
}
if expected.filters.is_unordered_set() {
normalize = normalize.unordered();
}
actual = normalize.normalize(actual, &expected);

(actual, expected)
}
Expand Down
10 changes: 10 additions & 0 deletions crates/snapbox/src/data/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ impl FilterSet {
self
}

pub(crate) fn unordered(mut self) -> Self {
self.set(Self::UNORDERED);
self
}

pub(crate) const fn is_redaction_set(&self) -> bool {
self.is_set(Self::REDACTIONS)
}
Expand All @@ -38,12 +43,17 @@ impl FilterSet {
pub(crate) const fn is_paths_set(&self) -> bool {
self.is_set(Self::PATHS)
}

pub(crate) const fn is_unordered_set(&self) -> bool {
self.is_set(Self::UNORDERED)
}
}

impl FilterSet {
const REDACTIONS: usize = 1 << 0;
const NEWLINES: usize = 1 << 1;
const PATHS: usize = 1 << 2;
const UNORDERED: usize = 1 << 3;

fn set(&mut self, flag: usize) -> &mut Self {
self.flags |= flag;
Expand Down
29 changes: 29 additions & 0 deletions crates/snapbox/src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,29 @@ pub trait IntoData: Sized {
self.into_data().raw()
}

/// Treat lines and json arrays as unordered
///
/// # Examples
///
/// ```rust
/// # #[cfg(feature = "json")] {
/// use snapbox::prelude::*;
/// use snapbox::str;
/// use snapbox::assert_data_eq;
///
/// let actual = str![[r#"["world", "hello"]"#]]
/// .is(snapbox::data::DataFormat::Json)
/// .unordered();
/// let expected = str![[r#"["hello", "world"]"#]]
/// .is(snapbox::data::DataFormat::Json)
/// .unordered();
/// assert_data_eq!(actual, expected);
/// # }
/// ```
fn unordered(self) -> Data {
self.into_data().unordered()
}

/// Initialize as [`format`][DataFormat] or [`Error`][DataFormat::Error]
///
/// This is generally used for `expected` data
Expand Down Expand Up @@ -301,6 +324,12 @@ impl Data {
self.filters = FilterSet::empty().newlines();
self
}

/// Treat lines and json arrays as unordered
pub fn unordered(mut self) -> Self {
self.filters = FilterSet::empty().unordered();
self
}
}

/// # Assertion frameworks operations
Expand Down
2 changes: 2 additions & 0 deletions crates/snapbox/src/filter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ mod redactions;
mod test;
#[cfg(test)]
mod test_redactions;
#[cfg(test)]
mod test_unordered_redactions;

use crate::data::DataInner;
use crate::Data;
Expand Down
Loading

0 comments on commit 4134e1a

Please sign in to comment.