From 7fd99460c2b4216315e9a1dbd9076192b9da470a Mon Sep 17 00:00:00 2001 From: Ian Chamberlain Date: Sun, 24 Sep 2023 17:27:13 -0400 Subject: [PATCH] Move macro to its own module This should make the diff a little nicer and wraps up all the doctests into their own module nicely. --- test-runner/src/lib.rs | 101 +------------------------------------- test-runner/src/macros.rs | 101 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 100 deletions(-) create mode 100644 test-runner/src/macros.rs diff --git a/test-runner/src/lib.rs b/test-runner/src/lib.rs index 23b30ce..a5a9abf 100644 --- a/test-runner/src/lib.rs +++ b/test-runner/src/lib.rs @@ -13,6 +13,7 @@ extern crate test; mod console; mod gdb; +mod macros; mod socket; use std::process::{ExitCode, Termination}; @@ -48,106 +49,6 @@ pub fn run_socket(tests: &[&TestDescAndFn]) { run::(tests); } -// Use a neat little trick with cfg(doctest) to make code fences appear in -// rustdoc output, but still compile normally when doctesting. This raises warnings -// for invalid code though, so we also silence that lint here. -#[cfg_attr(not(doctest), allow(rustdoc::invalid_rust_codeblocks))] -/// Helper macro for writing doctests using this runner. Wrap this macro around -/// your normal doctest to enable running it with the test runners in this crate. -/// -/// You may optionally specify a runner before the test body, and may use any of -/// the various [`fn main()`](https://doc.rust-lang.org/rustdoc/write-documentation/documentation-tests.html#using--in-doc-tests) -/// signatures allowed by documentation tests. -/// -/// # Examples -/// -/// ## Basic usage -/// -#[cfg_attr(not(doctest), doc = "````")] -/// ``` -/// test_runner::doctest! { -/// assert_eq!(2 + 2, 4); -/// } -/// ``` -#[cfg_attr(not(doctest), doc = "````")] -/// -/// ## Custom runner -/// -#[cfg_attr(not(doctest), doc = "````")] -/// ```no_run -/// test_runner::doctest! { SocketRunner, -/// assert_eq!(2 + 2, 4); -/// } -/// ``` -#[cfg_attr(not(doctest), doc = "````")] -/// -/// ## `should_panic` -/// -#[cfg_attr(not(doctest), doc = "````")] -/// ```should_panic -/// test_runner::doctest! { -/// assert_eq!(2 + 2, 5); -/// } -/// ``` -#[cfg_attr(not(doctest), doc = "````")] -/// -/// ## Custom `fn main` -/// -#[cfg_attr(not(doctest), doc = "````")] -/// ``` -/// test_runner::doctest! { -/// fn main() { -/// assert_eq!(2 + 2, 4); -/// } -/// } -/// ``` -#[cfg_attr(not(doctest), doc = "````")] -/// -#[cfg_attr(not(doctest), doc = "````")] -/// ``` -/// test_runner::doctest! { -/// fn main() -> Result<(), Box> { -/// assert_eq!(2 + 2, 4); -/// Ok(()) -/// } -/// } -/// ``` -#[cfg_attr(not(doctest), doc = "````")] -/// -/// ## Implicit return type -/// -/// Note that for the rustdoc preprocessor to understand the return type, the -/// `Ok(())` expression must be written _outside_ the `doctest!` invocation. -/// -#[cfg_attr(not(doctest), doc = "````")] -/// ``` -/// test_runner::doctest! { -/// assert_eq!(2 + 2, 4); -/// } -/// Ok::<(), std::io::Error>(()) -/// ``` -#[cfg_attr(not(doctest), doc = "````")] -#[macro_export] -macro_rules! doctest { - ($runner:ident, fn main() $(-> $ret:ty)? { $($body:tt)* } ) => { - fn main() $(-> $ret)? { - $crate::doctest!{ $runner, $($body)* } - } - }; - ($runner:ident, $($body:tt)*) => { - use $crate::TestRunner as _; - let mut _runner = $crate::$runner::default(); - _runner.setup(); - let _result = { $($body)* }; - _runner.cleanup(_result) - }; - ($($body:tt)*) => { - $crate::doctest!{ GdbRunner, - $($body)* - } - }; -} - fn run(tests: &[&TestDescAndFn]) { std::env::set_var("RUST_BACKTRACE", "1"); diff --git a/test-runner/src/macros.rs b/test-runner/src/macros.rs new file mode 100644 index 0000000..e198147 --- /dev/null +++ b/test-runner/src/macros.rs @@ -0,0 +1,101 @@ +//! Macros for working with test runners. + +// Use a neat little trick with cfg(doctest) to make code fences appear in +// rustdoc output, but still compile normally when doctesting. This raises warnings +// for invalid code though, so we also silence that lint here. +#[cfg_attr(not(doctest), allow(rustdoc::invalid_rust_codeblocks))] +/// Helper macro for writing doctests using this runner. Wrap this macro around +/// your normal doctest to enable running it with the test runners in this crate. +/// +/// You may optionally specify a runner before the test body, and may use any of +/// the various [`fn main()`](https://doc.rust-lang.org/rustdoc/write-documentation/documentation-tests.html#using--in-doc-tests) +/// signatures allowed by documentation tests. +/// +/// # Examples +/// +/// ## Basic usage +/// +#[cfg_attr(not(doctest), doc = "````")] +/// ``` +/// test_runner::doctest! { +/// assert_eq!(2 + 2, 4); +/// } +/// ``` +#[cfg_attr(not(doctest), doc = "````")] +/// +/// ## Custom runner +/// +#[cfg_attr(not(doctest), doc = "````")] +/// ```no_run +/// test_runner::doctest! { SocketRunner, +/// assert_eq!(2 + 2, 4); +/// } +/// ``` +#[cfg_attr(not(doctest), doc = "````")] +/// +/// ## `should_panic` +/// +#[cfg_attr(not(doctest), doc = "````")] +/// ```should_panic +/// test_runner::doctest! { +/// assert_eq!(2 + 2, 5); +/// } +/// ``` +#[cfg_attr(not(doctest), doc = "````")] +/// +/// ## Custom `fn main` +/// +#[cfg_attr(not(doctest), doc = "````")] +/// ``` +/// test_runner::doctest! { +/// fn main() { +/// assert_eq!(2 + 2, 4); +/// } +/// } +/// ``` +#[cfg_attr(not(doctest), doc = "````")] +/// +#[cfg_attr(not(doctest), doc = "````")] +/// ``` +/// test_runner::doctest! { +/// fn main() -> Result<(), Box> { +/// assert_eq!(2 + 2, 4); +/// Ok(()) +/// } +/// } +/// ``` +#[cfg_attr(not(doctest), doc = "````")] +/// +/// ## Implicit return type +/// +/// Note that for the rustdoc preprocessor to understand the return type, the +/// `Ok(())` expression must be written _outside_ the `doctest!` invocation. +/// +#[cfg_attr(not(doctest), doc = "````")] +/// ``` +/// test_runner::doctest! { +/// assert_eq!(2 + 2, 4); +/// } +/// Ok::<(), std::io::Error>(()) +/// ``` +#[cfg_attr(not(doctest), doc = "````")] +#[macro_export] +macro_rules! doctest { + ($runner:ident, fn main() $(-> $ret:ty)? { $($body:tt)* } ) => { + fn main() $(-> $ret)? { + $crate::doctest!{ $runner, $($body)* } + } + }; + ($runner:ident, $($body:tt)*) => { + use $crate::TestRunner as _; + let mut _runner = $crate::$runner::default(); + _runner.setup(); + let _result = { $($body)* }; + _runner.cleanup(_result) + }; + ($($body:tt)*) => { + $crate::doctest!{ GdbRunner, + $($body)* + } + }; +}