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.
This commit is contained in:
@@ -464,6 +464,15 @@ pub fn core_macros() -> ~str {
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
macro_rules! assert_eq (
|
||||||
|
($given:expr , $expected:expr) =>
|
||||||
|
({let given_val = $given;
|
||||||
|
let expected_val = $expected;
|
||||||
|
// check both directions of equality....
|
||||||
|
if !((given_val == expected_val) && (expected_val == given_val)) {
|
||||||
|
fail!(fmt!(\"expected: %?, given: %?\",expected_val,given_val));
|
||||||
|
}}))
|
||||||
|
|
||||||
macro_rules! condition (
|
macro_rules! condition (
|
||||||
|
|
||||||
{ $c:ident: $in:ty -> $out:ty; } => {
|
{ $c:ident: $in:ty -> $out:ty; } => {
|
||||||
@@ -481,6 +490,7 @@ pub fn core_macros() -> ~str {
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
}";
|
}";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
8
src/test/run-fail/assert-eq-macro-fail.rs
Normal file
8
src/test/run-fail/assert-eq-macro-fail.rs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
// error-pattern:expected: 15, given: 14
|
||||||
|
|
||||||
|
#[deriving_eq]
|
||||||
|
struct Point { x : int }
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
assert_eq!(14,15);
|
||||||
|
}
|
||||||
10
src/test/run-pass/assert-eq-macro-success.rs
Normal file
10
src/test/run-pass/assert-eq-macro-success.rs
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#[deriving_eq]
|
||||||
|
struct Point { x : int }
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
assert_eq!(14,14);
|
||||||
|
assert_eq!(~"abc",~"abc");
|
||||||
|
assert_eq!(~Point{x:34},~Point{x:34});
|
||||||
|
assert_eq!(&Point{x:34},&Point{x:34});
|
||||||
|
assert_eq!(@Point{x:34},@Point{x:34});
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user