Skip to content

Commit

Permalink
🆕 [example] cli showcase with filter
Browse files Browse the repository at this point in the history
  • Loading branch information
kris-jusiak committed Oct 26, 2019
1 parent db9ece3 commit 3cfc664
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 40 deletions.
35 changes: 18 additions & 17 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,29 @@
find_program(MEMORYCHECK_COMMAND valgrind)

if (ENABLE_MEMCHECK AND MEMORYCHECK_COMMAND)
function(example file target)
function(example file target args)
add_executable(${target} ${file}.cpp)
add_custom_command(TARGET ${target} COMMAND ${MEMORYCHECK_COMMAND}
--leak-check=full --error-exitcode=1 ./${target})
--leak-check=full --error-exitcode=1 ./${target} ${args})
endfunction()
else()
function(example file target)
function(example file target args)
add_executable(${target} ${file}.cpp)
add_custom_command(TARGET ${target} COMMAND ${target})
add_custom_command(TARGET ${target} COMMAND ${target} ${args})
endfunction()
endif()

example(cfg/runner runner)
example(cfg/reporter reporter)
example(assertion assertion)
example(BDD BDD)
example(exception exception)
example(filter filter)
example(hello_world hello_world)
example(logging logging)
example(parameterized parameterized)
example(run run)
example(section section)
example(skip skip)
example(suite suite)
example(cfg/runner runner "")
example(cfg/reporter reporter "")
example(assertion assertion "")
example(BDD BDD "")
example(cli cli "pass")
example(exception exception "")
example(filter filter "")
example(hello_world hello_world "")
example(logging logging "")
example(parameterized parameterized "")
example(run run "")
example(section section "")
example(skip skip "")
example(suite suite "")
17 changes: 17 additions & 0 deletions example/cli.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// Copyright (c) 2019 Kris Jusiak (kris at jusiak dot net)
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#include <boost/ut.hpp>

int main(int argc, const char** argv) {
using namespace boost::ut;

cfg<override> = {/*.filter =*/argc > 1 ? argv[1] : ""};

"pass"_test = [] { expect(42 == 42_i); };
"fail"_test = [] { expect(0 == 42_i); };
}
6 changes: 5 additions & 1 deletion example/skip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@

int main() {
using namespace boost::ut;
skip | "don't run"_test = [] { expect(42_i == 43) << "should not fire!"; };

skip | "don't run"_test = [] {
expect(42_i == 43) << "should not fire!";
expect(false) << "should fail!";
};
}
1 change: 1 addition & 0 deletions example/suite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace ut = boost::ut;

ut::suite _ = [] {
using namespace ut;

"test suite"_test = [] {
"should be equal"_test = [] { expect(42_i == 42); };
"should not be equal "_test = [] { expect(1_i != 2); };
Expand Down
52 changes: 30 additions & 22 deletions include/boost/ut.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,14 +387,34 @@ struct options {

template <class TReporter = reporter, auto MaxPathSize = 16>
class runner {
class filter {
static constexpr auto delim = ".";

public:
constexpr explicit(false) filter(std::string_view filter = {})
: path_{utility::split(filter, delim)} {}

template <class TPath>
constexpr auto operator()(const std::size_t level, const TPath& path) const
-> bool {
for (auto i = 0u; i < std::min(level + 1, std::size(path_)); ++i) {
if (not utility::is_match(path[i], path_[i])) {
return false;
}
}
return true;
}

private:
std::vector<std::string_view> path_{};
};

public:
constexpr runner() = default;
constexpr runner(TReporter reporter, std::size_t suites_size)
: reporter_{std::move(reporter)}, suites_(suites_size) {}

constexpr auto operator=(options options) {
filter_path_ = utility::split(options.filter, ".");
}
constexpr auto operator=(options options) { filter_ = options.filter; }

template <class TSuite>
auto on(events::suite<TSuite> suite) {
Expand All @@ -405,17 +425,7 @@ class runner {
auto on(events::test<Ts...> test) {
path_[level_] = test.name;

const auto should_run = [this] {
for (auto i = 0u; i < std::min(level_ + 1, std::size(filter_path_));
++i) {
if (not utility::is_match(path_[i], filter_path_[i])) {
return false;
}
}
return true;
}();

if (should_run) {
if (filter_(level_, path_)) {
if (not level_++) {
reporter_.on(events::test_begin{test.type, test.name});
} else {
Expand Down Expand Up @@ -457,19 +467,19 @@ class runner {
reporter_.on(fatal_assertion);
reporter_.on(events::test_end{});
reporter_.on(events::summary{});
test_abort();
std::abort();
}

auto on(events::log l) { reporter_.on(l); }

[[nodiscard]] auto run() -> int {
[[nodiscard]] auto run() -> bool {
run_ = true;
for (auto& suite : suites_) {
suite();
}
suites_.clear();

return fails_;
return fails_ > 0;
}

~runner() {
Expand All @@ -482,21 +492,19 @@ class runner {
reporter_.on(events::summary{});

if (should_run and fails_) {
std::exit(fails_);
std::exit(-1);
}
}

protected:
[[noreturn]] auto test_abort() -> void { std::abort(); }

TReporter reporter_{};
std::vector<void (*)()> suites_{};
std::size_t level_{};
bool run_{};
bool active_exception_{};
int fails_{};
std::size_t fails_{};
std::array<std::string_view, MaxPathSize> path_{};
std::vector<std::string_view> filter_path_{};
filter filter_{};
};

struct override {};
Expand Down

0 comments on commit 3cfc664

Please sign in to comment.