diff --git a/include/fuc2/run.hpp b/include/fuc2/run.hpp index fc7ee25..a9111cd 100644 --- a/include/fuc2/run.hpp +++ b/include/fuc2/run.hpp @@ -7,5 +7,6 @@ * for TAP output. */ namespace fuc2 { - int run(const Set& test_set); + int run(const Set& test_set, const Options& cli_opts, bool gave_opts); + int run_tests(const std::vector& tests, int argc, char* argv[]); } diff --git a/include/fuc2/testing.hpp b/include/fuc2/testing.hpp index a0c4adf..a66a4dc 100644 --- a/include/fuc2/testing.hpp +++ b/include/fuc2/testing.hpp @@ -16,6 +16,9 @@ namespace fuc2 { struct Options { bool fail_fast = false; + bool verbose = false; + bool disabled = false; + std::string matching{}; }; struct Set { diff --git a/src/run.cpp b/src/run.cpp index ac5cfb8..b562b58 100644 --- a/src/run.cpp +++ b/src/run.cpp @@ -1,19 +1,51 @@ #include #include #include +#include namespace fuc2 { - int run(const Set& test_set) { + Options parse_options(int argc, char* argv[]) { + Options result; + + int opt = 0; + while((opt = getopt(argc, argv, "fvn:")) != -1) { + switch(opt) { + case 'f': + result.fail_fast = true; + break; + case 'v': + result.verbose = true; + break; + case 'n': + result.matching = std::string(optarg); + break; + } + } + + return result; + } + + int run(const Set& test_set, const Options& cli_opts, bool gave_opts) { int fail_count = 0; std::vector errors; std::string file_name = test_set.location.file_name(); + Options opts = gave_opts ? cli_opts : test_set.options; + + if(opts.disabled) { + fmt::println("! DISABLED: {}", file_name); + return 0; + } + + if(opts.verbose) { + fmt::println("################# {} \"{}\" ###############", file_name, test_set.name); + } else { + fmt::println("> {} \"{}\"", file_name, test_set.name); + } - fmt::println("################# {} \"{}\" ###############", - file_name, test_set.name); for(const auto& [name, test] : test_set.tests) { bool did_fail = false; - fmt::println("----------- START {}", name); + if(opts.verbose) fmt::println("----------- START {}", name); try { test(); @@ -23,20 +55,24 @@ namespace fuc2 { fmt::println("🚨 FAIL! {}", e.what()); errors.push_back(e.what()); - if(test_set.options.fail_fast) { + if(opts.fail_fast) { + fmt::println("FAIL FAST (-f) SET, STOPPING"); break; } } std::string pass_fail = did_fail ? "🔴" : "🟢"; - fmt::println("{} {}:{}\n==========", pass_fail, file_name, name); + fmt::println("{} {}:{}", pass_fail, file_name, name); + if(opts.verbose) fmt::println("=========="); } if(fail_count > 0) { fmt::println("🚨🚨🚨🚨🚨 FAIL COUNT: {} in {}", fail_count, file_name); - for(auto& msg : errors) { - fmt::println("---------\n{}", msg); + if(opts.verbose) { + for(auto& msg : errors) { + fmt::println("---------\n{}", msg); + } } } else { fmt::println("👍 ALL PASS: {}", file_name); @@ -44,4 +80,15 @@ namespace fuc2 { return fail_count; } + + int run_tests(const std::vector& tests, int argc, char* argv[]) { + int fail_count = 0; + auto opts = parse_options(argc, argv); + + for(auto& test : tests) { + fail_count += run(test, opts, argc > 1); + } + + return fail_count; + } } diff --git a/tests/main.cpp b/tests/main.cpp index 346dcc6..1cf450c 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -14,6 +14,10 @@ TEST_SET(sample_failing); using namespace fuc2; int main(int argc, char* argv[]) { - run(sample1::TESTS); - run(sample_failing::TESTS); + std::vector tests{ + sample1::TESTS, + sample_failing::TESTS + }; + + return run_tests(tests, argc, argv); }