added test suite
This commit is contained in:
parent
984a4ed9ae
commit
d3bbb89e0f
|
|
@ -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 <array>
|
||||
#include <concepts>
|
||||
#include <tuple>
|
||||
|
||||
#include "test_ret_val.h"
|
||||
|
||||
template<std::invocable Func>
|
||||
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<Func> operator()() { return std::apply(func, std::make_tuple()); }
|
||||
};
|
||||
|
||||
template<std::invocable Func, typename ...Args>
|
||||
struct test_definition : public test_def_base<Func> {
|
||||
using base = test_def_base<Func>;
|
||||
static constexpr std::size_t ARG_SIZE = sizeof...(Args);
|
||||
|
||||
std::tuple<Args...> args;
|
||||
|
||||
constexpr test_definition(const char *name, Func func, Args ...args)
|
||||
: test_def_base<Func>(name, func), args(std::make_tuple(args...)) {}
|
||||
|
||||
constexpr std::invoke_result_t<Func, Args...> operator()() override { return std::apply(base::func, args); }
|
||||
};
|
||||
|
||||
|
||||
struct test_suite_base {
|
||||
const char *name;
|
||||
};
|
||||
|
||||
template<typename ...TestDefs>
|
||||
requires (sizeof...(TestDefs) == 0 || (std::derived_from<TestDefs, test_def_base<typename TestDefs::FuncType>> && ...))
|
||||
struct test_suite : public test_suite_base {
|
||||
|
||||
static constexpr std::size_t TEST_NR = sizeof...(TestDefs);
|
||||
|
||||
std::tuple<TestDefs...> tests;
|
||||
|
||||
constexpr test_suite(const char *name, std::tuple<TestDefs...> tests) : test_suite_base(name), tests(tests) {}
|
||||
|
||||
constexpr ret_val<TEST_NR> run() {
|
||||
|
||||
std::vector<ret_val_s> v;
|
||||
std::apply([&v](auto&& ...args) { ((v.push_back(args())), ...); }, tests);
|
||||
|
||||
ret_val<TEST_NR> ret = { name };
|
||||
std::ranges::move(v, ret.vals.begin());
|
||||
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Suite>
|
||||
struct quick_test_def {
|
||||
Suite current;
|
||||
|
||||
template<std::invocable Func, typename ...Args>
|
||||
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<decltype(new_suite)> { new_suite };
|
||||
}
|
||||
|
||||
constexpr operator Suite() {
|
||||
return current;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename ...TestDefs>
|
||||
test_suite(quick_test_def<test_suite<TestDefs...>>) -> test_suite<TestDefs...>;
|
||||
|
||||
constexpr auto define_tests(const char *name) {
|
||||
return quick_test_def { test_suite<>{ name, std::make_tuple() } };
|
||||
}
|
||||
|
||||
#endif //CONST_CONTAINER_TEST_DEFINE_TEST_HPP_
|
||||
|
|
@ -3,9 +3,16 @@
|
|||
|
||||
#include <const_vector.hpp>
|
||||
|
||||
#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;
|
||||
}
|
||||
Loading…
Reference in New Issue