Skip to content

Commit

Permalink
🆕 [ut] BDD/Spec syntax support
Browse files Browse the repository at this point in the history
Problem:
- `BDD` is available out of the box, however it should be opt in.
- `Spec` syntax is not supported.

Solution:
- Move `given/when/then` to `bdd` namespace and make it opt in.
- Add `describe/it` to `spec` namespace and make it opt in.
  • Loading branch information
kris-jusiak authored and krzysztof-jusiak committed Mar 11, 2020
1 parent dedb9dc commit 72e4bf8
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 9 deletions.
1 change: 1 addition & 0 deletions example/BDD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

int main() {
using namespace boost::ut;
using namespace boost::ut::bdd;

"Scenario"_test = [] {
given("I have...") = [] {
Expand Down
1 change: 1 addition & 0 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ example(run_report run_report)
example(section section)
example(should should)
example(skip skip)
example(spec spec)
example(suite suite)
example(test _test)
example(tmp tmp)
Expand Down
21 changes: 21 additions & 0 deletions example/spec.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// Copyright (c) 2019-2020 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>

constexpr auto sum = [](auto... args) { return (0 + ... + args); };

int main() {
using namespace boost::ut;
using namespace boost::ut::spec;

describe("sum") = [] {
it("should be 0") = [] { expect(sum() == 0_i); };

it("should sum args...") = [] { expect(sum(1, 2, 3) == 6_i); };
};
}
30 changes: 21 additions & 9 deletions include/boost/ut.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1987,15 +1987,6 @@ constexpr auto operator""_b(const char*, decltype(sizeof(""))) {
};
[[maybe_unused]] constexpr auto should = test;
[[maybe_unused]] constexpr auto skip = detail::skip{};
[[maybe_unused]] constexpr auto given = [](const auto name) {
return detail::test{"given", name};
};
[[maybe_unused]] constexpr auto when = [](const auto name) {
return detail::test{"when", name};
};
[[maybe_unused]] constexpr auto then = [](const auto name) {
return detail::test{"then", name};
};
template <class T = void>
[[maybe_unused]] constexpr auto type = detail::type_<T>();

Expand Down Expand Up @@ -2029,6 +2020,27 @@ template <class T>
return const_cast<T&>(t);
}

namespace bdd {
[[maybe_unused]] constexpr auto given = [](const auto name) {
return detail::test{"given", name};
};
[[maybe_unused]] constexpr auto when = [](const auto name) {
return detail::test{"when", name};
};
[[maybe_unused]] constexpr auto then = [](const auto name) {
return detail::test{"then", name};
};
} // namespace bdd

namespace spec {
[[maybe_unused]] constexpr auto describe = [](const auto name) {
return detail::test{"describe", name};
};
[[maybe_unused]] constexpr auto it = [](const auto name) {
return detail::test{"it", name};
};
} // namespace spec

using literals::operator""_test;
using literals::operator""_i;
using literals::operator""_s;
Expand Down
18 changes: 18 additions & 0 deletions test/ut/ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,8 @@ int main() {
{
test_cfg = fake_cfg{};

using namespace ut::bdd;

"scenario"_test = [] {
given("I have...") = [] {
when("I run...") = [] {
Expand All @@ -1304,6 +1306,20 @@ int main() {
test_assert("1 == 1" == test_cfg.assertion_calls[1].expr);
}

{
test_cfg = fake_cfg{};

using namespace ut::spec;

describe("describe") = [] { it("it") = [] { expect(1_u == 1u); }; };

test_assert(2 == std::size(test_cfg.run_calls));
test_assert("describe"sv == test_cfg.run_calls[0].name);
test_assert("it"sv == test_cfg.run_calls[1].type);
test_assert(test_cfg.assertion_calls[0].result);
test_assert("1 == 1" == test_cfg.assertion_calls[0].expr);
}

{
test_cfg = fake_cfg{};

Expand Down Expand Up @@ -1344,6 +1360,8 @@ int main() {

#if (__has_builtin(__builtin_FILE) and __has_builtin(__builtin_LINE))
{
using namespace ut::bdd;

test_cfg = fake_cfg{};

constexpr auto file = std::string_view{__FILE__};
Expand Down

0 comments on commit 72e4bf8

Please sign in to comment.