From bfe4f0c012d365facbb957a1e0cc379d22aac519 Mon Sep 17 00:00:00 2001 From: David Crespo Date: Fri, 18 Mar 2022 16:23:01 -0500 Subject: [PATCH 1/2] pretty print unified diff with 5 line before/after window --- Cargo.toml | 2 +- src/lib.rs | 8 ++++---- tests/data_a.txt | 20 ++++++++++++++++++++ tests/data_a2.txt | 24 ++++++++++++++++++++++++ tests/test_basic.rs | 7 +++++++ 5 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 tests/data_a2.txt diff --git a/Cargo.toml b/Cargo.toml index 925d0f5..e6e9df6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/lib.rs b/src/lib.rs index 44988ba..c36c839 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 @@ -69,9 +69,9 @@ pub fn assert_contents>(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, &expected, &actual, 5, 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"#, diff --git a/tests/data_a.txt b/tests/data_a.txt index fee1414..8da136d 100644 --- a/tests/data_a.txt +++ b/tests/data_a.txt @@ -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. \ No newline at end of file diff --git a/tests/data_a2.txt b/tests/data_a2.txt new file mode 100644 index 0000000..a89c772 --- /dev/null +++ b/tests/data_a2.txt @@ -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. \ No newline at end of file diff --git a/tests/test_basic.rs b/tests/test_basic.rs index 62b1c93..23b6c56 100644 --- a/tests/test_basic.rs +++ b/tests/test_basic.rs @@ -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); +} From d62d3eef0139202dd0de3daedcd494059e094df8 Mon Sep 17 00:00:00 2001 From: David Crespo Date: Mon, 21 Mar 2022 14:38:59 -0500 Subject: [PATCH 2/2] turn github commment into code comment --- src/lib.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c36c839..51449e9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -69,8 +69,13 @@ pub fn assert_contents>(path: P, actual: &str) { let expected = dos2unix(&expected_s); if expected != actual { - let diff = - unified_diff(Algorithm::Myers, &expected, &actual, 5, None); + 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