Skip to content

Commit b9b97b5

Browse files
committed
General restructuring, expected
1 parent df8a03a commit b9b97b5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+562
-762
lines changed

.clang-format

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
BasedOnStyle: Google
2+
ColumnLimit: 120

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function(add_spec source_file)
5050
target_link_libraries(${spec_name} c++spec)
5151

5252
set_target_properties(${spec_name} PROPERTIES
53-
CXX_STANDARD 20
53+
CXX_STANDARD 23
5454
CXX_STANDARD_REQUIRED YES
5555
)
5656
add_test(NAME ${spec_name} COMMAND ${spec_name} --verbose)

examples/sample/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ add_executable(jasmine_intro jasmine_intro.cpp)
88
target_link_libraries(jasmine_intro c++spec)
99

1010
set_target_properties(cppspec_sample jasmine_intro PROPERTIES
11-
CXX_STANDARD 20
12-
CXX_STANDARD_REQUIRED YES
13-
)
11+
CXX_STANDARD 23
12+
CXX_STANDARD_REQUIRED YES
13+
)

examples/sample/example_spec.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,24 @@ describe list_spec("A list spec", $ {
191191
});
192192
});
193193

194+
#include <expected>
195+
196+
describe expectation_spec("An expected", ${
197+
class Error{};
198+
std::expected<int, Error> expected{5};
199+
it("has a value", _{
200+
expect(expected).to_have_value();
201+
});
202+
203+
it("does not have an error", _{
204+
expect(expected).not_().to_have_error();
205+
});
206+
207+
it("contains the value", _{
208+
expect(expected.value()).to_equal(5);
209+
});
210+
});
211+
194212
/* Here is the declaration of fabs description defined in an other file (fabs_spec.c in this sample)*/
195213
int main(int argc, char **argv){
196214
return CppSpec::parse(argc, argv)
@@ -200,5 +218,6 @@ int main(int argc, char **argv){
200218
.add_spec(vector_spec)
201219
.add_spec(let_spec)
202220
.add_spec(list_spec)
221+
.add_spec(expectation_spec)
203222
.exec() ? EXIT_SUCCESS : EXIT_FAILURE;
204-
}
223+
}

include/argparse.hpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22

33
#include <argparse/argparse.hpp>
44
#include <string_view>
5-
#include "runner.hpp"
65

76
#include "formatters/progress.hpp"
87
#include "formatters/tap.hpp"
98
#include "formatters/verbose.hpp"
10-
9+
#include "runner.hpp"
1110

1211
namespace CppSpec {
1312

@@ -35,10 +34,7 @@ inline Runner parse(int argc, char** const argv) {
3534
.required()
3635
.help("set the output format");
3736

38-
program.add_argument("--verbose")
39-
.help("increase output verbosity")
40-
.default_value(false)
41-
.implicit_value(true);
37+
program.add_argument("--verbose").help("increase output verbosity").default_value(false).implicit_value(true);
4238

4339
try {
4440
program.parse_args(argc, argv);

include/child.hpp

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
* @author Katherine Whitlock (toroidalcode)
88
*/
99

10-
#ifndef CPPSPEC_CHILD_HPP
11-
#define CPPSPEC_CHILD_HPP
1210
#pragma once
1311

1412
#ifndef CPPSPEC_DEBUG
@@ -72,9 +70,7 @@ class Child {
7270
Child &operator=(const Child &) = default;
7371

7472
// Custom constructors
75-
static Child of(const Child &parent) noexcept {
76-
return Child().set_parent(&parent);
77-
}
73+
static Child of(const Child &parent) noexcept { return Child().set_parent(&parent); }
7874

7975
/*--------- Parent helper functions -------------*/
8076

@@ -108,14 +104,12 @@ class Child {
108104
// Get the printer from the tree
109105
[[nodiscard]] Formatters::BaseFormatter &get_formatter() const noexcept;
110106

111-
void set_formatter(Formatters::BaseFormatter &formatter) {
112-
this->formatter = &formatter;
113-
}
107+
void set_formatter(Formatters::BaseFormatter &formatter) { this->formatter = &formatter; }
114108

115109
/*--------- Primary member functions -------------*/
116110

117111
/** @brief Get the status of the object (success/failure) */
118-
[[nodiscard]] bool get_status() const noexcept { return this->status; }
112+
[[nodiscard]] bool get_status() const noexcept { return this->status; }
119113
void failed() noexcept; // Report failure to the object.
120114

121115
// Calculate the padding for printing this object
@@ -133,8 +127,9 @@ class Child {
133127
inline void Child::failed() noexcept {
134128
this->status = false;
135129
// propogates the failure up the tree
136-
if (this->has_parent()) { this->get_parent()->failed();
137-
}
130+
if (this->has_parent()) {
131+
this->get_parent()->failed();
132+
}
138133
}
139134

140135
/**
@@ -146,20 +141,23 @@ inline std::string Child::padding() const noexcept {
146141
}
147142

148143
inline bool Child::has_formatter() const noexcept {
149-
if (this->formatter != nullptr) { return true;
150-
}
151-
if (!this->has_parent()) { return false; // base case;
152-
}
144+
if (this->formatter != nullptr) {
145+
return true;
146+
}
147+
if (!this->has_parent()) {
148+
return false; // base case;
149+
}
153150
return parent->has_formatter();
154151
}
155152

156153
inline Formatters::BaseFormatter &Child::get_formatter() const noexcept {
157-
if (this->formatter != nullptr) { return *formatter;
158-
}
159-
if (!this->has_parent()) { std::terminate();
160-
}
154+
if (this->formatter != nullptr) {
155+
return *formatter;
156+
}
157+
if (!this->has_parent()) {
158+
std::terminate();
159+
}
161160
return parent->get_formatter();
162161
}
163162

164163
} // namespace CppSpec
165-
#endif // CPPSPEC_CHILD_HPP

include/class_description.hpp

Lines changed: 20 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
/** @file */
2-
#ifndef CPPSPEC_CLASS_DESCRIPTION_HPP
3-
#define CPPSPEC_CLASS_DESCRIPTION_HPP
42
#pragma once
53
#include <string>
64

@@ -34,15 +32,11 @@ class ClassDescription : public Description {
3432
// if there's no explicit subject given, then use
3533
// the default constructor of the given type as the implicit subject.
3634
ClassDescription(Block block)
37-
: Description(),
38-
block(block),
39-
type(" : " + Util::demangle(typeid(T).name())),
40-
subject(T()) {
35+
: Description(), block(block), type(" : " + Util::demangle(typeid(T).name())), subject(T()) {
4136
this->description = Pretty::to_word(subject);
4237
}
4338

44-
ClassDescription(std::string description, Block block)
45-
: Description(description), block(block), subject(T()) {}
39+
ClassDescription(std::string description, Block block) : Description(description), block(block), subject(T()) {}
4640

4741
ClassDescription(T subject, Block block)
4842
: Description(Pretty::to_word(subject)),
@@ -61,57 +55,45 @@ class ClassDescription : public Description {
6155

6256
template <typename U>
6357
ClassDescription(std::initializer_list<U> init_list, Block block)
64-
: block(block),
65-
type(" : " + Util::demangle(typeid(T).name())),
66-
subject(T(init_list)) {
58+
: block(block), type(" : " + Util::demangle(typeid(T).name())), subject(T(init_list)) {
6759
this->description = Pretty::to_word(subject);
6860
}
6961

7062
template <typename U>
71-
ClassDescription(std::string description, std::initializer_list<U> init_list,
72-
Block block)
63+
ClassDescription(std::string description, std::initializer_list<U> init_list, Block block)
7364
: Description(description), block(block), subject(T(init_list)) {}
7465

7566
Result it(std::string description, std::function<void(ItCD<T> &)> block);
7667
Result it(std::function<void(ItCD<T> &)> block);
7768
/** @brief an alias for it */
78-
Result specify(std::string description,
79-
std::function<void(ItCD<T> &)> block) {
80-
return it(description, block);
81-
}
69+
Result specify(std::string description, std::function<void(ItCD<T> &)> block) { return it(description, block); }
8270
/** @brief an alias for it */
8371
Result specify(std::function<void(ItCD<T> &)> block) { return it(block); }
8472

8573
template <class U = std::nullptr_t>
86-
Result context(std::string description,
87-
std::function<void(ClassDescription<T> &)> block);
74+
Result context(std::string description, std::function<void(ClassDescription<T> &)> block);
8875

8976
template <class U>
90-
Result context(std::string description, U subject,
91-
std::function<void(ClassDescription<U> &)> block);
77+
Result context(std::string description, U subject, std::function<void(ClassDescription<U> &)> block);
9278
template <class U>
93-
Result context(std::string description, U &subject,
94-
std::function<void(ClassDescription<U> &)> block);
79+
Result context(std::string description, U &subject, std::function<void(ClassDescription<U> &)> block);
9580
template <class U>
9681
Result context(U subject, std::function<void(ClassDescription<U> &)> block);
9782
template <class U>
9883
Result context(U &subject, std::function<void(ClassDescription<U> &)> block);
9984

10085
Result run(Formatters::BaseFormatter &printer) override;
10186

102-
[[nodiscard]] std::string get_subject_type() const noexcept override {
103-
return type;
104-
}
87+
[[nodiscard]] std::string get_subject_type() const noexcept override { return type; }
10588
};
10689

10790
template <class T>
10891
using ClassContext = ClassDescription<T>;
10992

11093
template <class T>
11194
template <class U>
112-
Result ClassDescription<T>::context(
113-
std::string description, U subject,
114-
std::function<void(ClassDescription<U> &)> block) {
95+
Result ClassDescription<T>::context(std::string description, U subject,
96+
std::function<void(ClassDescription<U> &)> block) {
11597
ClassContext<U> context(description, subject, block);
11698
context.set_parent(this);
11799
context.ClassContext<U>::before_eaches = this->before_eaches;
@@ -121,16 +103,14 @@ Result ClassDescription<T>::context(
121103

122104
template <class T>
123105
template <class U>
124-
Result ClassDescription<T>::context(
125-
U subject, std::function<void(ClassDescription<U> &)> block) {
106+
Result ClassDescription<T>::context(U subject, std::function<void(ClassDescription<U> &)> block) {
126107
return this->context("", std::forward<U>(subject), block);
127108
}
128109

129110
template <class T>
130111
template <class U>
131-
Result ClassDescription<T>::context(
132-
std::string description, U &subject,
133-
std::function<void(ClassDescription<U> &)> block) {
112+
Result ClassDescription<T>::context(std::string description, U &subject,
113+
std::function<void(ClassDescription<U> &)> block) {
134114
ClassContext<U> context(description, subject, block);
135115
context.set_parent(this);
136116
context.ClassContext<U>::before_eaches = this->before_eaches;
@@ -140,15 +120,13 @@ Result ClassDescription<T>::context(
140120

141121
template <class T>
142122
template <class U>
143-
Result ClassDescription<T>::context(
144-
U &subject, std::function<void(ClassDescription<U> &)> block) {
123+
Result ClassDescription<T>::context(U &subject, std::function<void(ClassDescription<U> &)> block) {
145124
return this->context("", std::forward<U>(subject), block);
146125
}
147126

148127
template <class T>
149128
template <class U>
150-
Result ClassDescription<T>::context(
151-
std::string description, std::function<void(ClassDescription<T> &)> block) {
129+
Result ClassDescription<T>::context(std::string description, std::function<void(ClassDescription<T> &)> block) {
152130
ClassContext<T> context(description, this->subject, block);
153131
context.set_parent(this);
154132
context.before_eaches = this->before_eaches;
@@ -157,14 +135,12 @@ Result ClassDescription<T>::context(
157135
}
158136

159137
template <class T>
160-
Result Description::context(T subject,
161-
std::function<void(ClassDescription<T> &)> block) {
138+
Result Description::context(T subject, std::function<void(ClassDescription<T> &)> block) {
162139
return this->context("", subject, block);
163140
}
164141

165142
template <class T>
166-
Result Description::context(std::string description, T subject,
167-
std::function<void(ClassDescription<T> &)> block) {
143+
Result Description::context(std::string description, T subject, std::function<void(ClassDescription<T> &)> block) {
168144
ClassContext<T> context(description, subject, block);
169145
context.set_parent(this);
170146
context.before_eaches = this->before_eaches;
@@ -173,8 +149,7 @@ Result Description::context(std::string description, T subject,
173149
}
174150

175151
template <class T, typename U>
176-
Result Description::context(std::initializer_list<U> init_list,
177-
std::function<void(ClassDescription<T> &)> block) {
152+
Result Description::context(std::initializer_list<U> init_list, std::function<void(ClassDescription<T> &)> block) {
178153
ClassContext<T> context(T(init_list), block);
179154
context.set_parent(this);
180155
context.before_eaches = this->before_eaches;
@@ -204,8 +179,7 @@ Result Description::context(std::initializer_list<U> init_list,
204179
* @return the result of the test
205180
*/
206181
template <class T>
207-
Result ClassDescription<T>::it(std::string name,
208-
std::function<void(ItCD<T> &)> block) {
182+
Result ClassDescription<T>::it(std::string name, std::function<void(ItCD<T> &)> block) {
209183
ItCD<T> it(*this, this->subject, name, block);
210184
Result result = it.run(this->get_formatter());
211185
exec_after_eaches();
@@ -276,4 +250,3 @@ Result ItCD<T>::run(Formatters::BaseFormatter &printer) {
276250
}
277251

278252
} // namespace CppSpec
279-
#endif // CPPSPEC_CLASS_DESCRIPTION_HPP

include/cppspec.hpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
* @file
33
* @brief The core header file for Cppspec
44
*/
5-
#ifndef CPPSPEC_HPP
6-
#define CPPSPEC_HPP
75
#pragma once
86

97
#include "argparse.hpp"
@@ -39,17 +37,14 @@
3937
#define after_each self.after_each
4038
#define let(name, body) auto(name) = self.let(body);
4139

42-
#define CPPSPEC_MAIN(spec) \
43-
int main(int argc, char **const argv) { \
44-
return CppSpec::parse(argc, argv).add_spec(spec).exec() ? EXIT_SUCCESS \
45-
: EXIT_FAILURE; \
40+
#define CPPSPEC_MAIN(spec) \
41+
int main(int argc, char **const argv) { \
42+
return CppSpec::parse(argc, argv).add_spec(spec).exec() ? EXIT_SUCCESS : EXIT_FAILURE; \
4643
}
4744

48-
#define CPPSPEC_SPEC(spec_name) \
49-
int spec_name##_spec(int argc, char **const argv) { \
50-
return CppSpec::parse(argc, argv).add_spec(spec_name).exec() \
51-
? EXIT_SUCCESS \
52-
: EXIT_FAILURE; \
45+
#define CPPSPEC_SPEC(spec_name) \
46+
int spec_name##_spec(int argc, char **const argv) { \
47+
return CppSpec::parse(argc, argv).add_spec(spec_name).exec() ? EXIT_SUCCESS : EXIT_FAILURE; \
5348
}
5449

5550
#endif
@@ -63,4 +58,3 @@ using describe_a = CppSpec::ClassDescription<T>;
6358
template <class T>
6459
using describe_an = CppSpec::ClassDescription<T>;
6560

66-
#endif // CPPSPEC_HPP

0 commit comments

Comments
 (0)