Skip to content

Commit

Permalink
🆕 [ut] Alternative should syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
kris-jusiak authored and krzysztof-jusiak committed Dec 3, 2019
1 parent 50f674b commit c2f93a9
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 3 deletions.
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<details open><summary>C++ <b>single header/single module, macro-free</b> μ(micro)/Unit Testing Framework</summary>
<p>

<p align="center">
<a href="https://godbolt.org/z/uVDxkW"><img src="doc/images/ut.png"></a>
</p>
Expand Down Expand Up @@ -291,7 +291,7 @@ asserts: 1 | 0 passed | 1 failed
"hello world"_test = [] { };
```
> Alternatively `test("hello world") = [] {}` can be used.
> Alternatively `test("hello world") = [] {}` or `should("print hello world") = [] {}` can be used.
```
All tests passed (0 asserts in 1 tests)
Expand Down Expand Up @@ -574,6 +574,8 @@ All tests passed (1 asserts in 1 tests)
All tests passed (4 asserts in 1 tests)
```

> Alternatively `should("resize bigger") = [=]() mutable { ... }` can be used.
> https://godbolt.org/z/qKxsf9)
</p>
Expand Down Expand Up @@ -848,11 +850,25 @@ namespace boost::ut::inline v1_1_3 {

/**
* Creates a test
* @example "test name"_test = [] {};
* @example "name"_test = [] {};
* @return test object to be executed
*/
constexpr auto operator""_test;

/**
* Creates a test
* @example test("name") = [] {};
* @return test object to be executed
*/
constexpr auto test = [](auto name);

/**
* Creates a test
* @example should("name") = [] {};
* @return test object to be executed
*/
constexpr auto should = [](auto name);

/**
* Behaviour Driven Development (BDD) helper functions
* @param name step name
Expand Down
2 changes: 2 additions & 0 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ example(matcher matcher)
example(parameterized parameterized)
example(run run)
example(section section)
example(should should)
example(skip skip)
example(suite suite)
example(test _test)
Expand All @@ -54,4 +55,5 @@ example(expect expect_cpp17)
example(test test_cpp17)
example(suite suite_cpp17)
example(section section_cpp17)
example(should should_cpp17)
example(skip skip_cpp17)
23 changes: 23 additions & 0 deletions example/should.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// 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() {
using namespace boost::ut;

auto i = 0;
!expect(i == 0_i);

should("return increased number for ++") = [i]() mutable {
expect(++i == 1_i);
};

should("return decreased number for --") = [i]() mutable {
expect(--i == -1_i);
};
}
1 change: 1 addition & 0 deletions include/boost/ut.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1891,6 +1891,7 @@ struct suite {
[[maybe_unused]] constexpr auto test = [](utility::string_view name) {
return detail::test{"test", name};
};
[[maybe_unused]] constexpr auto should = test;
[[maybe_unused]] constexpr auto skip = detail::skip{};
[[maybe_unused]] constexpr auto given = [](utility::string_view name) {
return detail::test{"given", name};
Expand Down
20 changes: 20 additions & 0 deletions test/ut/ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1204,6 +1204,26 @@ int main() {
test_assert("1 == 1" == test_cfg.assertion_calls[1].str);
}

{
test_cfg = fake_cfg{};

test("name") = [] {};

test_assert(1 == std::size(test_cfg.run_calls));
test_assert("name"sv == test_cfg.run_calls[0].name);
test_assert("test"sv == test_cfg.run_calls[0].type);
}

{
test_cfg = fake_cfg{};

should("name") = [] {};

test_assert(1 == std::size(test_cfg.run_calls));
test_assert("name"sv == test_cfg.run_calls[0].name);
test_assert("test"sv == test_cfg.run_calls[0].type);
}

{
test_cfg = fake_cfg{};

Expand Down

0 comments on commit c2f93a9

Please sign in to comment.