From 78f14711098ccab7c402ff0f32fe5303eef907c8 Mon Sep 17 00:00:00 2001 From: DNKpp Date: Mon, 17 Jun 2024 20:40:25 +0200 Subject: [PATCH] refactor: remove Times and implement factories in terms of RuntimeTimes --- include/mimic++/ExpectationPolicies.hpp | 88 +++++++++++-------------- test/unit-tests/ExpectationPolicies.cpp | 49 ++------------ 2 files changed, 43 insertions(+), 94 deletions(-) diff --git a/include/mimic++/ExpectationPolicies.hpp b/include/mimic++/ExpectationPolicies.hpp index ce466132d..1cff45880 100644 --- a/include/mimic++/ExpectationPolicies.hpp +++ b/include/mimic++/ExpectationPolicies.hpp @@ -84,47 +84,6 @@ namespace mimicpp::expectation_policies } }; - template - requires (min <= max) - class Times - { - public: - [[nodiscard]] - constexpr bool is_satisfied() const noexcept - { - return min <= m_Count - && m_Count <= max; - } - - [[nodiscard]] - constexpr bool is_applicable() const noexcept - { - return m_Count < max; - } - - [[nodiscard]] - StringT describe_state() const - { - return detail::describe_times_state( - m_Count, - min, - max); - } - - constexpr void consume() noexcept - { - ++m_Count; - } - - private: - std::size_t m_Count{}; - }; - - class InitTimes - : public Times<1u, 1u> - { - }; - class RuntimeTimes { public: @@ -172,6 +131,17 @@ namespace mimicpp::expectation_policies std::size_t m_Count{}; }; + class InitTimes + : public RuntimeTimes + { + public: + [[nodiscard]] + consteval InitTimes() noexcept + : RuntimeTimes{1u, 1u} + { + } + }; + template class Category { @@ -521,36 +491,52 @@ namespace mimicpp::expectation_policies::detail namespace mimicpp::expect { template + requires (min <= max) [[nodiscard]] - consteval expectation_policies::Times times() noexcept + consteval auto times() noexcept { - return {}; + return expectation_policies::RuntimeTimes{ + min, + max + }; } template [[nodiscard]] - consteval expectation_policies::Times::max()> at_least() noexcept + consteval auto at_least() noexcept { - return {}; + return expectation_policies::RuntimeTimes{ + min, + std::numeric_limits::max() + }; } template [[nodiscard]] - consteval expectation_policies::Times<0u, max> at_most() noexcept + consteval auto at_most() noexcept { - return {}; + return expectation_policies::RuntimeTimes{ + 0u, + max + }; } [[nodiscard]] - consteval expectation_policies::Times<1u, 1u> once() noexcept + consteval auto once() noexcept { - return {}; + return expectation_policies::RuntimeTimes{ + 1u, + 1u + }; } [[nodiscard]] - consteval expectation_policies::Times<2u, 2u> twice() noexcept + consteval auto twice() noexcept { - return {}; + return expectation_policies::RuntimeTimes{ + 2u, + 2u + }; } [[nodiscard]] diff --git a/test/unit-tests/ExpectationPolicies.cpp b/test/unit-tests/ExpectationPolicies.cpp index a2848511a..6697fe76f 100644 --- a/test/unit-tests/ExpectationPolicies.cpp +++ b/test/unit-tests/ExpectationPolicies.cpp @@ -22,8 +22,6 @@ using namespace mimicpp; TEMPLATE_TEST_CASE( "Given types satisfy mimicpp::times_policy concept.", "[expectation][expectation::builder]", - (expectation_policies::Times<0, 1>), - (expectation_policies::Times<1, 1>), expectation_policies::RuntimeTimes, expectation_policies::InitTimes ) @@ -90,41 +88,6 @@ TEST_CASE( REQUIRE(!policy.is_applicable()); } -TEMPLATE_TEST_CASE_SIG( - "expectation_policies::Times is configurable at compile-time.", - "[expectation][expectation::builder]", - ((std::size_t min, std::size_t max), min, max), - (0, 0), - (0, 1), - (0, 2), - (1, 2), - (2, 2) -) -{ - expectation_policies::Times policy{}; - - for ([[maybe_unused]] auto i : std::views::iota(0u, min)) - { - REQUIRE(!policy.is_satisfied()); - REQUIRE(policy.is_applicable()); - - REQUIRE_NOTHROW(policy.consume()); - } - - REQUIRE(policy.is_satisfied()); - - for ([[maybe_unused]] auto i : std::views::iota(min, max)) - { - REQUIRE(policy.is_satisfied()); - REQUIRE(policy.is_applicable()); - - REQUIRE_NOTHROW(policy.consume()); - } - - REQUIRE(policy.is_satisfied()); - REQUIRE(!policy.is_applicable()); -} - TEST_CASE( "expectation_policies::Times is configurable at runtime.", "[expectation][expectation::builder]" @@ -1170,7 +1133,7 @@ TEST_CASE( { SECTION("times with binary limits.") { - expectation_policies::Times times = expect::times<2, 5>(); + expectation_policies::RuntimeTimes times = expect::times<2, 5>(); REQUIRE(!times.is_satisfied()); REQUIRE(times.is_applicable()); @@ -1190,7 +1153,7 @@ TEST_CASE( SECTION("times with unary limits.") { - expectation_policies::Times times = expect::times<3>(); + expectation_policies::RuntimeTimes times = expect::times<3>(); REQUIRE(!times.is_satisfied()); REQUIRE(times.is_applicable()); @@ -1208,7 +1171,7 @@ TEST_CASE( SECTION("at_most") { - expectation_policies::Times times = expect::at_most<3>(); + expectation_policies::RuntimeTimes times = expect::at_most<3>(); REQUIRE(times.is_satisfied()); REQUIRE(times.is_applicable()); @@ -1226,7 +1189,7 @@ TEST_CASE( SECTION("at_least") { - expectation_policies::Times times = expect::at_least<3>(); + expectation_policies::RuntimeTimes times = expect::at_least<3>(); REQUIRE(!times.is_satisfied()); REQUIRE(times.is_applicable()); @@ -1252,7 +1215,7 @@ TEST_CASE( SECTION("once") { - expectation_policies::Times times = expect::once(); + expectation_policies::RuntimeTimes times = expect::once(); REQUIRE(!times.is_satisfied()); REQUIRE(times.is_applicable()); @@ -1264,7 +1227,7 @@ TEST_CASE( SECTION("twice") { - expectation_policies::Times times = expect::twice(); + expectation_policies::RuntimeTimes times = expect::twice(); REQUIRE(!times.is_satisfied()); REQUIRE(times.is_applicable());