Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 61 additions & 56 deletions googletest/src/assertions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,9 +513,9 @@ macro_rules! expect_true {
$crate::GoogleTestSupport::and_log_failure($crate::verify_true!($condition))
}};
($condition:expr, $($format_args:expr),* $(,)?) => {{
$crate::GoogleTestSupport::and_log_failure($crate::verify_true!($condition)
.with_failure_message(|| format!($($format_args),*))
);
$crate::GoogleTestSupport::and_log_failure_with_message(
$crate::verify_true!($condition),
|| format!($($format_args),*));
}};
}
pub use expect_true;
Expand Down Expand Up @@ -602,9 +602,9 @@ macro_rules! expect_false {
$crate::GoogleTestSupport::and_log_failure(($crate::verify_false!($condition)))
}};
($condition:expr, $($format_args:expr),* $(,)?) => {{
$crate::GoogleTestSupport::and_log_failure($crate::verify_false!($condition)
.with_failure_message(|| format!($($format_args),*))
);
$crate::GoogleTestSupport::and_log_failure_with_message(
$crate::verify_false!($condition),
|| format!($($format_args),*))
}};
}
pub use expect_false;
Expand Down Expand Up @@ -731,25 +731,25 @@ macro_rules! expect_eq {
$crate::GoogleTestSupport::and_log_failure($crate::verify_eq!($actual, [$($expected),*]));
}};
($actual:expr, [$($expected:expr),+ $(,)?], $($format_args:expr),* $(,)?) => {{
$crate::GoogleTestSupport::and_log_failure($crate::verify_eq!($actual, [$($expected),*])
.with_failure_message(|| format!($($format_args),*))
);
$crate::GoogleTestSupport::and_log_failure_with_message(
$crate::verify_eq!($actual, [$($expected),*]),
|| format!($($format_args),*));
}};
($actual:expr, {$($expected:expr),+ $(,)?} $(,)?) => {{
$crate::GoogleTestSupport::and_log_failure($crate::verify_eq!($actual, {$($expected),*}));
}};
($actual:expr, {$($expected:expr),+ $(,)?}, $($format_args:expr),* $(,)?) => {{
$crate::GoogleTestSupport::and_log_failure($crate::verify_eq!($actual, {$($expected),*})
.with_failure_message(|| format!($($format_args),*))
);
$crate::GoogleTestSupport::and_log_failure_with_message(
$crate::verify_eq!($actual, {$($expected),*}),
|| format!($($format_args),*));
}};
($actual:expr, $expected:expr $(,)?) => {{
$crate::GoogleTestSupport::and_log_failure($crate::verify_eq!($actual, $expected));
}};
($actual:expr, $expected:expr, $($format_args:expr),* $(,)?) => {{
$crate::GoogleTestSupport::and_log_failure($crate::verify_eq!($actual, $expected)
.with_failure_message(|| format!($($format_args),*))
);
$crate::GoogleTestSupport::and_log_failure_with_message(
$crate::verify_eq!($actual, $expected),
|| format!($($format_args),*));
}};
}
pub use expect_eq;
Expand Down Expand Up @@ -822,9 +822,9 @@ pub use verify_ne;
#[macro_export]
macro_rules! expect_ne {
($actual:expr, $expected:expr, $($format_args:expr),+ $(,)?) => {{
$crate::GoogleTestSupport::and_log_failure($crate::verify_ne!($actual, $expected)
.with_failure_message(|| format!($($format_args),*))
);
$crate::GoogleTestSupport::and_log_failure_with_message(
$crate::verify_ne!($actual, $expected),
|| format!($($format_args),*));
}};
($actual:expr, $expected:expr $(,)?) => {{
$crate::GoogleTestSupport::and_log_failure($crate::verify_ne!($actual, $expected));
Expand Down Expand Up @@ -900,9 +900,9 @@ pub use verify_lt;
#[macro_export]
macro_rules! expect_lt {
($actual:expr, $expected:expr, $($format_args:expr),+ $(,)?) => {{
$crate::GoogleTestSupport::and_log_failure($crate::verify_lt!($actual, $expected)
.with_failure_message(|| format!($($format_args),*))
);
$crate::GoogleTestSupport::and_log_failure_with_message(
$crate::verify_lt!($actual, $expected),
|| format!($($format_args),*));
}};
($actual:expr, $expected:expr $(,)?) => {{
$crate::GoogleTestSupport::and_log_failure($crate::verify_lt!($actual, $expected));
Expand Down Expand Up @@ -979,9 +979,9 @@ pub use verify_le;
#[macro_export]
macro_rules! expect_le {
($actual:expr, $expected:expr, $($format_args:expr),+ $(,)?) => {{
$crate::GoogleTestSupport::and_log_failure($crate::verify_le!($actual, $expected)
.with_failure_message(|| format!($($format_args),*))
);
$crate::GoogleTestSupport::and_log_failure_with_message(
$crate::verify_le!($actual, $expected),
|| format!($($format_args),*));
}};
($actual:expr, $expected:expr $(,)?) => {{
$crate::GoogleTestSupport::and_log_failure($crate::verify_le!($actual, $expected));
Expand Down Expand Up @@ -1057,9 +1057,9 @@ pub use verify_gt;
#[macro_export]
macro_rules! expect_gt {
($actual:expr, $expected:expr, $($format_args:expr),+ $(,)?) => {{
$crate::GoogleTestSupport::and_log_failure($crate::verify_gt!($actual, $expected)
.with_failure_message(|| format!($($format_args),*))
);
$crate::GoogleTestSupport::and_log_failure_with_message(
$crate::verify_gt!($actual, $expected),
|| format!($($format_args),*));
}};
($actual:expr, $expected:expr $(,)?) => {{
$crate::GoogleTestSupport::and_log_failure($crate::verify_gt!($actual, $expected));
Expand Down Expand Up @@ -1137,9 +1137,9 @@ pub use verify_ge;
#[macro_export]
macro_rules! expect_ge {
($actual:expr, $expected:expr, $($format_args:expr),+ $(,)?) => {{
$crate::GoogleTestSupport::and_log_failure($crate::verify_ge!($actual, $expected)
.with_failure_message(|| format!($($format_args),*))
);
$crate::GoogleTestSupport::and_log_failure_with_message(
$crate::verify_ge!($actual, $expected),
|| format!($($format_args),*));
}};
($actual:expr, $expected:expr $(,)?) => {{
$crate::GoogleTestSupport::and_log_failure($crate::verify_ge!($actual, $expected));
Expand Down Expand Up @@ -1232,9 +1232,9 @@ pub use verify_float_eq;
#[macro_export]
macro_rules! expect_float_eq {
($actual:expr, $expected:expr, $($format_args:expr),+ $(,)?) => {{
$crate::GoogleTestSupport::and_log_failure($crate::verify_float_eq!($actual, $expected)
.with_failure_message(|| format!($($format_args),*))
);
$crate::GoogleTestSupport::and_log_failure_with_message(
$crate::verify_float_eq!($actual, $expected),
|| format!($($format_args),*));
}};
($actual:expr, $expected:expr $(,)?) => {{
$crate::GoogleTestSupport::and_log_failure($crate::verify_float_eq!($actual, $expected));
Expand Down Expand Up @@ -1313,9 +1313,9 @@ pub use verify_near;
#[macro_export]
macro_rules! expect_near {
($actual:expr, $expected:expr, $max_abs_error:expr, $($format_args:expr),+ $(,)?) => {{
$crate::GoogleTestSupport::and_log_failure($crate::verify_near!($actual, $expected, $max_abs_error)
.with_failure_message(|| format!($($format_args),*))
);
$crate::GoogleTestSupport::and_log_failure_with_message(
$crate::verify_near!($actual, $expected, $max_abs_error),
|| format!($($format_args),*));
}};
($actual:expr, $expected:expr, $max_abs_error:expr $(,)?) => {{
$crate::GoogleTestSupport::and_log_failure($crate::verify_near!($actual, $expected, $max_abs_error));
Expand Down Expand Up @@ -1394,8 +1394,9 @@ macro_rules! assert_that {

// w/ format args, specialized to sequence:
($actual:expr, [ $($expected:expr),* ], $($format_args:expr),* $(,)?) => {
match $crate::verify_that!($actual, [ $($expected),* ])
.with_failure_message(|| format!($($format_args),*))
match $crate::GoogleTestSupport::with_failure_message(
$crate::verify_that!($actual, [ $($expected),* ]),
|| format!($($format_args),*))
{
Ok(_) => {}
Err(e) => {
Expand All @@ -1408,8 +1409,9 @@ macro_rules! assert_that {

// w/ format args, specialized to unordered sequence:
($actual:expr, { $($expected:expr),* }, $($format_args:expr),* $(,)?) => {
match $crate::verify_that!($actual, { $($expected),* })
.with_failure_message(|| format!($($format_args),*))
match $crate::GoogleTestSupport::with_failure_message(
$crate::verify_that!($actual, { $($expected),* }),
|| format!($($format_args),*))
{
Ok(_) => {}
Err(e) => {
Expand All @@ -1434,8 +1436,9 @@ macro_rules! assert_that {

// w/ format args, general case:
($actual:expr, $expected:expr, $($format_args:expr),* $(,)?) => {
match $crate::verify_that!($actual, $expected)
.with_failure_message(|| format!($($format_args),*))
match $crate::GoogleTestSupport::with_failure_message(
$crate::verify_that!($actual, $expected),
|| format!($($format_args),*))
{
Ok(_) => {}
Err(e) => {
Expand Down Expand Up @@ -1490,8 +1493,10 @@ macro_rules! assert_pred {

// w/ format args
($content:expr $(,)?, $($format_args:expr),* $(,)?) => {
match $crate::verify_pred!($content)
.with_failure_message(|| format!($($format_args),*)) {
match $crate::GoogleTestSupport::with_failure_message(
$crate::verify_pred!($content),
|| format!($($format_args),*)
) {
Ok(_) => {}
Err(e) => {
// The extra newline before the assertion failure message makes the failure a
Expand Down Expand Up @@ -1559,16 +1564,16 @@ macro_rules! expect_that {

// w/ format args, specialized to sequence:
($actual:expr, [$($expected:expr),*], $($format_args:expr),* $(,)?) => {
$crate::GoogleTestSupport::and_log_failure($crate::verify_that!($actual, [$($expected),*])
.with_failure_message(|| format!($($format_args),*))
)
$crate::GoogleTestSupport::and_log_failure_with_message(
$crate::verify_that!($actual, [$($expected),*]),
|| format!($($format_args),*))
};

// w/ format args, specialized to unordered sequence:
($actual:expr, {$($expected:expr),*}, $($format_args:expr),* $(,)?) => {
$crate::GoogleTestSupport::and_log_failure($crate::verify_that!($actual, {$($expected),*})
.with_failure_message(|| format!($($format_args),*))
)
$crate::GoogleTestSupport::and_log_failure_with_message(
$crate::verify_that!($actual, {$($expected),*}),
|| format!($($format_args),*))
};

// general case:
Expand All @@ -1578,9 +1583,9 @@ macro_rules! expect_that {

// w/ format args, general case:
($actual:expr, $expected:expr, $($format_args:expr),* $(,)?) => {
$crate::GoogleTestSupport::and_log_failure($crate::verify_that!($actual, $expected)
.with_failure_message(|| format!($($format_args),*))
)
$crate::GoogleTestSupport::and_log_failure_with_message(
$crate::verify_that!($actual, $expected),
|| format!($($format_args),*))
};
}
pub use expect_that;
Expand Down Expand Up @@ -1628,9 +1633,9 @@ macro_rules! expect_pred {
}};
// w/ format args
($content:expr $(,)?, $($format_args:expr),* $(,)?) => {
$crate::GoogleTestSupport::and_log_failure($crate::verify_pred!($content)
.with_failure_message(|| format!($($format_args),*))
)
$crate::GoogleTestSupport::and_log_failure_with_message(
$crate::verify_pred!($content),
|| format!($($format_args),*))
};
}
pub use expect_pred;
Expand Down
25 changes: 25 additions & 0 deletions googletest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,31 @@ pub trait GoogleTestSupport {
/// ```
fn and_log_failure(self);

/// If `self` is a `Result::Err`, writes to `stdout` with a failure report
/// and the message returned by `provider`.
///
/// This is equivalent to combining [`GoogleTestSupport::and_log_failure`]
/// with a call to [`GoogleTestSupport::with_failure_message`].
///
/// Example:
///
/// ```
/// # use googletest::GoogleTestSupport;
/// # use googletest::assertions::verify_eq;
/// # use googletest::internal::test_outcome::TestOutcome;
/// # TestOutcome::init_current_test_outcome();
/// let actual = 0;
/// verify_eq!(actual, 42)
/// .and_log_failure_with_message(|| format!("Actual {} was wrong!", actual));
/// # TestOutcome::close_current_test_outcome::<&str>(Ok(())).unwrap_err();
/// ```
fn and_log_failure_with_message(self, provider: impl FnOnce() -> String)
where
Self: Sized,
{
self.with_failure_message(provider).and_log_failure();
}

/// Adds `message` to the logged failure message if `self` is a
/// `Result::Err`. Otherwise, does nothing.
///
Expand Down
5 changes: 4 additions & 1 deletion integration_tests/src/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2002,6 +2002,9 @@ mod tests {
contains_substring("test tests::expect_false_works ... ok"),
contains_substring("test tests::verify_eq_works ... ok"),
contains_substring("test tests::expect_eq_works ... ok"),
contains_substring(
"test tests::expect_eq_with_message_compiles_without_trait_imports ... ok"
),
contains_substring("test tests::verify_ne_works ... ok"),
contains_substring("test tests::expect_ne_works ... ok"),
contains_substring("test tests::verify_lt_works ... ok"),
Expand All @@ -2020,7 +2023,7 @@ mod tests {
contains_substring("test tests::assert_pred_works ... ok"),
contains_substring("test tests::expect_that_works ... ok"),
contains_substring("test tests::expect_pred_works ... ok"),
contains_substring("test result: FAILED. 27 passed; 3 failed;")
contains_substring("test result: FAILED. 28 passed; 3 failed;")
)
)
}
Expand Down
5 changes: 5 additions & 0 deletions integration_tests/src/macro_hygiene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ mod tests {
googletest::expect_eq!(2 + 2, 4);
}

#[gtest]
fn expect_eq_with_message_compiles_without_trait_imports() {
googletest::expect_eq!(1, 1, "Some message.");
}

#[gtest]
fn verify_ne_works() -> googletest::Result<()> {
googletest::verify_ne!(2 + 2, 5)
Expand Down
Loading