Skip to content

Commit 59b83c5

Browse files
committed
feat(test2): Provide a #[test] macro
1 parent 4f5e41b commit 59b83c5

File tree

11 files changed

+172
-78
lines changed

11 files changed

+172
-78
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/libtest2-proc-macro/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
88
## [Unreleased] - ReleaseDate
99

1010
<!-- next-url -->
11-
[Unreleased]: https://github.com/epage/pytest-rs/compare/9d9263a628ea7d17e39cf5bfa22ee190fb6e3cc7...HEAD
11+
[Unreleased]: https://github.com/epage/pytest-rs/compare/106268b63bedd252fc285778e3bd99b6e16c8608...HEAD

crates/libtest2/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ color = ["libtest2-harness/color"]
2929
threads = ["libtest2-harness/threads"]
3030

3131
[dependencies]
32+
distributed-list = { version = "0.0.2", path = "../distributed_list" }
3233
libtest2-harness = { version = "0.0.3", path = "../libtest2-harness" }
34+
libtest2-proc-macro = { version = "0.0.3", path = "../libtest2-proc-macro" }
3335

3436
[dev-dependencies]
3537
dunce = "1.0.4"

crates/libtest2/examples/simple.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,31 @@ use libtest2::RunError;
22
use libtest2::RunResult;
33
use libtest2::TestContext;
44

5-
libtest2::main!(
6-
check_toph,
7-
check_katara,
8-
check_sokka,
9-
long_computation,
10-
compile_fail_dummy
11-
);
5+
#[libtest2::main]
6+
fn main() {}
127

138
// Tests
149

10+
#[libtest2::test]
1511
fn check_toph(_context: &TestContext) -> RunResult {
1612
Ok(())
1713
}
14+
#[libtest2::test]
1815
fn check_katara(_context: &TestContext) -> RunResult {
1916
Ok(())
2017
}
18+
#[libtest2::test]
2119
fn check_sokka(_context: &TestContext) -> RunResult {
2220
Err(RunError::fail("Sokka tripped and fell :("))
2321
}
22+
#[libtest2::test]
2423
fn long_computation(context: &TestContext) -> RunResult {
2524
context.ignore_for("slow")?;
2625

2726
std::thread::sleep(std::time::Duration::from_secs(1));
2827
Ok(())
2928
}
29+
#[libtest2::test]
3030
fn compile_fail_dummy(_context: &TestContext) -> RunResult {
3131
Ok(())
3232
}

crates/libtest2/src/case.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,28 @@ use libtest2_harness::TestKind;
55
use crate::RunResult;
66
use crate::TestContext;
77

8+
#[derive(Copy, Clone)]
9+
pub struct DynCase(pub &'static dyn Case);
10+
11+
impl Case for DynCase {
12+
fn name(&self) -> &str {
13+
self.0.name()
14+
}
15+
fn kind(&self) -> TestKind {
16+
self.0.kind()
17+
}
18+
fn source(&self) -> Option<&Source> {
19+
self.0.source()
20+
}
21+
fn exclusive(&self, context: &TestContext) -> bool {
22+
self.0.exclusive(context)
23+
}
24+
25+
fn run(&self, context: &TestContext) -> RunResult {
26+
self.0.run(context)
27+
}
28+
}
29+
830
pub struct FnCase {
931
name: String,
1032
#[allow(clippy::type_complexity)]
@@ -58,6 +80,8 @@ pub fn main(cases: impl IntoIterator<Item = impl Case + 'static>) {
5880
::std::process::exit(1);
5981
}
6082
};
83+
let mut cases = cases.into_iter().collect::<Vec<_>>();
84+
cases.sort_by_key(|c| c.name().to_owned());
6185
let harness = match harness.discover(cases) {
6286
Ok(harness) => harness,
6387
Err(err) => {

crates/libtest2/src/lib.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,21 @@
1212
//! harness = false
1313
//! ```
1414
//!
15-
//! And in `tests/mytest.rs` you would call [`main!`], passing it each of your tests:
15+
//! And in `tests/mytest.rs` you would wrap `main` with [`#[main]`]:
1616
//!
1717
//! ```no_run
1818
//! # use libtest2::RunError;
1919
//! # use libtest2::RunResult;
2020
//! # use libtest2::TestContext;
21+
//! #[libtest2::test]
2122
//! fn check_toph(_context: &TestContext) -> RunResult {
2223
//! Ok(())
2324
//! }
2425
//!
25-
//! libtest2::main!(check_toph);
26+
//! #[libtest2::main]
27+
//! fn main() {
28+
//! }
2629
//! ```
27-
//!
2830
2931
#![cfg_attr(docsrs, feature(doc_cfg))]
3032
//#![warn(clippy::print_stderr)]
@@ -35,15 +37,24 @@ mod macros;
3537

3638
#[doc(hidden)]
3739
pub mod _private {
38-
pub use crate::_main as main;
40+
pub use distributed_list::push;
41+
pub use distributed_list::DistributedList;
42+
pub use libtest2_harness::Case;
43+
pub use libtest2_harness::Source;
44+
pub use libtest2_harness::TestKind;
45+
46+
pub use crate::_main_parse as main_parse;
47+
pub use crate::_test_parse as test_parse;
48+
pub use crate::case::DynCase;
3949
}
4050

41-
pub use _private::main;
4251
pub use case::main;
4352
pub use case::FnCase;
4453
pub use libtest2_harness::RunError;
4554
pub use libtest2_harness::RunResult;
4655
pub use libtest2_harness::TestContext;
56+
pub use libtest2_proc_macro::main;
57+
pub use libtest2_proc_macro::test;
4758

4859
#[doc = include_str!("../README.md")]
4960
#[cfg(doctest)]

crates/libtest2/src/macros.rs

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,45 @@
1-
/// Expands to the test harness
21
#[macro_export]
3-
macro_rules! _main {
4-
( $( $test:path ),* $(,)*) => {
2+
macro_rules! _main_parse {
3+
(#[main] fn main $($item:tt)*) => {
4+
static TESTS: $crate::_private::DistributedList<$crate::_private::DynCase> = $crate::_private::DistributedList::root();
5+
56
fn main() {
6-
$crate::main([
7-
$($crate::FnCase::test(::std::stringify!($test), $test)),*
8-
]);
7+
fn inner $($item)*
8+
9+
inner();
10+
$crate::main(TESTS.iter().copied());
911
}
10-
}
12+
};
13+
}
14+
15+
#[macro_export]
16+
#[allow(clippy::crate_in_macro_def)] // accessing item defined by `_main_parse`
17+
macro_rules! _test_parse {
18+
(#[test] fn $name:ident $($item:tt)*) => {
19+
#[allow(non_camel_case_types)]
20+
struct $name;
21+
22+
impl $crate::_private::Case for $name {
23+
fn name(&self) -> &str {
24+
$crate::_private::push!(crate::TESTS, _: $crate::_private::DynCase = $crate::_private::DynCase(&$name));
25+
26+
stringify!($name)
27+
}
28+
fn kind(&self) -> $crate::_private::TestKind {
29+
Default::default()
30+
}
31+
fn source(&self) -> Option<&$crate::_private::Source> {
32+
None
33+
}
34+
fn exclusive(&self, _: &$crate::TestContext) -> bool {
35+
false
36+
}
37+
38+
fn run(&self, context: &$crate::TestContext) -> $crate::RunResult {
39+
fn run $($item)*
40+
41+
run(context)
42+
}
43+
}
44+
};
1145
}

crates/libtest2/tests/testsuite/all_passing.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,20 @@ fn test_cmd() -> snapbox::cmd::Command {
77
let (bin, current_dir) = BIN.get_or_init(|| {
88
let package_root = crate::util::new_test(
99
r#"
10-
libtest2::main!(foo, bar, barro);
10+
#[libtest2::main]
11+
fn main() {}
1112
13+
#[libtest2::test]
1214
fn foo(_context: &libtest2::TestContext) -> libtest2::RunResult {
1315
Ok(())
1416
}
1517
18+
#[libtest2::test]
1619
fn bar(_context: &libtest2::TestContext) -> libtest2::RunResult {
1720
Ok(())
1821
}
1922
23+
#[libtest2::test]
2024
fn barro(_context: &libtest2::TestContext) -> libtest2::RunResult {
2125
Ok(())
2226
}

crates/libtest2/tests/testsuite/argfile.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,25 @@ fn test_cmd() -> snapbox::cmd::Command {
77
let (bin, current_dir) = BIN.get_or_init(|| {
88
let package_root = crate::util::new_test(
99
r#"
10-
libtest2::main!(one, two, three, one_two);
10+
#[libtest2::main]
11+
fn main() {}
1112
13+
#[libtest2::test]
1214
fn one(_context: &libtest2::TestContext) -> libtest2::RunResult {
1315
Ok(())
1416
}
1517
18+
#[libtest2::test]
1619
fn two(_context: &libtest2::TestContext) -> libtest2::RunResult {
1720
Ok(())
1821
}
1922
23+
#[libtest2::test]
2024
fn three(_context: &libtest2::TestContext) -> libtest2::RunResult {
2125
Ok(())
2226
}
2327
28+
#[libtest2::test]
2429
fn one_two(_context: &libtest2::TestContext) -> libtest2::RunResult {
2530
Ok(())
2631
}
@@ -95,17 +100,17 @@ fn list() {
95100
0,
96101
str![[r#"
97102
one: test
98-
two: test
99-
three: test
100103
one_two: test
104+
three: test
105+
two: test
101106
102107
4 tests
103108
104109
105110
"#]],
106111
str![[r#"
107112
one: test
108-
two: test
113+
one_two: test
109114
...
110115
111116
4 tests

0 commit comments

Comments
 (0)