From 64d98f8ee037282c35007b64c2649055c56af1db Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:19:03 +0200 Subject: Merging upstream version 1.68.2+dfsg1. Signed-off-by: Daniel Baumann --- .../auxiliary/dynamic_runner.rs | 35 ++++++++++++++++++++++ .../auxiliary/example_runner.rs | 10 +++++++ tests/ui/custom_test_frameworks/dynamic.rs | 35 ++++++++++++++++++++++ tests/ui/custom_test_frameworks/full.rs | 28 +++++++++++++++++ tests/ui/custom_test_frameworks/mismatch.rs | 10 +++++++ tests/ui/custom_test_frameworks/mismatch.stderr | 14 +++++++++ 6 files changed, 132 insertions(+) create mode 100644 tests/ui/custom_test_frameworks/auxiliary/dynamic_runner.rs create mode 100644 tests/ui/custom_test_frameworks/auxiliary/example_runner.rs create mode 100644 tests/ui/custom_test_frameworks/dynamic.rs create mode 100644 tests/ui/custom_test_frameworks/full.rs create mode 100644 tests/ui/custom_test_frameworks/mismatch.rs create mode 100644 tests/ui/custom_test_frameworks/mismatch.stderr (limited to 'tests/ui/custom_test_frameworks') diff --git a/tests/ui/custom_test_frameworks/auxiliary/dynamic_runner.rs b/tests/ui/custom_test_frameworks/auxiliary/dynamic_runner.rs new file mode 100644 index 000000000..a56e0b1f5 --- /dev/null +++ b/tests/ui/custom_test_frameworks/auxiliary/dynamic_runner.rs @@ -0,0 +1,35 @@ +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> { + 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/tests/ui/custom_test_frameworks/auxiliary/example_runner.rs b/tests/ui/custom_test_frameworks/auxiliary/example_runner.rs new file mode 100644 index 000000000..dd68c0685 --- /dev/null +++ b/tests/ui/custom_test_frameworks/auxiliary/example_runner.rs @@ -0,0 +1,10 @@ +pub trait Testable { + fn name(&self) -> String; + fn run(&self) -> Option; // 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/tests/ui/custom_test_frameworks/dynamic.rs b/tests/ui/custom_test_frameworks/dynamic.rs new file mode 100644 index 000000000..6766ec542 --- /dev/null +++ b/tests/ui/custom_test_frameworks/dynamic.rs @@ -0,0 +1,35 @@ +// 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> { + self.0.split(" ").map(|word| + Box::new(IsFoo(word.into())) as Box + ).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/tests/ui/custom_test_frameworks/full.rs b/tests/ui/custom_test_frameworks/full.rs new file mode 100644 index 000000000..8c8188268 --- /dev/null +++ b/tests/ui/custom_test_frameworks/full.rs @@ -0,0 +1,28 @@ +// 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 { + 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/tests/ui/custom_test_frameworks/mismatch.rs b/tests/ui/custom_test_frameworks/mismatch.rs new file mode 100644 index 000000000..ac850552b --- /dev/null +++ b/tests/ui/custom_test_frameworks/mismatch.rs @@ -0,0 +1,10 @@ +// 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(){} +//~^ ERROR trait bound `TestDescAndFn: Testable` is not satisfied diff --git a/tests/ui/custom_test_frameworks/mismatch.stderr b/tests/ui/custom_test_frameworks/mismatch.stderr new file mode 100644 index 000000000..61061ae52 --- /dev/null +++ b/tests/ui/custom_test_frameworks/mismatch.stderr @@ -0,0 +1,14 @@ +error[E0277]: the trait bound `TestDescAndFn: Testable` is not satisfied + --> $DIR/mismatch.rs:9:1 + | +LL | #[test] + | ------- in this procedural macro expansion +LL | fn wrong_kind(){} + | ^^^^^^^^^^^^^^^^^ the trait `Testable` is not implemented for `TestDescAndFn` + | + = note: required for the cast from `TestDescAndFn` to the object type `dyn Testable` + = note: this error originates in the attribute macro `test` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. -- cgit v1.2.3