diff --git a/crates/libtest2/src/lib.rs b/crates/libtest2/src/lib.rs index 61f5d17..b95f651 100644 --- a/crates/libtest2/src/lib.rs +++ b/crates/libtest2/src/lib.rs @@ -46,6 +46,11 @@ pub mod _private { pub use crate::_main_parse as main_parse; pub use crate::_test_parse as test_parse; pub use crate::case::DynCase; + + /// Static variable used to collect functions annotated with `#[libtest2::test]` + /// + /// Values are pushed to this by the [`crate::macros::test_parse`](`test_parse`) macro + pub static TESTS: DistributedList = DistributedList::root(); } pub use case::main; @@ -59,3 +64,10 @@ pub use libtest2_proc_macro::test; #[doc = include_str!("../README.md")] #[cfg(doctest)] pub struct ReadmeDoctests; + +/// Get an iterator to all collected test functions +/// +/// Functions can be marked for collection by annotating them with the `#[libtest2::test]` macro +pub fn get_tests() -> impl Iterator { + _private::TESTS.iter().copied() +} diff --git a/crates/libtest2/src/macros.rs b/crates/libtest2/src/macros.rs index 6113b33..37776ff 100644 --- a/crates/libtest2/src/macros.rs +++ b/crates/libtest2/src/macros.rs @@ -1,13 +1,11 @@ #[macro_export] macro_rules! _main_parse { (#[main] fn main $($item:tt)*) => { - static TESTS: $crate::_private::DistributedList<$crate::_private::DynCase> = $crate::_private::DistributedList::root(); - fn main() { fn inner $($item)* inner(); - $crate::main(TESTS.iter().copied()); + $crate::main($crate::get_tests()); } }; } @@ -21,7 +19,7 @@ macro_rules! _test_parse { impl $crate::_private::Case for $name { fn name(&self) -> &str { - $crate::_private::push!(crate::TESTS, _: $crate::_private::DynCase = $crate::_private::DynCase(&$name)); + $crate::_private::push!($crate::_private::TESTS, _: $crate::_private::DynCase = $crate::_private::DynCase(&$name)); stringify!($name) }