Skip to content

Commit

Permalink
Pretty print unified diff with 5 line before/after window (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
david-crespo authored Mar 26, 2022
1 parent 1aac7fe commit e1666ca
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ keywords = ["test", "fixture"]
categories = ["development-tools::testing"]

[dependencies]
difference = "2.0.0"
similar = "2.1.0"
newline-converter = "0.2.0"
13 changes: 9 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
//! -Ten points for Gaston
//! ```
use difference::Changeset;
use newline_converter::dos2unix;
use similar::{udiff::unified_diff, Algorithm};
use std::{env, fs, path::Path};

/// Compare the contents of the file to the string provided
Expand Down Expand Up @@ -69,9 +69,14 @@ pub fn assert_contents<P: AsRef<Path>>(path: P, actual: &str) {
let expected = dos2unix(&expected_s);

if expected != actual {
let changeset =
Changeset::new(expected.as_ref(), actual.as_ref(), "\n");
println!("{}", changeset);
let diff = unified_diff(
Algorithm::Myers, // default algorithm used by git
&expected,
&actual,
5, // lines before and after
None,
);
println!("{}", diff);
panic!(
r#"assertion failed: string doesn't match the contents of file: "{}" see diffset above
set EXPECTORATE=overwrite if these changes are intentional"#,
Expand Down
20 changes: 20 additions & 0 deletions tests/data_a.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,23 @@ A eh?
a a A
a aye
eh.

A eh?
a a A
a aye
eh.

A eh?
a a A
a aye
eh.

A eh?
a a A
a aye
eh.

A eh?
a a A
a aye
eh.
24 changes: 24 additions & 0 deletions tests/data_a2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
A eh?
a a A
a aye
eh.

A eh?
a a A
a aye
eh.

A eh?
a a A
a aye
eh.

A eh?
this line changed
a aye
eh.

A eh?
a a A
a aye
eh.
7 changes: 7 additions & 0 deletions tests/test_basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@ fn bad() {
let actual = include_str!("data_a.txt");
assert_contents("tests/data_b.txt", actual);
}

#[test]
#[should_panic]
fn one_line_change() {
let actual = include_str!("data_a.txt");
assert_contents("tests/data_a2.txt", actual);
}

0 comments on commit e1666ca

Please sign in to comment.