Skip to content

Commit

Permalink
feat: Doctest adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
DNKpp committed Oct 21, 2024
1 parent 97e3081 commit 0b4aad1
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 1 deletion.
63 changes: 63 additions & 0 deletions include/mimic++/adapters/Doctest.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// // Copyright Dominic (DNKpp) Koepke 2024 - 2024.
// // 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)

#ifndef MIMICPP_ADAPTERS_DOCTEST_HPP
#define MIMICPP_ADAPTERS_DOCTEST_HPP

#pragma once

#include "mimic++/Reporter.hpp"

#if __has_include(<doctest/doctest.h>)
#include <doctest/doctest.h>
#else
#error "Unable to find Doctest includes."
#endif

namespace mimicpp::detail::doctest
{
using namespace ::doctest;

[[noreturn]]
inline void send_fail([[maybe_unused]] const StringViewT msg)
{
DOCTEST_FAIL(msg);
unreachable();
}

inline void send_success(const StringViewT msg)
{
DOCTEST_REQUIRE_MESSAGE(true, msg);
}

inline void send_warning(const StringViewT msg)
{
DOCTEST_MESSAGE(msg);
}
}

namespace mimicpp
{
/**
* \brief Reporter for the integration into Doctest.
* \ingroup REPORTING_ADAPTERS
* \details This reporter enables the integration of ``mimic++`` into ``Doctest`` and prefixes the headers
* of ``Doctest`` with ``doctest/``.
*
* This reporter installs itself by simply including this header file into any source file of the test executable.
*/
using DoctestReporterT = BasicReporter<
&detail::doctest::send_success,
&detail::doctest::send_warning,
&detail::doctest::send_fail
>;
}

namespace mimicpp::detail::doctest
{
inline const ReporterInstaller<DoctestReporterT> installer{};
}

#endif
3 changes: 2 additions & 1 deletion test/adapter-tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# (See accompanying file LICENSE_1_0.txt or copy at
# https://www.boost.org/LICENSE_1_0.txt)

add_subdirectory("boost-test")
add_subdirectory("catch2")
add_subdirectory("doctest")
add_subdirectory("gtest")
add_subdirectory("boost-test")
26 changes: 26 additions & 0 deletions test/adapter-tests/doctest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright Dominic (DNKpp) Koepke 2024 - 2024.
# 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)

set(TARGET_NAME "mimicpp-adapter-tests-doctest")
add_executable(${TARGET_NAME}
"main.cpp"
)

include(EnableWarnings)
include(EnableSanitizers)
enable_sanitizers(${TARGET_NAME})

CPMAddPackage("gh:doctest/[email protected]")

target_link_libraries(${TARGET_NAME}
PRIVATE
mimicpp::mimicpp
mimicpp::internal::warnings
doctest::doctest
)

list(APPEND CMAKE_MODULE_PATH "${doctest_SOURCE_DIR}/scripts/cmake")
include(doctest)
doctest_discover_tests(${TARGET_NAME})
106 changes: 106 additions & 0 deletions test/adapter-tests/doctest/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// // Copyright Dominic (DNKpp) Koepke 2024 - 2024.
// // 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)

#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "mimic++/adapters/Doctest.hpp"

namespace
{
inline std::atomic_int g_SuccessCounter{0};
inline std::atomic_int g_WarningCounter{0};

class SuccessListener final
: public doctest::IReporter
{
public:
explicit SuccessListener([[maybe_unused]] const doctest::ContextOptions& in)
{
}

void report_query([[maybe_unused]] const doctest::QueryData& in) override
{
}

void test_run_start() override
{
}

void test_run_end([[maybe_unused]] const doctest::TestRunStats& in) override
{
}

void test_case_start([[maybe_unused]] const doctest::TestCaseData& in) override
{
}

void test_case_reenter([[maybe_unused]] const doctest::TestCaseData& in) override
{
}

void test_case_end([[maybe_unused]] const doctest::CurrentTestCaseStats& in) override
{
}

void test_case_exception([[maybe_unused]] const doctest::TestCaseException& in) override
{
}

void subcase_start([[maybe_unused]] const doctest::SubcaseSignature& in) override
{
}

void subcase_end() override
{
}

void log_assert([[maybe_unused]] const doctest::AssertData& in) override
{
if (!in.m_failed)
{
++g_SuccessCounter;
}
}

void log_message([[maybe_unused]] const doctest::MessageData& in) override
{
if (in.m_string == "Warning")
{
++g_WarningCounter;
}
}

void test_case_skipped([[maybe_unused]] const doctest::TestCaseData& in) override
{
}
};
}

REGISTER_LISTENER("SuccessListener", 1, SuccessListener);

TEST_SUITE("adapter::doctest")
{
TEST_CASE("doctest::send_success notifies Doctest for success.")
{
g_SuccessCounter = 0;

mimicpp::detail::doctest::send_success("Success");

REQUIRE(g_SuccessCounter == 1);
}

TEST_CASE("doctest::send_warning notifies Doctest.")
{
mimicpp::detail::doctest::send_warning("Warning");

REQUIRE(g_WarningCounter == 1);
}

TEST_CASE(
"doctest::send_fail notifies Doctest for failures and aborts."
* doctest::should_fail{})
{
mimicpp::detail::doctest::send_fail("Fail");
}
}

0 comments on commit 0b4aad1

Please sign in to comment.