Skip to content

Commit 3783704

Browse files
committed
refactor: move method do_fail_with_message to the new trait DoFail and implement the trait for Spec
1 parent 25e8232 commit 3783704

2 files changed

Lines changed: 39 additions & 17 deletions

File tree

src/prelude.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ pub use super::{
2020
colored::{DEFAULT_DIFF_FORMAT, DIFF_FORMAT_NO_HIGHLIGHT},
2121
properties::*,
2222
spec::{
23-
assert_that, verify_that, CollectFailures, GetFailures, Location, PanicOnFail, SoftPanic,
23+
assert_that, verify_that, CollectFailures, DoFail, GetFailures, Location, PanicOnFail,
24+
SoftPanic,
2425
},
2526
verify_that,
2627
};

src/spec/mod.rs

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ impl<S, R> Spec<'_, S, R> {
664664
&self.diff_format
665665
}
666666

667-
/// Returns the failing strategy that is used in case an assertion fails.
667+
/// Returns the failing strategy used in case an assertion fails.
668668
pub fn failing_strategy(&self) -> &R {
669669
&self.failing_strategy
670670
}
@@ -956,20 +956,6 @@ where
956956
{
957957
self.expecting(satisfies(predicate).with_message(message))
958958
}
959-
960-
/// Fails the assertion according to the current failing strategy of this
961-
/// `Spec`.
962-
#[track_caller]
963-
pub fn do_fail_with_message(&mut self, message: impl Into<String>) {
964-
let message = message.into();
965-
let failure = AssertFailure {
966-
description: self.description.clone().map(String::from),
967-
message,
968-
location: self.location.map(OwnedLocation::from),
969-
};
970-
self.failures.push(failure);
971-
self.failing_strategy.do_fail_with(&self.failures);
972-
}
973959
}
974960

975961
impl<'a, I, R> Spec<'a, I, R> {
@@ -1140,9 +1126,44 @@ impl<'a, I, R> Spec<'a, I, R> {
11401126
}
11411127
}
11421128

1129+
/// Trigger failing of an assertion according to the failing strategy of its
1130+
/// implementing spec-like struct.
1131+
pub trait DoFail {
1132+
/// Fails the assertion with the given [`AssertFailure`]s according to the
1133+
/// current failing strategy of the `Spec` or other implementing
1134+
/// spec-like struct.
1135+
fn do_fail_with(&mut self, failures: impl IntoIterator<Item = AssertFailure>);
1136+
1137+
/// Fails the assertion with the given failure message according to the
1138+
/// current failing strategy of the `Spec` or other implementing
1139+
/// spec-like struct.
1140+
fn do_fail_with_message(&mut self, message: impl Into<String>);
1141+
}
1142+
1143+
impl<S, R> DoFail for Spec<'_, S, R>
1144+
where
1145+
R: FailingStrategy,
1146+
{
1147+
fn do_fail_with(&mut self, failures: impl IntoIterator<Item = AssertFailure>) {
1148+
self.failures.extend(failures);
1149+
self.failing_strategy.do_fail_with(&self.failures);
1150+
}
1151+
1152+
fn do_fail_with_message(&mut self, message: impl Into<String>) {
1153+
let message = message.into();
1154+
let failure = AssertFailure {
1155+
description: self.description.clone().map(String::from),
1156+
message,
1157+
location: self.location.map(OwnedLocation::from),
1158+
};
1159+
self.failures.push(failure);
1160+
self.failing_strategy.do_fail_with(&self.failures);
1161+
}
1162+
}
1163+
11431164
/// Turns assertions into "soft assertions".
11441165
///
1145-
/// see method [`soft_panic()`](SoftPanic::soft_panic) for details and how to
1166+
/// See method [`soft_panic()`](SoftPanic::soft_panic) for details and how to
11461167
/// use it.
11471168
pub trait SoftPanic {
11481169
/// Turns assertions into "soft assertions".

0 commit comments

Comments
 (0)