Skip to content

Commit

Permalink
complete emulation of Catch 2 --list-tests
Browse files Browse the repository at this point in the history
This complete emulation isn't necessary for test discovery in CLion, and
it involves a static variable, so this is a little gross. Not sure if
this will be kept
  • Loading branch information
CrustyAuklet committed May 11, 2024
1 parent f1b50c0 commit d9ccce7
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 120 deletions.
10 changes: 7 additions & 3 deletions include/snitch/snitch_reporter_console.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
#include <string_view>

namespace snitch::reporter::console {
SNITCH_EXPORT void initialize(registry& r) noexcept;
struct reporter {
std::size_t counter = 0;

SNITCH_EXPORT bool configure(registry&, std::string_view, std::string_view) noexcept;
SNITCH_EXPORT explicit reporter(registry& r) noexcept;

SNITCH_EXPORT void report(const registry& r, const snitch::event::data& event) noexcept;
SNITCH_EXPORT bool configure(registry&, std::string_view, std::string_view) noexcept;

SNITCH_EXPORT void report(const registry& r, const snitch::event::data& event) noexcept;
};
} // namespace snitch::reporter::console

#endif
215 changes: 101 additions & 114 deletions src/snitch_reporter_console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,115 +84,11 @@ void print_message(const registry& r, const assertion_data& data) {
}},
data);
}

struct default_reporter_functor {
const registry& r;

void operator()(const snitch::event::test_run_started& e) const noexcept {
r.print(
make_colored("starting ", r.with_color, color::highlight2),
make_colored(e.name, r.with_color, color::highlight1),
make_colored(" with ", r.with_color, color::highlight2),
make_colored("snitch v" SNITCH_FULL_VERSION "\n", r.with_color, color::highlight1));
r.print("==========================================\n");
}

void operator()(const snitch::event::test_run_ended& e) const noexcept {
r.print("==========================================\n");

if (e.success) {
r.print(
make_colored("success:", r.with_color, color::pass), " all tests passed (",
e.run_count, " test cases, ", e.assertion_count, " assertions");
} else {
r.print(
make_colored("error:", r.with_color, color::fail), " ",
(e.fail_count == e.run_count ? "all" : "some"), " tests failed (", e.fail_count,
" out of ", e.run_count, " test cases, ", e.assertion_count, " assertions");
}

if (e.skip_count > 0) {
r.print(", ", e.skip_count, " test cases skipped");
}

#if SNITCH_WITH_TIMINGS
r.print(", ", e.duration, " seconds");
#endif

r.print(")\n");
}

void operator()(const snitch::event::test_case_started& e) const noexcept {
small_string<max_test_name_length> full_name;
make_full_name(full_name, e.id);

r.print(
make_colored("starting:", r.with_color, color::status), " ",
make_colored(full_name, r.with_color, color::highlight1), " at ", e.location.file, ":",
e.location.line, "\n");
}

void operator()(const snitch::event::test_case_ended& e) const noexcept {
small_string<max_test_name_length> full_name;
make_full_name(full_name, e.id);

#if SNITCH_WITH_TIMINGS
r.print(
make_colored("finished:", r.with_color, color::status), " ",
make_colored(full_name, r.with_color, color::highlight1), " (", e.duration, "s)\n");
#else
r.print(
make_colored("finished:", r.with_color, color::status), " ",
make_colored(full_name, r.with_color, color::highlight1), "\n");
#endif
}

void operator()(const snitch::event::section_started& e) {}

void operator()(const snitch::event::section_ended& e) {}

void operator()(const snitch::event::test_case_skipped& e) const noexcept {
r.print(make_colored("skipped: ", r.with_color, color::skipped));
print_location(r, e.id, e.sections, e.captures, e.location);
r.print(" ", make_colored(e.message, r.with_color, color::highlight2), "\n");
}

void operator()(const snitch::event::assertion_failed& e) const noexcept {
if (e.expected) {
r.print(make_colored("expected failure: ", r.with_color, color::pass));
} else if (e.allowed) {
r.print(make_colored("allowed failure: ", r.with_color, color::pass));
} else {
r.print(make_colored("failed: ", r.with_color, color::fail));
}
print_location(r, e.id, e.sections, e.captures, e.location);
print_message(r, e.data);
}

void operator()(const snitch::event::assertion_succeeded& e) const noexcept {
r.print(make_colored("passed: ", r.with_color, color::pass));
print_location(r, e.id, e.sections, e.captures, e.location);
print_message(r, e.data);
}

void operator()(const snitch::event::list_test_run_started& e) const noexcept {
r.print("Matching test cases:\n");
}

void operator()(const snitch::event::list_test_run_ended&) const noexcept {}

void operator()(const snitch::event::test_case_listed& e) {
small_string<max_test_name_length> full_name;
make_full_name(full_name, e.id);
r.print(" ", full_name, "\n");
r.print(" ", e.id.tags, "\n");
}
};
} // namespace

void initialize(registry&) noexcept {}
reporter::reporter(registry&) noexcept {}

bool configure(registry& r, std::string_view option, std::string_view value) noexcept {
bool reporter::configure(registry& r, std::string_view option, std::string_view value) noexcept {
if (option == "color") {
parse_color_option(r, value);
return true;
Expand All @@ -205,14 +101,105 @@ bool configure(registry& r, std::string_view option, std::string_view value) noe
return false;
}

void report(const registry& r, const event::data& event) noexcept {
std::visit(default_reporter_functor{r}, event);
void reporter::report(const registry& r, const event::data& event) noexcept {
std::visit(
snitch::overload{
[&](const snitch::event::test_run_started& e) {
r.print(
make_colored("starting ", r.with_color, color::highlight2),
make_colored(e.name, r.with_color, color::highlight1),
make_colored(" with ", r.with_color, color::highlight2),
make_colored(
"snitch v" SNITCH_FULL_VERSION "\n", r.with_color, color::highlight1));
r.print("==========================================\n");
},
[&](const snitch::event::test_run_ended& e) {
r.print("==========================================\n");

if (e.success) {
r.print(
make_colored("success:", r.with_color, color::pass), " all tests passed (",
e.run_count, " test cases, ", e.assertion_count, " assertions");
} else {
r.print(
make_colored("error:", r.with_color, color::fail), " ",
(e.fail_count == e.run_count ? "all" : "some"), " tests failed (",
e.fail_count, " out of ", e.run_count, " test cases, ", e.assertion_count,
" assertions");
}

if (e.skip_count > 0) {
r.print(", ", e.skip_count, " test cases skipped");
}

#if SNITCH_WITH_TIMINGS
r.print(", ", e.duration, " seconds");
#endif

r.print(")\n");
},
[&](const snitch::event::test_case_started& e) {
small_string<max_test_name_length> full_name;
make_full_name(full_name, e.id);

r.print(
make_colored("starting:", r.with_color, color::status), " ",
make_colored(full_name, r.with_color, color::highlight1), " at ",
e.location.file, ":", e.location.line, "\n");
},
[&](const snitch::event::test_case_ended& e) {
small_string<max_test_name_length> full_name;
make_full_name(full_name, e.id);

#if SNITCH_WITH_TIMINGS
r.print(
make_colored("finished:", r.with_color, color::status), " ",
make_colored(full_name, r.with_color, color::highlight1), " (", e.duration,
"s)\n");
#else
r.print(
make_colored("finished:", r.with_color, color::status), " ",
make_colored(full_name, r.with_color, color::highlight1), "\n");
#endif
},
[&](const snitch::event::test_case_skipped& e) {
r.print(make_colored("skipped: ", r.with_color, color::skipped));
print_location(r, e.id, e.sections, e.captures, e.location);
r.print(
" ", make_colored(e.message, r.with_color, color::highlight2), "\n");
},
[&](const snitch::event::assertion_failed& e) {
if (e.expected) {
r.print(make_colored("expected failure: ", r.with_color, color::pass));
} else if (e.allowed) {
r.print(make_colored("allowed failure: ", r.with_color, color::pass));
} else {
r.print(make_colored("failed: ", r.with_color, color::fail));
}
print_location(r, e.id, e.sections, e.captures, e.location);
print_message(r, e.data);
},
[&](const snitch::event::assertion_succeeded& e) {
r.print(make_colored("passed: ", r.with_color, color::pass));
print_location(r, e.id, e.sections, e.captures, e.location);
print_message(r, e.data);
},
[&](const snitch::event::list_test_run_started&) {
r.print("Matching test cases:\n");
counter = 0;
},
[&](const snitch::event::list_test_run_ended&) {
r.print(counter, " matching test cases");
},
[&](const snitch::event::test_case_listed& e) {
small_string<max_test_name_length> full_name;
++counter;
make_full_name(full_name, e.id);
r.print(" ", full_name, "\n");
r.print(" ", e.id.tags, "\n");
}},
event);
}
} // namespace snitch::reporter::console

SNITCH_REGISTER_REPORTER_CALLBACKS(
"console",
&snitch::reporter::console::initialize,
&snitch::reporter::console::configure,
&snitch::reporter::console::report,
{});
SNITCH_REGISTER_REPORTER("console", snitch::reporter::console::reporter);
2 changes: 1 addition & 1 deletion tests/runtime_tests/registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ TEST_CASE("list tests", "[registry]") {
CHECK(console.messages == contains_substring("how many templated lights"));
CHECK(console.messages == contains_substring("hidden test 1"));
} else if (tag == "[wrong_tag]"sv || tag == "[.hidden]"sv) {
CHECK(console.messages.empty());
CHECK(console.messages == contains_substring("0 matching test cases"));
}
}
}
Expand Down
15 changes: 13 additions & 2 deletions tests/testing_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,21 @@ std::optional<snitch::source_location> get_location(const owning_event::data& e)
e);
}

namespace {
static std::optional<snitch::reporter::console::reporter> console_reporter;
auto constle_init = [](snitch::registry& r) { console_reporter.emplace(r); };
auto constle_configure = [](snitch::registry& r, std::string_view k, std::string_view v) noexcept {
return console_reporter.value().configure(r, k, v);
};
auto constle_report = [](const snitch::registry& r, const snitch::event::data& e) noexcept {
return console_reporter.value().report(r, e);
};
static auto constle_finish = [](snitch::registry&) noexcept { console_reporter.reset(); };
} // namespace

mock_framework::mock_framework() noexcept {
registry.add_reporter(
"console", &snitch::reporter::console::initialize, &snitch::reporter::console::configure,
&snitch::reporter::console::report, {});
"console", constle_init, constle_configure, constle_report, constle_finish);

registry.print_callback = [](std::string_view msg) noexcept {
snitch::cli::console_print(msg);
Expand Down

0 comments on commit d9ccce7

Please sign in to comment.