Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <vector>
- #include <utility>
- #include <stdexcept>
- #include <algorithm>
- #include <memory>
- #include <iostream>
- #include <cassert>
- class AbstractTest {
- public:
- virtual void SetUp() = 0;
- virtual void TearDown() = 0;
- virtual void Run() = 0;
- virtual ~AbstractTest() {
- }
- };
- class TestProducer {
- public:
- virtual std::unique_ptr<AbstractTest> Produce() = 0;
- };
- template <class TestType>
- class Producer : public TestProducer {
- public:
- Producer() = default;
- std::unique_ptr<AbstractTest> Produce() override {
- return std::make_unique<TestType>();
- }
- };
- class FullMatch {
- public:
- FullMatch(const std::string& s) : s_(s) {
- }
- bool operator()(const std::string& s) {
- return (s_ == s);
- }
- private:
- std::string s_;
- };
- class Substr {
- public:
- Substr(const std::string& s) : s_(s) {
- }
- bool operator()(const std::string& s) {
- return (s.find(s_) != std::string::npos);
- }
- private:
- std::string s_;
- };
- class TestRegistry {
- public:
- template <class TestClass>
- void RegisterClass(const std::string& class_name) {
- registry_.emplace_back(class_name);
- factory_.emplace_back(std::make_unique<Producer<TestClass>>());
- }
- std::unique_ptr<AbstractTest> CreateTest(const std::string& class_name) {
- int index = -1;
- for (int i = 0; i < registry_.size(); ++i) {
- if (class_name == registry_[i]) {
- index = i;
- break;
- }
- }
- if (index == -1) {
- throw std::out_of_range("test not found");
- }
- return factory_[index]->Produce();
- }
- void RunTest(const std::string& test_name) {
- }
- template <class Predicate>
- std::vector<std::string> ShowTests(Predicate callback) const {
- std::vector<std::string> ans;
- for (auto s : registry_) {
- if (callback(s)) {
- ans.push_back(s);
- }
- }
- return ans;
- }
- std::vector<std::string> ShowAllTests() const {
- std::vector<std::string> s_r = registry_;
- std::sort(s_r.begin(), s_r.end());
- return s_r;
- }
- template <class Predicate>
- void RunTests(Predicate callback) {
- for (auto s : ShowAllTests()) {
- }
- }
- void Clear() {
- }
- static TestRegistry& Instance() {
- return *test_registry_;
- }
- TestRegistry(const TestRegistry&) = delete;
- TestRegistry(TestRegistry&&) = delete;
- TestRegistry& operator=(const TestRegistry&) = delete;
- TestRegistry& operator=(TestRegistry&&) = delete;
- private:
- TestRegistry() {
- }
- static std::vector<std::string> registry_;
- static std::vector<std::unique_ptr<TestProducer>> factory_;
- static std::unique_ptr<TestRegistry> test_registry_;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement