Skip to content

Commit

Permalink
Support elements without op!= in VectorEquals
Browse files Browse the repository at this point in the history
Closes #2648
  • Loading branch information
horenmar committed Mar 4, 2023
1 parent 69f35a5 commit 0a0ebf5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/catch2/matchers/catch_matchers_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,10 @@ namespace Matchers {
// - a more general approach would be via a compare template that defaults
// to using !=. but could be specialised for, e.g. std::vector<T> etc
// - then just call that directly
if (m_comparator.size() != v.size())
return false;
for (std::size_t i = 0; i < v.size(); ++i)
if (m_comparator[i] != v[i])
return false;
if ( m_comparator.size() != v.size() ) { return false; }
for ( std::size_t i = 0; i < v.size(); ++i ) {
if ( !( m_comparator[i] == v[i] ) ) { return false; }
}
return true;
}
std::string describe() const override {
Expand Down
19 changes: 19 additions & 0 deletions tests/SelfTest/UsageTests/Matchers.tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,25 @@ TEST_CASE( "Vector matchers that fail", "[matchers][vector][.][failing]" ) {
}
}

namespace {
struct SomeType {
int i;
friend bool operator==( SomeType lhs, SomeType rhs ) {
return lhs.i == rhs.i;
}
};
} // end anonymous namespace

TEST_CASE( "Vector matcher with elements without !=", "[matchers][vector][approvals]" ) {
std::vector<SomeType> lhs, rhs;
lhs.push_back( { 1 } );
lhs.push_back( { 2 } );
rhs.push_back( { 1 } );
rhs.push_back( { 1 } );

REQUIRE_THAT( lhs, !Equals(rhs) );
}

TEST_CASE( "Exception matchers that succeed",
"[matchers][exceptions][!throws]" ) {
CHECK_THROWS_MATCHES(
Expand Down

0 comments on commit 0a0ebf5

Please sign in to comment.