Skip to content

Commit 06a2551

Browse files
committed
refactor(test2): Pull out trial.rs
1 parent c12446c commit 06a2551

File tree

2 files changed

+79
-74
lines changed

2 files changed

+79
-74
lines changed

crates/libtest2/src/lib.rs

Lines changed: 3 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#![warn(clippy::print_stdout)]
3232

3333
mod macros;
34+
mod trial;
3435

3536
#[doc(hidden)]
3637
pub mod _private {
@@ -41,80 +42,8 @@ pub use _private::main;
4142
pub use libtest2_harness::RunError;
4243
pub use libtest2_harness::RunResult;
4344
pub use libtest2_harness::TestContext;
44-
45-
use libtest2_harness::Case;
46-
use libtest2_harness::Source;
47-
use libtest2_harness::TestKind;
48-
49-
pub struct Trial {
50-
name: String,
51-
#[allow(clippy::type_complexity)]
52-
runner: Box<dyn Fn(&TestContext) -> RunResult + Send + Sync>,
53-
}
54-
55-
impl Trial {
56-
pub fn test(
57-
name: impl Into<String>,
58-
runner: impl Fn(&TestContext) -> RunResult + Send + Sync + 'static,
59-
) -> Self {
60-
Self {
61-
name: name.into(),
62-
runner: Box::new(runner),
63-
}
64-
}
65-
}
66-
67-
impl Case for Trial {
68-
fn name(&self) -> &str {
69-
&self.name
70-
}
71-
fn kind(&self) -> TestKind {
72-
Default::default()
73-
}
74-
fn source(&self) -> Option<&Source> {
75-
None
76-
}
77-
fn exclusive(&self, _: &TestContext) -> bool {
78-
false
79-
}
80-
81-
fn run(&self, context: &TestContext) -> RunResult {
82-
(self.runner)(context)
83-
}
84-
}
85-
86-
pub fn main(cases: impl IntoIterator<Item = impl Case + 'static>) {
87-
let harness = libtest2_harness::Harness::new();
88-
let harness = match harness.with_env() {
89-
Ok(harness) => harness,
90-
Err(err) => {
91-
eprintln!("{err}");
92-
::std::process::exit(1);
93-
}
94-
};
95-
let harness = match harness.parse() {
96-
Ok(harness) => harness,
97-
Err(err) => {
98-
eprintln!("{err}");
99-
::std::process::exit(1);
100-
}
101-
};
102-
let harness = match harness.discover(cases) {
103-
Ok(harness) => harness,
104-
Err(err) => {
105-
eprintln!("{err}");
106-
::std::process::exit(libtest2_harness::ERROR_EXIT_CODE)
107-
}
108-
};
109-
match harness.run() {
110-
Ok(true) => ::std::process::exit(0),
111-
Ok(false) => ::std::process::exit(libtest2_harness::ERROR_EXIT_CODE),
112-
Err(err) => {
113-
eprintln!("{err}");
114-
::std::process::exit(libtest2_harness::ERROR_EXIT_CODE)
115-
}
116-
}
117-
}
45+
pub use trial::main;
46+
pub use trial::Trial;
11847

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

crates/libtest2/src/trial.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 Trial {
9+
name: String,
10+
#[allow(clippy::type_complexity)]
11+
runner: Box<dyn Fn(&TestContext) -> RunResult + Send + Sync>,
12+
}
13+
14+
impl Trial {
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 Trial {
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+
}

0 commit comments

Comments
 (0)