File tree Expand file tree Collapse file tree 3 files changed +51
-1
lines changed Expand file tree Collapse file tree 3 files changed +51
-1
lines changed Original file line number Diff line number Diff line change 14
14
#define before_each self.before_each
15
15
#define after_all self.after_all
16
16
#define after_each self.after_each
17
+ #define let (name, body ) \
18
+ auto name = make_let(#name, body); \
19
+ self.let(#name, name)
17
20
18
21
typedef Description describe;
19
22
Original file line number Diff line number Diff line change 3
3
4
4
#include < string>
5
5
#include < queue>
6
- #include < map >
6
+ #include < unordered_map >
7
7
#include < functional>
8
8
#include < iostream>
9
9
#include < memory>
10
10
#include " util.hpp"
11
11
#include " runnable.hpp"
12
12
#include " it.hpp"
13
+ #include " let.hpp"
13
14
14
15
template <class T >
15
16
class ClassDescription ;
@@ -24,6 +25,7 @@ class Description : public Runnable {
24
25
std::deque<rule_block_t > after_alls;
25
26
std::deque<rule_block_t > before_eaches;
26
27
std::deque<rule_block_t > after_eaches;
28
+ std::unordered_map<std::string, Runnable *> lets;
27
29
28
30
explicit Description (std::string descr) : descr(descr){};
29
31
@@ -64,6 +66,8 @@ class Description : public Runnable {
64
66
void exec_before_eaches ();
65
67
void exec_after_eaches ();
66
68
69
+ void let (std::string name, Runnable &body);
70
+
67
71
bool run ();
68
72
};
69
73
@@ -144,6 +148,11 @@ void Description::exec_after_eaches() {
144
148
for (rule_block_t b : after_eaches) b ();
145
149
}
146
150
151
+ void Description::let (std::string name, Runnable &body) {
152
+ auto p = lets.insert ({name, &body});
153
+ if (!p.second ) p.first ->second = &body; // assign if the key is already there
154
+ }
155
+
147
156
bool Description::run () {
148
157
std::cout << padding () << descr << std::endl;
149
158
body (*this );
Original file line number Diff line number Diff line change
1
+ #ifndef LET_H
2
+ #define LET_H
3
+ #include < functional>
4
+ #include < type_traits>
5
+ #include < iostream>
6
+ #include " runnable.hpp"
7
+
8
+ template <typename T>
9
+ class Let : public Runnable {
10
+ typedef std::function<T()> block_t ;
11
+
12
+ std::string name;
13
+ block_t body;
14
+
15
+ public:
16
+ explicit Let (std::string name, block_t body) : name(name), body(body){};
17
+ bool run ();
18
+ };
19
+
20
+ template <typename T>
21
+ bool Let<T>::run() {
22
+ body ();
23
+ return true ;
24
+ }
25
+
26
+ /* *
27
+ * @brief Object generator for Let.
28
+ *
29
+ * @param body the body of the let statement
30
+ *
31
+ * @return a new Let object
32
+ */
33
+ template <typename T>
34
+ auto make_let (std::string name, T body) -> Let<decltype(body())> {
35
+ return Let<decltype (body ())>(name, body);
36
+ }
37
+
38
+ #endif /* LET_H */
You can’t perform that action at this time.
0 commit comments