From db0d9c7d6a72b4f86aad4d9203f2e7e7f45e3347 Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Fri, 2 Aug 2024 12:16:11 -0700 Subject: [PATCH] cranelift-frontend: Make `assert_eq_output!` usable in the whole crate (#9070) This moves the macro to `lib.rs` so that it can be used in all the tests in the whole crate. It also makes it a little more usable by trimming whitespace and printing both the expected and actual values, in addition to the diff. A follow-up commit will migrate various uses of `assert_eq!` over to it. Co-authored-by: Trevor Elliot --- cranelift/frontend/src/lib.rs | 28 ++++++++++++++++++++++++++++ cranelift/frontend/src/switch.rs | 13 ------------- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/cranelift/frontend/src/lib.rs b/cranelift/frontend/src/lib.rs index 53741869db..8784f36707 100644 --- a/cranelift/frontend/src/lib.rs +++ b/cranelift/frontend/src/lib.rs @@ -175,6 +175,34 @@ pub use crate::frontend::{FuncInstBuilder, FunctionBuilder, FunctionBuilderConte pub use crate::switch::Switch; pub use crate::variable::Variable; +#[cfg(test)] +macro_rules! assert_eq_output { + ( $left:expr, $right:expr $(,)? ) => {{ + let left = $left; + let left = left.trim(); + + let right = $right; + let right = right.trim(); + + assert_eq!( + left, + right, + "assertion failed, output not equal:\n\ + \n\ + =========== Diff ===========\n\ + {}\n\ + =========== Left ===========\n\ + {left}\n\ + =========== Right ===========\n\ + {right}\n\ + ", + similar::TextDiff::from_lines(left, right) + .unified_diff() + .header("left", "right") + ) + }}; +} + mod frontend; mod ssa; mod switch; diff --git a/cranelift/frontend/src/switch.rs b/cranelift/frontend/src/switch.rs index 92501b91f4..cc1c659012 100644 --- a/cranelift/frontend/src/switch.rs +++ b/cranelift/frontend/src/switch.rs @@ -371,19 +371,6 @@ mod tests { }}; } - macro_rules! assert_eq_output { - ($actual:ident, $expected:literal) => { - assert_eq!( - $actual, - $expected, - "\n{}", - similar::TextDiff::from_lines($expected, &$actual) - .unified_diff() - .header("expected", "actual") - ) - }; - } - #[test] fn switch_empty() { let func = setup!(42, []);