Advertisement
anechka_ne_plach

Untitled

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