Skip to content

Commit 1e7cece

Browse files
committed
Refactor ItExpBase and Let
1 parent bd48b6a commit 1e7cece

26 files changed

+138
-141
lines changed

include/child.hpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
/** @file */
12
#ifndef CPPSPEC_CHILD_HPP
23
#define CPPSPEC_CHILD_HPP
3-
#include <string>
4+
#include <iostream>
5+
#include <typeinfo>
46

57
namespace CppSpec {
68

@@ -62,9 +64,12 @@ class Child {
6264
/** @brief Check to see if the Child has a parent. */
6365
const bool has_parent() { return parent != nullptr; }
6466

67+
// TODO: Look in to making these references instead of pointer returns
6568
/** @brief Get the Child's parent. */
6669
Child *get_parent() { return parent; }
6770
const Child *get_parent() const { return const_cast<Child *>(parent); }
71+
template <class C>
72+
C get_parent_as();
6873

6974
/** @brief Set the Child's parent */
7075
void set_parent(Child *parent) { this->parent = parent; }
@@ -107,6 +112,19 @@ std::string Child::padding() {
107112
return has_parent() ? get_parent()->padding() + " " : "";
108113
}
109114

115+
template <class C>
116+
inline C Child::get_parent_as() {
117+
// rejected branch should get optimized out at compile-time
118+
if (CPPSPEC_DEBUG) {
119+
if (C casted = dynamic_cast<C>(get_parent())) {
120+
return casted;
121+
} else
122+
throw(std::bad_cast());
123+
} else {
124+
return static_cast<C>(get_parent());
125+
}
126+
}
127+
110128
const bool Child::has_formatter() {
111129
if (this->formatter != nullptr) return true;
112130
if (!this->has_parent()) return false; // base case;
@@ -115,9 +133,11 @@ const bool Child::has_formatter() {
115133

116134
Formatters::BaseFormatter &Child::get_formatter() {
117135
if (this->formatter != nullptr) return *formatter;
118-
if (!this->has_parent())
136+
if (!this->has_parent()) {
137+
std::cout << "Couldn't get printer!" << std::endl;
119138
throw "Couldn't get printer!"; // base case. This should never *ever*
120-
// happen.
139+
// happen.
140+
}
121141
return parent->get_formatter();
122142
}
123143

include/class_description.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/** @file */
12
#ifndef CPPSPEC_CLASS_DESCRIPTION_HPP
23
#define CPPSPEC_CLASS_DESCRIPTION_HPP
34
#include "description.hpp"

include/cppspec.hpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44
*/
55
#ifndef CPPSPEC_HPP
66
#define CPPSPEC_HPP
7-
#include "formatters/tap.hpp"
8-
#include "formatters/verbose.hpp"
9-
#include "formatters/progress.hpp"
7+
8+
#ifndef CPPSPEC_DEBUG
9+
#define CPPSPEC_DEBUG false
10+
#endif
1011
#include "runner.hpp"
1112

13+
/*>>>>>>>>>>>>>>>>>>>> MACROS <<<<<<<<<<<<<<<<<<<<<<*/
14+
1215
#define _ [=](auto &self) mutable
1316
#define $ [](auto &self)
1417
#define it self.it
@@ -22,13 +25,16 @@
2225
#define before_each self.before_each
2326
#define after_all self.after_all
2427
#define after_each self.after_each
25-
#define let(name, body) \
26-
auto name = self.make_let(#name, body); \
27-
self.add_let(#name, name);
28+
#define let(name, body) auto name = self.let(body);
29+
30+
/*>>>>>>>>>>>>>>>>>>> TYPEDEFS <<<<<<<<<<<<<<<<<<<<<*/
2831

2932
typedef CppSpec::Description describe;
3033

3134
template <class T>
3235
using describe_a = CppSpec::ClassDescription<T>;
3336

37+
template <class T>
38+
using describe_an = CppSpec::ClassDescription<T>;
39+
3440
#endif // CPPSPEC_HPP

include/description.hpp

Lines changed: 14 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#ifndef CPPSPEC_DESCRIPTION_HPP
66
#define CPPSPEC_DESCRIPTION_HPP
77
#include <queue>
8-
#include <unordered_map>
98
#include "it.hpp"
109

1110
namespace CppSpec {
@@ -23,7 +22,7 @@ class Description : public Runnable {
2322
std::deque<rule_block_t> after_alls;
2423
std::deque<rule_block_t> before_eaches;
2524
std::deque<rule_block_t> after_eaches;
26-
std::unordered_map<std::string, Runnable *> lets;
25+
std::unordered_set<LetBase *> lets;
2726

2827
Description(){};
2928
Description(std::string descr) : descr(descr){};
@@ -69,11 +68,11 @@ class Description : public Runnable {
6968
void exec_after_eaches();
7069

7170
template <typename T>
72-
auto make_let(std::string name, T body) -> Let<decltype(body())>;
73-
void add_let(std::string name, Runnable &body);
74-
virtual Result run(Formatters::BaseFormatter &printer) override;
71+
auto let(T body) -> Let<decltype(body())>;
7572
void reset_lets();
76-
Runnable *find_let(std::string);
73+
74+
virtual Result run(Formatters::BaseFormatter &printer) override;
75+
7776
virtual std::string get_descr() { return descr; }
7877
virtual const std::string get_descr() const { return descr; }
7978
virtual std::string get_subject_type() { return ""; }
@@ -156,12 +155,10 @@ void Description::exec_after_eaches() {
156155
* @return a new Let object
157156
*/
158157
template <typename T>
159-
auto Description::make_let(std::string name, T body) -> Let<decltype(body())> {
160-
return Let<decltype(body())>(*this, name, body);
161-
}
162-
163-
void Description::add_let(std::string name, Runnable &body) {
164-
lets.insert({name, &body});
158+
auto Description::let(T body) -> Let<decltype(body())> {
159+
Let<decltype(body())> let(body);
160+
lets.insert(&let);
161+
return let;
165162
}
166163

167164
Result Description::run(Formatters::BaseFormatter &printer) {
@@ -174,55 +171,16 @@ Result Description::run(Formatters::BaseFormatter &printer) {
174171
}
175172

176173
void Description::reset_lets() {
177-
for (auto &pair : lets) {
178-
auto base = static_cast<LetBase *>(pair.second); // downcast.
179-
base->reset();
180-
}
181-
182-
if (has_parent()) {
183-
auto parent = static_cast<Description *>(get_parent());
184-
parent->reset_lets();
185-
}
186-
}
187-
188-
Runnable *Description::find_let(std::string name) {
189-
auto got = lets.find(name);
190-
191-
if (got == lets.end()) {
192-
if (this->has_parent()) {
193-
auto parent = static_cast<Description *>(get_parent());
194-
return parent->find_let(name);
195-
} else { // this should never, *ever* happen.
196-
throw(std::out_of_range("Could not find let '" + name + "'"));
197-
}
198-
} else {
199-
return got->second;
200-
}
201-
}
202-
203-
template <class T>
204-
Expectations::Expectation<T> ItExpBase::expect(Let<T> let) {
205-
auto parent = static_cast<Description *>(this->get_parent());
206-
Let<T> *actual_let = static_cast<Let<T> *>(parent->find_let(let.get_name()));
207-
T res = actual_let->get_result();
208-
Expectations::Expectation<T> expectation(*this, res);
209-
return expectation;
174+
for (auto &let : lets) let->reset();
175+
if (this->has_parent()) this->get_parent_as<Description *>()->reset_lets();
210176
}
211177

212178
Result ItD::run(Formatters::BaseFormatter &printer) {
213-
// if (!this->needs_descr() && printer.mode == BaseFormatter::Mode::verbose) {
214-
// printer.format(*this);
215-
// }
216-
217179
body(*this);
218-
219180
printer.format(*this);
220-
221-
auto parent = static_cast<Description *>(this->get_parent());
222-
parent->reset_lets();
223-
181+
this->get_parent_as<Description *>()->reset_lets();
224182
return this->get_status() ? Result::success : Result::failure;
225183
}
226184

227-
} // ::CppSpec
228-
#endif // CPPSPEC_DESCRIPTION_HPP
185+
} // ::CppSpec
186+
#endif // CPPSPEC_DESCRIPTION_HPP

include/expectations/expectation.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*/
55
#ifndef CPPSPEC_EXPECTATIONS_EXPECTATION_HPP
66
#define CPPSPEC_EXPECTATIONS_EXPECTATION_HPP
7-
#include "let.hpp"
87
#include "matchers/be.hpp"
98
#include "matchers/be_between.hpp"
109
#include "matchers/be_within.hpp"

include/expectations/handler.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
/** @file */
12
#ifndef CPPSPEC_EXPECTATIONS_HANDLER_HPP
23
#define CPPSPEC_EXPECTATIONS_HANDLER_HPP
3-
#include "formatters/formatters_base.hpp"
4+
#include "result.hpp"
45

56
namespace CppSpec {
67
namespace Expectations {

include/formatters/formatters_base.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/** @file */
12
#ifndef CPPSPEC_FORMATTERS_FORMATTERS_BASE
23
#define CPPSPEC_FORMATTERS_FORMATTERS_BASE
34
#include <iostream>

include/formatters/progress.hpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1+
/** @file */
12
#ifndef CPPSPEC_FORMATTERS_PROGRESS_HPP
23
#define CPPSPEC_FORMATTERS_PROGRESS_HPP
3-
#include <list>
4-
#include <memory>
54
#include <forward_list>
6-
#include "util.hpp"
7-
#include "formatters_base.hpp"
8-
#include "it_base.hpp"
5+
#include "verbose.hpp"
96

107
namespace CppSpec {
118
namespace Formatters {

include/formatters/tap.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
/** @file */
12
#ifndef CPPSPEC_FORMATTERS_TAP_HPP
23
#define CPPSPEC_FORMATTERS_TAP_HPP
3-
#include <sstream>
44
#include <list>
55
#include "formatters_base.hpp"
6-
#include "description.hpp"
6+
#include "class_description.hpp"
77

88
namespace CppSpec {
99
namespace Formatters {

include/formatters/term_colors.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/** @file */
12
#ifndef CPPSPEC_TERM_COLORS_HPP
23
#define CPPSPEC_TERM_COLORS_HPP
34
// the following are Unix/BASH ONLY terminal color codes.

0 commit comments

Comments
 (0)