123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- // Copyright 2022 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- // TODO(danakj): Reuse code for comparison macros with an expect_op!() macro?
- /// Internal helper to log an expectation failure. Other expect_* macros invoke
- /// it with their standard expectation message, plus optionally a user-provided
- /// custom string.
- ///
- /// Both the the expectation message and the user-provided message are format
- /// strings with arguments. To disambiguate between them, the expectation
- /// message is wrapped in extra parentheses.
- #[macro_export]
- macro_rules! internal_add_expectation_failure {
- // Rule that both the below are forwarded to.
- (@imp $fmt:literal, $($arg:tt)+) => {
- $crate::__private::add_failure_at(
- file!(),
- line!(),
- &format!($fmt, $($arg)+),
- )
- };
- // Add a failure with the standard message.
- (($expectation:literal, $($e:tt)+)) => {
- $crate::internal_add_expectation_failure!(@imp $expectation, $($e)+)
- };
- // Add a failure with the standard message plus an additional message.
- (($expectation:literal, $($e:tt)+), $($arg:tt)+) => {
- $crate::internal_add_expectation_failure!(@imp
- "{}\n\n{}",
- format_args!($expectation, $($e)+),
- format_args!($($arg)+),
- )
- };
- }
- /// Evaluates the given expression. If false, a failure is reported to Gtest.
- ///
- /// # Examples
- /// ```
- /// expect_true(check_the_status_is_true());
- /// ```
- #[macro_export]
- macro_rules! expect_true {
- ($e:expr $(, $($arg:tt)*)?) => {
- match &$e {
- val => {
- if !(*val) {
- $crate::internal_add_expectation_failure!(
- (
- "Expected: {} is true\nActual: {} is {:?}",
- stringify!($e),
- stringify!($e),
- *val
- )
- $(, $($arg)*)?
- )
- }
- }
- }
- };
- }
- /// Evaluates the given expression. If true, a failure is reported to Gtest.
- ///
- /// # Examples
- /// ```
- /// expect_false(check_the_status_is_false());
- /// ```
- #[macro_export]
- macro_rules! expect_false {
- ($e:expr $(, $($arg:tt)*)?) => {
- match &$e {
- val => {
- if *val {
- $crate::internal_add_expectation_failure!(
- (
- "Expected: {} is false\nActual: {} is {:?}",
- stringify!($e),
- stringify!($e),
- *val
- )
- $(, $($arg)*)?
- )
- }
- }
- }
- };
- }
- /// Evaluates and compares the two given expressions. If not equal, a failure is reported to Gtest.
- /// The expressions must evaluate to the same type, which must be PartialEq.
- ///
- /// # Examples
- /// ```
- /// expect_eq(1 + 1, 2);
- /// ```
- #[macro_export]
- macro_rules! expect_eq {
- ($e1:expr, $e2:expr $(, $($arg:tt)*)?) => {
- match (&$e1, &$e2) {
- (val1, val2) => {
- if !(*val1 == *val2) {
- $crate::internal_add_expectation_failure!(
- (
- "Expected: {} == {}\nActual: {:?} vs {:?}",
- stringify!($e1),
- stringify!($e2),
- *val1,
- *val2
- )
- $(, $($arg)*)?
- )
- }
- }
- }
- };
- }
- /// Evaluates and compares the two given expressions. If equal, a failure is reported to Gtest.
- /// The expressions must evaluate to the same type, which must be PartialEq.
- ///
- /// # Examples
- /// ```
- /// expect_ne(1 + 1, 3);
- /// ```
- #[macro_export]
- macro_rules! expect_ne {
- ($e1:expr, $e2:expr $(, $($arg:tt)*)?) => {
- match (&$e1, &$e2) {
- (val1, val2) => {
- if !(*val1 != *val2) {
- $crate::internal_add_expectation_failure!(
- (
- "Expected: {} != {}\nActual: {:?} vs {:?}",
- stringify!($e1),
- stringify!($e2),
- *val1,
- *val2
- )
- $(, $($arg)*)?
- )
- }
- }
- }
- };
- }
- /// Evaluates and compares the two given expressions. If the first is not greater than the second, a
- /// failure is reported to Gtest. The expressions must evaluate to the same type, which must be
- /// PartialOrd. If a PartialOrd comparison fails, the result is false.
- ///
- /// # Examples
- /// ```
- /// expect_gt(1 + 1, 1);
- /// ```
- #[macro_export]
- macro_rules! expect_gt {
- ($e1:expr, $e2:expr $(, $($arg:tt)*)?) => {
- match (&$e1, &$e2) {
- (val1, val2) => {
- if !(*val1 > *val2) {
- $crate::internal_add_expectation_failure!(
- (
- "Expected: {} > {}\nActual: {:?} vs {:?}",
- stringify!($e1),
- stringify!($e2),
- *val1,
- *val2
- )
- $(, $($arg)*)?
- )
- }
- }
- }
- };
- }
- /// Evaluates and compares the two given expressions. If the first is not less than the second, a
- /// failure is reported to Gtest. The expressions must evaluate to the same type, which must be
- /// PartialOrd. If a PartialOrd comparison fails, the result is false.
- ///
- /// # Examples
- /// ```
- /// expect_lt(1 + 1, 1 + 2);
- /// ```
- #[macro_export]
- macro_rules! expect_lt {
- ($e1:expr, $e2:expr $(, $($arg:tt)*)?) => {
- match (&$e1, &$e2) {
- (val1, val2) => {
- if !(*val1 < *val2) {
- $crate::internal_add_expectation_failure!(
- (
- "Expected: {} < {}\nActual: {:?} vs {:?}",
- stringify!($e1),
- stringify!($e2),
- *val1,
- *val2
- )
- $(, $($arg)*)?
- )
- }
- }
- }
- };
- }
- /// Evaluates and compares the two given expressions. If the first is not greater than or equal to
- /// the second, a failure is reported to Gtest. The expressions must evaluate to the same type,
- /// which must be PartialOrd. If a PartialOrd comparison fails, the result is false.
- ///
- /// # Examples
- /// ```
- /// expect_ge(1 + 1, 2);
- /// ```
- #[macro_export]
- macro_rules! expect_ge {
- ($e1:expr, $e2:expr $(, $($arg:tt)*)?) => {
- match (&$e1, &$e2) {
- (val1, val2) => {
- if !(*val1 >= *val2) {
- $crate::internal_add_expectation_failure!(
- (
- "Expected: {} >= {}\nActual: {:?} vs {:?}",
- stringify!($e1),
- stringify!($e2),
- *val1,
- *val2
- ) $(, $($arg)*)?
- )
- }
- }
- }
- };
- }
- /// Evaluates and compares the two given expressions. If the first is not less than or equal to the
- /// second, a failure is reported to Gtest. The expressions must evaluate to the same type, /which
- /// must be PartialOrd. If a PartialOrd comparison fails, the result is false.
- ///
- /// # Examples
- /// ```
- /// expect_le(2, 1 + 1);
- /// ```
- #[macro_export]
- macro_rules! expect_le {
- ($e1:expr, $e2:expr $(, $($arg:tt)*)?) => {
- match (&$e1, &$e2) {
- (val1, val2) => {
- if !(*val1 <= *val2) {
- $crate::internal_add_expectation_failure!(
- (
- "Expected: {} <= {}\nActual: {:?} vs {:?}",
- stringify!($e1),
- stringify!($e2),
- *val1,
- *val2
- ) $(, $($arg)*)?
- )
- }
- }
- }
- };
- }
- // TODO(danakj): There's a bunch more useful macros to write, even ignoring gmock:
- // - EXPECT_NONFATAL_FAILURE
- // - SCOPED_TRACE
- // - EXPECT_DEATH
- // - FAIL (fatal failure, would this work?)
- // - ADD_FAILURE
- // - SUCCEED
- // - EXPECT_PANIC (catch_unwind() with an error if no panic, but this depends on us using a stdlib
- // with -Cpanic=unwind in tests, currently we use -Cpanic=abort.)
- // - EXPECT_NO_PANIC (Like above, depende on -Cpanic=unwind, or else all panics just abort the
- // process.)
- // - EXPECT_FLOAT_EQ (Comparison for equality within a small range.)
- // - EXPECT_NEAR (Comparison for equality within a user-specified range.)
|