Skip to content

Commit e53cc5f

Browse files
committed
Redesign to make synopsis complete.
1 parent 63fcef8 commit e53cc5f

4 files changed

Lines changed: 75 additions & 37 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#pragma once
2+
3+
#include "polyfill_v1/type_traits.hpp"
4+
#include <utility>
5+
6+
namespace testing_v1 {
7+
8+
template <class Action> struct test_t;
9+
10+
void run_all();
11+
12+
class Private {
13+
friend void run_all();
14+
template <class> friend struct test_t;
15+
16+
class Static;
17+
18+
class test_base_t;
19+
template <class Action> class test_t;
20+
};
21+
22+
} // namespace testing_v1
23+
24+
class testing_v1::Private::test_base_t {
25+
friend class Static;
26+
template <class> friend class test_t;
27+
28+
~test_base_t();
29+
test_base_t();
30+
31+
virtual void run() = 0;
32+
};
33+
34+
template <class Action> class testing_v1::Private::test_t : test_base_t {
35+
friend class Static;
36+
template <class> friend struct testing_v1::test_t;
37+
38+
template <class ForwardableAction> test_t(ForwardableAction &&action);
39+
40+
void run() override;
41+
42+
std::conditional_t<std::is_function_v<Action>, Action *, Action> action;
43+
};

‎provides/include/testing_v1/synopsis.hpp‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
#pragma once
22

3-
#include "polyfill_v1/type_traits.hpp"
3+
#include "testing_v1/private.hpp"
44

55
namespace testing_v1 {
66

77
// test.hpp ====================================================================
88

99
/// Holds a registered test action and should only be used at namespace scope.
10-
template <class Action> struct test_t;
10+
template <class Action> struct test_t : Private::test_t<Action> {
11+
/// Constructs a test from the given action.
12+
template <class ForwardableAction> test_t(ForwardableAction &&action);
13+
14+
/// Tests are not CopyConstructible.
15+
test_t(const test_t &) = delete;
16+
17+
/// Tests are not CopyAssignable.
18+
test_t &operator=(const test_t &) = delete;
19+
};
1120

1221
/// Creates and registers a test action and should only be used at namespace
1322
/// scope.

‎provides/include/testing_v1/test.hpp‎

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,19 @@
44

55
#include <utility>
66

7-
namespace testing_v1 {
8-
9-
class test_private;
10-
11-
class test_base_t {
12-
friend class test_private;
13-
14-
test_base_t(const test_base_t &) = delete;
15-
test_base_t &operator=(const test_base_t &) = delete;
16-
17-
virtual void run() = 0;
18-
19-
protected:
20-
test_base_t();
21-
~test_base_t();
22-
};
23-
24-
} // namespace testing_v1
25-
26-
template <class Action> struct testing_v1::test_t : test_base_t {
27-
template <class ForwardableAction>
28-
test_t(ForwardableAction &&action)
29-
: action(std::forward<ForwardableAction>(action)) {}
7+
template <class Action>
8+
template <class ForwardableAction>
9+
testing_v1::Private::test_t<Action>::test_t(ForwardableAction &&action)
10+
: action(std::forward<ForwardableAction>(action)) {}
3011

31-
void run() override { action(); }
12+
template <class Action> void testing_v1::Private::test_t<Action>::run() {
13+
action();
14+
}
3215

33-
private:
34-
std::conditional_t<std::is_function_v<Action>, Action *, Action> action;
35-
};
16+
template <class Action>
17+
template <class ForwardableAction>
18+
testing_v1::test_t<Action>::test_t(ForwardableAction &&action)
19+
: Private::test_t<Action>(std::forward<ForwardableAction>(action)) {}
3620

3721
template <class Action>
3822
testing_v1::test_t<std::remove_cvref_t<Action>>

‎provides/library/test.cpp‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,31 @@ void testing_v1::verify(bool ok) {
1313
exit(1);
1414
}
1515

16-
class testing_v1::test_private {
16+
class testing_v1::Private::Static {
17+
friend void run_all();
1718
friend class test_base_t;
1819

19-
using tests_t = std::vector<testing_v1::test_base_t *>;
20+
using tests_t = std::vector<test_base_t *>;
2021

2122
static tests_t &tests() {
2223
static tests_t tests;
2324
return tests;
2425
}
2526

26-
public:
2727
static void run_all() {
2828
for (auto test : tests())
2929
test->run();
3030
}
3131
};
3232

33-
testing_v1::test_base_t::test_base_t() {
34-
test_private::tests().push_back(this);
33+
void testing_v1::run_all() { Private::Static::run_all(); }
34+
35+
testing_v1::Private::test_base_t::test_base_t() {
36+
Static::tests().push_back(this);
3537
}
3638

37-
testing_v1::test_base_t::~test_base_t() {
38-
auto &tests = test_private::tests();
39+
testing_v1::Private::test_base_t::~test_base_t() {
40+
auto &tests = Static::tests();
3941
auto it = std::find(tests.rbegin(), tests.rend(), this);
4042
if (it != tests.rend()) {
4143
std::swap(*it, tests.back());
@@ -44,6 +46,6 @@ testing_v1::test_base_t::~test_base_t() {
4446
}
4547

4648
int main() {
47-
testing_v1::test_private::run_all();
49+
testing_v1::run_all();
4850
return 0;
4951
}

0 commit comments

Comments
 (0)