Skip to content

Commit 7df1f8b

Browse files
committed
use _testing_impl namespace consistently, pull implementation details out of primary headers
1 parent a009545 commit 7df1f8b

14 files changed

Lines changed: 208 additions & 196 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#pragma once
2+
#include <vector>
3+
4+
#include <rsl/span>
5+
#include <rsl/string_view>
6+
#include <rsl/testing/annotations.hpp>
7+
8+
#include "util.hpp"
9+
10+
namespace rsl::testing::_testing_impl {
11+
struct Annotations { // consteval-only
12+
rsl::span<ParamSet const> targets;
13+
rsl::span<annotations::Params const> params;
14+
15+
bool expect_failure = false;
16+
bool (*skip)() = nullptr; // this is a function to support conditional skipping
17+
rsl::string_view name; // custom base name
18+
19+
bool is_property_test = false; // expand missing args as fixtures if false,
20+
// otherwise parameterize with domain
21+
bool is_fuzz_test = false;
22+
23+
consteval explicit Annotations(std::meta::info fnc) {
24+
std::vector<ParamSet> tp_sets;
25+
std::vector<annotations::Params> p;
26+
27+
for (auto annotation : annotations_of(fnc)) {
28+
auto type = remove_cvref(type_of(annotation));
29+
if (type == ^^annotations::TParams) {
30+
tp_sets.append_range(extract<annotations::TParams>(constant_of(annotation)).value);
31+
} else if (type == ^^annotations::Params) {
32+
p.push_back(extract<annotations::Params>(constant_of(annotation)));
33+
} else if (type == ^^annotations::ExpectFailureTag) {
34+
constexpr_assert(!expect_failure, "Cannot annotate with expect_failure more than once.");
35+
expect_failure = true;
36+
} else if (type == ^^annotations::Skip) {
37+
// constexpr_assert(skip == nullptr, "Cannot have more than one skip annotation.");
38+
skip = extract<annotations::Skip>(constant_of(annotation)).value;
39+
} else if (type == ^^annotations::Rename) {
40+
constexpr_assert(name.empty(), "Cannot rename more than once.");
41+
name = extract<annotations::Rename>(constant_of(annotation)).value;
42+
} else if (type == ^^annotations::PropertyTag) {
43+
is_property_test = true;
44+
} else if (type == ^^annotations::FuzzTag) {
45+
is_fuzz_test = true;
46+
}
47+
}
48+
49+
targets = define_static_array(tp_sets);
50+
params = define_static_array(p);
51+
52+
if (skip == nullptr) {
53+
skip = &constant_predicate<false>;
54+
}
55+
}
56+
};
57+
} // namespace rsl::testing::_testing_impl

include/rsl/testing/discovery.hpp renamed to include/rsl/testing/_testing_impl/discovery.hpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,28 @@
1010
# define RSLTEST_SCAN_GLOBAL_NAMESPACE 0
1111
#endif
1212

13-
#include <rsl/testing/annotations.hpp>
1413
#include <rsl/testing/test.hpp>
15-
#include <rsl/testing/_impl/fixture.hpp>
16-
#include <rsl/testing/_impl/util.hpp>
1714

18-
namespace rsl::testing {
19-
namespace _testing_impl {
15+
#include "fixture.hpp"
16+
#include "annotations.hpp"
17+
#include "util.hpp"
18+
19+
namespace rsl::testing::_testing_impl {
20+
21+
template <std::meta::info R>
22+
Test make_test_impl() {
23+
if constexpr (has_identifier(R) && identifier_of(R) == "_rsl_test_surrogate") {
24+
constexpr auto target = [:R:]();
25+
return Test(target, R);
26+
} else {
27+
return Test(R, R);
28+
}
29+
}
30+
31+
consteval TestDef make_test(std::meta::info R) {
32+
return extract<TestDef>(substitute(^^make_test_impl, {reflect_constant(R)}));
33+
}
34+
2035
consteval std::vector<TestDef> expand_class(std::meta::info class_r) {
2136
std::vector<TestDef> tests{};
2237

@@ -99,8 +114,6 @@ struct TestDiscovery {
99114
}
100115
};
101116
std::set<TestDef>& registry();
102-
} // namespace _testing_impl
103-
104117

105118
template <std::meta::info NS, auto TUTag = [] {}>
106119
bool enable_tests() {
@@ -111,4 +124,4 @@ bool enable_tests() {
111124
return true;
112125
}
113126

114-
} // namespace rsl::testing
127+
} // namespace rsl::testing::_testing_impl
Lines changed: 80 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,105 @@
11
#pragma once
2-
#include <functional>
32
#include <string>
3+
#include <functional>
44
#include <meta>
5-
#include <tuple>
65
#include <vector>
6+
#include <tuple>
77

88
#include <rsl/assert>
9-
#include <libassert/assert.hpp> // TODO use repr instead
9+
#include <libassert/assert.hpp>
1010

11+
#include <rsl/testing/fuzz.hpp>
12+
13+
#include "fixture.hpp"
1114
#include "annotations.hpp"
12-
#include "_impl/fixture.hpp"
1315

1416
namespace rsl::testing {
17+
// TODO move to testing/test.hpp
1518
struct TestResult;
16-
struct Test;
17-
1819
struct TestRun {
19-
Test const* test;
20+
class Test const* test;
2021
std::function<void()> fnc;
2122
std::string name;
2223

2324
[[nodiscard]] TestResult run() const;
25+
};
26+
} // namespace rsl::testing
27+
28+
namespace rsl::testing::_testing_impl {
29+
template <std::meta::info R, std::meta::info Target>
30+
struct FuzzRunner {
31+
static int run(uint8_t const* Data, size_t Size) {
32+
// TODO
33+
return 0;
34+
}
2435

25-
template <std::meta::info Def, std::meta::info Target>
26-
struct Runner {
27-
template <typename T>
28-
static void run_one(T const& tuple) {
29-
if constexpr (is_class_member(Def)) {
30-
auto fixture = [:parent_of(Def):]();
31-
std::apply(fixture.[:Target:], tuple);
32-
} else if constexpr (is_variable(Def)) {
33-
std::apply([:Def:].[:Target:], tuple);
34-
} else {
35-
std::apply([:Target:], tuple);
36-
}
36+
// mutator must be able to consider domains
37+
static size_t mutate(uint8_t* Data, size_t Size, size_t MaxSize, unsigned int Seed) {
38+
// TODO
39+
return 0;
40+
}
41+
};
42+
43+
template <std::meta::info Def, std::meta::info Target>
44+
struct TestRunner {
45+
template <typename T>
46+
static void run_one(T const& tuple) {
47+
if constexpr (is_class_member(Def)) {
48+
auto fixture = [:parent_of(Def):]();
49+
std::apply(fixture.[:Target:], tuple);
50+
} else if constexpr (is_variable(Def)) {
51+
std::apply([:Def:].[:Target:], tuple);
52+
} else {
53+
std::apply([:Target:], tuple);
3754
}
55+
}
3856

39-
template <typename... Ts>
40-
static std::string get_name(std::tuple<Ts...> args) {
41-
std::string name;
42-
if constexpr (is_variable(Def)) {
57+
template <typename... Ts>
58+
static std::string get_name(std::tuple<Ts...> args) {
59+
std::string name;
60+
if constexpr (is_variable(Def)) {
61+
name += identifier_of(Def);
62+
} else {
63+
if constexpr (has_template_arguments(Target)) {
4364
name += identifier_of(Def);
4465
} else {
45-
if constexpr (has_template_arguments(Target)) {
46-
name += identifier_of(Def);
47-
} else {
48-
name += identifier_of(Target);
49-
}
66+
name += identifier_of(Target);
5067
}
68+
}
5169

52-
if constexpr (has_template_arguments(Target)) {
53-
name += "<";
54-
bool first = true;
55-
template for (constexpr auto T : define_static_array(template_arguments_of(Target))) {
56-
if (first) {
57-
first = false;
58-
} else {
59-
name += ", ";
60-
}
61-
// TODO stringify aliases properly
62-
name += display_string_of(T);
70+
if constexpr (has_template_arguments(Target)) {
71+
name += "<";
72+
bool first = true;
73+
template for (constexpr auto T : define_static_array(template_arguments_of(Target))) {
74+
if (first) {
75+
first = false;
76+
} else {
77+
name += ", ";
6378
}
64-
name += ">";
79+
// TODO stringify aliases properly
80+
name += display_string_of(T);
6581
}
66-
name += std::apply(
67-
[](auto&&... args) {
68-
std::string result = "(";
69-
std::size_t i = 0;
70-
((result += (i++ ? ", " : "") + libassert::stringify(args)), ...);
71-
result += ")";
72-
return result;
73-
},
74-
args);
75-
return name;
82+
name += ">";
7683
}
84+
name += std::apply(
85+
[](auto&&... args) {
86+
std::string result = "(";
87+
std::size_t i = 0;
88+
((result += (i++ ? ", " : "") + libassert::stringify(args)), ...);
89+
result += ")";
90+
return result;
91+
},
92+
args);
93+
return name;
94+
}
7795

78-
template <typename... Ts>
79-
static TestRun bind(Test const* group, std::tuple<Ts...> args) {
80-
return {group, std::bind_front(run_one<std::tuple<Ts...>>, args), get_name(args)};
81-
}
82-
};
96+
template <typename... Ts>
97+
static TestRun bind(Test const* group, std::tuple<Ts...> args) {
98+
return {group, std::bind_front(run_one<std::tuple<Ts...>>, args), get_name(args)};
99+
}
83100
};
84101

85-
namespace _impl {
86-
template <std::meta::info R, Annotations A>
102+
template <std::meta::info R, _testing_impl::Annotations A>
87103
struct Expand {
88104
std::vector<TestRun> runs;
89105
Test const* group;
@@ -109,30 +125,31 @@ struct Expand {
109125

110126
template <std::meta::info Target>
111127
void expand_params() {
112-
using Runner = TestRun::Runner<R, Target>;
128+
using runner = TestRunner<R, Target>;
113129

114130
if constexpr (A.params.size() == 0) {
115131
// expand fixtures
116-
runs.push_back(Runner::bind(group, rsl::testing::_testing_impl::evaluate_fixtures<Target>()));
132+
runs.push_back(runner::bind(group, rsl::testing::_testing_impl::evaluate_fixtures<Target>()));
117133
} else {
118134
// expand param annotations
119135
template for (constexpr auto generator : A.params) {
120-
expand_param_generator<Runner, generator>();
136+
expand_param_generator<runner, generator>();
121137
}
122138
}
123139
}
124140

125141
template <std::meta::info Target>
126142
void expand_tparams() {
127-
constexpr_assert(A.targets.size() != 0,
143+
constexpr_assert(
144+
A.targets.size() != 0,
128145
std::string("template argument parameterization required for ") + display_string_of(R));
129146

130147
template for (constexpr auto set : A.targets) {
131148
expand_params<substitute(Target, set.value)>();
132149
}
133150
}
134151

135-
explicit Expand(Test const* group) : group(group){
152+
explicit Expand(Test const* group) : group(group) {
136153
constexpr auto target_count = A.targets.size();
137154
constexpr auto param_count = A.params.size();
138155
if constexpr (is_variable(R)) {
@@ -157,6 +174,4 @@ struct Expand {
157174
}
158175
}
159176
};
160-
} // namespace _impl
161-
162-
} // namespace rsl::testing
177+
} // namespace rsl::testing::_testing_impl

include/rsl/testing/_impl/factory.hpp renamed to include/rsl/testing/_testing_impl/factory.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
#include <meta>
66
#include <unordered_map>
77

8-
#include "../annotations.hpp"
8+
#include "annotations.hpp"
99

10-
namespace rsl::testing::_impl {
10+
namespace rsl::testing::_testing_impl {
1111
template <class Base, class... Args>
1212
class Factory {
1313
public:

include/rsl/testing/_impl/fixture.hpp renamed to include/rsl/testing/_testing_impl/fixture.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include <cstddef>
33
#include <meta>
44

5-
#include <rsl/testing/_impl/util.hpp>
5+
#include "util.hpp"
66

77
namespace rsl::testing::_testing_impl {
88
template <std::size_t Idx>

include/rsl/testing/paramset.hpp renamed to include/rsl/testing/_testing_impl/paramset.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <utility>
55
#include <rsl/span>
66

7-
namespace rsl::testing {
7+
namespace rsl::testing::_testing_impl {
88
struct ParamSet {
99
rsl::span<std::meta::info const> value;
1010

include/rsl/testing/_impl/util.hpp renamed to include/rsl/testing/_testing_impl/util.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
#include <meta>
55

66
namespace rsl::testing::_testing_impl {
7+
template <bool V>
8+
bool constant_predicate() {
9+
return V;
10+
}
11+
712
template <typename T>
813
consteval bool has_annotation(std::meta::info R) {
914
return !annotations_of(R, dealias(^^T)).empty();
@@ -53,4 +58,4 @@ consteval std::vector<std::string_view> get_fully_qualified_name(std::meta::info
5358
std::ranges::reverse(name);
5459
return name;
5560
}
56-
} // namespace rsl::_testing_impl
61+
} // namespace rsl::testing::_testing_impl

0 commit comments

Comments
 (0)