Last active
March 15, 2019 21:42
-
-
Save haampie/b0c25450b18f186738e72af5cd6e0410 to your computer and use it in GitHub Desktop.
everything_compile_time.cpp
This file contains hidden or 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 <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{"[email protected]"} | |
}; | |
} | |
auto when() | |
{ | |
return EmailChanged{"[email protected]"}; | |
} | |
bool then(User const &user) | |
{ | |
return user.getEmail() == "[email protected]"; | |
} | |
}; | |
int main() { | |
UserScenarioTest test; | |
test.run(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment