jotdown/tests/html-ut/cmp.rs
Noah Hellman 3cea79a122 mv suite{_bench} to test-html-{ut,ref} crates
- allow compiling/running html tests without compiling main crate tests
  (useful when e.g. making type changes to events but html unaffected)
- avoid need for future flags in main crate
2023-05-04 19:34:23 +02:00

47 lines
1.8 KiB
Rust

#[macro_export]
macro_rules! compare {
($src:expr, $expected:expr) => {
use jotdown::Render;
let src = $src;
let expected = $expected;
let p = jotdown::Parser::new(src);
let mut actual = String::new();
jotdown::html::Renderer::default()
.push(p, &mut actual)
.unwrap();
assert_eq!(
actual.trim(),
expected.trim(),
concat!(
"\n",
"\x1b[0;1m========================= INPUT ============================\x1b[0m\n",
"\x1b[2m{}",
"\x1b[0;1m=================== ACTUAL vs EXPECTED =====================\x1b[0m\n",
"{}",
"\x1b[0;1m============================================================\x1b[0m\n",
),
$src,
{
let a = actual.trim().split('\n');
let b = expected.trim().split('\n');
let max = a.clone().count().max(b.clone().count());
let a_width = a.clone().map(|a| a.len()).max().unwrap_or(0);
a.chain(std::iter::repeat(""))
.zip(b.chain(std::iter::repeat("")))
.take(max)
.map(|(a, b)| {
format!(
"\x1b[{}m{:a_width$}\x1b[0m {}= \x1b[{}m{}\x1b[0m\n",
if a == b { "2" } else { "31" },
a,
if a == b { '=' } else { '!' },
if a == b { "2" } else { "32" },
b,
a_width = a_width,
)
})
.collect::<String>()
},
);
};
}