Skip to content

Commit

Permalink
fix(filter)!: Rename Normalize trait to Filter
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Apr 22, 2024
1 parent 203add4 commit 94cb553
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 119 deletions.
14 changes: 7 additions & 7 deletions crates/snapbox/src/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use anstream::stderr;
#[cfg(not(feature = "color"))]
use std::io::stderr;

use crate::data::{Normalize as _, NormalizeMatches, NormalizeNewlines, NormalizePaths};
use crate::data::{Filter as _, FilterNewlines, FilterPaths, FilterRedactions};
use crate::Action;

/// Snapshot assertion against a file's contents
Expand Down Expand Up @@ -143,11 +143,11 @@ impl Assert {
expected: crate::Data,
mut actual: crate::Data,
) -> (crate::Data, crate::Data) {
let expected = NormalizeNewlines.normalize(expected);
let expected = FilterNewlines.filter(expected);
// On `expected` being an error, make a best guess
let format = expected.intended_format();

actual = NormalizeNewlines.normalize(actual.coerce_to(format));
actual = FilterNewlines.filter(actual.coerce_to(format));

(expected, actual)
}
Expand All @@ -157,19 +157,19 @@ impl Assert {
expected: crate::Data,
mut actual: crate::Data,
) -> (crate::Data, crate::Data) {
let expected = NormalizeNewlines.normalize(expected);
let expected = FilterNewlines.filter(expected);
// On `expected` being an error, make a best guess
let format = expected.intended_format();
actual = actual.coerce_to(format);

if self.normalize_paths {
actual = NormalizePaths.normalize(actual);
actual = FilterPaths.filter(actual);
}
// Always normalize new lines
actual = NormalizeNewlines.normalize(actual);
actual = FilterNewlines.filter(actual);

// If expected is not an error normalize matches
actual = NormalizeMatches::new(&self.substitutions, &expected).normalize(actual);
actual = FilterRedactions::new(&self.substitutions, &expected).filter(actual);

(expected, actual)
}
Expand Down
8 changes: 4 additions & 4 deletions crates/snapbox/src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ mod source;
mod tests;

pub use format::DataFormat;
pub use normalize::Normalize;
pub use normalize::NormalizeMatches;
pub use normalize::NormalizeNewlines;
pub use normalize::NormalizePaths;
pub use normalize::Filter;
pub use normalize::FilterNewlines;
pub use normalize::FilterPaths;
pub use normalize::FilterRedactions;
pub use source::DataSource;
pub use source::Inline;
pub use source::Position;
Expand Down
26 changes: 13 additions & 13 deletions crates/snapbox/src/data/normalize.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use super::Data;
use super::DataInner;

pub trait Normalize {
fn normalize(&self, data: Data) -> Data;
pub trait Filter {
fn filter(&self, data: Data) -> Data;
}

pub struct NormalizeNewlines;
impl Normalize for NormalizeNewlines {
fn normalize(&self, data: Data) -> Data {
pub struct FilterNewlines;
impl Filter for FilterNewlines {
fn filter(&self, data: Data) -> Data {
let mut new = match data.inner {
DataInner::Error(err) => err.into(),
DataInner::Binary(bin) => Data::binary(bin),
Expand Down Expand Up @@ -38,9 +38,9 @@ impl Normalize for NormalizeNewlines {
}
}

pub struct NormalizePaths;
impl Normalize for NormalizePaths {
fn normalize(&self, data: Data) -> Data {
pub struct FilterPaths;
impl Filter for FilterPaths {
fn filter(&self, data: Data) -> Data {
let mut new = match data.inner {
DataInner::Error(err) => err.into(),
DataInner::Binary(bin) => Data::binary(bin),
Expand Down Expand Up @@ -71,22 +71,22 @@ impl Normalize for NormalizePaths {
}
}

pub struct NormalizeMatches<'a> {
pub struct FilterRedactions<'a> {
substitutions: &'a crate::Redactions,
pattern: &'a Data,
}

impl<'a> NormalizeMatches<'a> {
impl<'a> FilterRedactions<'a> {
pub fn new(substitutions: &'a crate::Redactions, pattern: &'a Data) -> Self {
NormalizeMatches {
FilterRedactions {
substitutions,
pattern,
}
}
}

impl Normalize for NormalizeMatches<'_> {
fn normalize(&self, data: Data) -> Data {
impl Filter for FilterRedactions<'_> {
fn filter(&self, data: Data) -> Data {
let mut new = match data.inner {
DataInner::Error(err) => err.into(),
DataInner::Binary(bin) => Data::binary(bin),
Expand Down
42 changes: 17 additions & 25 deletions crates/snapbox/src/data/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,9 @@ fn json_to_text_coerce_equals_render() {
fn json_normalize_paths_and_lines() {
let json = json!({"name": "John\\Doe\r\n"});
let data = Data::json(json);
let data = NormalizePaths.normalize(data);
let data = FilterPaths.filter(data);
assert_eq!(Data::json(json!({"name": "John/Doe\r\n"})), data);
let data = NormalizeNewlines.normalize(data);
let data = FilterNewlines.filter(data);
assert_eq!(Data::json(json!({"name": "John/Doe\n"})), data);
}

Expand All @@ -237,15 +237,15 @@ fn json_normalize_obj_paths_and_lines() {
}
});
let data = Data::json(json);
let data = NormalizePaths.normalize(data);
let data = FilterPaths.filter(data);
let assert = json!({
"person": {
"name": "John/Doe\r\n",
"nickname": "Jo/hn\r\n",
}
});
assert_eq!(Data::json(assert), data);
let data = NormalizeNewlines.normalize(data);
let data = FilterNewlines.filter(data);
let assert = json!({
"person": {
"name": "John/Doe\n",
Expand All @@ -260,10 +260,10 @@ fn json_normalize_obj_paths_and_lines() {
fn json_normalize_array_paths_and_lines() {
let json = json!({"people": ["John\\Doe\r\n", "Jo\\hn\r\n"]});
let data = Data::json(json);
let data = NormalizePaths.normalize(data);
let data = FilterPaths.filter(data);
let paths = json!({"people": ["John/Doe\r\n", "Jo/hn\r\n"]});
assert_eq!(Data::json(paths), data);
let data = NormalizeNewlines.normalize(data);
let data = FilterNewlines.filter(data);
let new_lines = json!({"people": ["John/Doe\n", "Jo/hn\n"]});
assert_eq!(Data::json(new_lines), data);
}
Expand All @@ -280,7 +280,7 @@ fn json_normalize_array_obj_paths_and_lines() {
]
});
let data = Data::json(json);
let data = NormalizePaths.normalize(data);
let data = FilterPaths.filter(data);
let paths = json!({
"people": [
{
Expand All @@ -290,7 +290,7 @@ fn json_normalize_array_obj_paths_and_lines() {
]
});
assert_eq!(Data::json(paths), data);
let data = NormalizeNewlines.normalize(data);
let data = FilterNewlines.filter(data);
let new_lines = json!({
"people": [
{
Expand All @@ -308,8 +308,7 @@ fn json_normalize_matches_string() {
let exp = json!({"name": "{...}"});
let expected = Data::json(exp);
let actual = json!({"name": "JohnDoe"});
let actual =
NormalizeMatches::new(&Default::default(), &expected).normalize(Data::json(actual));
let actual = FilterRedactions::new(&Default::default(), &expected).filter(Data::json(actual));
if let (DataInner::Json(exp), DataInner::Json(act)) = (expected.inner, actual.inner) {
assert_eq!(exp, act);
}
Expand All @@ -328,8 +327,7 @@ fn json_normalize_matches_array() {
}
]
});
let actual =
NormalizeMatches::new(&Default::default(), &expected).normalize(Data::json(actual));
let actual = FilterRedactions::new(&Default::default(), &expected).filter(Data::json(actual));
if let (DataInner::Json(exp), DataInner::Json(act)) = (expected.inner, actual.inner) {
assert_eq!(exp, act);
}
Expand All @@ -346,8 +344,7 @@ fn json_normalize_matches_obj() {
"nickname": "John",
}
});
let actual =
NormalizeMatches::new(&Default::default(), &expected).normalize(Data::json(actual));
let actual = FilterRedactions::new(&Default::default(), &expected).filter(Data::json(actual));
if let (DataInner::Json(exp), DataInner::Json(act)) = (expected.inner, actual.inner) {
assert_eq!(exp, act);
}
Expand All @@ -363,8 +360,7 @@ fn json_normalize_matches_diff_order_array() {
let actual = json!({
"people": ["Jane", "John"]
});
let actual =
NormalizeMatches::new(&Default::default(), &expected).normalize(Data::json(actual));
let actual = FilterRedactions::new(&Default::default(), &expected).filter(Data::json(actual));
if let (DataInner::Json(exp), DataInner::Json(act)) = (expected.inner, actual.inner) {
assert_ne!(exp, act);
}
Expand Down Expand Up @@ -399,8 +395,7 @@ fn json_normalize_wildcard_object_first() {
}
]
});
let actual =
NormalizeMatches::new(&Default::default(), &expected).normalize(Data::json(actual));
let actual = FilterRedactions::new(&Default::default(), &expected).filter(Data::json(actual));
if let (DataInner::Json(exp), DataInner::Json(act)) = (expected.inner, actual.inner) {
assert_eq!(exp, act);
}
Expand Down Expand Up @@ -431,8 +426,7 @@ fn json_normalize_wildcard_array_first() {
"nickname": "3",
}
]);
let actual =
NormalizeMatches::new(&Default::default(), &expected).normalize(Data::json(actual));
let actual = FilterRedactions::new(&Default::default(), &expected).filter(Data::json(actual));
if let (DataInner::Json(exp), DataInner::Json(act)) = (expected.inner, actual.inner) {
assert_eq!(exp, act);
}
Expand Down Expand Up @@ -468,8 +462,7 @@ fn json_normalize_wildcard_array_first_last() {
"nickname": "4",
}
]);
let actual =
NormalizeMatches::new(&Default::default(), &expected).normalize(Data::json(actual));
let actual = FilterRedactions::new(&Default::default(), &expected).filter(Data::json(actual));
if let (DataInner::Json(exp), DataInner::Json(act)) = (expected.inner, actual.inner) {
assert_eq!(exp, act);
}
Expand Down Expand Up @@ -513,8 +506,7 @@ fn json_normalize_wildcard_array_middle_last() {
"nickname": "5",
}
]);
let actual =
NormalizeMatches::new(&Default::default(), &expected).normalize(Data::json(actual));
let actual = FilterRedactions::new(&Default::default(), &expected).filter(Data::json(actual));
if let (DataInner::Json(exp), DataInner::Json(act)) = (expected.inner, actual.inner) {
assert_eq!(exp, act);
}
Expand Down Expand Up @@ -555,7 +547,7 @@ fn json_normalize_wildcard_array_middle_last_early_return() {
}
]);
let actual_normalized =
NormalizeMatches::new(&Default::default(), &expected).normalize(Data::json(actual.clone()));
FilterRedactions::new(&Default::default(), &expected).filter(Data::json(actual.clone()));
if let DataInner::Json(act) = actual_normalized.inner {
assert_eq!(act, actual);
}
Expand Down
15 changes: 7 additions & 8 deletions crates/snapbox/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub use crate::current_dir;
pub use crate::current_rs;

#[cfg(feature = "path")]
use crate::data::{Normalize as _, NormalizeMatches, NormalizeNewlines, NormalizePaths};
use crate::data::{Filter as _, FilterNewlines, FilterPaths, FilterRedactions};

/// Working directory for tests
#[derive(Debug)]
Expand Down Expand Up @@ -189,10 +189,9 @@ impl PathDiff {
crate::Data::try_read_from(&actual_path, None).map_err(Self::Failure)?;

let expected =
NormalizeNewlines.normalize(crate::Data::read_from(&expected_path, None));
FilterNewlines.filter(crate::Data::read_from(&expected_path, None));

actual =
NormalizeNewlines.normalize(actual.coerce_to(expected.intended_format()));
actual = FilterNewlines.filter(actual.coerce_to(expected.intended_format()));

if expected != actual {
return Err(Self::ContentMismatch {
Expand Down Expand Up @@ -266,14 +265,14 @@ impl PathDiff {
crate::Data::try_read_from(&actual_path, None).map_err(Self::Failure)?;

let expected =
NormalizeNewlines.normalize(crate::Data::read_from(&expected_path, None));
FilterNewlines.filter(crate::Data::read_from(&expected_path, None));

actual = actual.coerce_to(expected.intended_format());
if normalize_paths {
actual = NormalizePaths.normalize(actual);
actual = FilterPaths.filter(actual);
}
actual = NormalizeMatches::new(substitutions, &expected)
.normalize(NormalizeNewlines.normalize(actual));
actual = FilterRedactions::new(substitutions, &expected)
.filter(FilterNewlines.filter(actual));

if expected != actual {
return Err(Self::ContentMismatch {
Expand Down
8 changes: 4 additions & 4 deletions src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::eprintln;
use std::io::stderr;

use rayon::prelude::*;
use snapbox::data::{DataFormat, Normalize as _, NormalizeNewlines, NormalizePaths};
use snapbox::data::{DataFormat, Filter as _, FilterNewlines, FilterPaths};
use snapbox::path::FileType;

#[derive(Debug)]
Expand Down Expand Up @@ -437,8 +437,8 @@ impl Case {
}

if let Some(expected_content) = expected_content {
stream.content = snapbox::data::NormalizeMatches::new(substitutions, expected_content)
.normalize(stream.content);
stream.content = snapbox::data::FilterRedactions::new(substitutions, expected_content)
.filter(stream.content);

if stream.content != *expected_content {
stream.status = StreamStatus::Expected(expected_content.clone());
Expand Down Expand Up @@ -732,7 +732,7 @@ impl Stream {
if content.format() != DataFormat::Text {
self.status = StreamStatus::Failure("Unable to convert underlying Data to Text".into());
}
self.content = NormalizeNewlines.normalize(NormalizePaths.normalize(content));
self.content = FilterNewlines.filter(FilterPaths.filter(content));
self
}

Expand Down
Loading

0 comments on commit 94cb553

Please sign in to comment.