Skip to content

Commit ec6d78f

Browse files
authored
feat(test2): Allow use of custom error types (#144)
This is part of #13
2 parents c61a9e5 + e313c58 commit ec6d78f

File tree

12 files changed

+611
-340
lines changed

12 files changed

+611
-340
lines changed

crates/libtest2-harness/src/case.rs

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -48,68 +48,3 @@ pub enum Source {
4848
},
4949
Path(std::path::PathBuf),
5050
}
51-
52-
pub type RunResult = Result<(), RunError>;
53-
54-
#[derive(Debug)]
55-
pub struct RunError {
56-
status: notify::MessageKind,
57-
cause: Option<Box<dyn std::error::Error + Send + Sync + 'static>>,
58-
}
59-
60-
impl RunError {
61-
pub fn with_cause(cause: impl std::error::Error + Send + Sync + 'static) -> Self {
62-
Self {
63-
status: notify::MessageKind::Error,
64-
cause: Some(Box::new(cause)),
65-
}
66-
}
67-
68-
pub fn fail(cause: impl std::fmt::Display) -> Self {
69-
Self::with_cause(Message(cause.to_string()))
70-
}
71-
72-
/// Should not be called with `libtest_lexarg::RunIgnored::Yes`
73-
pub fn ignore() -> Self {
74-
Self {
75-
status: notify::MessageKind::Ignored,
76-
cause: None,
77-
}
78-
}
79-
80-
/// Should not be called with `libtest_lexarg::RunIgnored::Yes`
81-
pub fn ignore_for(reason: String) -> Self {
82-
Self {
83-
status: notify::MessageKind::Ignored,
84-
cause: Some(Box::new(Message(reason))),
85-
}
86-
}
87-
88-
pub(crate) fn status(&self) -> notify::MessageKind {
89-
self.status
90-
}
91-
92-
pub(crate) fn cause(&self) -> Option<&(dyn std::error::Error + Send + Sync)> {
93-
self.cause.as_ref().map(|b| b.as_ref())
94-
}
95-
}
96-
97-
impl<E> From<E> for RunError
98-
where
99-
E: std::error::Error + Send + Sync + 'static,
100-
{
101-
fn from(error: E) -> Self {
102-
Self::with_cause(error)
103-
}
104-
}
105-
106-
#[derive(Debug)]
107-
struct Message(String);
108-
109-
impl std::fmt::Display for Message {
110-
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
111-
self.0.fmt(formatter)
112-
}
113-
}
114-
115-
impl std::error::Error for Message {}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
pub(crate) use crate::*;
2+
3+
pub type RunResult = Result<(), RunError>;
4+
5+
#[derive(Debug)]
6+
pub struct RunError {
7+
status: notify::MessageKind,
8+
cause: Option<Box<dyn std::error::Error + Send + Sync + 'static>>,
9+
}
10+
11+
impl RunError {
12+
pub fn with_cause(cause: impl std::error::Error + Send + Sync + 'static) -> Self {
13+
Self {
14+
status: notify::MessageKind::Error,
15+
cause: Some(Box::new(cause)),
16+
}
17+
}
18+
19+
pub fn fail(cause: impl std::fmt::Display) -> Self {
20+
Self::with_cause(Message(cause.to_string()))
21+
}
22+
23+
/// Should not be called with `libtest_lexarg::RunIgnored::Yes`
24+
pub fn ignore() -> Self {
25+
Self {
26+
status: notify::MessageKind::Ignored,
27+
cause: None,
28+
}
29+
}
30+
31+
/// Should not be called with `libtest_lexarg::RunIgnored::Yes`
32+
pub fn ignore_for(reason: String) -> Self {
33+
Self {
34+
status: notify::MessageKind::Ignored,
35+
cause: Some(Box::new(Message(reason))),
36+
}
37+
}
38+
39+
pub(crate) fn status(&self) -> notify::MessageKind {
40+
self.status
41+
}
42+
43+
pub(crate) fn cause(&self) -> Option<&(dyn std::error::Error + Send + Sync)> {
44+
self.cause.as_ref().map(|b| b.as_ref())
45+
}
46+
}
47+
48+
impl<E> From<E> for RunError
49+
where
50+
E: std::error::Error + Send + Sync + 'static,
51+
{
52+
fn from(error: E) -> Self {
53+
Self::with_cause(error)
54+
}
55+
}
56+
57+
#[derive(Debug)]
58+
struct Message(String);
59+
60+
impl std::fmt::Display for Message {
61+
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
62+
self.0.fmt(formatter)
63+
}
64+
}
65+
66+
impl std::error::Error for Message {}
67+
68+
pub trait IntoRunResult {
69+
fn into_run_result(self) -> RunResult;
70+
}
71+
72+
impl IntoRunResult for () {
73+
fn into_run_result(self) -> RunResult {
74+
Ok(())
75+
}
76+
}
77+
78+
impl IntoRunResult for RunResult {
79+
fn into_run_result(self) -> RunResult {
80+
self
81+
}
82+
}
83+
84+
impl<E> IntoRunResult for Result<(), E>
85+
where
86+
E: std::error::Error + Send + Sync + 'static,
87+
{
88+
fn into_run_result(self) -> RunResult {
89+
self.map_err(RunError::with_cause)
90+
}
91+
}

crates/libtest2-harness/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66

77
mod case;
88
mod context;
9+
mod error;
910
mod harness;
1011
mod notify;
1112

1213
pub mod cli;
1314

1415
pub use case::*;
1516
pub use context::*;
17+
pub use error::*;
1618
pub use harness::*;
1719
pub use notify::RunMode;
1820

crates/libtest2-mimic/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,15 @@ impl RunError {
189189
}
190190
}
191191

192+
impl<E> From<E> for RunError
193+
where
194+
E: std::error::Error + Send + Sync + 'static,
195+
{
196+
fn from(error: E) -> Self {
197+
Self::with_cause(error)
198+
}
199+
}
200+
192201
pub struct RunContext<'t> {
193202
inner: &'t libtest2_harness::TestContext,
194203
}

0 commit comments

Comments
 (0)