Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: MIMICPP_ASSERT #103

Merged
merged 15 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ jobs:
git config --global --add safe.directory ${GITHUB_WORKSPACE}
gcovr -j4 -v \
--filter "include/mimic++" \
--exclude-lines-by-pattern "(\s*assert\(|\s*unreachable\(\);)" \
--exclude-lines-by-pattern "\s*(MIMICPP_ASSERT|assert|unreachable)\(" \
--exclude-unreachable-branches \
--exclude-function-lines \
--exclude-noncode-lines \
Expand Down
8 changes: 8 additions & 0 deletions cmake/Findlibassert.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright Dominic (DNKpp) Koepke 2024 - 2025.
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at
# https://www.boost.org/LICENSE_1_0.txt)

include(get_cpm)

CPMAddPackage("gh:jeremy-rifkin/[email protected]")
15 changes: 9 additions & 6 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,20 @@ endif ()
include(EnableSanitizers)
enable_sanitizers(${TARGET_NAME})

include(EnableAdditionalTestFlags)
include(EnableWarnings)
include(LinkStdStacktrace)
find_package(Catch2 REQUIRED)
find_package(trompeloeil REQUIRED)
find_package(libassert REQUIRED)
target_link_libraries(${TARGET_NAME}
PRIVATE
mimicpp::mimicpp
mimicpp::internal::additional-test-flags
mimicpp::internal::warnings
mimicpp::internal::link-std-stacktrace
mimicpp::test::basics
Catch2::Catch2WithMain
)

target_precompile_headers(${TARGET_NAME}
PRIVATE
<TestAssert.hpp>
<catch2/catch_all.hpp>
)

catch_discover_tests(${TARGET_NAME})
3 changes: 0 additions & 3 deletions examples/CustomPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@

#include "mimic++/Printing.hpp"

#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_string.hpp>

namespace
{
//! [my_type]
Expand Down
2 changes: 0 additions & 2 deletions examples/CustomString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#include "mimic++/Mock.hpp"
#include "mimic++/String.hpp"

#include <catch2/catch_test_macros.hpp>

namespace
{
//! [MyString]
Expand Down
2 changes: 0 additions & 2 deletions examples/Finalizers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#include "mimic++/Mock.hpp"
#include "mimic++/policies/FinalizerPolicies.hpp"

#include <catch2/catch_test_macros.hpp>

#include <stdexcept>

TEST_CASE(
Expand Down
2 changes: 0 additions & 2 deletions examples/InterfaceMock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#include "mimic++/InterfaceMock.hpp"
#include "mimic++/policies/FinalizerPolicies.hpp"

#include <catch2/catch_test_macros.hpp>

TEST_CASE(
"Mocking interface methods by hand.",
"[example][example::mock][example::mock::interface]")
Expand Down
2 changes: 0 additions & 2 deletions examples/Mock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#include "mimic++/Mock.hpp"
#include "mimic++/policies/FinalizerPolicies.hpp"

#include <catch2/catch_test_macros.hpp>

namespace
{
//! [stack concept]
Expand Down
2 changes: 0 additions & 2 deletions examples/RegisterCallConvention.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#include "mimic++/CallConvention.hpp"
#include "mimic++/InterfaceMock.hpp"

#include <catch2/catch_test_macros.hpp>

//! [register __stdcall]
MIMICPP_REGISTER_CALL_CONVENTION(__stdcall, detail::stdcall_convention);

Expand Down
2 changes: 0 additions & 2 deletions examples/Requirements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#include "mimic++/Mock.hpp"
#include "mimic++/matchers/RangeMatchers.hpp"

#include <catch2/catch_test_macros.hpp>

#include <string>

TEST_CASE(
Expand Down
2 changes: 0 additions & 2 deletions examples/Sequences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#include "mimic++/Mock.hpp"
#include "mimic++/Sequence.hpp"

#include <catch2/catch_test_macros.hpp>

TEST_CASE(
"Sequences allow strong ordering between two or more expectations.",
"[example][example::sequence]")
Expand Down
2 changes: 0 additions & 2 deletions examples/SideEffects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#include "mimic++/Mock.hpp"
#include "mimic++/policies/SideEffectPolicies.hpp"

#include <catch2/catch_test_macros.hpp>

TEST_CASE(
"Side-effects can execute arbitrary actions.",
"[example][example::side-effect]")
Expand Down
2 changes: 0 additions & 2 deletions examples/Times.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

#include "mimic++/Mock.hpp"

#include <catch2/catch_test_macros.hpp>

TEST_CASE(
"once() requires the expectation to be matched exactly once.",
"[example][example::times]")
Expand Down
2 changes: 0 additions & 2 deletions examples/VariadicMocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
#include "mimic++/matchers/RangeMatchers.hpp"
#include "mimic++/policies/FinalizerPolicies.hpp"

#include <catch2/catch_test_macros.hpp>

namespace
{
//! [variadic mock def]
Expand Down
2 changes: 0 additions & 2 deletions examples/Watcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#include "mimic++/ObjectWatcher.hpp"
#include "mimic++/policies/SideEffectPolicies.hpp"

#include <catch2/catch_test_macros.hpp>

TEST_CASE(
"Watched<T, LifetimeWatcher> reports violations during destruction.",
"[example][example::watched][example::watched::life-time]")
Expand Down
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: 4 additions & 0 deletions include/mimic++/config/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,8 @@
#endif
#endif

#ifndef MIMICPP_ASSERT
#define MIMICPP_ASSERT(condition, msg) (void(0))
#endif

#endif
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
Loading
Loading