about summary refs log tree commit diff
path: root/src/test/ui/custom_test_frameworks
diff options
context:
space:
mode:
authorJohn Renner <john@jrenner.net>2018-07-20 18:04:02 -0700
committerJohn Renner <john@jrenner.net>2018-09-04 22:33:00 -0700
commit9b27de41d4e00cb6c23df270572472fd4c6f47f8 (patch)
tree9f51c7baf3dd1fe2b1d021007b36aa70fb36689d /src/test/ui/custom_test_frameworks
parent0be2c303692cab31390e52701007cfa87867bf74 (diff)
downloadrust-9b27de41d4e00cb6c23df270572472fd4c6f47f8.tar.gz
rust-9b27de41d4e00cb6c23df270572472fd4c6f47f8.zip
Introduce Custom Test Frameworks
Diffstat (limited to 'src/test/ui/custom_test_frameworks')
-rw-r--r--src/test/ui/custom_test_frameworks/auxiliary/dynamic_runner.rs45
-rw-r--r--src/test/ui/custom_test_frameworks/auxiliary/example_runner.rs20
-rw-r--r--src/test/ui/custom_test_frameworks/dynamic.rs45
-rw-r--r--src/test/ui/custom_test_frameworks/full.rs38
-rw-r--r--src/test/ui/custom_test_frameworks/mismatch.rs19
-rw-r--r--src/test/ui/custom_test_frameworks/mismatch.stderr11
6 files changed, 178 insertions, 0 deletions
diff --git a/src/test/ui/custom_test_frameworks/auxiliary/dynamic_runner.rs b/src/test/ui/custom_test_frameworks/auxiliary/dynamic_runner.rs
new file mode 100644
index 00000000000..c204e69eafc
--- /dev/null
+++ b/src/test/ui/custom_test_frameworks/auxiliary/dynamic_runner.rs
@@ -0,0 +1,45 @@
+// Copyright 2018 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.
+
+use std::process::exit;
+
+pub trait Testable {
+    // Name of the test
+    fn name(&self) -> String;
+
+    // Tests pass by default
+    fn run(&self) -> bool {
+        true
+    }
+
+    // A test can generate subtests
+    fn subtests(&self) -> Vec<Box<dyn Testable>> {
+        vec![]
+    }
+}
+
+fn run_test(t: &dyn Testable) -> bool {
+    let success = t.subtests().into_iter().all(|sub_t| run_test(&*sub_t)) && t.run();
+    println!("{}...{}", t.name(), if success { "SUCCESS" } else { "FAIL" });
+    success
+}
+
+pub fn runner(tests: &[&dyn Testable]) {
+    let mut failed = false;
+    for t in tests {
+        if !run_test(*t) {
+            failed = true;
+        }
+    }
+
+    if failed {
+        exit(1);
+    }
+}
diff --git a/src/test/ui/custom_test_frameworks/auxiliary/example_runner.rs b/src/test/ui/custom_test_frameworks/auxiliary/example_runner.rs
new file mode 100644
index 00000000000..7b6b5e02955
--- /dev/null
+++ b/src/test/ui/custom_test_frameworks/auxiliary/example_runner.rs
@@ -0,0 +1,20 @@
+// Copyright 2018 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.
+
+pub trait Testable {
+    fn name(&self) -> String;
+    fn run(&self) -> Option<String>; // None will be success, Some is the error message
+}
+
+pub fn runner(tests: &[&dyn Testable]) {
+    for t in tests {
+        print!("{}........{}", t.name(), t.run().unwrap_or_else(|| "SUCCESS".to_string()));
+    }
+}
diff --git a/src/test/ui/custom_test_frameworks/dynamic.rs b/src/test/ui/custom_test_frameworks/dynamic.rs
new file mode 100644
index 00000000000..f82571b948a
--- /dev/null
+++ b/src/test/ui/custom_test_frameworks/dynamic.rs
@@ -0,0 +1,45 @@
+// Copyright 2018 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.
+
+// run-pass
+// aux-build:dynamic_runner.rs
+// compile-flags:--test
+#![feature(custom_test_frameworks)]
+#![test_runner(dynamic_runner::runner)]
+
+extern crate dynamic_runner;
+
+pub struct AllFoo(&'static str);
+struct IsFoo(String);
+
+impl dynamic_runner::Testable for AllFoo {
+    fn name(&self) -> String {
+        String::from(self.0)
+    }
+
+    fn subtests(&self) -> Vec<Box<dyn dynamic_runner::Testable>> {
+        self.0.split(" ").map(|word|
+            Box::new(IsFoo(word.into())) as Box<dyn dynamic_runner::Testable>
+        ).collect()
+    }
+}
+
+impl dynamic_runner::Testable for IsFoo {
+    fn name(&self) -> String {
+        self.0.clone()
+    }
+
+    fn run(&self) -> bool {
+        self.0 == "foo"
+    }
+}
+
+#[test_case]
+const TEST_2: AllFoo = AllFoo("foo foo");
diff --git a/src/test/ui/custom_test_frameworks/full.rs b/src/test/ui/custom_test_frameworks/full.rs
new file mode 100644
index 00000000000..9fcf76ec33e
--- /dev/null
+++ b/src/test/ui/custom_test_frameworks/full.rs
@@ -0,0 +1,38 @@
+// Copyright 2018 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.
+
+// run-pass
+// aux-build:example_runner.rs
+// compile-flags:--test
+
+#![feature(custom_test_frameworks)]
+#![test_runner(example_runner::runner)]
+extern crate example_runner;
+
+pub struct IsFoo(&'static str);
+
+impl example_runner::Testable for IsFoo {
+    fn name(&self) -> String {
+        self.0.to_string()
+    }
+
+    fn run(&self) -> Option<String> {
+        if self.0 != "foo" {
+            return Some(format!("{} != foo", self.0));
+        }
+        None
+    }
+}
+
+#[test_case]
+const TEST_1: IsFoo = IsFoo("hello");
+
+#[test_case]
+const TEST_2: IsFoo = IsFoo("foo");
diff --git a/src/test/ui/custom_test_frameworks/mismatch.rs b/src/test/ui/custom_test_frameworks/mismatch.rs
new file mode 100644
index 00000000000..28753f1649a
--- /dev/null
+++ b/src/test/ui/custom_test_frameworks/mismatch.rs
@@ -0,0 +1,19 @@
+// Copyright 2018 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.
+
+// aux-build:example_runner.rs
+// compile-flags:--test
+#![feature(custom_test_frameworks)]
+#![test_runner(example_runner::runner)]
+
+extern crate example_runner;
+
+#[test]
+fn wrong_kind(){}
diff --git a/src/test/ui/custom_test_frameworks/mismatch.stderr b/src/test/ui/custom_test_frameworks/mismatch.stderr
new file mode 100644
index 00000000000..8e2afaedfff
--- /dev/null
+++ b/src/test/ui/custom_test_frameworks/mismatch.stderr
@@ -0,0 +1,11 @@
+error[E0277]: the trait bound `test::TestDescAndFn: example_runner::Testable` is not satisfied
+  --> $DIR/mismatch.rs:19:1
+   |
+LL | fn wrong_kind(){}
+   | ^^^^^^^^^^^^^^^^^ the trait `example_runner::Testable` is not implemented for `test::TestDescAndFn`
+   |
+   = note: required for the cast to the object type `dyn example_runner::Testable`
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0277`.