From e46aa63b4160b1024ea2fba3b68081ee0c0e9a13 Mon Sep 17 00:00:00 2001 From: Alex Badics Date: Fri, 2 Sep 2022 14:16:39 +0200 Subject: [PATCH] test_utils: replace "colored-diff" with "similar" + manual implementation --- Cargo.toml | 3 ++- tests/test_utils.rs | 52 ++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 097b371..c0d0bb8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,9 +32,10 @@ tempfile = "3" ureq = "2.4" [dev-dependencies] -colored-diff = "= 0.2.2" # Version 0.2.3 broke the pretty display: https://github.com/CAD97/colored-diff/issues/8 +colored = "2.0" datatest-stable = "0.1.2" pretty_assertions = "1" +similar = { version = "2.2" , features = ["inline"] } [build-dependencies] phf_codegen = "0.11" diff --git a/tests/test_utils.rs b/tests/test_utils.rs index 57504b7..a1b015e 100644 --- a/tests/test_utils.rs +++ b/tests/test_utils.rs @@ -15,6 +15,7 @@ // along with Hun-law. If not, see . use std::{ + fmt::Display, fs::File, io::{self, Read}, path::Path, @@ -22,6 +23,7 @@ use std::{ use anyhow::{anyhow, Result}; use chrono::NaiveDate; +use colored::*; use hun_law::{ identifier::ActIdentifier, parser::{mk_act_section::ActRawText, structure::parse_act_structure}, @@ -29,6 +31,7 @@ use hun_law::{ util::{indentedline::IndentedLine, singleton_yaml}, }; use serde::Serialize; +use similar::{ChangeTag, TextDiff}; pub use tempfile::TempDir; pub fn read_all(path: impl AsRef) -> io::Result> { @@ -37,6 +40,49 @@ pub fn read_all(path: impl AsRef) -> io::Result> { Ok(result) } +struct PrettyDiff { + pub left: String, + pub right: String, +} + +impl Display for PrettyDiff { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let diff = TextDiff::from_lines(&self.left, &self.right); + + for (idx, group) in diff.grouped_ops(3).iter().enumerate() { + if idx > 0 { + println!("{:-^1$}", "-", 80); + } + for op in group { + for change in diff.iter_inline_changes(op) { + let tag = match change.tag() { + ChangeTag::Delete => "-".red(), + ChangeTag::Insert => "+".green(), + ChangeTag::Equal => " ".white(), + }; + tag.fmt(f)?; + + for (emphasized, value) in change.iter_strings_lossy() { + if emphasized { + write!( + f, + "{}", + value.color(tag.fgcolor().unwrap()).underline().bold() + )?; + } else { + write!(f, "{}", value.color(tag.fgcolor().unwrap()))?; + } + } + if change.missing_newline() { + writeln!(f)?; + } + } + } + } + Ok(()) + } +} + pub fn ensure_eq(expected: &T, actual: &U, message: &str) -> Result<()> where T: Serialize + ?Sized + PartialEq, @@ -48,9 +94,9 @@ where Err(anyhow!( "{}\n{}", message, - colored_diff::PrettyDifference { - expected: &singleton_yaml::to_string(expected).unwrap(), - actual: &singleton_yaml::to_string(actual).unwrap() + PrettyDiff { + left: singleton_yaml::to_string(expected).unwrap(), + right: singleton_yaml::to_string(actual).unwrap(), } )) } else {