Skip to content

Commit

Permalink
test_utils: replace "colored-diff" with "similar" + manual implementa…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
badicsalex committed Sep 2, 2022
1 parent dddd67f commit e46aa63
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Expand Up @@ -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"
Expand Down
52 changes: 49 additions & 3 deletions tests/test_utils.rs
Expand Up @@ -15,20 +15,23 @@
// along with Hun-law. If not, see <http://www.gnu.org/licenses/>.

use std::{
fmt::Display,
fs::File,
io::{self, Read},
path::Path,
};

use anyhow::{anyhow, Result};
use chrono::NaiveDate;
use colored::*;
use hun_law::{
identifier::ActIdentifier,
parser::{mk_act_section::ActRawText, structure::parse_act_structure},
structure::{Act, ActChild, ParagraphChildren, SAEBody},
util::{indentedline::IndentedLine, singleton_yaml},
};
use serde::Serialize;
use similar::{ChangeTag, TextDiff};
pub use tempfile::TempDir;

pub fn read_all(path: impl AsRef<Path>) -> io::Result<Vec<u8>> {
Expand All @@ -37,6 +40,49 @@ pub fn read_all(path: impl AsRef<Path>) -> io::Result<Vec<u8>> {
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<T, U>(expected: &T, actual: &U, message: &str) -> Result<()>
where
T: Serialize + ?Sized + PartialEq<U>,
Expand All @@ -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 {
Expand Down

0 comments on commit e46aa63

Please sign in to comment.