Skip to content

Commit

Permalink
Merge pull request #336 from epage/term
Browse files Browse the repository at this point in the history
fix(filter): Normalize without consideration for TermSvg headers
  • Loading branch information
epage authored May 27, 2024
2 parents 828ebb7 + e607393 commit 58f2474
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 59 deletions.
18 changes: 13 additions & 5 deletions crates/snapbox/src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ impl Data {
#[cfg(feature = "json")]
DataInner::JsonLines(_) => None,
#[cfg(feature = "term-svg")]
DataInner::TermSvg(data) => text_elem(data),
DataInner::TermSvg(data) => term_svg_body(data),
}
}
}
Expand Down Expand Up @@ -674,8 +674,8 @@ impl PartialEq for Data {
#[cfg(feature = "term-svg")]
(DataInner::TermSvg(left), DataInner::TermSvg(right)) => {
// HACK: avoid including `width` and `height` in the comparison
let left = text_elem(left.as_str());
let right = text_elem(right.as_str());
let left = term_svg_body(left.as_str()).unwrap_or(left.as_str());
let right = term_svg_body(right.as_str()).unwrap_or(right.as_str());
left == right
}
(_, _) => false,
Expand Down Expand Up @@ -710,7 +710,13 @@ fn parse_jsonlines(text: &str) -> Result<Vec<serde_json::Value>, serde_json::Err
}

#[cfg(feature = "term-svg")]
fn text_elem(svg: &str) -> Option<&str> {
fn term_svg_body(svg: &str) -> Option<&str> {
let (_header, body, _footer) = split_term_svg(svg)?;
Some(body)
}

#[cfg(feature = "term-svg")]
pub(crate) fn split_term_svg(svg: &str) -> Option<(&str, &str, &str)> {
let open_elem_start_idx = svg.find("<text")?;
_ = svg[open_elem_start_idx..].find('>')?;
let open_elem_line_start_idx = svg[..open_elem_start_idx]
Expand All @@ -725,8 +731,10 @@ fn text_elem(svg: &str) -> Option<&str> {
.map(|idx| idx + close_elem_start_idx + 1)
.unwrap_or(svg.len());

let header = &svg[..open_elem_line_start_idx];
let body = &svg[open_elem_line_start_idx..close_elem_line_end_idx];
Some(body)
let footer = &svg[close_elem_line_end_idx..];
Some((header, body, footer))
}

impl Eq for Data {}
Expand Down
12 changes: 6 additions & 6 deletions crates/snapbox/src/data/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,14 @@ fn json_to_text_coerce_equals_render() {
}

#[cfg(feature = "term-svg")]
mod text_elem {
mod term_svg_body {
use super::super::*;

#[test]
fn empty() {
let input = "";
let expected = None;
let actual = text_elem(input);
let actual = term_svg_body(input);
assert_eq!(expected, actual);
}

Expand All @@ -233,7 +233,7 @@ mod text_elem {
</text>
world!";
let expected = None;
let actual = text_elem(input);
let actual = term_svg_body(input);
assert_eq!(expected, actual);
}

Expand All @@ -244,7 +244,7 @@ Hello
<text
world!";
let expected = None;
let actual = text_elem(input);
let actual = term_svg_body(input);
assert_eq!(expected, actual);
}

Expand All @@ -262,7 +262,7 @@ world
</text>
",
);
let actual = text_elem(input);
let actual = term_svg_body(input);
assert_eq!(expected, actual);
}

Expand All @@ -276,7 +276,7 @@ world";
"<text>
world",
);
let actual = text_elem(input);
let actual = term_svg_body(input);
assert_eq!(expected, actual);
}
}
102 changes: 54 additions & 48 deletions crates/snapbox/src/filter/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ impl Default for NormalizeToExpected<'_> {
fn normalize_data_to_unordered(actual: Data, expected: &Data) -> Data {
let source = actual.source;
let filters = actual.filters;
let inner = match actual.inner {
DataInner::Error(err) => DataInner::Error(err),
DataInner::Binary(bin) => DataInner::Binary(bin),
DataInner::Text(text) => {
let inner = match (actual.inner, &expected.inner) {
(DataInner::Error(err), _) => DataInner::Error(err),
(DataInner::Binary(bin), _) => DataInner::Binary(bin),
(DataInner::Text(text), _) => {
if let Some(pattern) = expected.render() {
let lines = normalize_str_to_unordered(&text, &pattern);
DataInner::Text(lines)
Expand All @@ -92,30 +92,32 @@ fn normalize_data_to_unordered(actual: Data, expected: &Data) -> Data {
}
}
#[cfg(feature = "json")]
DataInner::Json(value) => {
(DataInner::Json(value), DataInner::Json(exp)) => {
let mut value = value;
if let DataInner::Json(exp) = &expected.inner {
normalize_value_to_unordered(&mut value, exp);
}
normalize_value_to_unordered(&mut value, exp);
DataInner::Json(value)
}
#[cfg(feature = "json")]
DataInner::JsonLines(value) => {
(DataInner::JsonLines(value), DataInner::JsonLines(exp)) => {
let mut value = value;
if let DataInner::Json(exp) = &expected.inner {
normalize_value_to_unordered(&mut value, exp);
}
normalize_value_to_unordered(&mut value, exp);
DataInner::JsonLines(value)
}
#[cfg(feature = "term-svg")]
DataInner::TermSvg(text) => {
if let Some(pattern) = expected.render() {
let lines = normalize_str_to_unordered(&text, &pattern);
DataInner::TermSvg(lines)
(DataInner::TermSvg(text), DataInner::TermSvg(exp)) => {
if let (Some((header, body, footer)), Some((_, exp, _))) = (
crate::data::split_term_svg(&text),
crate::data::split_term_svg(exp),
) {
let lines = normalize_str_to_unordered(body, exp);
DataInner::TermSvg(format!("{header}{lines}{footer}"))
} else {
DataInner::TermSvg(text)
}
}
// reachable if more than one structured data format is enabled
#[allow(unreachable_patterns)]
(inner, _) => inner,
};
Data {
inner,
Expand Down Expand Up @@ -208,10 +210,10 @@ fn normalize_data_to_unordered_redactions(
) -> Data {
let source = actual.source;
let filters = actual.filters;
let inner = match actual.inner {
DataInner::Error(err) => DataInner::Error(err),
DataInner::Binary(bin) => DataInner::Binary(bin),
DataInner::Text(text) => {
let inner = match (actual.inner, &expected.inner) {
(DataInner::Error(err), _) => DataInner::Error(err),
(DataInner::Binary(bin), _) => DataInner::Binary(bin),
(DataInner::Text(text), _) => {
if let Some(pattern) = expected.render() {
let lines = normalize_str_to_unordered_redactions(&text, &pattern, substitutions);
DataInner::Text(lines)
Expand All @@ -220,30 +222,32 @@ fn normalize_data_to_unordered_redactions(
}
}
#[cfg(feature = "json")]
DataInner::Json(value) => {
(DataInner::Json(value), DataInner::Json(exp)) => {
let mut value = value;
if let DataInner::Json(exp) = &expected.inner {
normalize_value_to_unordered_redactions(&mut value, exp, substitutions);
}
normalize_value_to_unordered_redactions(&mut value, exp, substitutions);
DataInner::Json(value)
}
#[cfg(feature = "json")]
DataInner::JsonLines(value) => {
(DataInner::JsonLines(value), DataInner::JsonLines(exp)) => {
let mut value = value;
if let DataInner::Json(exp) = &expected.inner {
normalize_value_to_unordered_redactions(&mut value, exp, substitutions);
}
normalize_value_to_unordered_redactions(&mut value, exp, substitutions);
DataInner::JsonLines(value)
}
#[cfg(feature = "term-svg")]
DataInner::TermSvg(text) => {
if let Some(pattern) = expected.render() {
let lines = normalize_str_to_unordered_redactions(&text, &pattern, substitutions);
DataInner::TermSvg(lines)
(DataInner::TermSvg(text), DataInner::TermSvg(exp)) => {
if let (Some((header, body, footer)), Some((_, exp, _))) = (
crate::data::split_term_svg(&text),
crate::data::split_term_svg(exp),
) {
let lines = normalize_str_to_unordered_redactions(body, exp, substitutions);
DataInner::TermSvg(format!("{header}{lines}{footer}"))
} else {
DataInner::TermSvg(text)
}
}
// reachable if more than one structured data format is enabled
#[allow(unreachable_patterns)]
(inner, _) => inner,
};
Data {
inner,
Expand Down Expand Up @@ -369,10 +373,10 @@ fn normalize_data_to_redactions(
) -> Data {
let source = actual.source;
let filters = actual.filters;
let inner = match actual.inner {
DataInner::Error(err) => DataInner::Error(err),
DataInner::Binary(bin) => DataInner::Binary(bin),
DataInner::Text(text) => {
let inner = match (actual.inner, &expected.inner) {
(DataInner::Error(err), _) => DataInner::Error(err),
(DataInner::Binary(bin), _) => DataInner::Binary(bin),
(DataInner::Text(text), _) => {
if let Some(pattern) = expected.render() {
let lines = normalize_str_to_redactions(&text, &pattern, substitutions);
DataInner::Text(lines)
Expand All @@ -381,30 +385,32 @@ fn normalize_data_to_redactions(
}
}
#[cfg(feature = "json")]
DataInner::Json(value) => {
(DataInner::Json(value), DataInner::Json(exp)) => {
let mut value = value;
if let DataInner::Json(exp) = &expected.inner {
normalize_value_to_redactions(&mut value, exp, substitutions);
}
normalize_value_to_redactions(&mut value, exp, substitutions);
DataInner::Json(value)
}
#[cfg(feature = "json")]
DataInner::JsonLines(value) => {
(DataInner::JsonLines(value), DataInner::JsonLines(exp)) => {
let mut value = value;
if let DataInner::Json(exp) = &expected.inner {
normalize_value_to_redactions(&mut value, exp, substitutions);
}
normalize_value_to_redactions(&mut value, exp, substitutions);
DataInner::JsonLines(value)
}
#[cfg(feature = "term-svg")]
DataInner::TermSvg(text) => {
if let Some(pattern) = expected.render() {
let lines = normalize_str_to_redactions(&text, &pattern, substitutions);
DataInner::TermSvg(lines)
(DataInner::TermSvg(text), DataInner::TermSvg(exp)) => {
if let (Some((header, body, footer)), Some((_, exp, _))) = (
crate::data::split_term_svg(&text),
crate::data::split_term_svg(exp),
) {
let lines = normalize_str_to_redactions(body, exp, substitutions);
DataInner::TermSvg(format!("{header}{lines}{footer}"))
} else {
DataInner::TermSvg(text)
}
}
// reachable if more than one structured data format is enabled
#[allow(unreachable_patterns)]
(inner, _) => inner,
};
Data {
inner,
Expand Down

0 comments on commit 58f2474

Please sign in to comment.