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