Unverified Commit 61f3fc1c authored by Shariar Azad Riday's avatar Shariar Azad Riday Committed by GitHub

Implement regex filtering for test suites

parent f42cf82d
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
#include "caf/config_option_adder.hpp" #include "caf/config_option_adder.hpp"
#include "caf/config_option_set.hpp" #include "caf/config_option_set.hpp"
#include "caf/detail/log_level.hpp" #include "caf/detail/log_level.hpp"
#include "caf/settings.hpp"
#include "caf/test/context.hpp" #include "caf/test/context.hpp"
#include "caf/test/nesting_error.hpp" #include "caf/test/nesting_error.hpp"
#include "caf/test/reporter.hpp" #include "caf/test/reporter.hpp"
...@@ -16,6 +15,7 @@ ...@@ -16,6 +15,7 @@
#include <iostream> #include <iostream>
#include <optional> #include <optional>
#include <regex>
#include <string> #include <string>
namespace caf::test { namespace caf::test {
...@@ -53,6 +53,26 @@ std::optional<unsigned> parse_log_level(std::string_view x) { ...@@ -53,6 +53,26 @@ std::optional<unsigned> parse_log_level(std::string_view x) {
return {}; return {};
} }
std::optional<std::regex> to_regex(std::string_view regex_string) {
#ifdef CAF_ENABLE_EXCEPTIONS
using detail::format_to;
auto err_out = [] { return std::ostream_iterator<char>{std::cerr}; };
try {
return std::regex{regex_string.begin(), regex_string.end()};
} catch (const std::exception& err) {
format_to(err_out(), "error while parsing argument '{}': {}\n",
regex_string, err.what());
return std::nullopt;
} catch (...) {
format_to(err_out(), "error while parsing argument '{}': unknown error\n",
regex_string);
return std::nullopt;
}
#else
return std::regex{regex_string.begin(), regex_string.end()};
#endif
}
} // namespace } // namespace
runner::runner() : suites_(caf::test::registry::suites()) { runner::runner() : suites_(caf::test::registry::suites()) {
...@@ -67,8 +87,19 @@ int runner::run(int argc, char** argv) { ...@@ -67,8 +87,19 @@ int runner::run(int argc, char** argv) {
} else if (help_printed) { } else if (help_printed) {
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
auto suite_regex = to_regex(get_or(cfg_, "suites", ".*"));
if (!suite_regex) {
return EXIT_FAILURE;
}
default_reporter->start(); default_reporter->start();
auto enabled = [](const std::regex& selected,
std::string_view search_string) {
return std::regex_search(search_string.begin(), search_string.end(),
selected);
};
for (auto& [suite_name, suite] : suites_) { for (auto& [suite_name, suite] : suites_) {
if (!enabled(*suite_regex, suite_name))
continue;
default_reporter->begin_suite(suite_name); default_reporter->begin_suite(suite_name);
for (auto [test_name, factory_instance] : suite) { for (auto [test_name, factory_instance] : suite) {
auto state = std::make_shared<context>(); auto state = std::make_shared<context>();
...@@ -109,27 +140,26 @@ int runner::run(int argc, char** argv) { ...@@ -109,27 +140,26 @@ int runner::run(int argc, char** argv) {
runner::parse_cli_result runner::parse_cli(int argc, char** argv) { runner::parse_cli_result runner::parse_cli(int argc, char** argv) {
using detail::format_to; using detail::format_to;
caf::settings cfg;
std::vector<std::string> args_cpy{argv + 1, argv + argc}; std::vector<std::string> args_cpy{argv + 1, argv + argc};
auto options = make_option_set(); auto options = make_option_set();
auto res = options.parse(cfg, args_cpy); auto res = options.parse(cfg_, args_cpy);
auto err = std::ostream_iterator<char>{std::cerr}; auto err = std::ostream_iterator<char>{std::cerr};
if (res.first != caf::pec::success) { if (res.first != caf::pec::success) {
format_to(err, "error while parsing argument '{}': {}\n\n{}\n", *res.second, format_to(err, "error while parsing argument '{}': {}\n\n{}\n", *res.second,
to_string(res.first), options.help_text()); to_string(res.first), options.help_text());
return {false, true}; return {false, true};
} }
if (get_or(cfg, "help", false)) { if (get_or(cfg_, "help", false)) {
format_to(err, "{}\n", options.help_text()); format_to(err, "{}\n", options.help_text());
return {true, true}; return {true, true};
} }
if (get_or(cfg, "available-suites", false)) { if (get_or(cfg_, "available-suites", false)) {
format_to(err, "available suites:\n"); format_to(err, "available suites:\n");
for (auto& [suite_name, suite] : suites_) for (auto& [suite_name, suite] : suites_)
format_to(err, "- {}\n", suite_name); format_to(err, "- {}\n", suite_name);
return {true, true}; return {true, true};
} }
if (auto suite_name = get_as<std::string>(cfg, "available-tests")) { if (auto suite_name = get_as<std::string>(cfg_, "available-tests")) {
auto i = suites_.find(*suite_name); auto i = suites_.find(*suite_name);
if (i == suites_.end()) { if (i == suites_.end()) {
format_to(err, "no such suite: {}\n", *suite_name); format_to(err, "no such suite: {}\n", *suite_name);
...@@ -140,7 +170,7 @@ runner::parse_cli_result runner::parse_cli(int argc, char** argv) { ...@@ -140,7 +170,7 @@ runner::parse_cli_result runner::parse_cli(int argc, char** argv) {
format_to(err, "- {}\n", test_name); format_to(err, "- {}\n", test_name);
return {true, true}; return {true, true};
} }
if (auto verbosity = get_as<std::string>(cfg, "verbosity")) { if (auto verbosity = get_as<std::string>(cfg_, "verbosity")) {
auto level = parse_log_level(*verbosity); auto level = parse_log_level(*verbosity);
if (!level) { if (!level) {
format_to(err, format_to(err,
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#pragma once #pragma once
#include "caf/detail/test_export.hpp" #include "caf/detail/test_export.hpp"
#include "caf/settings.hpp"
#include "caf/test/registry.hpp" #include "caf/test/registry.hpp"
namespace caf::test { namespace caf::test {
...@@ -28,6 +29,7 @@ private: ...@@ -28,6 +29,7 @@ private:
parse_cli_result parse_cli(int argc, char** argv); parse_cli_result parse_cli(int argc, char** argv);
registry::suites_map suites_; registry::suites_map suites_;
caf::settings cfg_;
}; };
} // namespace caf::test } // namespace caf::test
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment