diff options
Diffstat (limited to 'zencore')
| -rw-r--r-- | zencore/include/zencore/testing.h | 54 | ||||
| -rw-r--r-- | zencore/testing.cpp | 52 |
2 files changed, 99 insertions, 7 deletions
diff --git a/zencore/include/zencore/testing.h b/zencore/include/zencore/testing.h index bd55524aa..a00ee3166 100644 --- a/zencore/include/zencore/testing.h +++ b/zencore/include/zencore/testing.h @@ -4,24 +4,64 @@ #include <zencore/zencore.h> -#ifdef ZEN_TEST_WITH_RUNNER -# define CATCH_CONFIG_RUNNER +#include <memory> + +#ifndef ZEN_TEST_WITH_RUNNER +# define ZEN_TEST_WITH_RUNNER 0 +#endif + +#if ZEN_TEST_WITH_RUNNER # define DOCTEST_CONFIG_IMPLEMENT #endif #if ZEN_WITH_TESTS # include <doctest/doctest.h> -# define ZEN_RUN_TESTS(argC, argV) doctest::Context(argc, argv).run() inline auto Approx(auto Value) { return doctest::Approx(Value); } -#else -# define ZEN_RUN_TESTS(argC, argV) #endif -#ifdef ZEN_TEST_WITH_RUNNER -# undef CATCH_CONFIG_RUNNER +/** + * Test runner helper + * + * This acts as a thin layer between the test app and the test + * framework, which is used to customize configuration logic + * and to set up logging. + * + * If you don't want to implement custom setup then the + * ZEN_RUN_TESTS macro can be used instead. + */ + +#if ZEN_WITH_TESTS +namespace zen::testing { + +class TestRunner +{ +public: + TestRunner(); + ~TestRunner(); + + int ApplyCommandLine(int argc, char const* const* argv); + int Run(); + +private: + struct Impl; + + std::unique_ptr<Impl> m_Impl; +}; + +# define ZEN_RUN_TESTS(argC, argV) \ + [&] { \ + zen::testing::TestRunner Runner; \ + Runner.ApplyCommandLine(argC, argV); \ + return Runner.Run(); \ + }() + +} // namespace zen::testing +#endif + +#if ZEN_TEST_WITH_RUNNER # undef DOCTEST_CONFIG_IMPLEMENT #endif diff --git a/zencore/testing.cpp b/zencore/testing.cpp new file mode 100644 index 000000000..15be4b716 --- /dev/null +++ b/zencore/testing.cpp @@ -0,0 +1,52 @@ +#include "zencore/testing.h" +#include "zencore/logging.h" + +#if ZEN_WITH_TESTS + +namespace zen::testing { + +using namespace std::literals; + +struct TestRunner::Impl +{ + doctest::Context Session; +}; + +TestRunner::TestRunner() +{ + m_Impl = std::make_unique<Impl>(); +} + +TestRunner::~TestRunner() +{ +} + +int +TestRunner::ApplyCommandLine(int argc, char const* const* argv) +{ + m_Impl->Session.applyCommandLine(argc, argv); + + for (int i = 1; i < argc; ++i) + { + if (argv[i] == "--debug"sv) + { + spdlog::set_level(spdlog::level::debug); + } + } + + return 0; +} + +int +TestRunner::Run() +{ + int Rv = 0; + + m_Impl->Session.run(); + + return Rv; +} + +} // namespace zen::testing + +#endif |