From c8db4e77c4c9aa20776db162e982f1af693ab8fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Wed, 9 Oct 2019 14:51:24 +0200 Subject: [PATCH] Add a from_range(Container) overload to the generator helpers --- docs/generators.md | 1 + .../internal/catch_generators_specific.hpp | 7 + .../Baselines/compact.sw.approved.txt | 8 + .../Baselines/console.std.approved.txt | 2 +- .../Baselines/console.sw.approved.txt | 97 ++++++++- .../SelfTest/Baselines/junit.sw.approved.txt | 6 +- .../SelfTest/Baselines/xml.sw.approved.txt | 203 +++++++++++++----- .../SelfTest/UsageTests/Generators.tests.cpp | 29 ++- 8 files changed, 293 insertions(+), 60 deletions(-) diff --git a/docs/generators.md b/docs/generators.md index 934a6731..34565a50 100644 --- a/docs/generators.md +++ b/docs/generators.md @@ -72,6 +72,7 @@ type, making their usage much nicer. These are * `range(start, end)` for `RangeGenerator` with a step size of `1` * `range(start, end, step)` for `RangeGenerator` with a custom step size * `from_range(InputIterator from, InputIterator to)` for `IteratorGenerator` +* `from_range(Container const&)` for `IteratorGenerator` > `chunk()`, `random()` and both `range()` functions were introduced in Catch 2.7.0. diff --git a/include/internal/catch_generators_specific.hpp b/include/internal/catch_generators_specific.hpp index 0734ee78..f57d29b6 100644 --- a/include/internal/catch_generators_specific.hpp +++ b/include/internal/catch_generators_specific.hpp @@ -161,6 +161,13 @@ GeneratorWrapper from_range(InputIterator from, InputSentinel to) { return GeneratorWrapper(pf::make_unique>(from, to)); } +template +GeneratorWrapper from_range(Container const& cnt) { + return GeneratorWrapper(pf::make_unique>(cnt.begin(), cnt.end())); +} + + } // namespace Generators } // namespace Catch diff --git a/projects/SelfTest/Baselines/compact.sw.approved.txt b/projects/SelfTest/Baselines/compact.sw.approved.txt index ac775b4f..9a5ce16f 100644 --- a/projects/SelfTest/Baselines/compact.sw.approved.txt +++ b/projects/SelfTest/Baselines/compact.sw.approved.txt @@ -320,6 +320,14 @@ Generators.tests.cpp:: passed: elem % 2 == 1 for: 1 == 1 Generators.tests.cpp:: passed: elem % 2 == 1 for: 1 == 1 Generators.tests.cpp:: passed: elem % 2 == 1 for: 1 == 1 Generators.tests.cpp:: passed: elem % 2 == 1 for: 1 == 1 +Generators.tests.cpp:: passed: elem % 2 == 1 for: 1 == 1 +Generators.tests.cpp:: passed: elem % 2 == 1 for: 1 == 1 +Generators.tests.cpp:: passed: elem % 2 == 1 for: 1 == 1 +Generators.tests.cpp:: passed: elem % 2 == 1 for: 1 == 1 +Generators.tests.cpp:: passed: elem % 2 == 1 for: 1 == 1 +Generators.tests.cpp:: passed: elem % 2 == 1 for: 1 == 1 +Generators.tests.cpp:: passed: call_count == 1 for: 1 == 1 +Generators.tests.cpp:: passed: make_data().size() == test_count for: 6 == 6 Exception.tests.cpp:: failed: unexpected exception with message: 'custom exception - not std'; expression was: throwCustom() Exception.tests.cpp:: failed: unexpected exception with message: 'custom exception - not std'; expression was: throwCustom(), std::exception Exception.tests.cpp:: failed: unexpected exception with message: 'custom std exception' diff --git a/projects/SelfTest/Baselines/console.std.approved.txt b/projects/SelfTest/Baselines/console.std.approved.txt index 0bd5c84f..5a3fb409 100644 --- a/projects/SelfTest/Baselines/console.std.approved.txt +++ b/projects/SelfTest/Baselines/console.std.approved.txt @@ -1381,5 +1381,5 @@ due to unexpected exception with message: =============================================================================== test cases: 303 | 229 passed | 70 failed | 4 failed as expected -assertions: 1589 | 1437 passed | 131 failed | 21 failed as expected +assertions: 1597 | 1445 passed | 131 failed | 21 failed as expected diff --git a/projects/SelfTest/Baselines/console.sw.approved.txt b/projects/SelfTest/Baselines/console.sw.approved.txt index d491697f..1e290592 100644 --- a/projects/SelfTest/Baselines/console.sw.approved.txt +++ b/projects/SelfTest/Baselines/console.sw.approved.txt @@ -2409,6 +2409,7 @@ with expansion: ------------------------------------------------------------------------------- Copy and then generate a range + from var and iterators ------------------------------------------------------------------------------- Generators.tests.cpp: ............................................................................... @@ -2420,6 +2421,7 @@ with expansion: ------------------------------------------------------------------------------- Copy and then generate a range + from var and iterators ------------------------------------------------------------------------------- Generators.tests.cpp: ............................................................................... @@ -2431,6 +2433,7 @@ with expansion: ------------------------------------------------------------------------------- Copy and then generate a range + from var and iterators ------------------------------------------------------------------------------- Generators.tests.cpp: ............................................................................... @@ -2442,6 +2445,7 @@ with expansion: ------------------------------------------------------------------------------- Copy and then generate a range + from var and iterators ------------------------------------------------------------------------------- Generators.tests.cpp: ............................................................................... @@ -2453,6 +2457,7 @@ with expansion: ------------------------------------------------------------------------------- Copy and then generate a range + from var and iterators ------------------------------------------------------------------------------- Generators.tests.cpp: ............................................................................... @@ -2464,6 +2469,7 @@ with expansion: ------------------------------------------------------------------------------- Copy and then generate a range + from var and iterators ------------------------------------------------------------------------------- Generators.tests.cpp: ............................................................................... @@ -2473,6 +2479,95 @@ Generators.tests.cpp:: PASSED: with expansion: 1 == 1 +------------------------------------------------------------------------------- +Copy and then generate a range + From a temporary container +------------------------------------------------------------------------------- +Generators.tests.cpp: +............................................................................... + +Generators.tests.cpp:: PASSED: + REQUIRE( elem % 2 == 1 ) +with expansion: + 1 == 1 + +------------------------------------------------------------------------------- +Copy and then generate a range + From a temporary container +------------------------------------------------------------------------------- +Generators.tests.cpp: +............................................................................... + +Generators.tests.cpp:: PASSED: + REQUIRE( elem % 2 == 1 ) +with expansion: + 1 == 1 + +------------------------------------------------------------------------------- +Copy and then generate a range + From a temporary container +------------------------------------------------------------------------------- +Generators.tests.cpp: +............................................................................... + +Generators.tests.cpp:: PASSED: + REQUIRE( elem % 2 == 1 ) +with expansion: + 1 == 1 + +------------------------------------------------------------------------------- +Copy and then generate a range + From a temporary container +------------------------------------------------------------------------------- +Generators.tests.cpp: +............................................................................... + +Generators.tests.cpp:: PASSED: + REQUIRE( elem % 2 == 1 ) +with expansion: + 1 == 1 + +------------------------------------------------------------------------------- +Copy and then generate a range + From a temporary container +------------------------------------------------------------------------------- +Generators.tests.cpp: +............................................................................... + +Generators.tests.cpp:: PASSED: + REQUIRE( elem % 2 == 1 ) +with expansion: + 1 == 1 + +------------------------------------------------------------------------------- +Copy and then generate a range + From a temporary container +------------------------------------------------------------------------------- +Generators.tests.cpp: +............................................................................... + +Generators.tests.cpp:: PASSED: + REQUIRE( elem % 2 == 1 ) +with expansion: + 1 == 1 + +------------------------------------------------------------------------------- +Copy and then generate a range + Final validation +------------------------------------------------------------------------------- +Generators.tests.cpp: +............................................................................... + +Generators.tests.cpp:: PASSED: + REQUIRE( call_count == 1 ) +with expansion: + 1 == 1 + +Generators.tests.cpp:: PASSED: + REQUIRE( make_data().size() == test_count ) +with expansion: + 6 == 6 + ------------------------------------------------------------------------------- Custom exceptions can be translated when testing for nothrow ------------------------------------------------------------------------------- @@ -12711,5 +12806,5 @@ Misc.tests.cpp:: PASSED: =============================================================================== test cases: 303 | 213 passed | 86 failed | 4 failed as expected -assertions: 1606 | 1437 passed | 148 failed | 21 failed as expected +assertions: 1614 | 1445 passed | 148 failed | 21 failed as expected diff --git a/projects/SelfTest/Baselines/junit.sw.approved.txt b/projects/SelfTest/Baselines/junit.sw.approved.txt index a78ac477..ed1ef6b7 100644 --- a/projects/SelfTest/Baselines/junit.sw.approved.txt +++ b/projects/SelfTest/Baselines/junit.sw.approved.txt @@ -1,7 +1,7 @@ - + @@ -255,7 +255,9 @@ Matchers.tests.cpp: Matchers.tests.cpp: - + + + custom exception - not std diff --git a/projects/SelfTest/Baselines/xml.sw.approved.txt b/projects/SelfTest/Baselines/xml.sw.approved.txt index a05b0ec8..f61217b6 100644 --- a/projects/SelfTest/Baselines/xml.sw.approved.txt +++ b/projects/SelfTest/Baselines/xml.sw.approved.txt @@ -2852,54 +2852,157 @@ Nor would this - - - elem % 2 == 1 - - - 1 == 1 - - - - - elem % 2 == 1 - - - 1 == 1 - - - - - elem % 2 == 1 - - - 1 == 1 - - - - - elem % 2 == 1 - - - 1 == 1 - - - - - elem % 2 == 1 - - - 1 == 1 - - - - - elem % 2 == 1 - - - 1 == 1 - - +
+ + + elem % 2 == 1 + + + 1 == 1 + + + +
+
+ + + elem % 2 == 1 + + + 1 == 1 + + + +
+
+ + + elem % 2 == 1 + + + 1 == 1 + + + +
+
+ + + elem % 2 == 1 + + + 1 == 1 + + + +
+
+ + + elem % 2 == 1 + + + 1 == 1 + + + +
+
+ + + elem % 2 == 1 + + + 1 == 1 + + + +
+
+ + + elem % 2 == 1 + + + 1 == 1 + + + +
+
+ + + elem % 2 == 1 + + + 1 == 1 + + + +
+
+ + + elem % 2 == 1 + + + 1 == 1 + + + +
+
+ + + elem % 2 == 1 + + + 1 == 1 + + + +
+
+ + + elem % 2 == 1 + + + 1 == 1 + + + +
+
+ + + elem % 2 == 1 + + + 1 == 1 + + + +
+
+ + + call_count == 1 + + + 1 == 1 + + + + + make_data().size() == test_count + + + 6 == 6 + + + +
@@ -15117,7 +15220,7 @@ loose text artifact - + - + diff --git a/projects/SelfTest/UsageTests/Generators.tests.cpp b/projects/SelfTest/UsageTests/Generators.tests.cpp index 0d58186b..0e39bd5c 100644 --- a/projects/SelfTest/UsageTests/Generators.tests.cpp +++ b/projects/SelfTest/UsageTests/Generators.tests.cpp @@ -214,9 +214,15 @@ TEST_CASE("Nested generators and captured variables", "[generators]") { } namespace { + size_t call_count = 0; + size_t test_count = 0; std::vector make_data() { return { 1, 3, 5, 7, 9, 11 }; } + std::vector make_data_counted() { + ++call_count; + return make_data(); + } } #if defined(__clang__) @@ -225,13 +231,24 @@ namespace { #endif TEST_CASE("Copy and then generate a range", "[generators]") { - static auto data = make_data(); + SECTION("from var and iterators") { + static auto data = make_data(); - // It is important to notice that a generator is only initialized - // **once** per run. What this means is that modifying data will not - // modify the underlying generator. - auto elem = GENERATE_REF(from_range(data.begin(), data.end())); - REQUIRE(elem % 2 == 1); + // It is important to notice that a generator is only initialized + // **once** per run. What this means is that modifying data will not + // modify the underlying generator. + auto elem = GENERATE_REF(from_range(data.begin(), data.end())); + REQUIRE(elem % 2 == 1); + } + SECTION("From a temporary container") { + auto elem = GENERATE(from_range(make_data_counted())); + ++test_count; + REQUIRE(elem % 2 == 1); + } + SECTION("Final validation") { + REQUIRE(call_count == 1); + REQUIRE(make_data().size() == test_count); + } } #if defined(__clang__)