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
EXPECT_TRUE(str==s1 || str==s2)
? – Ulcerstr
didn't equals1
. – Wahkuna