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 // A function that runs a test. If the function returns successfully,
78 // the test succeeds; if the function panics then the test fails. We
79 // may need to come up with a more clever definition of test in order
80 // to support isolation of tests into threads.
83 StaticBenchFn(fn(&mut Bencher)),
84 DynTestFn(Box<dyn FnOnce() + Send>),
85 DynBenchFn(Box<dyn Fn(&mut Bencher) + Send>),
89 pub fn padding(&self) -> NamePadding {
91 StaticTestFn(..) => PadNone,
92 StaticBenchFn(..) => PadOnRight,
93 DynTestFn(..) => PadNone,
94 DynBenchFn(..) => PadOnRight,
99 impl fmt::Debug for TestFn {
100 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
101 f.write_str(match *self {
102 StaticTestFn(..) => "StaticTestFn(..)",
103 StaticBenchFn(..) => "StaticBenchFn(..)",
104 DynTestFn(..) => "DynTestFn(..)",
105 DynBenchFn(..) => "DynBenchFn(..)",
110 // A unique integer associated with each test.
111 #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
112 pub struct TestId(pub usize);
114 // The definition of a single test. A test runner will run a list of
116 #[derive(Clone, Debug)]
117 pub struct TestDesc {
120 pub ignore_message: Option<&'static str>,
121 pub should_panic: options::ShouldPanic,
122 pub compile_fail: bool,
124 pub test_type: TestType,
128 pub fn padded_name(&self, column_count: usize, align: NamePadding) -> String {
129 let mut name = String::from(self.name.as_slice());
130 let fill = column_count.saturating_sub(name.len());
131 let pad = " ".repeat(fill);
141 /// Returns None for ignored test or that that are just run, otherwise give a description of the type of test.
142 /// Descriptions include "should panic", "compile fail" and "compile".
143 pub fn test_mode(&self) -> Option<&'static str> {
147 match self.should_panic {
148 options::ShouldPanic::Yes | options::ShouldPanic::YesWithMessage(_) => {
149 return Some("should panic");
151 options::ShouldPanic::No => {}
153 if self.compile_fail {
154 return Some("compile fail");
157 return Some("compile");
164 pub struct TestDescAndFn {