about summary refs log tree commit diff
path: root/src/test
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2014-12-08 12:12:23 +0000
committerbors <bors@rust-lang.org>2014-12-08 12:12:23 +0000
commitc7a9b49d1b5d4e520f25355f26a93dfac4ffa146 (patch)
tree7a2a559658447be5fe71de276b7e8296b2999770 /src/test
parentcf0b4e068227dd33fa15f3ffe24f29e0535d197f (diff)
parenta20926a51add66ab67053843e244efb1a4d7ad76 (diff)
downloadrust-c7a9b49d1b5d4e520f25355f26a93dfac4ffa146.tar.gz
rust-c7a9b49d1b5d4e520f25355f26a93dfac4ffa146.zip
auto merge of #19560 : sfackler/rust/should-fail-reason, r=alexcrichton
The test harness will make sure that the panic message contains the
specified string. This is useful to help make `#[should_fail]` tests a
bit less brittle by decreasing the chance that the test isn't
"accidentally" passing due to a panic occurring earlier than expected.
The behavior is in some ways similar to JUnit's `expected` feature:
`@Test(expected=NullPointerException.class)`.

Without the message assertion, this test would pass even though it's not
actually reaching the intended part of the code:
```rust
#[test]
#[should_fail(message = "out of bounds")]
fn test_oob_array_access() {
    let idx: uint = from_str("13o").unwrap(); // oops, this will panic
    [1i32, 2, 3][idx];
}
```
Diffstat (limited to 'src/test')
-rw-r--r--src/test/run-fail/test-should-fail-bad-message.rs22
-rw-r--r--src/test/run-pass/test-should-fail-good-message.rs26
2 files changed, 48 insertions, 0 deletions
diff --git a/src/test/run-fail/test-should-fail-bad-message.rs b/src/test/run-fail/test-should-fail-bad-message.rs
new file mode 100644
index 00000000000..76a5022e3be
--- /dev/null
+++ b/src/test/run-fail/test-should-fail-bad-message.rs
@@ -0,0 +1,22 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// check-stdout
+// error-pattern:task 'test_foo' panicked at
+// compile-flags: --test
+// ignore-pretty: does not work well with `--test`
+
+#[test]
+#[should_fail(expected = "foobar")]
+fn test_foo() {
+    panic!("blah")
+}
+
+
diff --git a/src/test/run-pass/test-should-fail-good-message.rs b/src/test/run-pass/test-should-fail-good-message.rs
new file mode 100644
index 00000000000..dcb2fe6dfc0
--- /dev/null
+++ b/src/test/run-pass/test-should-fail-good-message.rs
@@ -0,0 +1,26 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// compile-flags: --test
+// ignore-pretty: does not work well with `--test`
+
+#[test]
+#[should_fail(expected = "foo")]
+fn test_foo() {
+    panic!("foo bar")
+}
+
+#[test]
+#[should_fail(expected = "foo")]
+fn test_foo_dynamic() {
+    panic!("{} bar", "foo")
+}
+
+