Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions crates/libtest2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DynCase> = DistributedList::root();
}

pub use case::main;
Expand All @@ -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<Item = case::DynCase> {
_private::TESTS.iter().copied()
}
6 changes: 2 additions & 4 deletions crates/libtest2/src/macros.rs
Original file line number Diff line number Diff line change
@@ -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());
}
};
}
Expand All @@ -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)
}
Expand Down