Skip to content

Commit ab8e46b

Browse files
committed
add assert_eq! macro
the assert_eq! macro compares its arguments and fails if they're not equal. It's more informative than fail_unless!, because it explicitly writes the given and expected arguments on failure.
1 parent 0847d52 commit ab8e46b

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

src/libsyntax/ext/expand.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,15 @@ pub fn core_macros() -> ~str {
464464
}
465465
)
466466

467+
macro_rules! assert_eq (
468+
($given:expr , $expected:expr) =>
469+
({let given_val = $given;
470+
let expected_val = $expected;
471+
// check both directions of equality....
472+
if !((given_val == expected_val) && (expected_val == given_val)) {
473+
fail!(fmt!(\"expected: %?, given: %?\",expected_val,given_val));
474+
}}))
475+
467476
macro_rules! condition (
468477

469478
{ $c:ident: $in:ty -> $out:ty; } => {
@@ -481,6 +490,7 @@ pub fn core_macros() -> ~str {
481490
}
482491
)
483492

493+
484494
}";
485495
}
486496
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// error-pattern:expected: 15, given: 14
2+
3+
#[deriving_eq]
4+
struct Point { x : int }
5+
6+
fn main() {
7+
assert_eq!(14,15);
8+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#[deriving_eq]
2+
struct Point { x : int }
3+
4+
fn main() {
5+
assert_eq!(14,14);
6+
assert_eq!(~"abc",~"abc");
7+
assert_eq!(~Point{x:34},~Point{x:34});
8+
assert_eq!(&Point{x:34},&Point{x:34});
9+
assert_eq!(@Point{x:34},@Point{x:34});
10+
}

0 commit comments

Comments
 (0)