Consider that I have two strings:
std::string s1 = "ab";
std::string s2 = "cd";
and I want to check (e.g. using EXPECT_EQ) if some given std::string str
is equal either to s1
or s2
.
If gtest's ASSERT_*
and EXPECT_*
would return bool
I could have written
EXPECT_TRUE(EXPECT_EQ(str, s1) || EXPECT_EQ(str, s2));
but, unfortunately, they don't.
There is one problem with EXPECT_TRUE
in this case. In gtest's doc it is described as:
sometimes a user has to use EXPECT_TRUE() to check a complex expression, for lack of a better macro. This has the problem of not showing you the values of the parts of the expression, making it hard to understand what went wrong.
So it is suggested to use EXPECT_PRED
:
TEST(CompareStr, Test1) {
std::string s1 = "ab";
std::string s2 = "cd";
std::string str;
EXPECT_PRED3([](auto str, auto s1, auto s2) {
return str == s1 || str == s2;}, str, s1, s2);
}
It gives a bit better diagnostic if a unittest fails:
[ RUN ] CompareStr.Test1
Test.cpp:5: Failure
[](auto str, auto s1, auto s2) { return str == s1 || str == s2;}(str, s1, s2) evaluates to false, where
str evaluates to
s1 evaluates to ab
s2 evaluates to cd
You can compare the message above with the output from EXPECT_TRUE
:
Value of: s1 == str || s2 == str
Actual: false
Expected: true