Skip to content

Commit

Permalink
refactor: replace assert usages with MIMICPP_ASSERT
Browse files Browse the repository at this point in the history
  • Loading branch information
DNKpp committed Mar 4, 2025
1 parent 39da6e5 commit 28b0ab7
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 62 deletions.
17 changes: 8 additions & 9 deletions include/mimic++/Expectation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@
#pragma once

#include "mimic++/Call.hpp"
#include "mimic++/Fwd.hpp"
#include "mimic++/Sequence.hpp"
#include "mimic++/TypeTraits.hpp"
#include "mimic++/config/Config.hpp"
#include "mimic++/reporting/CallReport.hpp"
#include "mimic++/reporting/ExpectationReport.hpp"
#include "mimic++/reporting/GlobalReporter.hpp"
#include "mimic++/reporting/TargetReport.hpp"
#include "mimic++/utilities/SourceLocation.hpp"

#include <algorithm>
#include <cassert>
#include <concepts>
#include <functional>
#include <memory>
Expand Down Expand Up @@ -307,9 +308,7 @@ namespace mimicpp
{
const std::scoped_lock lock{m_ExpectationsMx};

assert(
std::ranges::find(m_Expectations, expectation) == std::ranges::end(m_Expectations)
&& "Expectation already belongs to this storage.");
MIMICPP_ASSERT(std::ranges::find(m_Expectations, expectation) == std::ranges::end(m_Expectations), "Expectation already belongs to this storage.");

m_Expectations.emplace_back(std::move(expectation));
}
Expand All @@ -326,7 +325,7 @@ namespace mimicpp
const std::scoped_lock lock{m_ExpectationsMx};

auto iter = std::ranges::find(m_Expectations, expectation);
assert(iter != std::ranges::end(m_Expectations) && "Expectation does not belong to this storage.");
MIMICPP_ASSERT(iter != std::ranges::end(m_Expectations), "Expectation does not belong to this storage.");
m_Expectations.erase(iter);

if (!expectation->is_satisfied())
Expand Down Expand Up @@ -359,9 +358,9 @@ namespace mimicpp
if (!std::ranges::empty(matches))
{
std::vector reports = detail::gather_expectation_reports(matches);
assert(matches.size() == reports.size() && "Size mismatch.");
MIMICPP_ASSERT(matches.size() == reports.size(), "Size mismatch.");
auto const bestIndex = detail::find_best_match(reports);
assert(0 <= bestIndex && bestIndex < std::ssize(reports) && "Invalid index.");
MIMICPP_ASSERT(0 <= bestIndex && bestIndex < std::ssize(reports), "Invalid index.");

auto& report = reports[bestIndex];
auto& expectation = *matches[bestIndex];
Expand Down Expand Up @@ -679,8 +678,8 @@ namespace mimicpp
: m_Storage{std::move(storage)},
m_Expectation{std::move(expectation)}
{
assert(m_Storage && "Storage is nullptr.");
assert(m_Expectation && "Expectation is nullptr.");
MIMICPP_ASSERT(m_Storage, "Storage is nullptr.");
MIMICPP_ASSERT(m_Expectation, "Expectation is nullptr.");

m_Storage->push(m_Expectation);
}
Expand Down
4 changes: 3 additions & 1 deletion include/mimic++/ExpectationBuilder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
#pragma once

#include "mimic++/Expectation.hpp"
#include "mimic++/Fwd.hpp"
#include "mimic++/Sequence.hpp"
#include "mimic++/config/Config.hpp"
#include "mimic++/matchers/Common.hpp"
#include "mimic++/matchers/GeneralMatchers.hpp"
#include "mimic++/matchers/StringMatchers.hpp"
Expand Down Expand Up @@ -55,7 +57,7 @@ namespace mimicpp
m_FinalizePolicy{std::forward<FinalizePolicyArg>(finalizePolicyArg)},
m_ExpectationPolicies{std::forward<PolicyListArg>(policyListArg)}
{
assert(m_Storage && "Storage is nullptr.");
MIMICPP_ASSERT(m_Storage, "Storage is nullptr.");
}

BasicExpectationBuilder(const BasicExpectationBuilder&) = delete;
Expand Down
3 changes: 2 additions & 1 deletion include/mimic++/Mock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "mimic++/Fwd.hpp"
#include "mimic++/Stacktrace.hpp"
#include "mimic++/TypeTraits.hpp"
#include "mimic++/config/Config.hpp"
#include "mimic++/policies/GeneralPolicies.hpp"
#include "mimic++/printing/TypePrinter.hpp"
#include "mimic++/reporting/TargetReport.hpp"
Expand Down Expand Up @@ -317,7 +318,7 @@ namespace mimicpp::detail
: m_Expectations{std::move(collection)},
m_Settings{std::move(settings)}
{
assert(m_Settings.name && "Empty mock-name.");
MIMICPP_ASSERT(m_Settings.name, "Empty mock-name.");

m_Settings.stacktraceSkip += 2u; // skips the operator() and the handle_call from the stacktrace
}
Expand Down
26 changes: 12 additions & 14 deletions include/mimic++/Sequence.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
#pragma once

#include "mimic++/Fwd.hpp"
#include "mimic++/config/Config.hpp"
#include "mimic++/printing/Format.hpp"
#include "mimic++/reporting/GlobalReporter.hpp"
#include "mimic++/utilities/C++23Backports.hpp"

#include <algorithm>
#include <array>
#include <cassert>
#include <functional>
#include <span>
#include <tuple>
Expand Down Expand Up @@ -118,7 +118,7 @@ namespace mimicpp::sequence
[[nodiscard]]
constexpr std::optional<int> priority_of(const IdT id) const noexcept
{
assert(is_valid(id));
MIMICPP_ASSERT(is_valid(id), "Invalid id given.");

if (is_consumable(id))
{
Expand All @@ -133,31 +133,29 @@ namespace mimicpp::sequence

constexpr void set_satisfied(const IdT id) noexcept
{
assert(is_valid(id));
assert(m_Cursor <= util::to_underlying(id));
MIMICPP_ASSERT(is_valid(id), "Invalid id given.");
MIMICPP_ASSERT(m_Cursor <= util::to_underlying(id), "Invalid state.");

auto& element = m_Entries[util::to_underlying(id)];
assert(element == State::unsatisfied);
MIMICPP_ASSERT(element == State::unsatisfied, "Element is in unexpected state.");
element = State::satisfied;
}

constexpr void set_saturated(const IdT id) noexcept
{
assert(is_valid(id));
MIMICPP_ASSERT(is_valid(id), "Invalid id given.");
const auto index = util::to_underlying(id);
assert(m_Cursor <= index);
MIMICPP_ASSERT(m_Cursor <= index, "Invalid state.");

auto& element = m_Entries[index];
assert(
element == State::unsatisfied
|| element == State::satisfied);
MIMICPP_ASSERT(element == State::unsatisfied || element == State::satisfied, "Element is in unexpected state.");
element = State::saturated;
}

[[nodiscard]]
constexpr bool is_consumable(const IdT id) const noexcept
{
assert(is_valid(id));
MIMICPP_ASSERT(is_valid(id), "Invalid id given.");

const int index = util::to_underlying(id);
const auto state = m_Entries[index];
Expand All @@ -175,7 +173,7 @@ namespace mimicpp::sequence

constexpr void consume(const IdT id) noexcept
{
assert(is_consumable(id));
MIMICPP_ASSERT(is_consumable(id), "Sequence is not in consumable state.");

m_Cursor = util::to_underlying(id);
}
Expand Down Expand Up @@ -227,7 +225,7 @@ namespace mimicpp::sequence
constexpr int operator()(const auto id, const int cursor) const noexcept
{
const auto index = util::to_underlying(id);
assert(std::cmp_less_equal(cursor, index));
MIMICPP_ASSERT(std::cmp_less_equal(cursor, index), "Invalid state.");

return std::numeric_limits<int>::max()
- (static_cast<int>(index) - cursor);
Expand All @@ -241,7 +239,7 @@ namespace mimicpp::sequence
constexpr int operator()(const auto id, const int cursor) const noexcept
{
const auto index = util::to_underlying(id);
assert(std::cmp_less_equal(cursor, index));
MIMICPP_ASSERT(std::cmp_less_equal(cursor, index), "Invalid state.");

return static_cast<int>(index) - cursor;
}
Expand Down
4 changes: 2 additions & 2 deletions include/mimic++/Stacktrace.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
#pragma once

#include "mimic++/Fwd.hpp"
#include "mimic++/config/Config.hpp"
#include "mimic++/printing/PathPrinter.hpp"
#include "mimic++/printing/StatePrinter.hpp"
#include "mimic++/utilities/AlwaysFalse.hpp"
#include "mimic++/utilities/PriorityTag.hpp"

#include <algorithm>
#include <any>
#include <cassert>
// ReSharper disable once CppUnusedIncludeDirective
#include <functional> // std::invoke
#include <ranges>
Expand Down Expand Up @@ -381,7 +381,7 @@ namespace mimicpp::stacktrace::detail
template <print_iterator OutIter>
OutIter print_entry(OutIter out, Stacktrace const& stacktrace, std::size_t const index)
{
assert(index < stacktrace.size() && "Index out of bounds.");
MIMICPP_ASSERT(index < stacktrace.size(), "Index out of bounds.");

out = format::format_to(std::move(out), "`");
out = print_path(std::move(out), stacktrace.source_file(index));
Expand Down
4 changes: 2 additions & 2 deletions include/mimic++/policies/ControlPolicies.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

#include "mimic++/Fwd.hpp"
#include "mimic++/Sequence.hpp"
#include "mimic++/config/Config.hpp"
#include "mimic++/reporting/ExpectationReport.hpp"

#include <cassert>
#include <limits>
#include <memory>
#include <optional>
Expand Down Expand Up @@ -195,7 +195,7 @@ namespace mimicpp

constexpr void consume() noexcept
{
assert(is_applicable());
MIMICPP_ASSERT(is_applicable(), "Policy is inapplicable.");

std::apply(
[](auto&... entries) noexcept {
Expand Down
6 changes: 3 additions & 3 deletions include/mimic++/policies/GeneralPolicies.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
#pragma once

#include "mimic++/Fwd.hpp"
#include "mimic++/config/Config.hpp"
#include "mimic++/printing/StatePrinter.hpp"

#include <cassert>
#include <iterator>

namespace mimicpp::detail
Expand Down Expand Up @@ -58,7 +58,7 @@ namespace mimicpp::expectation_policies
template <typename Return, typename... Args>
static constexpr void consume([[maybe_unused]] const call::Info<Return, Args...>& info) noexcept
{
assert(mimicpp::detail::is_matching(info.fromCategory, expected) && "Call does not match.");
MIMICPP_ASSERT(mimicpp::detail::is_matching(info.fromCategory, expected), "Call does not match.");
}

[[nodiscard]]
Expand Down Expand Up @@ -91,7 +91,7 @@ namespace mimicpp::expectation_policies
template <typename Return, typename... Args>
static constexpr void consume([[maybe_unused]] const call::Info<Return, Args...>& info) noexcept
{
assert(mimicpp::detail::is_matching(info.fromConstness, constness) && "Call does not match.");
MIMICPP_ASSERT(mimicpp::detail::is_matching(info.fromConstness, constness), "Call does not match.");
}

[[nodiscard]]
Expand Down
25 changes: 12 additions & 13 deletions include/mimic++/printing/type/PostProcessing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include "mimic++/utilities/Algorithm.hpp"

#include <array>
#include <cassert>
#include <iterator>
#include <optional>
#include <ranges>
Expand Down Expand Up @@ -80,8 +79,8 @@ namespace mimicpp::printing::type::detail
auto const& prefixMatches,
auto const& suffixMatches)
{
assert(prefixMatches.size() == 2 && "Regex out-of-sync.");
assert(suffixMatches.size() == 1 && "Regex out-of-sync.");
MIMICPP_ASSERT(prefixMatches.size() == 2, "Regex out-of-sync.");
MIMICPP_ASSERT(suffixMatches.size() == 1, "Regex out-of-sync.");

StringViewT const functionName{prefixMatches[1].first, prefixMatches[1].second};
StringViewT const prefix{prefixMatches[0].first, prefixMatches[0].second};
Expand All @@ -103,7 +102,7 @@ namespace mimicpp::printing::type::detail
| std::views::reverse
| std::views::drop(templateScopeSuffix.size());
auto const iter = util::find_closing_token(reversedName, '>', '<');
assert(iter != reversedName.end() && "No template begin found.");
MIMICPP_ASSERT(iter != reversedName.end(), "No template begin found.");
out = format::format_to(
std::move(out),
"{}::",
Expand All @@ -125,17 +124,17 @@ namespace mimicpp::printing::type::detail
StringViewT const prefix,
StringViewT const scope)
{
assert(scope.data() == prefix.data() && "Prefix and scope must be aligned.");
MIMICPP_ASSERT(scope.data() == prefix.data(), "Prefix and scope must be aligned.");

StringViewT rest{prefix.data() + prefix.size(), scope.data() + scope.size()};
auto const closingIter = util::find_closing_token(rest, '(', ')');
assert(closingIter != rest.cend() && "No corresponding closing-token found.");
MIMICPP_ASSERT(closingIter != rest.cend(), "No corresponding closing-token found.");

SVMatchT suffixMatches{};
rest = StringViewT{closingIter, rest.cend()};
std::regex_search(rest.cbegin(), rest.cend(), suffixMatches, suffixRegex);
assert(!suffixMatches.empty() && "No function suffix found.");
assert(suffixMatches.size() == 2 && "Regex out-of-sync.");
MIMICPP_ASSERT(!suffixMatches.empty(), "No function suffix found.");
MIMICPP_ASSERT(suffixMatches.size() == 2, "Regex out-of-sync.");
StringViewT const suffix{suffixMatches[0].first, suffixMatches[0].second};

out = std::ranges::copy(StringViewT{"lambda#"}, std::move(out)).out;
Expand Down Expand Up @@ -229,7 +228,7 @@ namespace mimicpp::printing::type::detail
StringViewT const rest{scope.cbegin(), matches[0].first};
SVMatchT prefixMatches{};
std::regex_search(rest.cbegin(), rest.cend(), prefixMatches, functionScopePrefix);
assert(!prefixMatches.empty() && "No corresponding function prefix found.");
MIMICPP_ASSERT(!prefixMatches.empty(), "No corresponding function prefix found.");

std::size_t count{};
std::tie(out, count) = prettify_function_scope(
Expand Down Expand Up @@ -337,7 +336,7 @@ namespace mimicpp::printing::type::detail
StringViewT const rest{scope.cbegin(), matches[0].first};
SVMatchT prefixMatches{};
std::regex_search(rest.cbegin(), rest.cend(), prefixMatches, functionScopePrefix);
assert(!prefixMatches.empty() && "No corresponding function prefix found.");
MIMICPP_ASSERT(!prefixMatches.empty(), "No corresponding function prefix found.");

std::size_t count{};
std::tie(out, count) = prettify_function_scope(
Expand Down Expand Up @@ -449,7 +448,7 @@ namespace mimicpp::printing::type::detail
| std::views::reverse
| std::views::drop(1);
auto const iter = util::find_closing_token(reversedName, '>', '<');
assert(iter != reversedName.end() && "No template begin found.");
MIMICPP_ASSERT(iter != reversedName.end(), "No template begin found.");
std::tuple info{
"<",
StringT{iter.base(), name.end() - 1},
Expand All @@ -473,7 +472,7 @@ namespace mimicpp::printing::type::detail
| std::views::reverse
| std::views::drop(matches[0].length());
auto const iter = util::find_closing_token(reversedName, '}', '{');
assert(iter != reversedName.end() && "No lambda begin found.");
MIMICPP_ASSERT(iter != reversedName.end(), "No lambda begin found.");

std::tuple info{
format::format("lambda#{}", StringViewT{matches[1].first, matches[1].second}),
Expand Down Expand Up @@ -501,7 +500,7 @@ namespace mimicpp::printing::type::detail
| std::views::reverse
| std::views::drop(matches[0].length());
auto const iter = util::find_closing_token(reversedName, ')', '(');
assert(iter != reversedName.end() && "No function begin found.");
MIMICPP_ASSERT(iter != reversedName.end(), "No function begin found.");

StringViewT const argList{iter.base(), name.end() - matches[0].length()};
std::tuple info{
Expand Down
3 changes: 2 additions & 1 deletion include/mimic++/printing/type/Templated.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#pragma once

#include "mimic++/Fwd.hpp"
#include "mimic++/config/Config.hpp"
#include "mimic++/printing/Format.hpp"
#include "mimic++/printing/Fwd.hpp"
#include "mimic++/printing/type/PrintType.hpp"
Expand All @@ -31,7 +32,7 @@ namespace mimicpp::printing::type::detail
{
StringT name = type_name<T>();
auto const iter = std::ranges::find(name, '<');
assert(iter != name.cend() && "Given name is not a template.");
MIMICPP_ASSERT(iter != name.cend(), "Given name is not a template.");
name.erase(iter, name.end());

return type::prettify_identifier(std::move(out), std::move(name));
Expand Down
5 changes: 3 additions & 2 deletions include/mimic++/reporting/DefaultReporter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@

#pragma once

#include "mimic++/Fwd.hpp"
#include "mimic++/config/Config.hpp"
#include "mimic++/reporting/CallReport.hpp"
#include "mimic++/reporting/ExpectationReport.hpp"
#include "mimic++/reporting/IReporter.hpp"
#include "mimic++/reporting/NoMatchReport.hpp"
#include "mimic++/reporting/StringifyReports.hpp"

#include <cassert>
#include <cstddef>
#include <exception>
#include <ostream>
Expand Down Expand Up @@ -111,7 +112,7 @@ namespace mimicpp::reporting
[[maybe_unused]] CallReport const call,
[[maybe_unused]] ExpectationReport const expectationReport) noexcept override
{
assert(std::holds_alternative<state_applicable>(expectationReport.controlReport) && "Report denotes inapplicable expectation.");
MIMICPP_ASSERT(std::holds_alternative<state_applicable>(expectationReport.controlReport), "Report denotes inapplicable expectation.");
}

void report_unfulfilled_expectation(ExpectationReport expectationReport) override
Expand Down
Loading

0 comments on commit 28b0ab7

Please sign in to comment.