Skip to content

Commit

Permalink
Merge pull request #285 from epage/rename
Browse files Browse the repository at this point in the history
fix(filters)!: Follow naming pattern of `insta`
  • Loading branch information
epage authored Apr 22, 2024
2 parents ed9fbae + 38c4fb8 commit 1a9f590
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 118 deletions.
8 changes: 4 additions & 4 deletions crates/snapbox/src/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct Assert {
pub(crate) action: Action,
action_var: Option<String>,
normalize_paths: bool,
substitutions: crate::Substitutions,
substitutions: crate::Redactions,
pub(crate) palette: crate::report::Palette,
}

Expand Down Expand Up @@ -432,8 +432,8 @@ impl Assert {
self
}

/// Override the default [`Substitutions`][crate::Substitutions]
pub fn substitutions(mut self, substitutions: crate::Substitutions) -> Self {
/// Override the default [`Redactions`][crate::Redactions]
pub fn substitutions(mut self, substitutions: crate::Redactions) -> Self {
self.substitutions = substitutions;
self
}
Expand All @@ -456,6 +456,6 @@ impl Default for Assert {
substitutions: Default::default(),
palette: crate::report::Palette::color(),
}
.substitutions(crate::Substitutions::with_exe())
.substitutions(crate::Redactions::with_exe())
}
}
22 changes: 11 additions & 11 deletions crates/snapbox/src/data/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@ impl Normalize for NormalizeNewlines {
DataInner::Error(err) => err.into(),
DataInner::Binary(bin) => Data::binary(bin),
DataInner::Text(text) => {
let lines = crate::normalization::normalize_lines(&text);
let lines = crate::filters::normalize_lines(&text);
Data::text(lines)
}
#[cfg(feature = "json")]
DataInner::Json(value) => {
let mut value = value;
normalize_value(&mut value, crate::normalization::normalize_lines);
normalize_value(&mut value, crate::filters::normalize_lines);
Data::json(value)
}
#[cfg(feature = "json")]
DataInner::JsonLines(value) => {
let mut value = value;
normalize_value(&mut value, crate::normalization::normalize_lines);
normalize_value(&mut value, crate::filters::normalize_lines);
DataInner::JsonLines(value).into()
}
#[cfg(feature = "term-svg")]
DataInner::TermSvg(text) => {
let lines = crate::normalization::normalize_lines(&text);
let lines = crate::filters::normalize_lines(&text);
DataInner::TermSvg(lines).into()
}
};
Expand All @@ -45,24 +45,24 @@ impl Normalize for NormalizePaths {
DataInner::Error(err) => err.into(),
DataInner::Binary(bin) => Data::binary(bin),
DataInner::Text(text) => {
let lines = crate::normalization::normalize_paths(&text);
let lines = crate::filters::normalize_paths(&text);
Data::text(lines)
}
#[cfg(feature = "json")]
DataInner::Json(value) => {
let mut value = value;
normalize_value(&mut value, crate::normalization::normalize_paths);
normalize_value(&mut value, crate::filters::normalize_paths);
Data::json(value)
}
#[cfg(feature = "json")]
DataInner::JsonLines(value) => {
let mut value = value;
normalize_value(&mut value, crate::normalization::normalize_paths);
normalize_value(&mut value, crate::filters::normalize_paths);
DataInner::JsonLines(value).into()
}
#[cfg(feature = "term-svg")]
DataInner::TermSvg(text) => {
let lines = crate::normalization::normalize_paths(&text);
let lines = crate::filters::normalize_paths(&text);
DataInner::TermSvg(lines).into()
}
};
Expand All @@ -72,12 +72,12 @@ impl Normalize for NormalizePaths {
}

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

impl<'a> NormalizeMatches<'a> {
pub fn new(substitutions: &'a crate::Substitutions, pattern: &'a Data) -> Self {
pub fn new(substitutions: &'a crate::Redactions, pattern: &'a Data) -> Self {
NormalizeMatches {
substitutions,
pattern,
Expand Down Expand Up @@ -153,7 +153,7 @@ fn normalize_value(value: &mut serde_json::Value, op: fn(&str) -> String) {
fn normalize_value_matches(
actual: &mut serde_json::Value,
expected: &serde_json::Value,
substitutions: &crate::Substitutions,
substitutions: &crate::Redactions,
) {
use serde_json::Value::*;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
mod substitutions;
//! Normalize `actual` or `expected` data
//!
//! This can be done for
//! - Making snapshots consistent across platforms or conditional compilation
//! - Focusing snapshots on the characteristics of the data being tested
mod redactions;

pub use redactions::RedactedValue;
pub use redactions::Redactions;
#[cfg(feature = "regex")]
pub use regex::Regex;
pub use substitutions::SubstitutedValue;
pub use substitutions::Substitutions;

/// Normalize line endings
pub fn normalize_lines(data: &str) -> String {
Expand All @@ -15,6 +21,11 @@ fn normalize_lines_chars(data: impl Iterator<Item = char>) -> impl Iterator<Item
}

/// Normalize path separators
///
/// [`std::path::MAIN_SEPARATOR`] can vary by platform, so make it consistent
///
/// Note: this cannot distinguish between when a character is being used as a path separator or not
/// and can "normalize" unrelated data
pub fn normalize_paths(data: &str) -> String {
normalize_paths_chars(data.chars()).collect()
}
Expand Down
Loading

0 comments on commit 1a9f590

Please sign in to comment.