Skip to content

Commit 1506371

Browse files
committed
Adding some const-ness to make things better
1 parent 6b10cfd commit 1506371

File tree

12 files changed

+325
-247
lines changed

12 files changed

+325
-247
lines changed

include/child.hpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ class Child {
6262

6363
// Custom constructors
6464
explicit Child(Child &parent) noexcept : parent(&parent) {}
65+
explicit Child(const Child &parent) noexcept
66+
: parent(&const_cast<Child &>(parent)) {}
6567
explicit Child(Child *parent) noexcept : parent(parent) {}
6668
explicit Child(const Child *parent) noexcept
6769
: parent(const_cast<Child *>(parent)) {}
@@ -70,6 +72,7 @@ class Child {
7072

7173
/** @brief Check to see if the Child has a parent. */
7274
const bool has_parent() noexcept { return parent != nullptr; }
75+
const bool has_parent() const noexcept { return parent != nullptr; }
7376

7477
// TODO: Look in to making these references instead of pointer returns
7578
/** @brief Get the Child's parent. */
@@ -83,6 +86,11 @@ class Child {
8386
return static_cast<C>(get_parent());
8487
}
8588

89+
template <class C>
90+
C get_parent_as() const noexcept {
91+
return static_cast<C>(get_parent());
92+
}
93+
8694
/** @brief Set the Child's parent */
8795
void set_parent(Child *parent) noexcept { this->parent = parent; }
8896
void set_parent(const Child *parent) noexcept {
@@ -110,6 +118,7 @@ class Child {
110118

111119
// Calculate the padding for printing this object
112120
std::string padding() noexcept;
121+
std::string padding() const noexcept;
113122
};
114123

115124
/*>>>>>>>>>>>>>>>>>>>> Child IMPLEMENTATION <<<<<<<<<<<<<<<<<<<<<<<<<*/
@@ -123,15 +132,19 @@ class Child {
123132
inline void Child::failed() noexcept {
124133
this->status = false;
125134
// propogates the failure up the tree
126-
if (has_parent()) this->get_parent()->failed();
135+
if (this->has_parent()) this->get_parent()->failed();
127136
}
128137

129138
/**
130139
* @brief Generate padding (indentation) fore the current object.
131140
* @return A string of spaces for use in pretty-printing.
132141
*/
133142
inline std::string Child::padding() noexcept {
134-
return has_parent() ? get_parent()->padding() + " " : "";
143+
return this->has_parent() ? this->get_parent()->padding() + " " : "";
144+
}
145+
146+
inline std::string Child::padding() const noexcept {
147+
return this->has_parent() ? this->get_parent()->padding() + " " : "";
135148
}
136149

137150
inline const bool Child::has_formatter() noexcept {

include/class_description.hpp

Lines changed: 72 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ namespace CppSpec {
1919
*/
2020
template <class T>
2121
class ClassDescription : public Description {
22-
typedef std::function<void(ClassDescription<T> &)> block_t;
23-
block_t body;
24-
bool first;
22+
using Block = std::function<void(ClassDescription<T> &)>;
23+
24+
Block block;
2525
std::string type = "";
2626

2727
public:
@@ -30,80 +30,80 @@ class ClassDescription : public Description {
3030
// Constructor
3131
// if there's no explicit subject given, then use
3232
// the default constructor of the given type as the implicit subject.
33-
ClassDescription<T>(block_t body)
33+
ClassDescription<T>(Block block)
3434
: Description(),
35-
body(body),
35+
block(block),
3636
type(" : " + Util::demangle(typeid(T).name())),
3737
subject(T()) {
38-
this->descr = Pretty::to_word(subject);
38+
this->description = Pretty::to_word(subject);
3939
}
4040

41-
ClassDescription<T>(std::string descr, block_t body)
42-
: Description(descr), body(body), subject(T()) {}
41+
ClassDescription<T>(std::string description, Block block)
42+
: Description(description), block(block), subject(T()) {}
4343

44-
ClassDescription(T subject, block_t body)
44+
ClassDescription(T subject, Block block)
4545
: Description(Pretty::to_word(subject)),
46-
body(body),
46+
block(block),
4747
type(" : " + Util::demangle(typeid(T).name())),
4848
subject(subject) {}
4949

50-
ClassDescription(std::string descr, T subject, block_t body)
51-
: Description(descr), body(body), subject(subject) {}
50+
ClassDescription(std::string description, T subject, Block block)
51+
: Description(description), block(block), subject(subject) {}
5252

53-
ClassDescription(T &subject, block_t body)
53+
ClassDescription(T &subject, Block block)
5454
: Description(Pretty::to_word(subject)),
55-
body(body),
55+
block(block),
5656
type(" : " + Util::demangle(typeid(T).name())),
5757
subject(subject) {}
5858

59-
// ClassDescription(std::string descr, T &subject, block_t body)
60-
// : Description(descr), body(body), subject(subject) {}
61-
6259
template <typename U>
63-
ClassDescription(std::initializer_list<U> init_list, block_t body)
64-
: body(body),
60+
ClassDescription(std::initializer_list<U> init_list, Block block)
61+
: block(block),
6562
type(" : " + Util::demangle(typeid(T).name())),
6663
subject(T(init_list)) {
67-
this->descr = Pretty::to_word(subject);
64+
this->description = Pretty::to_word(subject);
6865
}
6966

7067
template <typename U>
71-
ClassDescription(std::string descr, std::initializer_list<U> init_list,
72-
block_t body)
73-
: Description(descr), body(body), subject(T(init_list)) {}
68+
ClassDescription(std::string description, std::initializer_list<U> init_list,
69+
Block block)
70+
: Description(description), block(block), subject(T(init_list)) {}
7471

7572
ClassDescription<T>(Description &d) : Description(d) {}
7673

7774
const bool has_subject = true;
7875

79-
Result it(std::string descr, std::function<void(ItCd<T> &)> body);
80-
Result it(std::function<void(ItCd<T> &)> body);
76+
Result it(std::string description, std::function<void(ItCD<T> &)> block);
77+
Result it(std::function<void(ItCD<T> &)> block);
8178
/** @brief an alias for it */
82-
Result specify(std::string descr, std::function<void(ItCd<T> &)> body) {
83-
return it(descr, body);
79+
Result specify(std::string description,
80+
std::function<void(ItCD<T> &)> block) {
81+
return it(description, block);
8482
}
8583
/** @brief an alias for it */
86-
Result specify(std::function<void(ItCd<T> &)> body) { return it(body); }
84+
Result specify(std::function<void(ItCD<T> &)> block) { return it(block); }
8785

8886
template <class U>
89-
Result context(std::string descr, U subject,
90-
std::function<void(ClassDescription<U> &)> body);
87+
Result context(std::string description, U subject,
88+
std::function<void(ClassDescription<U> &)> block);
9189
template <class U>
92-
Result context(std::string descr, U &subject,
93-
std::function<void(ClassDescription<U> &)> body);
90+
Result context(std::string description, U &subject,
91+
std::function<void(ClassDescription<U> &)> block);
9492
template <class U>
95-
Result context(U subject, std::function<void(ClassDescription<U> &)> body);
93+
Result context(U subject, std::function<void(ClassDescription<U> &)> block);
9694
template <class U>
97-
Result context(U &subject, std::function<void(ClassDescription<U> &)> body);
95+
Result context(U &subject, std::function<void(ClassDescription<U> &)> block);
9896

99-
Result context(std::string descr, std::function<void(ClassDescription<T> &)> body);
97+
Result context(std::string description,
98+
std::function<void(ClassDescription<T> &)> block);
10099

101100
Result run(Formatters::BaseFormatter &printer) override;
102101

103-
std::string get_descr() override { return descr; }
104-
const std::string get_descr() const override { return descr; }
105-
std::string get_subject_type() override { return type; }
106-
const std::string get_subject_type() const override { return type; }
102+
// std::string get_descr() noexcept override { return description; }
103+
// const std::string get_descr() const noexcept override { return
104+
// description; }
105+
std::string get_subject_type() noexcept override { return type; }
106+
const std::string get_subject_type() const noexcept override { return type; }
107107
};
108108

109109
template <class T>
@@ -112,9 +112,9 @@ using ClassContext = ClassDescription<T>;
112112
template <class T>
113113
template <class U>
114114
Result ClassDescription<T>::context(
115-
std::string descr, U subject,
116-
std::function<void(ClassDescription<U> &)> body) {
117-
ClassContext<U> context(descr, subject, body);
115+
std::string description, U subject,
116+
std::function<void(ClassDescription<U> &)> block) {
117+
ClassContext<U> context(description, subject, block);
118118
context.set_parent(this);
119119
context.ClassContext<U>::before_eaches = this->before_eaches;
120120
context.ClassContext<U>::after_eaches = this->after_eaches;
@@ -124,16 +124,16 @@ Result ClassDescription<T>::context(
124124
template <class T>
125125
template <class U>
126126
Result ClassDescription<T>::context(
127-
U subject, std::function<void(ClassDescription<U> &)> body) {
128-
return context("", std::forward<U>(subject), body);
127+
U subject, std::function<void(ClassDescription<U> &)> block) {
128+
return this->context("", std::forward<U>(subject), block);
129129
}
130130

131131
template <class T>
132132
template <class U>
133133
Result ClassDescription<T>::context(
134-
std::string descr, U &subject,
135-
std::function<void(ClassDescription<U> &)> body) {
136-
ClassContext<U> context(descr, subject, body);
134+
std::string description, U &subject,
135+
std::function<void(ClassDescription<U> &)> block) {
136+
ClassContext<U> context(description, subject, block);
137137
context.set_parent(this);
138138
context.ClassContext<U>::before_eaches = this->before_eaches;
139139
context.ClassContext<U>::after_eaches = this->after_eaches;
@@ -143,14 +143,14 @@ Result ClassDescription<T>::context(
143143
template <class T>
144144
template <class U>
145145
Result ClassDescription<T>::context(
146-
U &subject, std::function<void(ClassDescription<U> &)> body) {
147-
return context("", std::forward<U>(subject), body);
146+
U &subject, std::function<void(ClassDescription<U> &)> block) {
147+
return this->context("", std::forward<U>(subject), block);
148148
}
149149

150150
template <class T>
151-
Result ClassDescription<T>::context(std::string descr,
152-
std::function<void(ClassDescription<T> &)> body) {
153-
ClassContext<T> context(descr, this->subject, body);
151+
Result ClassDescription<T>::context(
152+
std::string description, std::function<void(ClassDescription<T> &)> block) {
153+
ClassContext<T> context(description, this->subject, block);
154154
context.set_parent(this);
155155
context.before_eaches = this->before_eaches;
156156
context.after_eaches = this->after_eaches;
@@ -159,31 +159,24 @@ Result ClassDescription<T>::context(std::string descr,
159159

160160
template <class T>
161161
Result Description::context(T subject,
162-
std::function<void(ClassDescription<T> &)> body) {
163-
return this->context("", subject, body);
162+
std::function<void(ClassDescription<T> &)> block) {
163+
return this->context("", subject, block);
164164
}
165165

166166
template <class T>
167-
Result Description::context(std::string descr, T subject,
168-
std::function<void(ClassDescription<T> &)> body) {
169-
ClassContext<T> context(descr, subject, body);
167+
Result Description::context(std::string description, T subject,
168+
std::function<void(ClassDescription<T> &)> block) {
169+
ClassContext<T> context(description, subject, block);
170170
context.set_parent(this);
171171
context.before_eaches = this->before_eaches;
172172
context.after_eaches = this->after_eaches;
173173
return context.run(this->get_formatter());
174174
}
175175

176-
177-
// template <class T>
178-
// ClassContext<T>& Description::context(
179-
// T& subject, std::function<void(ClassDescription<T>&)> body) {
180-
// return context(subject, body);
181-
// }
182-
183176
template <class T, typename U>
184177
Result Description::context(std::initializer_list<U> init_list,
185-
std::function<void(ClassDescription<T> &)> body) {
186-
ClassContext<T> context(T(init_list), body);
178+
std::function<void(ClassDescription<T> &)> block) {
179+
ClassContext<T> context(T(init_list), block);
187180
context.set_parent(this);
188181
context.before_eaches = this->before_eaches;
189182
context.after_eaches = this->after_eaches;
@@ -194,7 +187,7 @@ Result Description::context(std::initializer_list<U> init_list,
194187
* Jasmine-style `it` declaration, with an explicit docstring
195188
* provided for verbose printing.
196189
*
197-
* As this is an ItCd, it passes along associated type information
190+
* As this is an ItCD, it passes along associated type information
198191
* about the implicit subject from the containing
199192
* ClassDescription / ClassContext.
200193
*
@@ -207,14 +200,14 @@ Result Description::context(std::initializer_list<U> init_list,
207200
* @endcode
208201
*
209202
* @param name the name of the test
210-
* @param body the contents of test
203+
* @param block the contents of test
211204
*
212205
* @return the result of the test
213206
*/
214207
template <class T>
215208
Result ClassDescription<T>::it(std::string name,
216-
std::function<void(ItCd<T> &)> body) {
217-
ItCd<T> it(*this, this->subject, name, body);
209+
std::function<void(ItCD<T> &)> block) {
210+
ItCD<T> it(*this, this->subject, name, block);
218211
Result result = it.run(this->get_formatter());
219212
exec_after_eaches();
220213
exec_before_eaches();
@@ -228,7 +221,7 @@ Result ClassDescription<T>::it(std::string name,
228221
* is a desire to be verbose, as each expectation prints its
229222
* own docstring.
230223
*
231-
* As this is an ItCd, it passes along associated type information
224+
* As this is an ItCD, it passes along associated type information
232225
* about the implicit subject from the containing
233226
* ClassDescription / ClassContext.
234227
*
@@ -238,13 +231,13 @@ Result ClassDescription<T>::it(std::string name,
238231
* });
239232
* @endcode
240233
*
241-
* @param body the contents of the test
234+
* @param block the contents of the test
242235
*
243236
* @return the result of the test
244237
*/
245238
template <class T>
246-
Result ClassDescription<T>::it(std::function<void(ItCd<T> &)> body) {
247-
ItCd<T> it(*this, this->subject, body);
239+
Result ClassDescription<T>::it(std::function<void(ItCD<T> &)> block) {
240+
ItCD<T> it(*this, this->subject, block);
248241
Result result = it.run(this->get_formatter());
249242
exec_after_eaches();
250243
exec_before_eaches();
@@ -255,29 +248,23 @@ template <class T>
255248
Result ClassDescription<T>::run(Formatters::BaseFormatter &printer) {
256249
if (not this->has_formatter()) this->set_printer(printer);
257250
printer.format(*this);
258-
body(*this);
259-
for (auto a : after_alls) a();
251+
this->block(*this);
252+
for (const auto &a : after_alls) a();
260253
if (this->get_parent() == nullptr) printer.flush();
261254
return this->get_status() ? Result::success() : Result::failure();
262255
}
263256

264257
template <class T>
265-
Expectations::ExpectationValue <T> ItCd<T>::is_expected() {
258+
Expectations::ExpectationValue<T> ItCD<T>::is_expected() {
266259
auto cd = static_cast<ClassDescription<T> *>(this->get_parent());
267260
Expectations::ExpectationValue<T> expectation(*this, cd->subject);
268261
return expectation;
269262
}
270263

271264
template <class T>
272-
Result ItCd<T>::run(Formatters::BaseFormatter &printer) {
273-
// if (!this->needs_descr() && printer.mode == BaseFormatter::Mode::verbose)
274-
// {
275-
// printer.format(*this);
276-
// }
277-
278-
body(*this);
265+
Result ItCD<T>::run(Formatters::BaseFormatter &printer) {
266+
this->block(*this);
279267
printer.format(*this);
280-
281268
auto cd = static_cast<ClassDescription<T> *>(this->get_parent());
282269
cd->reset_lets();
283270
return this->get_status() ? Result::success() : Result::failure();

0 commit comments

Comments
 (0)