From 974313eb87b5086ddb07ab14c50192fcb9dcd5e9 Mon Sep 17 00:00:00 2001 From: Jesse Bakker Date: Wed, 6 Jan 2021 18:13:29 +0100 Subject: Replace last usages of difference with dissimilar --- crates/proc_macro_srv/Cargo.toml | 1 - crates/test_utils/Cargo.toml | 2 +- crates/test_utils/src/lib.rs | 19 ++++++++++++++++--- 3 files changed, 17 insertions(+), 5 deletions(-) (limited to 'crates') diff --git a/crates/proc_macro_srv/Cargo.toml b/crates/proc_macro_srv/Cargo.toml index df9a55c10..f78c17194 100644 --- a/crates/proc_macro_srv/Cargo.toml +++ b/crates/proc_macro_srv/Cargo.toml @@ -21,7 +21,6 @@ test_utils = { path = "../test_utils", version = "0.0.0" } [dev-dependencies] cargo_metadata = "=0.12.0" -difference = "2.0.0" # used as proc macro test targets serde_derive = "1.0.106" diff --git a/crates/test_utils/Cargo.toml b/crates/test_utils/Cargo.toml index 93eecc678..06341f003 100644 --- a/crates/test_utils/Cargo.toml +++ b/crates/test_utils/Cargo.toml @@ -11,7 +11,7 @@ doctest = false [dependencies] # Avoid adding deps here, this crate is widely used in tests it should compile fast! -difference = "2.0.0" +dissimilar = "1.0.2" text-size = "1.0.0" serde_json = "1.0.48" rustc-hash = "1.1.0" diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index 05940a546..656dd2072 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -20,7 +20,7 @@ use serde_json::Value; use stdx::lines_with_ends; use text_size::{TextRange, TextSize}; -pub use difference::Changeset as __Changeset; +pub use dissimilar::diff as __diff; pub use rustc_hash::FxHashMap; pub use crate::fixture::Fixture; @@ -45,8 +45,8 @@ macro_rules! assert_eq_text { if left.trim() == right.trim() { std::eprintln!("Left:\n{:?}\n\nRight:\n{:?}\n\nWhitespace difference\n", left, right); } else { - let changeset = $crate::__Changeset::new(left, right, "\n"); - std::eprintln!("Left:\n{}\n\nRight:\n{}\n\nDiff:\n{}\n", left, right, changeset); + let diff = $crate::__diff(left, right); + std::eprintln!("Left:\n{}\n\nRight:\n{}\n\nDiff:\n{}\n", left, right, $crate::format_diff(diff)); } std::eprintln!($($tt)*); panic!("text differs"); @@ -392,3 +392,16 @@ pub fn project_dir() -> PathBuf { let dir = env!("CARGO_MANIFEST_DIR"); PathBuf::from(dir).parent().unwrap().parent().unwrap().to_owned() } + +pub fn format_diff(chunks: Vec) -> String { + let mut buf = String::new(); + for chunk in chunks { + let formatted = match chunk { + dissimilar::Chunk::Equal(text) => text.into(), + dissimilar::Chunk::Delete(text) => format!("\x1b[41m{}\x1b[0m", text), + dissimilar::Chunk::Insert(text) => format!("\x1b[42m{}\x1b[0m", text), + }; + buf.push_str(&formatted); + } + buf +} -- cgit v1.2.3