More work on testing framework

This commit is contained in:
2024-10-23 00:20:05 +02:00
parent 8c95db33be
commit e1f9d9ca1e
8 changed files with 136 additions and 22 deletions

View File

@ -17,4 +17,4 @@ build --cxxopt=-Wno-extra-semi-stmt
build --cxxopt=-Wno-extra-semi
build --cxxopt=-Wno-global-constructors
test --test_output=errors
test --test_output=all

14
MODULE.bazel.lock generated
View File

@ -64,20 +64,20 @@
"@@apple_support~//crosstool:setup.bzl%apple_cc_configure_extension": {
"general": {
"bzlTransitiveDigest": "PjIds3feoYE8SGbbIq2SFTZy3zmxeO2tQevJZNDo7iY=",
"usagesDigest": "aLmqbvowmHkkBPve05yyDNGN7oh7QE9kBADr3QIZTZs=",
"usagesDigest": "+hz7IHWN6A1oVJJWNDB6yZRG+RYhF76wAYItpAeIUIg=",
"recordedFileInputs": {},
"recordedDirentsInputs": {},
"envVariables": {},
"generatedRepoSpecs": {
"local_config_apple_cc": {
"bzlFile": "@@apple_support~//crosstool:setup.bzl",
"ruleClassName": "_apple_cc_autoconf",
"attributes": {}
},
"local_config_apple_cc_toolchains": {
"bzlFile": "@@apple_support~//crosstool:setup.bzl",
"ruleClassName": "_apple_cc_autoconf_toolchains",
"attributes": {}
},
"local_config_apple_cc": {
"bzlFile": "@@apple_support~//crosstool:setup.bzl",
"ruleClassName": "_apple_cc_autoconf",
"attributes": {}
}
},
"recordedRepoMappingEntries": [
@ -92,7 +92,7 @@
"@@platforms//host:extension.bzl%host_platform": {
"general": {
"bzlTransitiveDigest": "xelQcPZH8+tmuOHVjL9vDxMnnQNMlwj0SlvgoqBkm4U=",
"usagesDigest": "meSzxn3DUCcYEhq4HQwExWkWtU4EjriRBQLsZN+Q0SU=",
"usagesDigest": "pCYpDQmqMbmiiPI1p2Kd3VLm5T48rRAht5WdW0X2GlA=",
"recordedFileInputs": {},
"recordedDirentsInputs": {},
"envVariables": {},

View File

@ -70,6 +70,11 @@ void asl::format_internals::format(
f.write(begin, fmt - begin);
}
void asl::AslFormat(formatter& f, const char* str)
{
f.write(str, static_cast<int64_t>(__builtin_strlen(str)));
}
void asl::AslFormat(formatter& f, float)
{
f.write("<FLOAT>", 7); // @Todo Float formatting

View File

@ -81,6 +81,8 @@ void AslFormat(formatter& f, const char (&str)[N])
f.write(str, N - 1);
}
void AslFormat(formatter& f, const char* str);
void AslFormat(formatter& f, float);
void AslFormat(formatter& f, double);

View File

@ -1,14 +1,73 @@
#include "asl/testing/testing.hpp"
int asl::testing::register_test(
const char* suite_name,
const char* case_name,
TestFunction* fn)
#include <asl/print.hpp>
static asl::testing::Test* g_head = nullptr;
static asl::testing::Test* g_tail = nullptr;
void asl::testing::register_test(Test* test)
{
return 0;
if (g_head == nullptr && g_tail == nullptr)
{
g_head = test;
g_tail = test;
}
else
{
g_tail->m_next = test;
test->m_prev = asl::exchange(g_tail, test);
}
}
int main(int argc, char* argv[])
static bool g_current_test_fail = false;
void asl::testing::report_failure(const char* msg, const char* file, int line)
{
asl::eprint("--------------------------------------------------------------\n");
asl::eprint("Test assertion failed at {}, line {}:\n", file, line);
asl::eprint(" {}:\n", msg);
asl::eprint("--------------------------------------------------------------\n");
g_current_test_fail = true;
}
#define RESET "\x1b[0m"
#define RED "\x1b[0;31m"
#define GREEN "\x1b[0;32m"
int main([[maybe_unused]] int argc, [[maybe_unused]] char* argv[])
{
int fail = 0;
int pass = 0;
for (auto* it = g_head; it != nullptr; it = it->m_next)
{
asl::eprint(GREEN "[ RUN ]" RESET " {}\n", it->m_case_name);
g_current_test_fail = false;
it->m_fn();
if (!g_current_test_fail)
{
asl::eprint(GREEN "[ OK ]" RESET " {}\n", it->m_case_name);
pass += 1;
}
else
{
asl::eprint(RED "[ FAILED ]" RESET " {}\n", it->m_case_name);
fail += 1;
}
}
asl::eprint(GREEN "[----------]" RESET " {} test(s) run\n", fail + pass);
if (fail == 0)
{
asl::eprint(GREEN "[ PASSED ]" RESET " Good job!\n");
}
else
{
asl::eprint(RED "[ FAILED ]" RESET " {} test(s) failed\n", fail);
}
return 0;
}

View File

@ -1,15 +1,38 @@
#pragma once
#include <asl/utility.hpp>
namespace asl::testing
{
struct Test;
void register_test(Test*);
void report_failure(const char* msg, const char* file, int line);
using TestFunction = void();
int register_test(const char* suite_name, const char* case_name, TestFunction* fn);
struct Test
{
const char* m_case_name;
TestFunction* m_fn;
Test* m_next{};
Test* m_prev{};
constexpr explicit Test(const char* case_name, TestFunction* fn)
: m_case_name{case_name}
, m_fn{fn}
{
register_test(this);
}
};
} // namespace asl::testing
#define ASL_TEST(SUITE, CASE) \
static void asl_test_fn_##SUITE##_##CASE(); \
static const int asl_test_##SUITE##_##CASE = ::asl::testing::register_test( \
#SUITE, #CASE, asl_test_fn_##SUITE##_##CASE); \
void asl_test_fn_##SUITE##_##CASE()
#define ASL_TEST(CASE) \
static void asl_test_fn_##CASE(); \
static ::asl::testing::Test asl_test_##CASE( \
#CASE, \
asl_test_fn_##CASE); \
void asl_test_fn_##CASE()

View File

@ -24,7 +24,32 @@ static_assert(asl::move_assignable<asl::option<int>>);
static_assert(asl::move_assignable<asl::option<CopyAssignable>>);
static_assert(!asl::move_assignable<asl::option<NonMoveAssignable>>);
ASL_TEST(Option, cheese)
ASL_TEST(Option_cheese)
{
asl::option<int> a;
asl::option<int> b;
a = ASL_MOVE(b);
}
ASL_TEST(Option_cheese2)
{
asl::option<int> a;
asl::option<int> b;
a = ASL_MOVE(b);
}
ASL_TEST(Option_cheese3)
{
asl::option<int> a;
asl::option<int> b;
a = ASL_MOVE(b);
asl::testing::report_failure("OH NO", __FILE__, __LINE__);
}
ASL_TEST(Option_cheese4)
{
asl::option<int> a;
asl::option<int> b;

View File

@ -14,7 +14,7 @@ template<typename T, typename U>
T exchange(T& obj, U&& new_value)
{
T old_value = ASL_MOVE(obj);
obj = ASL_FORWARD(new_value);
obj = ASL_FWD(new_value);
return old_value;
}