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
119 changes: 119 additions & 0 deletions googletest/src/assertions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,66 @@
}
pub use expect_true;

/// Marks the test as failed and panics if the given value is `false`.
///
/// This is a *fatal* assertion: the test panics
/// in the event of assertion failure.
///
/// This can only be invoked inside tests with the
/// [`gtest`][crate::gtest] attribute. The assertion must
/// occur in the same thread as that running the test itself.
///
/// Example:
///
/// ```ignore
/// use googletest::prelude::*;
///
/// #[gtest]
/// fn should_fail() {
/// assert_true!(false); // panic!
/// }
/// ```
///
/// One may optionally add arguments which will be formatted and appended to a
/// failure message. For example:
///
/// ```ignore
/// use googletest::prelude::*;
///
/// #[gtest]
/// fn should_fail() {
/// let extra_information = "Some additional information";
/// assert_true!(false, "Test failed. Extra information: {extra_information}.");
/// }
/// ```
///
/// The output is as follows:
///
/// ```text
/// Value of: false
/// Expected: is equal to true
/// Actual: false,
/// which isn't equal to true
/// Test failed. Extra information: Some additional information.
/// ```
#[macro_export]
macro_rules! assert_true {
($condition:expr) => {
if let Err(e) = $crate::verify_true!($condition) {
panic!("\n{}", e);
}
};
($condition:expr, $($format_args:expr),* $(,)?) => {
if let Err(e) = $crate::GoogleTestSupport::with_failure_message(
$crate::verify_true!($condition),
|| format!($($format_args),*))
{
panic!("\n{}", e);
}
};
}
pub use assert_true;

/// Verify if the condition evaluates to false and returns `Result`.
///
/// Evaluates to `Result::Ok(())` if the condition is false and
Expand Down Expand Up @@ -613,6 +673,65 @@
}
pub use expect_false;

/// Marks the test as failed and panics if the given value is `true`.
///
/// This is a *fatal* assertion: the test panics
/// in the event of assertion failure.
///
/// This can only be invoked inside tests with the
/// [`gtest`][crate::gtest] attribute. The assertion must
/// occur in the same thread as that running the test itself.
///
/// Example:
/// ```ignore
/// use googletest::prelude::*;
///
/// #[gtest]
/// fn should_fail() {
/// assert_false!(true); // panic!
/// }
/// ```
///
/// One may optionally add arguments which will be formatted and appended to a
/// failure message. For example:
///
/// ``` ignore
/// use googletest::prelude::*;
///
/// #[gtest]
/// fn should_fail() {
/// let extra_information = "Some additional information";
/// assert_false!(true, "Test failed. Extra information: {extra_information}.");
/// }
/// ```
///
/// The output is as follows:
///
/// ```text
/// Value of: true
/// Expected: is equal to false
/// Actual: true,
/// which isn't equal to false
/// Test failed. Extra information: Some additional information.
/// ```
#[macro_export]
macro_rules! assert_false {
($condition:expr) => {
if let Err(e) = $crate::verify_false!($condition) {
panic!("\n{}", e);
}
};
($condition:expr, $($format_args:expr),* $(,)?) => {
if let Err(e) = $crate::GoogleTestSupport::with_failure_message(
$crate::verify_false!($condition),
|| format!($($format_args),*))
{
panic!("\n{}", e);
}
};
}
pub use assert_false;

/// Checks whether the second argument is equal to the first argument.
///
/// Evaluates to `Result::Ok(())` if they are equal and
Expand Down Expand Up @@ -1713,7 +1832,7 @@
mod tests {
use crate::{
self as googletest,
assertions::{verify_eq, verify_that},

Check warning on line 1835 in googletest/src/assertions.rs

View workflow job for this annotation

GitHub Actions / clippy

unused imports: `verify_eq` and `verify_that`

warning: unused imports: `verify_eq` and `verify_that` --> googletest/src/assertions.rs:1835:22 | 1835 | assertions::{verify_eq, verify_that}, | ^^^^^^^^^ ^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` (part of `#[warn(unused)]`) on by default
matchers::{anything, err},
test, Result as TestResult,
};
Expand Down
10 changes: 5 additions & 5 deletions googletest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ pub mod prelude {
pub use super::Result;
// Assert macros
pub use super::{
add_failure, add_failure_at, assert_pred, assert_that, expect_eq, expect_false,
expect_float_eq, expect_ge, expect_gt, expect_le, expect_lt, expect_ne, expect_near,
expect_pred, expect_that, expect_true, fail, succeed, verify_eq, verify_false,
verify_float_eq, verify_ge, verify_gt, verify_le, verify_lt, verify_ne, verify_near,
verify_pred, verify_that, verify_true,
add_failure, add_failure_at, assert_false, assert_pred, assert_that, assert_true,
expect_eq, expect_false, expect_float_eq, expect_ge, expect_gt, expect_le, expect_lt,
expect_ne, expect_near, expect_pred, expect_that, expect_true, fail, succeed, verify_eq,
verify_false, verify_float_eq, verify_ge, verify_gt, verify_le, verify_lt, verify_ne,
verify_near, verify_pred, verify_that, verify_true,
};
}

Expand Down
Loading