Skip to content

Commit

Permalink
Merge pull request #313 from epage/normalize
Browse files Browse the repository at this point in the history
refactor(filter): Prepare for changing eliding
  • Loading branch information
epage authored May 16, 2024
2 parents 5c9fe2e + e6523a5 commit 487864b
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions crates/snapbox/src/filter/redactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ impl Redactions {
}

fn clear<'v>(&self, pattern: &'v str) -> Cow<'v, str> {
if pattern.contains('[') {
let mut pattern = Cow::Borrowed(pattern);
if !self.unused.is_empty() && pattern.contains('[') {
let mut pattern = pattern.to_owned();
for var in self.unused.iter() {
pattern = Cow::Owned(pattern.replace(var, ""));
pattern = pattern.replace(var, "");
}
pattern
Cow::Owned(pattern)
} else {
Cow::Borrowed(pattern)
}
Expand Down Expand Up @@ -315,6 +315,15 @@ mod test {
assert_eq!(expected, actual);
}

#[test]
fn elide_delimited_with_sub() {
let input = "Hello World\nHow are you?\nGoodbye World";
let pattern = "Hello [..]\n...\nGoodbye [..]";
let expected = "Hello [..]\nHow are you?\nGoodbye World";
let actual = normalize(input, pattern, &Redactions::new());
assert_eq!(expected, actual);
}

#[test]
fn leading_elide() {
let input = "Hello\nWorld\nGoodbye";
Expand Down Expand Up @@ -428,4 +437,24 @@ mod test {
assert_eq!(expected, actual, "placeholder={:?}", placeholder);
}
}

#[test]
fn substitute_literal() {
let input = "Hello world!";
let pattern = "Hello [OBJECT]!";
let mut sub = Redactions::new();
sub.insert("[OBJECT]", "world").unwrap();
let actual = normalize(input, pattern, &sub);
assert_eq!(actual, pattern);
}

#[test]
fn substitute_disabled() {
let input = "cargo";
let pattern = "cargo[EXE]";
let mut sub = Redactions::new();
sub.insert("[EXE]", "").unwrap();
let actual = normalize(input, pattern, &sub);
assert_eq!(actual, pattern);
}
}

0 comments on commit 487864b

Please sign in to comment.