OTest2
A C++ testing framework
objectrepeatermultiimpl.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_OBJECTREPEATERMULTIIMPL_H_
21 #define OTest2_INCLUDE_OTEST2_OBJECTREPEATERMULTIIMPL_H_
22 
23 #include <iostream>
24 #include <memory>
25 
26 #include <otest2/objectpath.h>
27 #include <otest2/objectrepeater.h>
30 
31 namespace OTest2 {
32 
33 template<typename Object_, typename Repeater_>
35  private:
36  std::string section_path;
37  int index;
38  std::shared_ptr<Repeater_> repeater_object;
39 
40  public:
42  const std::string& section_path_) :
43  section_path(section_path_),
44  index(1),
45  repeater_object() {
46 
47  }
48 
49  virtual ~ObjectRepeaterMultiRoot() = default;
50 
51  /* -- avoid copying */
53  const ObjectRepeaterMultiRoot&) = delete;
55  const ObjectRepeaterMultiRoot&) = delete;
56 
57  virtual bool hasNextRun(
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 void modifyObjectPath(
67  const Context& context_,
68  ObjectPath& path_) const override {
69  std::ostringstream oss_;
70  oss_ << index;
71  path_.appendParameter("run", oss_.str());
72  }
73 
75  const Context& context_,
76  const std::string& case_name_,
77  ObjectPtr parent_) override {
78  ++index;
79  return std::make_shared<Object_>(context_, section_path, std::ref(repeater_object));
80  }
81 };
82 
83 template<typename Object_, typename Repeater_>
85  public:
87  virtual ~ObjectRepeaterFactoryMultiRoot() = default;
88 
89  /* -- avoid copying */
91  const ObjectRepeaterFactoryMultiRoot&) = delete;
93  const ObjectRepeaterFactoryMultiRoot&) = delete;
94 
96  const Context& context_,
97  const std::string& section_path_) const {
98  return std::make_shared<ObjectRepeaterMultiRoot<Object_, Repeater_> >(section_path_);
99  }
100 };
101 
102 template<typename Parent_, typename Object_, typename Repeater_>
104  public:
105  typedef ObjectScenarioPtr (Parent_::* FactoryMethod)(
106  const Context&,
107  const std::string&,
108  std::shared_ptr<Repeater_>&);
109 
110  private:
111  FactoryMethod factory_method;
112  std::string section_path;
113  int index;
114  std::shared_ptr<Repeater_> repeater_object;
115 
116  public:
118  FactoryMethod factory_method_,
119  const std::string& section_path_) :
120  factory_method(factory_method_),
121  section_path(section_path_),
122  index(1),
123  repeater_object() {
124  assert(factory_method != nullptr);
125 
126  }
127 
128  virtual ~ObjectRepeaterMultiNested() = default;
129 
130  /* -- avoid copying */
132  const ObjectRepeaterMultiNested&) = delete;
134  const ObjectRepeaterMultiNested&) = delete;
135 
136  virtual bool hasNextRun(
137  const Context& context_) const override {
138  /* -- first run */
139  if(repeater_object == nullptr)
140  return true;
141  /* -- following runs */
142  return repeater_object->hasNextRun(context_);
143  }
144 
145  virtual void modifyObjectPath(
146  const Context& context_,
147  ObjectPath& path_) const override {
148  std::ostringstream oss_;
149  oss_ << index;
150  path_.appendParameter("run", oss_.str());
151  }
152 
154  const Context& context_,
155  const std::string& case_name_,
156  ObjectPtr parent_) override {
157  ++index;
158  auto parent_typed_(std::dynamic_pointer_cast<Parent_>(parent_));
159  assert(parent_typed_ != nullptr);
160  return (parent_typed_.get()->*factory_method)(context_, section_path, repeater_object);
161  }
162 };
163 
164 template<typename Parent_, typename Object_, typename Repeater_>
166  public:
168 
169  private:
170  FactoryMethod factory_method;
171 
172  public:
174  FactoryMethod factory_method_) :
175  factory_method(factory_method_) {
176 
177  }
178 
179  virtual ~ObjectRepeaterFactoryMultiNested() = default;
180 
181  /* -- avoid copying */
183  const ObjectRepeaterFactoryMultiNested&) = delete;
185  const ObjectRepeaterFactoryMultiNested&) = delete;
186 
188  const Context& context_,
189  const std::string& section_path_) const {
190  return std::make_shared<ObjectRepeaterMultiNested<Parent_, Object_, Repeater_> >(factory_method, section_path_);
191  }
192 };
193 
194 } /* -- namespace OTest2 */
195 
196 #endif /* -- OTest2_INCLUDE_OTEST2_OBJECTREPEATERMULTIIMPL_H_ */
OTest2::ObjectRepeater
Generic interface of a repeater of a testing object (this repeater is not a part of the OTest2 API)
Definition: objectrepeater.h:38
OTest2::ObjectRepeaterMultiNested::hasNextRun
virtual bool hasNextRun(const Context &context_) const override
Check whether next run of the object is planned.
Definition: objectrepeatermultiimpl.h:136
OTest2::ObjectRepeaterMultiRoot::operator=
ObjectRepeaterMultiRoot & operator=(const ObjectRepeaterMultiRoot &)=delete
OTest2::ObjectRepeaterMultiRoot::createObject
virtual ObjectScenarioPtr createObject(const Context &context_, const std::string &case_name_, ObjectPtr parent_) override
Create the testing object.
Definition: objectrepeatermultiimpl.h:74
OTest2::ObjectScenarioPtr
std::shared_ptr< ObjectScenario > ObjectScenarioPtr
Shared pointer of a testing object scheduled by the scenario.
Definition: objectscenarioptr.h:27
OTest2::ObjectRepeaterMultiNested::~ObjectRepeaterMultiNested
virtual ~ObjectRepeaterMultiNested()=default
OTest2::ObjectRepeaterMultiNested
Definition: objectrepeatermultiimpl.h:103
objectrepeater.h
OTest2::ObjectPath
This is a simple object keeping path to current testing object.
Definition: objectpath.h:32
OTest2::ObjectRepeaterMultiRoot::~ObjectRepeaterMultiRoot
virtual ~ObjectRepeaterMultiRoot()=default
OTest2::ObjectRepeaterFactoryMultiNested::FactoryMethod
ObjectRepeaterMultiNested< Parent_, Object_, Repeater_ >::FactoryMethod FactoryMethod
Definition: objectrepeatermultiimpl.h:167
OTest2::ObjectRepeaterMultiNested::createObject
virtual ObjectScenarioPtr createObject(const Context &context_, const std::string &case_name_, ObjectPtr parent_) override
Create the testing object.
Definition: objectrepeatermultiimpl.h:153
OTest2::ObjectRepeaterFactoryMultiNested
Definition: objectrepeatermultiimpl.h:165
OTest2::ObjectRepeaterFactoryMultiRoot
Definition: objectrepeatermultiimpl.h:84
OTest2::ObjectRepeaterFactoryMultiRoot::operator=
ObjectRepeaterFactoryMultiRoot & operator=(const ObjectRepeaterFactoryMultiRoot &)=delete
OTest2::ObjectRepeaterMultiNested::modifyObjectPath
virtual void modifyObjectPath(const Context &context_, ObjectPath &path_) const override
Change current object path.
Definition: objectrepeatermultiimpl.h:145
objectscenarioptr.h
OTest2::ObjectRepeaterMultiRoot::ObjectRepeaterMultiRoot
ObjectRepeaterMultiRoot(const std::string &section_path_)
Definition: objectrepeatermultiimpl.h:41
objectpath.h
OTest2::ObjectRepeaterMultiRoot::hasNextRun
virtual bool hasNextRun(const Context &context_) const override
Check whether next run of the object is planned.
Definition: objectrepeatermultiimpl.h:57
OTest2::ObjectRepeaterFactoryMultiRoot::~ObjectRepeaterFactoryMultiRoot
virtual ~ObjectRepeaterFactoryMultiRoot()=default
OTest2::ObjectPtr
std::shared_ptr< Object > ObjectPtr
Shared pointer to a testing object.
Definition: objectptr.h:27
OTest2::ObjectRepeaterMultiNested::FactoryMethod
ObjectScenarioPtr(Parent_::* FactoryMethod)(const Context &, const std::string &, std::shared_ptr< Repeater_ > &)
Definition: objectrepeatermultiimpl.h:105
OTest2
Definition: assertbean.h:25
OTest2::ObjectRepeaterFactoryMultiNested::~ObjectRepeaterFactoryMultiNested
virtual ~ObjectRepeaterFactoryMultiNested()=default
OTest2::ObjectRepeaterMultiRoot
Definition: objectrepeatermultiimpl.h:34
OTest2::ObjectRepeaterMultiRoot::modifyObjectPath
virtual void modifyObjectPath(const Context &context_, ObjectPath &path_) const override
Change current object path.
Definition: objectrepeatermultiimpl.h:66
OTest2::ObjectRepeaterFactoryMultiRoot::ObjectRepeaterFactoryMultiRoot
ObjectRepeaterFactoryMultiRoot()=default
OTest2::ObjectRepeaterPtr
std::shared_ptr< ObjectRepeater > ObjectRepeaterPtr
Shared pointer of the object repeater objects.
Definition: objectrepeaterptr.h:27
OTest2::ObjectRepeaterFactoryMultiNested::ObjectRepeaterFactoryMultiNested
ObjectRepeaterFactoryMultiNested(FactoryMethod factory_method_)
Definition: objectrepeatermultiimpl.h:173
OTest2::ObjectRepeaterFactoryMultiRoot::createRepeater
virtual ObjectRepeaterPtr createRepeater(const Context &context_, const std::string &section_path_) const
Create repeater object.
Definition: objectrepeatermultiimpl.h:95
OTest2::ObjectRepeaterMultiNested::ObjectRepeaterMultiNested
ObjectRepeaterMultiNested(FactoryMethod factory_method_, const std::string &section_path_)
Definition: objectrepeatermultiimpl.h:117
OTest2::ObjectRepeaterFactoryMultiNested::operator=
ObjectRepeaterFactoryMultiNested & operator=(const ObjectRepeaterFactoryMultiNested &)=delete
OTest2::ObjectRepeaterFactory
Factory of object repeater objects.
Definition: objectrepeaterfactory.h:35
OTest2::ObjectRepeaterMultiNested::operator=
ObjectRepeaterMultiNested & operator=(const ObjectRepeaterMultiNested &)=delete
OTest2::ObjectPath::appendParameter
void appendParameter(const std::string &name_, const std::string &value_)
Append parameter to current testing object.
Definition: objectpath.cpp:98
OTest2::ObjectRepeaterFactoryMultiNested::createRepeater
virtual ObjectRepeaterPtr createRepeater(const Context &context_, const std::string &section_path_) const
Create repeater object.
Definition: objectrepeatermultiimpl.h:187
OTest2::Context
OTest2 runtime context.
Definition: context.h:38
objectrepeaterfactory.h