Provide overloads for {Unordered}RangeEquals taking a std::initializer_list

This allows writing something like

  const auto v = calculateSomeVectorOfInts();
  CHECK_THAT(v, RangeEquals({1, 2, 3}));

Fixes #2915.
This commit is contained in:
Stefan Haller
2024-10-12 17:18:59 +02:00
committed by Martin Hořeňovský
parent 1e0ccb1b21
commit 69d62abc9a
15 changed files with 238 additions and 11 deletions

View File

@@ -727,6 +727,15 @@ TEST_CASE( "Usage of RangeEquals range matcher", "[matchers][templated][quantifi
} ) );
}
SECTION( "Compare against std::initializer_list" ) {
const std::array<int, 3> array_a{ { 1, 2, 3 } };
REQUIRE_THAT( array_a, RangeEquals( { 1, 2, 3 } ) );
REQUIRE_THAT( array_a, RangeEquals( { 2, 4, 6 }, []( int l, int r ) {
return l * 2 == r;
} ) );
}
SECTION("Check short-circuiting behaviour") {
with_mocked_iterator_access<int> const mocked1{ 1, 2, 3, 4 };
@@ -820,6 +829,16 @@ TEST_CASE( "Usage of UnorderedRangeEquals range matcher",
REQUIRE_THAT( needs_adl1, UnorderedRangeEquals( needs_adl2 ) );
}
SECTION( "Compare against std::initializer_list" ) {
const std::array<int, 3> array_a{ { 1, 10, 20 } };
REQUIRE_THAT( array_a, UnorderedRangeEquals( { 10, 20, 1 } ) );
REQUIRE_THAT( array_a,
UnorderedRangeEquals( { 11, 21, 2 }, []( int l, int r ) {
return std::abs( l - r ) <= 1;
} ) );
}
}
/**