Ugly but I can now set options that override the test's

master
Zed A. Shaw 1 month ago
parent 8061fbfa78
commit d6cf4e6fcf
  1. 3
      include/fuc2/run.hpp
  2. 3
      include/fuc2/testing.hpp
  3. 63
      src/run.cpp
  4. 8
      tests/main.cpp

@ -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<Set>& tests, int argc, char* argv[]);
}

@ -16,6 +16,9 @@ namespace fuc2 {
struct Options {
bool fail_fast = false;
bool verbose = false;
bool disabled = false;
std::string matching{};
};
struct Set {

@ -1,19 +1,51 @@
#include <string>
#include <fmt/core.h>
#include <fuc2/run.hpp>
#include <unistd.h>
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<std::string> 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<Set>& 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;
}
}

@ -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<fuc2::Set> tests{
sample1::TESTS,
sample_failing::TESTS
};
return run_tests(tests, argc, argv);
}

Loading…
Cancel
Save