]> git.lizzy.rs Git - rust.git/commitdiff
add assert_eq! macro
authorJohn Clements <clements@racket-lang.org>
Wed, 13 Mar 2013 19:10:32 +0000 (12:10 -0700)
committerJohn Clements <clements@racket-lang.org>
Wed, 20 Mar 2013 20:27:45 +0000 (13:27 -0700)
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.

src/libsyntax/ext/expand.rs
src/test/run-fail/assert-eq-macro-fail.rs [new file with mode: 0644]
src/test/run-pass/assert-eq-macro-success.rs [new file with mode: 0644]

index ad05e2f21e64ba9a28f1eff46dd3a8ecc3c050ee..030cdab7951c14aa908ef34ed709416cd5ce8abd 100644 (file)
@@ -464,6 +464,15 @@ macro_rules! fail_unless(
         }
     )
 
+    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 (
 
         { $c:ident: $in:ty -> $out:ty; } => {
@@ -481,6 +490,7 @@ fn key(_x: @::core::condition::Handler<$in,$out>) { }
         }
     )
 
+
 }";
 }
 
diff --git a/src/test/run-fail/assert-eq-macro-fail.rs b/src/test/run-fail/assert-eq-macro-fail.rs
new file mode 100644 (file)
index 0000000..a5f4389
--- /dev/null
@@ -0,0 +1,8 @@
+// error-pattern:expected: 15, given: 14
+
+#[deriving_eq]
+struct Point { x : int }
+
+fn main() {
+    assert_eq!(14,15);
+}
diff --git a/src/test/run-pass/assert-eq-macro-success.rs b/src/test/run-pass/assert-eq-macro-success.rs
new file mode 100644 (file)
index 0000000..c929b5c
--- /dev/null
@@ -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});
+}