diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index f68e8f7..19913d1 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -13,6 +13,8 @@ jobs: runs-on: ubuntu-24.04 steps: - uses: actions/checkout@v4 - - run: sudo apt-get install clang-19 -y + - run: sudo apt-get install g++-14 -y - run: cmake -S . -B build && cmake --build build + env: + CXX: /usr/bin/g++-14 - run: ./build/willow_test diff --git a/.gitignore b/.gitignore index ec9da65..10f2747 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ iris.log build/ .gdb_history .cache +core diff --git a/CHANGELOG.md b/CHANGELOG.md index 785ecc2..0dca844 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ +### main/HEAD +* Add `Willow::runSingleTest()` function to select a test on the command line ### v0.0.1 - 03/11/25 * Initial repo setup diff --git a/CMakeLists.txt b/CMakeLists.txt index b47a6af..7d32bf7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,8 +4,6 @@ project(willow-test LANGUAGES CXX) add_subdirectory(src/willow) if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) - message("LOCAL BUILD ONLY") - set(CMAKE_CXX_COMPILER "/usr/bin/clang++") set(CMAKE_CXX_STANDARD 23) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) diff --git a/README.md b/README.md index 53922d4..01f0cd9 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ A unit testing library for modern c++23 # How to use Currently, willow only supports CMake for distribution, although if you know -what you're doing, feel free to clone the repo and do what you need yourself. +what you're doing, feel free to clone the repo and build it yourself ```cmake set(CMAKE_CXX_STANDARD 23) @@ -45,13 +45,16 @@ auto main() -> int { // are available Willow::DefaultReporter reporter = {}; - // Your executable will exit with a return code displaying the number of - // failed tests - return Willow::runTests({ + // Register each function to test + Willow::registerTests({ // Each test function is given a name to display as output. you can also // mark tests for skipping {"addition test", test_add}, {"skipped test", test_add, Willow::Status::Skip}, - }, reporter); + }); + + // Your executable will exit with a return code displaying the number of + // failed tests + return Willow::runTests(reporter); } ``` diff --git a/src/main.cpp b/src/main.cpp index 5b15020..13a3f1f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,18 +4,21 @@ #include "willow/reporters.h" #include "willow/willow.h" -auto main() -> int { +auto main(int argc, char* argv[]) -> int { Willow::PreCommitReporter reporter = {}; - return Willow::runTests( - { - {"test_runTests", test_runTests}, - {"test_toString", test_toString}, - {"test_Test_alert", test_Test_alert}, - {"test_Test_Operator()", test_Test_Operator}, - {"PreCommitReporter::print", TestPreCommitReporter::test_print}, - {"PreCommitReporter::cleanup", TestPreCommitReporter::test_cleanup}, - {"PreCommitReporter::highlight", TestPreCommitReporter::test_highlight}, - }, - reporter); + Willow::registerTests({ + {"test_runTests", test_runTests}, + {"test_toString", test_toString}, + {"test_Test_alert", test_Test_alert}, + {"test_Test_Operator()", test_Test_Operator}, + {"PreCommitReporter::print", TestPreCommitReporter::test_print}, + {"PreCommitReporter::cleanup", TestPreCommitReporter::test_cleanup}, + {"PreCommitReporter::highlight", TestPreCommitReporter::test_highlight}, + }); + + if (argc > 1) { + return Willow::runSingleTest(std::string(argv[1]), reporter); + } + return Willow::runTests(reporter); } diff --git a/src/tests/test_willow.h b/src/tests/test_willow.h index 5e3fdaf..afb3aa7 100644 --- a/src/tests/test_willow.h +++ b/src/tests/test_willow.h @@ -12,11 +12,14 @@ namespace FixtureFuncs { constexpr auto test_runTests([[maybe_unused]] Willow::Test* test) -> int { SilentReporter r = {}; - int ret = Willow::runTests( + std::vector storage = Willow::global_tests; + + Willow::registerTests( {{"pass", FixtureFuncs::pass}, {"fail", FixtureFuncs::fail}, - {"skip", FixtureFuncs::fail, Willow::Status::Skip}}, - r); + {"skip", FixtureFuncs::fail, Willow::Status::Skip}}); + int ret = Willow::runTests(r); + Willow::global_tests = storage; return !(ret == 1); } diff --git a/src/willow/willow.h b/src/willow/willow.h index 787ec96..1ca6f2b 100644 --- a/src/willow/willow.h +++ b/src/willow/willow.h @@ -7,8 +7,14 @@ #include "test.h" namespace Willow { - constexpr auto runTests(std::vector tests, Reporter& reporter) -> int { - for (auto&& test : tests) { + inline std::vector global_tests = {}; + + constexpr auto registerTests(std::vector tests) -> void { + global_tests = tests; + } + + constexpr auto runTests(Reporter& reporter) -> int { + for (auto&& test : global_tests) { if (test.status == Status::Skip) { continue; } @@ -22,14 +28,35 @@ namespace Willow { test.status = Status::Pass; } - std::for_each(tests.begin(), tests.end(), [&reporter](Test& t) { reporter.print(t); }); + std::for_each( + global_tests.begin(), global_tests.end(), [&reporter](Test& t) { reporter.print(t); }); reporter.cleanup(); - return int32_t(std::count_if(tests.begin(), tests.end(), [](Test& t) { + return int32_t(std::count_if(global_tests.begin(), global_tests.end(), [](Test& t) { return t.status == Status::Fail; })); } + constexpr auto runSingleTest(std::string_view test_name, Reporter& reporter) -> int { + auto it = std::find_if(global_tests.begin(), global_tests.end(), [&test_name](Test& t) { + return t.name.contains(test_name); + }); + if (it == global_tests.end()) { + return 1; + } + + Test& exec = *it; + if (exec.status == Status::Skip) { + return 2; + } + + exec(); + exec.status = (exec.retcode == 0 ? Status::Pass : Status::Fail); + reporter.print(exec); + + return exec.retcode; + } + } // namespace Willow #endif // WILLOW_H