1 //! Common types used by `libtest`.
6 use super::bench::Bencher;
9 pub use NamePadding::*;
13 /// Type of the test according to the [rust book](https://doc.rust-lang.org/cargo/guide/tests.html)
15 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
17 /// Unit-tests are expected to be in the `src` folder of the crate.
19 /// Integration-style tests are expected to be in the `tests` folder of the crate.
21 /// Doctests are created by the `librustdoc` manually, so it's a different type of test.
23 /// Tests for the sources that don't follow the project layout convention
24 /// (e.g. tests in raw `main.rs` compiled by calling `rustc --test` directly).
28 #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
29 pub enum NamePadding {
34 // The name of a test. By convention this follows the rules for rust
35 // paths; i.e., it should be a series of identifiers separated by double
36 // colons. This way if some test runner wants to arrange the tests
37 // hierarchically it may.
38 #[derive(Clone, PartialEq, Eq, Hash, Debug)]
40 StaticTestName(&'static str),
42 AlignedTestName(Cow<'static, str>, NamePadding),
46 pub fn as_slice(&self) -> &str {
48 StaticTestName(s) => s,
49 DynTestName(ref s) => s,
50 AlignedTestName(ref s, _) => &*s,
54 pub fn padding(&self) -> NamePadding {
56 &AlignedTestName(_, p) => p,
61 pub fn with_padding(&self, padding: NamePadding) -> TestName {
62 let name = match *self {
63 TestName::StaticTestName(name) => Cow::Borrowed(name),
64 TestName::DynTestName(ref name) => Cow::Owned(name.clone()),
65 TestName::AlignedTestName(ref name, _) => name.clone(),
68 TestName::AlignedTestName(name, padding)
71 impl fmt::Display for TestName {
72 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73 fmt::Display::fmt(self.as_slice(), f)
77 /// Represents a benchmark function.
78 pub trait TDynBenchFn: Send {
79 fn run(&self, harness: &mut Bencher);
82 // A function that runs a test. If the function returns successfully,
83 // the test succeeds; if the function panics then the test fails. We
84 // may need to come up with a more clever definition of test in order
85 // to support isolation of tests into threads.
88 StaticBenchFn(fn(&mut Bencher)),
89 DynTestFn(Box<dyn FnOnce() + Send>),
90 DynBenchFn(Box<dyn TDynBenchFn + 'static>),
94 pub fn padding(&self) -> NamePadding {
96 StaticTestFn(..) => PadNone,
97 StaticBenchFn(..) => PadOnRight,
98 DynTestFn(..) => PadNone,
99 DynBenchFn(..) => PadOnRight,
104 impl fmt::Debug for TestFn {
105 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
106 f.write_str(match *self {
107 StaticTestFn(..) => "StaticTestFn(..)",
108 StaticBenchFn(..) => "StaticBenchFn(..)",
109 DynTestFn(..) => "DynTestFn(..)",
110 DynBenchFn(..) => "DynBenchFn(..)",
115 // The definition of a single test. A test runner will run a list of
117 #[derive(Clone, Debug, PartialEq, Eq, Hash)]
118 pub struct TestDesc {
121 pub should_panic: options::ShouldPanic,
122 pub allow_fail: bool,
123 pub test_type: TestType,
127 pub fn padded_name(&self, column_count: usize, align: NamePadding) -> String {
128 let mut name = String::from(self.name.as_slice());
129 let fill = column_count.saturating_sub(name.len());
130 let pad = " ".repeat(fill);
142 pub struct TestDescAndFn {