Skip to content

Instantly share code, notes, and snippets.

@haampie
Last active March 15, 2019 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haampie/b0c25450b18f186738e72af5cd6e0410 to your computer and use it in GitHub Desktop.
Save haampie/b0c25450b18f186738e72af5cd6e0410 to your computer and use it in GitHub Desktop.
everything_compile_time.cpp
#include <iostream>
#include <vector>
#include <string>
#include <tuple>
using namespace std;
// Some instances of events; we're using "public" const data members.
struct UsernameChanged {
string const username;
};
struct EmailChanged {
string const email;
};
// Example implementation of a store, where we simply store a username & email
// of a user.
class User {
string username;
string email;
public:
void handle(UsernameChanged const &event) {
username = event.username;
}
void handle(EmailChanged const &event) {
email = event.email;
}
// Some getters
string getUsername() const {
return username;
}
string getEmail() const {
return email;
}
};
template<class Store, class Test>
class Scenario
{
public:
void run() {
Store store;
auto test = static_cast<Test*>(this);
// Apply given
apply([&store](auto&&... args){
(store.handle(args), ...);
}, test->given());
// Apply when
store.handle(test->when());
// Check then.
cout << (test->then(store) ? "Test OK!\n" : "Test failed.\n");
}
};
class UserScenarioTest : public Scenario<User, UserScenarioTest>
{
public:
auto given()
{
return tuple{
UsernameChanged{"Henk"},
EmailChanged{"henk@henk.com"}
};
}
auto when()
{
return EmailChanged{"foo@bar.com"};
}
bool then(User const &user)
{
return user.getEmail() == "foo@bar.com";
}
};
int main() {
UserScenarioTest test;
test.run();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment