diff options
Diffstat (limited to 'asl/testing')
-rw-r--r-- | asl/testing/BUILD.bazel | 26 | ||||
-rw-r--r-- | asl/testing/testing.cpp | 162 | ||||
-rw-r--r-- | asl/testing/testing.hpp | 92 |
3 files changed, 140 insertions, 140 deletions
diff --git a/asl/testing/BUILD.bazel b/asl/testing/BUILD.bazel index 2942073..22f59e1 100644 --- a/asl/testing/BUILD.bazel +++ b/asl/testing/BUILD.bazel @@ -1,13 +1,13 @@ -cc_library(
- name = "testing",
- hdrs = [
- "testing.hpp",
- ],
- srcs = [
- "testing.cpp",
- ],
- deps = [
- "//asl",
- ],
- visibility = ["//visibility:public"],
-)
+cc_library( + name = "testing", + hdrs = [ + "testing.hpp", + ], + srcs = [ + "testing.cpp", + ], + deps = [ + "//asl", + ], + visibility = ["//visibility:public"], +) diff --git a/asl/testing/testing.cpp b/asl/testing/testing.cpp index aff6a74..1aa0571 100644 --- a/asl/testing/testing.cpp +++ b/asl/testing/testing.cpp @@ -1,81 +1,81 @@ -#include "asl/testing/testing.hpp"
-
-#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)
-{
- 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);
- }
-}
-
-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(S) "\x1b[0;31m" S RESET
-#define GREEN(S) "\x1b[0;32m" S RESET
-
-int main([[maybe_unused]] int argc, [[maybe_unused]] char* argv[])
-{
- int fail = 0;
- int pass = 0;
-
- asl::testing::Test* failed_head = nullptr;
-
- for (auto* it = g_head; it != nullptr; it = it->m_next)
- {
- asl::eprint(GREEN("[ RUN ]") " {}\n", it->m_case_name);
-
- g_current_test_fail = false;
- it->m_fn();
-
- if (!g_current_test_fail)
- {
- asl::eprint(GREEN("[ OK ]") " {}\n", it->m_case_name);
- pass += 1;
- }
- else
- {
- asl::eprint(RED("[ FAILED ]") " {}\n", it->m_case_name);
- fail += 1;
-
- it->m_next = asl::exchange(failed_head, it);
- }
- }
-
- asl::eprint(GREEN("[----------]") " {} test(s) run\n", fail + pass);
-
- if (fail == 0)
- {
- asl::eprint(GREEN("[ PASSED ]") " Good job!\n");
- }
- else
- {
- asl::eprint(RED("[ FAILED ]") " {} test(s) failed\n", fail);
- for (auto* it = failed_head; it != nullptr; it = it->m_next)
- {
- asl::eprint(RED("[ FAILED ]") " {}\n", it->m_case_name);
- }
- }
-
- return fail;
-}
+#include "asl/testing/testing.hpp" + +#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) +{ + 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); + } +} + +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(S) "\x1b[0;31m" S RESET +#define GREEN(S) "\x1b[0;32m" S RESET + +int main([[maybe_unused]] int argc, [[maybe_unused]] char* argv[]) +{ + int fail = 0; + int pass = 0; + + asl::testing::Test* failed_head = nullptr; + + for (auto* it = g_head; it != nullptr; it = it->m_next) + { + asl::eprint(GREEN("[ RUN ]") " {}\n", it->m_case_name); + + g_current_test_fail = false; + it->m_fn(); + + if (!g_current_test_fail) + { + asl::eprint(GREEN("[ OK ]") " {}\n", it->m_case_name); + pass += 1; + } + else + { + asl::eprint(RED("[ FAILED ]") " {}\n", it->m_case_name); + fail += 1; + + it->m_next = asl::exchange(failed_head, it); + } + } + + asl::eprint(GREEN("[----------]") " {} test(s) run\n", fail + pass); + + if (fail == 0) + { + asl::eprint(GREEN("[ PASSED ]") " Good job!\n"); + } + else + { + asl::eprint(RED("[ FAILED ]") " {} test(s) failed\n", fail); + for (auto* it = failed_head; it != nullptr; it = it->m_next) + { + asl::eprint(RED("[ FAILED ]") " {}\n", it->m_case_name); + } + } + + return fail; +} diff --git a/asl/testing/testing.hpp b/asl/testing/testing.hpp index bf8f54e..e23325f 100644 --- a/asl/testing/testing.hpp +++ b/asl/testing/testing.hpp @@ -1,46 +1,46 @@ -#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();
-
-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(CASE) \
- static void asl_test_fn_##CASE(); \
- static ::asl::testing::Test asl_test_##CASE( \
- #CASE, \
- asl_test_fn_##CASE); \
- void asl_test_fn_##CASE()
-
-#define ASL_TEST_ASSERT(EXPR) \
- if (EXPR) {} \
- else { ::asl::testing::report_failure(#EXPR, __FILE__, __LINE__); return; }
-
-#define ASL_TEST_EXPECT(EXPR) \
- if (EXPR) {} \
- else { ::asl::testing::report_failure(#EXPR, __FILE__, __LINE__); }
+#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(); + +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(CASE) \ + static void asl_test_fn_##CASE(); \ + static ::asl::testing::Test asl_test_##CASE( \ + #CASE, \ + asl_test_fn_##CASE); \ + void asl_test_fn_##CASE() + +#define ASL_TEST_ASSERT(EXPR) \ + if (EXPR) {} \ + else { ::asl::testing::report_failure(#EXPR, __FILE__, __LINE__); return; } + +#define ASL_TEST_EXPECT(EXPR) \ + if (EXPR) {} \ + else { ::asl::testing::report_failure(#EXPR, __FILE__, __LINE__); } |