Advertisement
anechka_ne_plach

Untitled

Oct 29th, 2021
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.88 KB | None | 0 0
  1. #include <string>
  2. #include <vector>
  3. #include <utility>
  4. #include <stdexcept>
  5. #include <algorithm>
  6. #include <memory>
  7. #include <iostream>
  8. #include <cassert>
  9.  
  10. class AbstractTest {
  11. public:
  12.     virtual void SetUp() = 0;
  13.     virtual void TearDown() = 0;
  14.     virtual void Run() = 0;
  15.     virtual ~AbstractTest() {
  16.     }
  17. };
  18.  
  19. class TestProducer {
  20. public:
  21.     virtual std::unique_ptr<AbstractTest> Produce() = 0;
  22. };
  23.  
  24. template <class TestType>
  25. class Producer : public TestProducer {
  26. public:
  27.     Producer() = default;
  28.     std::unique_ptr<AbstractTest> Produce() override {
  29.         return std::make_unique<TestType>();
  30.     }
  31. };
  32.  
  33. class FullMatch {
  34. public:
  35.     FullMatch(const std::string& s) : s_(s) {
  36.     }
  37.     bool operator()(const std::string& s) {
  38.         return (s_ == s);
  39.     }
  40.  
  41. private:
  42.     std::string s_;
  43. };
  44.  
  45. class Substr {
  46. public:
  47.     Substr(const std::string& s) : s_(s) {
  48.     }
  49.     bool operator()(const std::string& s) {
  50.         return (s.find(s_) != std::string::npos);
  51.     }
  52.  
  53. private:
  54.     std::string s_;
  55. };
  56.  
  57. class TestRegistry {
  58. public:
  59.     template <class TestClass>
  60.     void RegisterClass(const std::string& class_name) {
  61.         registry_.emplace_back(class_name);
  62.         factory_.emplace_back(std::make_unique<Producer<TestClass>>());
  63.     }
  64.  
  65.     std::unique_ptr<AbstractTest> CreateTest(const std::string& class_name) {
  66.         int index = -1;
  67.         for (int i = 0; i < registry_.size(); ++i) {
  68.             if (class_name == registry_[i]) {
  69.                 index = i;
  70.                 break;
  71.             }
  72.         }
  73.         if (index == -1) {
  74.             throw std::out_of_range("test not found");
  75.         }
  76.         return factory_[index]->Produce();
  77.     }
  78.  
  79.     void RunTest(const std::string& test_name) {
  80.  
  81.     }
  82.  
  83.     template <class Predicate>
  84.     std::vector<std::string> ShowTests(Predicate callback) const {
  85.         std::vector<std::string> ans;
  86.         for (auto s : registry_) {
  87.             if (callback(s)) {
  88.                 ans.push_back(s);
  89.             }
  90.         }
  91.         return ans;
  92.     }
  93.  
  94.     std::vector<std::string> ShowAllTests() const {
  95.         std::vector<std::string> s_r = registry_;
  96.         std::sort(s_r.begin(), s_r.end());
  97.         return s_r;
  98.     }
  99.  
  100.     template <class Predicate>
  101.     void RunTests(Predicate callback) {
  102.         for (auto s : ShowAllTests()) {
  103.  
  104.         }
  105.     }
  106.  
  107.     void Clear() {
  108.     }
  109.  
  110.     static TestRegistry& Instance() {
  111.         return *test_registry_;
  112.     }
  113.  
  114.     TestRegistry(const TestRegistry&) = delete;
  115.     TestRegistry(TestRegistry&&) = delete;
  116.     TestRegistry& operator=(const TestRegistry&) = delete;
  117.     TestRegistry& operator=(TestRegistry&&) = delete;
  118.  
  119. private:
  120.     TestRegistry() {
  121.     }
  122.     static std::vector<std::string> registry_;
  123.     static std::vector<std::unique_ptr<TestProducer>> factory_;
  124.     static std::unique_ptr<TestRegistry> test_registry_;
  125. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement