Skip to content

Commit db0d9c7

Browse files
fitzgenelliottt
andauthored
cranelift-frontend: Make assert_eq_output! usable in the whole crate (bytecodealliance#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 <[email protected]>
1 parent cb0cac2 commit db0d9c7

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

cranelift/frontend/src/lib.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,34 @@ pub use crate::frontend::{FuncInstBuilder, FunctionBuilder, FunctionBuilderConte
175175
pub use crate::switch::Switch;
176176
pub use crate::variable::Variable;
177177

178+
#[cfg(test)]
179+
macro_rules! assert_eq_output {
180+
( $left:expr, $right:expr $(,)? ) => {{
181+
let left = $left;
182+
let left = left.trim();
183+
184+
let right = $right;
185+
let right = right.trim();
186+
187+
assert_eq!(
188+
left,
189+
right,
190+
"assertion failed, output not equal:\n\
191+
\n\
192+
=========== Diff ===========\n\
193+
{}\n\
194+
=========== Left ===========\n\
195+
{left}\n\
196+
=========== Right ===========\n\
197+
{right}\n\
198+
",
199+
similar::TextDiff::from_lines(left, right)
200+
.unified_diff()
201+
.header("left", "right")
202+
)
203+
}};
204+
}
205+
178206
mod frontend;
179207
mod ssa;
180208
mod switch;

cranelift/frontend/src/switch.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -371,19 +371,6 @@ mod tests {
371371
}};
372372
}
373373

374-
macro_rules! assert_eq_output {
375-
($actual:ident, $expected:literal) => {
376-
assert_eq!(
377-
$actual,
378-
$expected,
379-
"\n{}",
380-
similar::TextDiff::from_lines($expected, &$actual)
381-
.unified_diff()
382-
.header("expected", "actual")
383-
)
384-
};
385-
}
386-
387374
#[test]
388375
fn switch_empty() {
389376
let func = setup!(42, []);

0 commit comments

Comments
 (0)