Skip to content

Commit 4090196

Browse files
authored
fix(test2)!: Prep API for macros (#133)
2 parents 8d68adb + e74d06c commit 4090196

File tree

9 files changed

+109
-96
lines changed

9 files changed

+109
-96
lines changed

crates/libtest2/examples/simple.rs

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

5-
libtest2::libtest2_main!(
5+
libtest2::main!(
66
check_toph,
77
check_katara,
88
check_sokka,

crates/libtest2/examples/tidy.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
use libtest2::FnCase;
12
use libtest2::RunError;
23
use libtest2::RunResult;
3-
use libtest2::Trial;
44

55
fn main() -> std::io::Result<()> {
66
let harness = ::libtest2_harness::Harness::new();
@@ -23,8 +23,8 @@ fn main() -> std::io::Result<()> {
2323

2424
/// Creates one test for each `.rs` file in the current directory or
2525
/// sub-directories of the current directory.
26-
fn collect_tests() -> std::io::Result<Vec<Trial>> {
27-
fn visit_dir(path: &std::path::Path, tests: &mut Vec<Trial>) -> std::io::Result<()> {
26+
fn collect_tests() -> std::io::Result<Vec<FnCase>> {
27+
fn visit_dir(path: &std::path::Path, tests: &mut Vec<FnCase>) -> std::io::Result<()> {
2828
let current_dir = std::env::current_dir()?;
2929
for entry in std::fs::read_dir(path)? {
3030
let entry = entry?;
@@ -44,7 +44,7 @@ fn collect_tests() -> std::io::Result<Vec<Trial>> {
4444
.to_string_lossy()
4545
.into_owned();
4646

47-
let test = Trial::test(name, move |_| check_file(&path));
47+
let test = FnCase::test(name, move |_| check_file(&path));
4848
tests.push(test);
4949
}
5050
} else if file_type.is_dir() {

crates/libtest2/src/case.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
use libtest2_harness::Case;
2+
use libtest2_harness::Source;
3+
use libtest2_harness::TestKind;
4+
5+
use crate::RunResult;
6+
use crate::TestContext;
7+
8+
pub struct FnCase {
9+
name: String,
10+
#[allow(clippy::type_complexity)]
11+
runner: Box<dyn Fn(&TestContext) -> RunResult + Send + Sync>,
12+
}
13+
14+
impl FnCase {
15+
pub fn test(
16+
name: impl Into<String>,
17+
runner: impl Fn(&TestContext) -> RunResult + Send + Sync + 'static,
18+
) -> Self {
19+
Self {
20+
name: name.into(),
21+
runner: Box::new(runner),
22+
}
23+
}
24+
}
25+
26+
impl Case for FnCase {
27+
fn name(&self) -> &str {
28+
&self.name
29+
}
30+
fn kind(&self) -> TestKind {
31+
Default::default()
32+
}
33+
fn source(&self) -> Option<&Source> {
34+
None
35+
}
36+
fn exclusive(&self, _: &TestContext) -> bool {
37+
false
38+
}
39+
40+
fn run(&self, context: &TestContext) -> RunResult {
41+
(self.runner)(context)
42+
}
43+
}
44+
45+
pub fn main(cases: impl IntoIterator<Item = impl Case + 'static>) {
46+
let harness = libtest2_harness::Harness::new();
47+
let harness = match harness.with_env() {
48+
Ok(harness) => harness,
49+
Err(err) => {
50+
eprintln!("{err}");
51+
::std::process::exit(1);
52+
}
53+
};
54+
let harness = match harness.parse() {
55+
Ok(harness) => harness,
56+
Err(err) => {
57+
eprintln!("{err}");
58+
::std::process::exit(1);
59+
}
60+
};
61+
let harness = match harness.discover(cases) {
62+
Ok(harness) => harness,
63+
Err(err) => {
64+
eprintln!("{err}");
65+
::std::process::exit(libtest2_harness::ERROR_EXIT_CODE)
66+
}
67+
};
68+
match harness.run() {
69+
Ok(true) => ::std::process::exit(0),
70+
Ok(false) => ::std::process::exit(libtest2_harness::ERROR_EXIT_CODE),
71+
Err(err) => {
72+
eprintln!("{err}");
73+
::std::process::exit(libtest2_harness::ERROR_EXIT_CODE)
74+
}
75+
}
76+
}

crates/libtest2/src/lib.rs

Lines changed: 13 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -12,112 +12,38 @@
1212
//! harness = false
1313
//! ```
1414
//!
15-
//! And in `tests/mytest.rs` you would call [`libtest2_main`], passing it each of your tests:
15+
//! And in `tests/mytest.rs` you would call [`main!`], passing it each of your tests:
1616
//!
1717
//! ```no_run
1818
//! # use libtest2::RunError;
1919
//! # use libtest2::RunResult;
2020
//! # use libtest2::TestContext;
21-
//! # use libtest2::libtest2_main;
2221
//! fn check_toph(_context: &TestContext) -> RunResult {
2322
//! Ok(())
2423
//! }
2524
//!
26-
//! libtest2_main!(check_toph);
25+
//! libtest2::main!(check_toph);
2726
//! ```
2827
//!
2928
3029
#![cfg_attr(docsrs, feature(doc_cfg))]
3130
//#![warn(clippy::print_stderr)]
3231
#![warn(clippy::print_stdout)]
3332

34-
pub use libtest2_harness::Harness;
35-
pub use libtest2_harness::RunError;
36-
pub use libtest2_harness::RunResult;
37-
pub use libtest2_harness::TestContext;
38-
pub use libtest2_harness::TestKind;
39-
pub use libtest2_harness::ERROR_EXIT_CODE;
40-
41-
use libtest2_harness::Case;
42-
use libtest2_harness::Source;
33+
mod case;
34+
mod macros;
4335

44-
pub struct Trial {
45-
name: String,
46-
#[allow(clippy::type_complexity)]
47-
runner: Box<dyn Fn(&TestContext) -> Result<(), RunError> + Send + Sync>,
36+
#[doc(hidden)]
37+
pub mod _private {
38+
pub use crate::_main as main;
4839
}
4940

50-
impl Trial {
51-
pub fn test(
52-
name: impl Into<String>,
53-
runner: impl Fn(&TestContext) -> Result<(), RunError> + Send + Sync + 'static,
54-
) -> Self {
55-
Self {
56-
name: name.into(),
57-
runner: Box::new(runner),
58-
}
59-
}
60-
}
61-
62-
impl Case for Trial {
63-
fn name(&self) -> &str {
64-
&self.name
65-
}
66-
fn kind(&self) -> TestKind {
67-
Default::default()
68-
}
69-
fn source(&self) -> Option<&Source> {
70-
None
71-
}
72-
fn exclusive(&self, _: &TestContext) -> bool {
73-
false
74-
}
75-
76-
fn run(&self, context: &TestContext) -> Result<(), RunError> {
77-
(self.runner)(context)
78-
}
79-
}
80-
81-
/// Expands to the test harness
82-
#[macro_export]
83-
macro_rules! libtest2_main {
84-
( $( $test:path ),* $(,)*) => {
85-
fn main() {
86-
let harness = $crate::Harness::new();
87-
let harness = match harness.with_env() {
88-
Ok(harness) => harness,
89-
Err(err) => {
90-
eprintln!("{err}");
91-
::std::process::exit(1);
92-
}
93-
};
94-
let harness = match harness.parse() {
95-
Ok(harness) => harness,
96-
Err(err) => {
97-
eprintln!("{err}");
98-
::std::process::exit(1);
99-
}
100-
};
101-
let harness = match harness.discover([
102-
$($crate::Trial::test(::std::stringify!($test), $test)),*
103-
]) {
104-
Ok(harness) => harness,
105-
Err(err) => {
106-
eprintln!("{err}");
107-
::std::process::exit($crate::ERROR_EXIT_CODE)
108-
}
109-
};
110-
match harness.run() {
111-
Ok(true) => ::std::process::exit(0),
112-
Ok(false) => ::std::process::exit($crate::ERROR_EXIT_CODE),
113-
Err(err) => {
114-
eprintln!("{err}");
115-
::std::process::exit($crate::ERROR_EXIT_CODE)
116-
}
117-
}
118-
}
119-
}
120-
}
41+
pub use _private::main;
42+
pub use case::main;
43+
pub use case::FnCase;
44+
pub use libtest2_harness::RunError;
45+
pub use libtest2_harness::RunResult;
46+
pub use libtest2_harness::TestContext;
12147

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

crates/libtest2/src/macros.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// Expands to the test harness
2+
#[macro_export]
3+
macro_rules! _main {
4+
( $( $test:path ),* $(,)*) => {
5+
fn main() {
6+
$crate::main([
7+
$($crate::FnCase::test(::std::stringify!($test), $test)),*
8+
]);
9+
}
10+
}
11+
}

crates/libtest2/tests/testsuite/all_passing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ 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::libtest2_main!(foo, bar, barro);
10+
libtest2::main!(foo, bar, barro);
1111
1212
fn foo(_context: &libtest2::TestContext) -> libtest2::RunResult {
1313
Ok(())

crates/libtest2/tests/testsuite/argfile.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ 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::libtest2_main!(one, two, three, one_two);
10+
libtest2::main!(one, two, three, one_two);
1111
1212
fn one(_context: &libtest2::TestContext) -> libtest2::RunResult {
1313
Ok(())

crates/libtest2/tests/testsuite/mixed_bag.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ 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::libtest2_main!(cat, dog, fox, bunny, frog, owl, fly, bear);
10+
libtest2::main!(cat, dog, fox, bunny, frog, owl, fly, bear);
1111
1212
fn cat(_context: &libtest2::TestContext) -> libtest2::RunResult {
1313
Ok(())

crates/libtest2/tests/testsuite/panic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ 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::libtest2_main!(passes, panics);
10+
libtest2::main!(passes, panics);
1111
1212
fn passes(_context: &libtest2::TestContext) -> libtest2::RunResult {
1313
Ok(())

0 commit comments

Comments
 (0)