Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicit argc/argv passing #572

Merged
merged 6 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions example/cfg/reporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class reporter {
auto on(ut::events::fatal_assertion) -> void {}
auto on(ut::events::exception) -> void {}
auto on(ut::events::summary) -> void {}
auto on(ut::events::run_begin) -> void {}
};
} // namespace cfg

Expand Down
5 changes: 3 additions & 2 deletions example/run.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ ut::suite _ = [] {
};
};

int main() {
return ut::cfg<>.run(); // explicitly run registered test suites
int main(int argc, const char** argv) {
// explicitly run registered test suites and manually pass argc/argv
return ut::cfg<>.run({.argc = argc, .argv = argv});
}
33 changes: 25 additions & 8 deletions include/boost/ut.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,10 @@ fixed_string(const CharT (&str)[N]) -> fixed_string<CharT, N - 1>;
struct none {};

namespace events {
struct run_begin {
int argc{};
const char** argv{};
};
struct test_begin {
std::string_view type{};
std::string_view name{};
Expand Down Expand Up @@ -752,11 +756,17 @@ struct cfg {
std::cout << "version: " << BOOST_UT_VERSION << std::endl;
}

static inline void parse_arg_with_fallback(int argc, const char* argv[]) {
if (argc > 0 && argv != nullptr) {
cfg::largc = argc;
cfg::largv = argv;
}
parse(cfg::largc, cfg::largv);
}

static inline void parse(int argc, const char* argv[]) {
const std::size_t n_args = static_cast<std::size_t>(argc);
if (n_args > 0 && argv != nullptr) {
cfg::largc = argc;
cfg::largv = argv;
executable_name = argv[0];
}
query_pattern = "";
Expand All @@ -766,15 +776,15 @@ struct cfg {
auto cmd_option = find_arg(cmd);
if (!cmd_option.has_value()) {
if (found_first_option) {
std::cerr << "unknown option: '" << argv[i] << "' run:" << std::endl;
std::cerr << "'" << argv[0] << " --help'" << std::endl;
std::cerr << "unknown option: '" << cmd << "' run:" << std::endl;
std::cerr << "'" << executable_name << " --help'" << std::endl;
std::cerr << "for additional help" << std::endl;
std::exit(-1);
} else {
if (i > 1U) {
query_pattern.append(" ");
}
query_pattern.append(argv[i]);
query_pattern.append(cmd);
}
continue;
}
Expand Down Expand Up @@ -1428,6 +1438,8 @@ class reporter {
printer_ = static_cast<TPrinter&&>(printer);
}

auto on(events::run_begin) -> void {}

auto on(events::test_begin test_begin) -> void {
printer_ << "Running \"" << test_begin.name << "\"...";
fails_ = asserts_.fail;
Expand Down Expand Up @@ -1631,8 +1643,11 @@ class reporter_junit {
constexpr auto operator=(TPrinter printer) {
printer_ = static_cast<TPrinter&&>(printer);
}
reporter_junit() : lcout_(std::cout.rdbuf()) {
::boost::ut::detail::cfg::parse(detail::cfg::largc, detail::cfg::largv);
reporter_junit() : lcout_(std::cout.rdbuf()) {}
~reporter_junit() { std::cout.rdbuf(cout_save); }

auto on(events::run_begin run) {
::boost::ut::detail::cfg::parse_arg_with_fallback(run.argc, run.argv);

if (detail::cfg::show_reporters) {
std::cout << "available reporter:\n";
Expand All @@ -1652,7 +1667,6 @@ class reporter_junit {
std::cout.rdbuf(ss_out_.rdbuf());
}
}
~reporter_junit() { std::cout.rdbuf(cout_save); }

auto on(events::suite_begin suite) -> void {
while (active_test_.size() > 0) {
Expand Down Expand Up @@ -1912,6 +1926,8 @@ struct options {

struct run_cfg {
bool report_errors{false};
int argc{0};
const char** argv{nullptr};
};

template <class TReporter = reporter<printer>, auto MaxPathSize = 16>
Expand Down Expand Up @@ -2115,6 +2131,7 @@ class runner {

[[nodiscard]] auto run(run_cfg rc = {}) -> bool {
run_ = true;
reporter_.on(events::run_begin{.argc = rc.argc, .argv = rc.argv});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if constexpr (requires { reporter_.on(events::run_begin{}); })

for (const auto& [suite, suite_name] : suites_) {
// add reporter in/out
if constexpr (requires { reporter_.on(events::suite_begin{}); }) {
Expand Down
19 changes: 19 additions & 0 deletions test/ut/ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,13 +282,22 @@ struct test_summary_reporter : ut::reporter<ut::printer> {
summary_counter_ = &counter;
}

auto count_runs(std::size_t& counter) -> void { runs_counter_ = &counter; }

auto on(ut::events::summary) -> void {
if (summary_counter_) {
++*summary_counter_;
}
}

auto on(ut::events::run_begin) -> void {
if (runs_counter_) {
++*runs_counter_;
}
}

std::size_t* summary_counter_{};
std::size_t* runs_counter_{};
};

struct test_summary_runner : ut::runner<test_summary_reporter> {
Expand Down Expand Up @@ -826,6 +835,16 @@ int main() {
test_assert(1 == summary_count);
}

{
std::size_t run_count = 0;
{
auto run = test_summary_runner{};
run.reporter_.count_runs(run_count);
test_assert(false == run.run({.report_errors = true}));
}
test_assert(1 == run_count);
}

auto& test_cfg = ut::cfg<ut::override>;

{
Expand Down