Skip to content

Commit eeca4a7

Browse files
authored
docs(mimic): Try to better specify behavior (#123)
2 parents 08fc2a0 + 0ef683e commit eeca4a7

1 file changed

Lines changed: 60 additions & 7 deletions

File tree

  • crates/libtest2-mimic/src

crates/libtest2-mimic/src/lib.rs

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
1-
//! An experimental replacement for libtest-mimic
1+
//! An experimental replacement for
2+
//! [libtest-mimic](https://docs.rs/libtest-mimic/latest/libtest_mimic/)
3+
//!
4+
//! Write your own tests that look and behave like built-in tests!
5+
//!
6+
//! This is a simple and small test harness that mimics the original `libtest`
7+
//! (used by `cargo test`/`rustc --test`). That means: all output looks pretty
8+
//! much like `cargo test` and most CLI arguments are understood and used. With
9+
//! that plumbing work out of the way, your test runner can focus on the actual
10+
//! testing.
11+
//!
12+
//! For a small real world example, see [`examples/mimic-tidy.rs`][1].
13+
//!
14+
//! [1]: https://github.com/assert-rs/libtest2/blob/main/crates/libtest2-mimic/examples/mimic-tidy.rs
215
//!
316
//! # Usage
417
//!
@@ -15,10 +28,37 @@
1528
//! And in `tests/mytest.rs` you would call [`Harness::main`] in the `main` function:
1629
//!
1730
//! ```no_run
18-
//! libtest2_mimic::Harness::with_env()
31+
//! # use libtest2_mimic::Trial;
32+
//! # use libtest2_mimic::Harness;
33+
//! # use libtest2_mimic::RunError;
34+
//! Harness::with_env()
35+
//! .discover([
36+
//! Trial::test("succeeding_test", move |_| Ok(())),
37+
//! Trial::test("failing_test", move |_| Err(RunError::fail("Woops"))),
38+
//! ])
1939
//! .main();
2040
//! ```
41+
//! Instead of returning `Ok` or `Err` directly, you want to actually perform
42+
//! your tests, of course. See [`Trial::test`] for more information on how to
43+
//! define a test. You can of course list all your tests manually. But in many
44+
//! cases it is useful to generate one test per file in a directory, for
45+
//! example.
46+
//!
47+
//! You can then run `cargo test --test mytest` to run it. To see the CLI
48+
//! arguments supported by this crate, run `cargo test --test mytest -- -h`.
2149
//!
50+
//! # Known limitations and differences to the official test harness
51+
//!
52+
//! `libtest2-mimic` aims to be fully compatible with stable, non-deprecated parts of `libtest`
53+
//! but there are differences for now.
54+
//!
55+
//! Some of the notable differences:
56+
//!
57+
//! - Output capture and `--no-capture`: simply not supported. The official
58+
//! `libtest` uses internal `std` functions to temporarily redirect output.
59+
//! `libtest-mimic` cannot use those, see also [libtest2#12](https://github.com/assert-rs/libtest2/issues/12)
60+
//! - `--format=json` (unstable): our schema is part of an experiment to see what should be
61+
//! stabilized for `libtest`, see also [libtest2#42](https://github.com/assert-rs/libtest2/issues/42)
2262
2363
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
2464
//#![warn(clippy::print_stderr)]
@@ -30,23 +70,27 @@ pub struct Harness {
3070
}
3171

3272
impl Harness {
73+
/// Read the process's CLI arguments
74+
pub fn with_env() -> Self {
75+
let raw = std::env::args_os();
76+
Self::with_args(raw)
77+
}
78+
79+
/// Manually specify CLI arguments
3380
pub fn with_args(args: impl IntoIterator<Item = impl Into<std::ffi::OsString>>) -> Self {
3481
Self {
3582
raw: args.into_iter().map(|a| a.into()).collect(),
3683
cases: Vec::new(),
3784
}
3885
}
3986

40-
pub fn with_env() -> Self {
41-
let raw = std::env::args_os();
42-
Self::with_args(raw)
43-
}
44-
87+
/// Enumerate all test [`Trial`]s
4588
pub fn discover(mut self, cases: impl IntoIterator<Item = Trial>) -> Self {
4689
self.cases.extend(cases);
4790
self
4891
}
4992

93+
/// Perform the tests and exit
5094
pub fn main(self) -> ! {
5195
match self.run() {
5296
Ok(true) => std::process::exit(0),
@@ -79,6 +123,7 @@ impl Harness {
79123
}
80124
}
81125

126+
/// A test case to be run
82127
pub struct Trial {
83128
name: String,
84129
#[allow(clippy::type_complexity)]
@@ -149,10 +194,18 @@ pub struct RunContext<'t> {
149194
}
150195

151196
impl<'t> RunContext<'t> {
197+
/// Request this test to be ignored
198+
///
199+
/// May be overridden by the CLI
200+
///
201+
/// **Note:** prefer [`RunContext::ignore_for`]
152202
pub fn ignore(&self) -> Result<(), RunError> {
153203
self.inner.ignore().map_err(|e| RunError { inner: e })
154204
}
155205

206+
/// Request this test to be ignored
207+
///
208+
/// May be overridden by the CLI
156209
pub fn ignore_for(&self, reason: impl std::fmt::Display) -> Result<(), RunError> {
157210
self.inner
158211
.ignore_for(reason)

0 commit comments

Comments
 (0)