Skip to content

Commit

Permalink
refactor: remove Times and implement factories in terms of RuntimeTimes
Browse files Browse the repository at this point in the history
  • Loading branch information
DNKpp committed Jun 17, 2024
1 parent 1b35cf9 commit 78f1471
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 94 deletions.
88 changes: 37 additions & 51 deletions include/mimic++/ExpectationPolicies.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,47 +84,6 @@ namespace mimicpp::expectation_policies
}
};

template <std::size_t min, std::size_t max>
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:
Expand Down Expand Up @@ -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 <ValueCategory expected>
class Category
{
Expand Down Expand Up @@ -521,36 +491,52 @@ namespace mimicpp::expectation_policies::detail
namespace mimicpp::expect
{
template <std::size_t min, std::size_t max = min>
requires (min <= max)
[[nodiscard]]
consteval expectation_policies::Times<min, max> times() noexcept
consteval auto times() noexcept
{
return {};
return expectation_policies::RuntimeTimes{
min,
max
};
}

template <std::size_t min>
[[nodiscard]]
consteval expectation_policies::Times<min, std::numeric_limits<std::size_t>::max()> at_least() noexcept
consteval auto at_least() noexcept
{
return {};
return expectation_policies::RuntimeTimes{
min,
std::numeric_limits<std::size_t>::max()
};
}

template <std::size_t max>
[[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]]
Expand Down
49 changes: 6 additions & 43 deletions test/unit-tests/ExpectationPolicies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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<min, max> 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]"
Expand Down Expand Up @@ -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());

Expand All @@ -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());

Expand All @@ -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());

Expand All @@ -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());

Expand All @@ -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());

Expand All @@ -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());

Expand Down

0 comments on commit 78f1471

Please sign in to comment.