forked from withoutboats/fehler
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
throws_expr!
for wrapping closures/async blocks
Also `#[throws]` should now work on closures/async blocks with the nightly feature active
- Loading branch information
Showing
6 changed files
with
261 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
use culpa::{throw, throws_expr}; | ||
|
||
#[test] | ||
#[allow(clippy::unused_unit)] | ||
fn unit() { | ||
type Error = (); | ||
let ok = Result::<(), ()>::Ok(()); | ||
assert_eq!(ok, poll(throws_expr!(async {}))); | ||
assert_eq!(ok, poll(throws_expr!(async { () }))); | ||
assert_eq!( | ||
ok, | ||
poll(throws_expr!(async { | ||
return; | ||
})) | ||
); | ||
assert_eq!( | ||
ok, | ||
poll(throws_expr!(async { | ||
return (); | ||
})) | ||
); | ||
} | ||
|
||
#[test] | ||
fn integer() { | ||
type Error = (); | ||
let ok = Result::<i32, ()>::Ok(1); | ||
assert_eq!(ok, poll(throws_expr!(async { 1 }))); | ||
assert_eq!( | ||
ok, | ||
poll(throws_expr!(async { | ||
return 1; | ||
})) | ||
); | ||
} | ||
|
||
#[test] | ||
fn throws_unit() { | ||
type Error = (); | ||
let err = Result::<(), ()>::Err(()); | ||
assert_eq!(err, poll(throws_expr!(async { throw!(()) }))); | ||
} | ||
|
||
#[test] | ||
fn throws_integer() { | ||
type Error = i32; | ||
let err = Result::<(), i32>::Err(1); | ||
assert_eq!(err, poll(throws_expr!(async { throw!(1) }))); | ||
} | ||
|
||
#[test] | ||
fn has_inner_fn() { | ||
type Error = (); | ||
assert_eq!( | ||
Result::<(), ()>::Ok(()), | ||
poll(throws_expr!(async { | ||
async fn foo() -> i32 { | ||
5 | ||
} | ||
assert_eq!(5, foo().await); | ||
})), | ||
); | ||
} | ||
|
||
#[test] | ||
fn has_inner_closure() { | ||
type Error = (); | ||
assert_eq!( | ||
Result::<(), ()>::Ok(()), | ||
poll(throws_expr!(async { | ||
assert_eq!(5, async { 5 }.await); | ||
})), | ||
); | ||
} | ||
|
||
fn poll<F: std::future::Future>(f: F) -> F::Output { | ||
struct NoopWake; | ||
impl std::task::Wake for NoopWake { | ||
fn wake(self: std::sync::Arc<Self>) {} | ||
} | ||
let std::task::Poll::Ready(output) = std::pin::pin!(f).poll( | ||
&mut std::task::Context::from_waker(&std::sync::Arc::new(NoopWake).into()), | ||
) else { | ||
panic!("future was not ready") | ||
}; | ||
output | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#![allow(unused_braces)] | ||
#![allow(clippy::redundant_closure_call)] | ||
|
||
use culpa::{throw, throws_expr}; | ||
|
||
#[test] | ||
#[rustfmt::skip] | ||
fn unit() { | ||
type Error = (); | ||
let ok = Result::<(), ()>::Ok(()); | ||
assert_eq!(ok, throws_expr!(|| {})()); | ||
assert_eq!(ok, throws_expr!(|| ())()); | ||
assert_eq!(ok, throws_expr!(|| -> () {})()); | ||
assert_eq!(ok, throws_expr!(|| { return; })()); | ||
assert_eq!(ok, throws_expr!(|| { return (); })()); | ||
assert_eq!(ok, throws_expr!(|| -> () { return; })()); | ||
assert_eq!(ok, throws_expr!(|| -> () { return (); })()); | ||
} | ||
|
||
#[test] | ||
#[rustfmt::skip] | ||
fn integer() { | ||
type Error = (); | ||
let ok = Result::<i32, ()>::Ok(1); | ||
assert_eq!(ok, throws_expr!(|| { 1 })()); | ||
assert_eq!(ok, throws_expr!(|| 1)()); | ||
assert_eq!(ok, throws_expr!(|| -> i32 { 1 })()); | ||
assert_eq!(ok, throws_expr!(|| { return 1; })()); | ||
assert_eq!(ok, throws_expr!(|| -> i32 { return 1; })()); | ||
assert_eq!(ok, throws_expr!(|| -> _ { 1 })()); | ||
} | ||
|
||
#[test] | ||
#[rustfmt::skip] | ||
fn throws_unit() { | ||
type Error = (); | ||
let err = Result::<(), ()>::Err(()); | ||
assert_eq!(err, throws_expr!(|| { throw!(()) })()); | ||
assert_eq!(err, throws_expr!(|| throw!(()))()); | ||
assert_eq!(err, throws_expr!(|| -> () { throw!(()) })()); | ||
} | ||
|
||
#[test] | ||
#[rustfmt::skip] | ||
fn throws_integer() { | ||
type Error = i32; | ||
let err = Result::<(), i32>::Err(1); | ||
assert_eq!(err, throws_expr!(|| { throw!(1)} )()); | ||
assert_eq!(err, throws_expr!(|| throw!(1))()); | ||
assert_eq!(err, throws_expr!(|| -> () { throw!(1) })()); | ||
} | ||
|
||
#[test] | ||
fn has_inner_fn() { | ||
type Error = (); | ||
assert_eq!( | ||
Result::<(), ()>::Ok(()), | ||
throws_expr!(|| { | ||
fn foo() -> i32 { | ||
5 | ||
} | ||
assert_eq!(5, foo()); | ||
})(), | ||
); | ||
} | ||
|
||
#[test] | ||
fn has_inner_closure() { | ||
type Error = (); | ||
assert_eq!( | ||
Result::<(), ()>::Ok(()), | ||
throws_expr!(|| { | ||
assert_eq!(5, (|| 5)()); | ||
})(), | ||
); | ||
} |