Skip to content

Commit 1b119bf

Browse files
committed
Add tests with modified prop_assert.
1 parent 2459c8d commit 1b119bf

File tree

1 file changed

+48
-10
lines changed

1 file changed

+48
-10
lines changed

tests/proptest_fn.rs

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ async fn async_expr_test_prop_assert_false() {
117117
prop_assert!(false);
118118
}
119119

120-
struct NotImplError;
120+
struct NotImplError0;
121+
struct NotImplError1;
121122

122123
#[derive(Debug)]
123124
struct CustomError(#[allow(dead_code)] TestCaseError);
@@ -133,30 +134,63 @@ impl From<TestCaseError> for CustomError {
133134
CustomError(e)
134135
}
135136
}
136-
impl From<NotImplError> for CustomError {
137-
fn from(_: NotImplError) -> Self {
137+
impl From<NotImplError0> for CustomError {
138+
fn from(_: NotImplError0) -> Self {
139+
CustomError(TestCaseError::fail("Custom"))
140+
}
141+
}
142+
impl From<NotImplError1> for CustomError {
143+
fn from(_: NotImplError1) -> Self {
138144
CustomError(TestCaseError::fail("Custom"))
139145
}
140146
}
141147

148+
macro_rules! prop_assert2 {
149+
($cond:expr) => {
150+
prop_assert2!($cond, concat!("assertion failed: ", stringify!($cond)))
151+
};
152+
($cond:expr, $($fmt:tt)*) => {
153+
if !$cond {
154+
let message = format!($($fmt)*);
155+
let message = format!("{} at {}:{}", message, file!(), line!());
156+
::core::result::Result::Err(::proptest::test_runner::TestCaseError::fail(message))?;
157+
}
158+
};
159+
}
160+
142161
#[proptest]
143162
fn custom_error_ok(#[strategy(1..10u8)] x: u8) -> Result<(), CustomError> {
144-
assert!(x < 10);
163+
prop_assert2!(x < 10);
145164
not_impl_error_ok()?;
146165
Ok(())
147166
}
148167

168+
#[should_panic]
169+
#[proptest]
170+
fn custom_error_ok_prop_assesrt_fail(#[strategy(1..10u8)] x: u8) -> Result<(), CustomError> {
171+
prop_assert2!(x >= 10);
172+
Ok(())
173+
}
174+
149175
#[proptest]
150176
#[should_panic]
151177
fn custom_error_err(#[strategy(1..10u8)] x: u8) -> Result<(), CustomError> {
152-
assert!(x < 10);
178+
prop_assert2!(x < 10);
153179
not_impl_error_err()?;
154180
Ok(())
155181
}
156182

183+
#[proptest]
184+
fn custom_error_ok_2(#[strategy(1..10u8)] x: u8) -> Result<(), CustomError> {
185+
prop_assert2!(x < 10);
186+
not_impl_error_ok()?;
187+
not_impl_error_ok_1()?;
188+
Ok(())
189+
}
190+
157191
#[proptest(async = "tokio")]
158192
async fn custom_error_async_ok(#[strategy(1..10u8)] x: u8) -> Result<(), CustomError> {
159-
assert!(x < 10);
193+
prop_assert2!(x < 10);
160194
not_impl_error_ok()?;
161195
yield_now().await;
162196
Ok(())
@@ -165,15 +199,19 @@ async fn custom_error_async_ok(#[strategy(1..10u8)] x: u8) -> Result<(), CustomE
165199
#[proptest(async = "tokio")]
166200
#[should_panic]
167201
async fn custom_error_async_err(#[strategy(1..10u8)] x: u8) -> Result<(), CustomError> {
168-
assert!(x < 10);
202+
prop_assert2!(x < 10);
169203
not_impl_error_err()?;
170204
yield_now().await;
171205
Ok(())
172206
}
173207

174-
fn not_impl_error_ok() -> Result<(), NotImplError> {
208+
fn not_impl_error_ok() -> Result<(), NotImplError0> {
175209
Ok(())
176210
}
177-
fn not_impl_error_err() -> Result<(), NotImplError> {
178-
Err(NotImplError)
211+
fn not_impl_error_err() -> Result<(), NotImplError0> {
212+
Err(NotImplError0)
213+
}
214+
215+
fn not_impl_error_ok_1() -> Result<(), NotImplError1> {
216+
Ok(())
179217
}

0 commit comments

Comments
 (0)