I have the following logic in one of my tests:
EXPECT_TRUE(a == x || b == y);
Is there a neat way to rewrite it to EXPECT_EQ
so that I can see the values a, b, x, y
in the failing test's output?
If it was conjunction EXPECT_TRUE(a == x && b == y);
I could trivially split it into EXPECT_EQ(a, x); EXPECT(b, y);
.
If it was repeating value on either side like EXPECT_TRUE(a == x || a == y);
then I could use gmock matchers EXPECT_THAT(a, testing::AnyOf(x, y));
.
I cannot figure out anything better than streaming the values into EXPECT_TRUE
with <<
, or duplicating the condition inside an if
:
if (a != x && b != y) {
EXPECT_EQ(a, x);
EXPECT_EQ(b, y);
}
If you are stubborn to use built-in gtest assertions and results printing, you can try ASSERT_PRED* family :
TEST(xxx, yyy) {
int a = 1;
int b = 2;
int x = 3;
int y = 4;
ASSERT_PRED4([](int a, int b, int x, int y) {
return a==x || b==y;
}, a, b, x, y);
}
This will print out the entire predicate body and each of the arguments, sth like:
Failure
[](int a, int b, int x, int y) { return a==x || b==y; }(a, b, x, y) evaluates to false, where
a evaluates to 1
b evaluates to 2
x evaluates to 3
y evaluates to 4
But I am not convinced that it's cleaner than <<
streaming the custom diagnostics to ASSERT_TRUE
, like suggested in the comments (with EXPECT_TRUE
):
EXPECT_TRUE(a == x || b == y) << a << b;