Skip to content

Commit

Permalink
Print mismatches for UnorderedElements() of different sizes.
Browse files Browse the repository at this point in the history
Changes the behavior of UnorderedElements()/UnorderedElementsAreArray() to print items-without-matchers and matchers-without-items in the case where the actual and expected are different sizes.

PiperOrigin-RevId: 635451316
Change-Id: I2181bb28a14c14cdb577af9268d403e12e942bea
  • Loading branch information
Abseil Team authored and Copybara-Service committed May 20, 2024
1 parent 33af80a commit c8393f8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
27 changes: 13 additions & 14 deletions googlemock/src/gmock-matchers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,8 @@ static void LogElementMatcherPairVec(const ElementMatcherPairs& pairs,
os << "{";
const char* sep = "";
for (Iter it = pairs.begin(); it != pairs.end(); ++it) {
os << sep << "\n ("
<< "element #" << it->first << ", "
<< "matcher #" << it->second << ")";
os << sep << "\n (" << "element #" << it->first << ", " << "matcher #"
<< it->second << ")";
sep = ",";
}
os << "\n}";
Expand Down Expand Up @@ -374,20 +373,20 @@ bool UnorderedElementsAreMatcherImplBase::VerifyMatchMatrix(
return true;
}

if (match_flags() == UnorderedMatcherRequire::ExactMatch) {
if (matrix.LhsSize() != matrix.RhsSize()) {
// The element count doesn't match. If the container is empty,
// there's no need to explain anything as Google Mock already
// prints the empty container. Otherwise we just need to show
// how many elements there actually are.
if (matrix.LhsSize() != 0 && listener->IsInterested()) {
*listener << "which has " << Elements(matrix.LhsSize());
}
return false;
const bool is_exact_match_with_size_discrepency =
match_flags() == UnorderedMatcherRequire::ExactMatch &&
matrix.LhsSize() != matrix.RhsSize();
if (is_exact_match_with_size_discrepency) {
// The element count doesn't match. If the container is empty,
// there's no need to explain anything as Google Mock already
// prints the empty container. Otherwise we just need to show
// how many elements there actually are.
if (matrix.LhsSize() != 0 && listener->IsInterested()) {
*listener << "which has " << Elements(matrix.LhsSize()) << "\n";
}
}

bool result = true;
bool result = !is_exact_match_with_size_discrepency;
::std::vector<char> element_matched(matrix.LhsSize(), 0);
::std::vector<char> matcher_matched(matrix.RhsSize(), 0);

Expand Down
17 changes: 14 additions & 3 deletions googlemock/test/gmock-matchers-containers_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2014,15 +2014,26 @@ TEST_F(UnorderedElementsAreTest, FailMessageCountWrong) {
StringMatchResultListener listener;
EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3), v, &listener))
<< listener.str();
EXPECT_THAT(listener.str(), Eq("which has 1 element"));
EXPECT_THAT(listener.str(),
Eq("which has 1 element\n"
"where the following matchers don't match any elements:\n"
"matcher #0: is equal to 1,\n"
"matcher #1: is equal to 2,\n"
"matcher #2: is equal to 3\n"
"and where the following elements don't match any matchers:\n"
"element #0: 4"));
}

TEST_F(UnorderedElementsAreTest, FailMessageCountWrongZero) {
std::vector<int> v;
StringMatchResultListener listener;
EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3), v, &listener))
<< listener.str();
EXPECT_THAT(listener.str(), Eq(""));
EXPECT_THAT(listener.str(),
Eq("where the following matchers don't match any elements:\n"
"matcher #0: is equal to 1,\n"
"matcher #1: is equal to 2,\n"
"matcher #2: is equal to 3"));
}

TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedMatchers) {
Expand Down Expand Up @@ -2438,7 +2449,7 @@ TEST(UnorderedPointwiseTest, RejectsWrongSize) {
const double lhs[2] = {1, 2};
const int rhs[1] = {0};
EXPECT_THAT(lhs, Not(UnorderedPointwise(Gt(), rhs)));
EXPECT_EQ("which has 2 elements",
EXPECT_EQ("which has 2 elements\n",
Explain(UnorderedPointwise(Gt(), rhs), lhs));

const int rhs2[3] = {0, 1, 2};
Expand Down

0 comments on commit c8393f8

Please sign in to comment.