Created
June 28, 2014 20:54
-
-
Save toroidal-code/bcb771510ef3b54f8c72 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <functional> | |
#include <string> | |
#include <vector> | |
#include <iostream> | |
using namespace std; | |
struct Runnable { | |
virtual ~Runnable(){}; | |
virtual int operator()() = 0; | |
}; | |
struct TaskContainer; | |
typedef function<void(TaskContainer&)> block_t; | |
struct It : public Runnable { | |
string descr; | |
function<int(It&)> body; | |
int operator()() { | |
std::cout << "Called It::()" << std::endl; | |
return body(*this); | |
} | |
It(string descr, function<int(It&)> body) : descr(descr), body(body) {} | |
}; | |
struct TaskContainer : public Runnable { | |
vector <Runnable*> tasks; | |
void it(string name, function<int(It&)> body) { | |
std::cout << "Called TaskContainer::it" << std::endl; | |
tasks.push_back(new It(name, body)); | |
}; | |
int operator()() { | |
std::cout << "Called TaskContainer::()" << std::endl; | |
int result = 0; | |
for (Runnable *task : tasks) { | |
result |= (*task)(); | |
} | |
return result; | |
} | |
}; | |
class Description : public TaskContainer { | |
string descr; | |
block_t body; | |
public: | |
Description(string descr, block_t body) : descr(descr), body(body) {}; | |
int operator()(){ | |
body(*this); | |
return TaskContainer::operator()(); | |
} | |
}; | |
#define _desc [](TaskContainer &self) | |
#define _it [](It &self) | |
#define it self.it | |
typedef Description describe; | |
int main(void) { | |
describe my_test("my test", _desc{ | |
it("adds two numbers", _it{ | |
return 2 + 2 ; | |
}); | |
}); | |
cout << my_test(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment