From 4f58ff285e759b532feb98c349c8dc63c4f99f8b Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Tue, 16 Jun 2026 00:30:17 -0400 Subject: [PATCH] A small source refactor to make it easier to include in other projects without potentially clashing. Not that there's other people with fuc2 directories. --- include/fuc2/run.hpp | 11 ++++++ include/{fuc2.hpp => fuc2/testing.hpp} | 2 -- meson.build | 3 +- src/run.cpp | 46 ++++++++++++++++++++++++++ src/{fuc2.cpp => testing.cpp} | 45 +------------------------ tests/main.cpp | 2 +- tests/sample1.cpp | 3 +- tests/sample_failing.cpp | 2 +- 8 files changed, 63 insertions(+), 51 deletions(-) create mode 100644 include/fuc2/run.hpp rename include/{fuc2.hpp => fuc2/testing.hpp} (97%) create mode 100644 src/run.cpp rename src/{fuc2.cpp => testing.cpp} (54%) diff --git a/include/fuc2/run.hpp b/include/fuc2/run.hpp new file mode 100644 index 0000000..fc7ee25 --- /dev/null +++ b/include/fuc2/run.hpp @@ -0,0 +1,11 @@ +#pragma once +#include + +/* + * This is kind of dumb right now but soon I'll have different run + * functions if you want to fork or thread and such. Possibly also + * for TAP output. + */ +namespace fuc2 { + int run(const Set& test_set); +} diff --git a/include/fuc2.hpp b/include/fuc2/testing.hpp similarity index 97% rename from include/fuc2.hpp rename to include/fuc2/testing.hpp index a24789d..ab3a0f2 100644 --- a/include/fuc2.hpp +++ b/include/fuc2/testing.hpp @@ -1,8 +1,6 @@ #pragma once #include #include -#include -#include #include #include diff --git a/meson.build b/meson.build index bd01bd1..cf6b5e0 100644 --- a/meson.build +++ b/meson.build @@ -22,7 +22,8 @@ fmt = subproject('fmt').get_variable('fmt_dep') dependencies = [fmt] sources = [ - 'src/fuc2.cpp', + 'src/testing.cpp', + 'src/run.cpp', ] fuc2_includes = include_directories('include') diff --git a/src/run.cpp b/src/run.cpp new file mode 100644 index 0000000..19c92a8 --- /dev/null +++ b/src/run.cpp @@ -0,0 +1,46 @@ +#include +#include +#include + +namespace fuc2 { + int run(const Set& test_set) { + int fail_count = 0; + std::vector errors; + + fmt::println("################# {} \"{}\" ###############", + test_set.location.file_name(), test_set.name); + + for(const auto& [name, test] : test_set.tests) { + bool did_fail = false; + fmt::println("----------- START {}", name); + + try { + test(); + } catch(const std::exception& e) { + fail_count++; + did_fail = true; + fmt::println("🚨 FAIL! {}", e.what()); + errors.push_back(e.what()); + + if(test_set.options.fail_fast) { + break; + } + } + + std::string pass_fail = did_fail ? "🔴" : "🟢"; + fmt::println("{} {}\n==========", pass_fail, name); + } + + if(fail_count > 0) { + fmt::println("🚨🚨🚨🚨🚨 FAIL COUNT: {} in {}", fail_count, test_set.location.file_name()); + + for(auto& msg : errors) { + fmt::println("---------\n{}", msg); + } + } else { + fmt::println("👍 ALL PASS: {}", test_set.location.file_name()); + } + + return fail_count; + } +} diff --git a/src/fuc2.cpp b/src/testing.cpp similarity index 54% rename from src/fuc2.cpp rename to src/testing.cpp index 32d8c03..3111ce8 100644 --- a/src/fuc2.cpp +++ b/src/testing.cpp @@ -1,9 +1,7 @@ #include #include -#include -#include #include -#include "fuc2.hpp" +#include "fuc2/testing.hpp" namespace fuc2 { std::string craft_error( @@ -49,45 +47,4 @@ namespace fuc2 { throw std::runtime_error(craft_error("BLOWSUP", "", message, location)); } } - - int run(const Set& test_set) { - int fail_count = 0; - std::vector errors; - - fmt::println("################# {} \"{}\" ###############", - test_set.location.file_name(), test_set.name); - - for(const auto& [name, test] : test_set.tests) { - bool did_fail = false; - fmt::println("----------- START {}", name); - - try { - test(); - } catch(const std::exception& e) { - fail_count++; - did_fail = true; - fmt::println("🚨 FAIL! {}", e.what()); - errors.push_back(e.what()); - - if(test_set.options.fail_fast) { - break; - } - } - - std::string pass_fail = did_fail ? "🔴" : "🟢"; - fmt::println("{} {}\n==========", pass_fail, name); - } - - if(fail_count > 0) { - fmt::println("🚨🚨🚨🚨🚨 FAIL COUNT: {} in {}", fail_count, test_set.location.file_name()); - - for(auto& msg : errors) { - fmt::println("---------\n{}", msg); - } - } else { - fmt::println("👍 ALL PASS: {}", test_set.location.file_name()); - } - - return fail_count; - } } diff --git a/tests/main.cpp b/tests/main.cpp index 104091e..7ea24c8 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -1,4 +1,4 @@ -#include +#include // you can also place these into a .hpp you include if // they're too numerous to manage diff --git a/tests/sample1.cpp b/tests/sample1.cpp index ff82677..543d8ba 100644 --- a/tests/sample1.cpp +++ b/tests/sample1.cpp @@ -1,11 +1,10 @@ #include #include #include -#include +#include using namespace fuc2; - namespace sample1 { void test_push_pop_back() { diff --git a/tests/sample_failing.cpp b/tests/sample_failing.cpp index d4cc7a3..3d68c7d 100644 --- a/tests/sample_failing.cpp +++ b/tests/sample_failing.cpp @@ -1,7 +1,7 @@ #include #include #include -#include +#include namespace sample_failing { using namespace fuc2;