From d3bbb89e0f002dae244dcc3b7ce2de81151e1c2b Mon Sep 17 00:00:00 2001 From: cyborg1811m Date: Mon, 8 Apr 2024 14:50:48 +0200 Subject: [PATCH] added test suite --- test/common_helper/test_define_test.hpp | 89 +++++++++++++++++++ .../const_vector_constructor.test.cpp | 10 +++ 2 files changed, 99 insertions(+) create mode 100644 test/common_helper/test_define_test.hpp diff --git a/test/common_helper/test_define_test.hpp b/test/common_helper/test_define_test.hpp new file mode 100644 index 0000000..270ea1f --- /dev/null +++ b/test/common_helper/test_define_test.hpp @@ -0,0 +1,89 @@ +// +// Created by Patrick Maschek on 08/04/2024. +// + +#ifndef CONST_CONTAINER_TEST_DEFINE_TEST_HPP_ +#define CONST_CONTAINER_TEST_DEFINE_TEST_HPP_ + +#include +#include +#include + +#include "test_ret_val.h" + +template +struct test_def_base { + using FuncType = Func; + + const char *name; + Func func; + + constexpr test_def_base(const char * name, Func func) : name(name), func(func) {} + + virtual constexpr std::invoke_result_t operator()() { return std::apply(func, std::make_tuple()); } +}; + +template +struct test_definition : public test_def_base { + using base = test_def_base; + static constexpr std::size_t ARG_SIZE = sizeof...(Args); + + std::tuple args; + + constexpr test_definition(const char *name, Func func, Args ...args) + : test_def_base(name, func), args(std::make_tuple(args...)) {} + + constexpr std::invoke_result_t operator()() override { return std::apply(base::func, args); } +}; + + +struct test_suite_base { + const char *name; +}; + +template + requires (sizeof...(TestDefs) == 0 || (std::derived_from> && ...)) +struct test_suite : public test_suite_base { + + static constexpr std::size_t TEST_NR = sizeof...(TestDefs); + + std::tuple tests; + + constexpr test_suite(const char *name, std::tuple tests) : test_suite_base(name), tests(tests) {} + + constexpr ret_val run() { + + std::vector v; + std::apply([&v](auto&& ...args) { ((v.push_back(args())), ...); }, tests); + + ret_val ret = { name }; + std::ranges::move(v, ret.vals.begin()); + + return ret; + } +}; + +template +struct quick_test_def { + Suite current; + + template + constexpr auto operator()(const char *name, Func func, Args... args) { + auto test = test_definition(name, func, args...); + auto new_suite = test_suite(current.name, std::tuple_cat(current.tests, std::make_tuple(test))); + return quick_test_def { new_suite }; + } + + constexpr operator Suite() { + return current; + } +}; + +template +test_suite(quick_test_def>) -> test_suite; + +constexpr auto define_tests(const char *name) { + return quick_test_def { test_suite<>{ name, std::make_tuple() } }; +} + +#endif //CONST_CONTAINER_TEST_DEFINE_TEST_HPP_ diff --git a/test/const_vector/const_vector_constructor.test.cpp b/test/const_vector/const_vector_constructor.test.cpp index 44bdbb0..869b19f 100644 --- a/test/const_vector/const_vector_constructor.test.cpp +++ b/test/const_vector/const_vector_constructor.test.cpp @@ -3,9 +3,16 @@ #include +#include "test_define_test.hpp" #include "test_util.hpp" #include "test_ret_val.h" +test_suite tests = define_tests("Tests") + ("Test1", [](){ + + return TEST_PASS("Test1", "PASS"); + }); + constexpr auto test_func(const char *name) { auto ret_val = run_tests(name, @@ -25,6 +32,9 @@ int main() { report(ret); report(cret); + + auto sret = tests.run(); + report(sret); return 0; } \ No newline at end of file