OTest2
A C++ testing framework
repeatermulti.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020 Ondrej Starek
3  *
4  * This file is part of OTest2
5  *
6  * OTest2 is free software: you can redistribute it and/or modify it under
7  * the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License,
9  * or (at your option) any later version.
10  *
11  * OTest2 is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14  * License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with OTest2. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef OTest2_INCLUDE_OTEST2_REPEATERMULTI_H_
21 #define OTest2_INCLUDE_OTEST2_REPEATERMULTI_H_
22 
23 #include <assert.h>
24 #include <functional>
25 #include <iostream>
26 #include <memory>
27 #include <otest2/caseptr.h>
28 #include <otest2/caserepeater.h>
29 #include <otest2/suiteptr.h>
30 #include <otest2/suiterepeater.h>
31 #include <sstream>
32 #include <string>
33 
34 namespace OTest2 {
35 
36 template<typename Suite_, typename Repeater_>
37 class SuiteRepeaterMulti : public SuiteRepeater {
38  private:
39  int index;
40  std::shared_ptr<Repeater_> repeater_object;
41 
42  public:
43  /* -- avoid copying */
45  const SuiteRepeaterMulti&) = delete;
47  const SuiteRepeaterMulti&) = delete;
48 
49  explicit SuiteRepeaterMulti() :
50  index(1),
51  repeater_object() {
52 
53  }
54 
55  virtual ~SuiteRepeaterMulti() = default;
56 
57  virtual bool isNextRun(
58  const Context& context_) const override {
59  /* -- first run */
60  if(repeater_object == nullptr)
61  return true;
62  /* -- following runs */
63  return repeater_object->hasNextRun(context_);
64  }
65 
66  virtual std::string transformName(
67  const Context& context_,
68  const std::string& suite_name_) const override {
69  std::ostringstream oss_;
70  oss_ << suite_name_ << " (" << index << ")";
71  return oss_.str();
72  }
73 
74  virtual SuitePtr createSuite(
75  const Context& context_,
76  const std::string& case_name_) override {
77  ++index;
78  return makePointer<Suite_>(context_, std::ref(repeater_object));
79  }
80 };
81 
82 template<typename Suite_, typename Case_, typename Repeater_>
83 class CaseRepeaterMulti : public CaseRepeater {
84  public:
85  typedef CasePtr (Suite_::* FactoryMethod)(
86  const Context&,
87  std::shared_ptr<Repeater_>&);
88 
89  private:
90  int index;
91  std::shared_ptr<Repeater_> repeater_object;
92  Suite_* suite;
93  FactoryMethod factory_method;
94 
95  public:
96  /* -- avoid copying */
98  const CaseRepeaterMulti&) = delete;
100  const CaseRepeaterMulti&) = delete;
101 
103  Suite_* suite_,
104  FactoryMethod factory_method_) :
105  index(1),
106  repeater_object(),
107  suite(suite_),
108  factory_method(factory_method_) {
109  assert(suite != nullptr && factory_method != nullptr);
110 
111  }
112 
113  virtual ~CaseRepeaterMulti() = default;
114 
115 
116  virtual bool isNextRun(
117  const Context& context_) const override {
118  /* -- first run */
119  if(repeater_object == nullptr)
120  return true;
121  /* -- following runs */
122  return repeater_object->hasNextRun(context_);
123  }
124 
125  virtual std::string transformName(
126  const Context& context_,
127  const std::string& case_name_) const override {
128  std::ostringstream oss_;
129  oss_ << case_name_ << " (" << index << ")";
130  return oss_.str();
131  }
132 
133  virtual CasePtr createCase(
134  const Context& context_,
135  const std::string& case_name_) override {
136  ++index;
137  return (suite->*factory_method)(context_, repeater_object);
138  }
139 };
140 
141 } /* -- namespace OTest2 */
142 
143 #endif /* -- OTest2_INCLUDE_OTEST2_REPEATERMULTI_H_ */
OTest2::SuiteRepeaterMulti::createSuite
virtual SuitePtr createSuite(const Context &context_, const std::string &case_name_) override
Definition: repeatermulti.h:74
OTest2::SuiteRepeaterMulti::~SuiteRepeaterMulti
virtual ~SuiteRepeaterMulti()=default
OTest2::CaseRepeaterMulti::operator=
CaseRepeaterMulti & operator=(const CaseRepeaterMulti &)=delete
OTest2::CaseRepeaterMulti::~CaseRepeaterMulti
virtual ~CaseRepeaterMulti()=default
OTest2::CaseRepeaterMulti::isNextRun
virtual bool isNextRun(const Context &context_) const override
Definition: repeatermulti.h:116
OTest2::SuiteRepeaterMulti::SuiteRepeaterMulti
SuiteRepeaterMulti()
Definition: repeatermulti.h:49
OTest2::CaseRepeaterMulti::createCase
virtual CasePtr createCase(const Context &context_, const std::string &case_name_) override
Definition: repeatermulti.h:133
OTest2::CaseRepeaterMulti::CaseRepeaterMulti
CaseRepeaterMulti(Suite_ *suite_, FactoryMethod factory_method_)
Definition: repeatermulti.h:102
OTest2::CaseRepeaterMulti
Definition: repeatermulti.h:83
OTest2
Definition: assertbean.h:25
OTest2::SuiteRepeaterMulti
Definition: repeatermulti.h:37
OTest2::SuiteRepeaterMulti::isNextRun
virtual bool isNextRun(const Context &context_) const override
Definition: repeatermulti.h:57
OTest2::CaseRepeaterMulti::transformName
virtual std::string transformName(const Context &context_, const std::string &case_name_) const override
Definition: repeatermulti.h:125
OTest2::SuiteRepeaterMulti::operator=
SuiteRepeaterMulti & operator=(const SuiteRepeaterMulti &)=delete
OTest2::Context
OTest2 runtime context.
Definition: context.h:38
OTest2::SuiteRepeaterMulti::transformName
virtual std::string transformName(const Context &context_, const std::string &suite_name_) const override
Definition: repeatermulti.h:66
OTest2::CaseRepeaterMulti::FactoryMethod
CasePtr(Suite_::* FactoryMethod)(const Context &, std::shared_ptr< Repeater_ > &)
Definition: repeatermulti.h:85
OTest2::CaseRepeaterMulti::CaseRepeaterMulti
CaseRepeaterMulti(const CaseRepeaterMulti &)=delete