OTest2
A C++ testing framework
scenarioroot.cpp
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 #include <otest2/scenarioroot.h>
20 
21 #include <assert.h>
22 #include <map>
23 #include <memory>
24 #include <string>
25 
26 #include <context.h>
27 #include <objectpath.h>
28 #include <objectrepeateronce.h>
29 #include <reporter.h>
30 #include "scenarioitercontainer.h"
31 #include <scenariocontainerptr.h>
32 #include <scenarioptr.h>
33 #include <semanticstack.h>
34 #include <testroot.h>
35 #include <utils.h>
36 
37 namespace OTest2 {
38 
39 namespace {
40 
41 class ObjectRepeaterRoot : public ObjectRepeaterOnce {
42  private:
43  virtual ObjectScenarioPtr doCreateObject(
44  const Context& context_,
45  const std::string& decorated_name_,
46  ObjectPtr parent_) override;
47 
48  public:
49  ObjectRepeaterRoot() = default;
50  virtual ~ObjectRepeaterRoot() = default;
51 
52  /* -- avoid copying */
53  ObjectRepeaterRoot(
54  const ObjectRepeaterRoot&) = delete;
55  ObjectRepeaterRoot& operator = (
56  const ObjectRepeaterRoot&) = delete;
57 };
58 
59 ObjectScenarioPtr ObjectRepeaterRoot::doCreateObject(
60  const Context& context_,
61  const std::string& decorated_name_,
62  ObjectPtr parent_) {
63  return std::make_shared<TestRoot>(decorated_name_);
64 }
65 
66 } /* -- namespace */
67 
68 struct ScenarioRoot::Impl {
69  std::string name;
70  typedef std::vector<ScenarioPtr> Children;
71  Children children;
72 
73  /* -- avoid copying */
74  Impl(
75  const Impl&) = delete;
76  Impl& operator = (
77  const Impl&) = delete;
78 
79  explicit Impl(
80  const std::string& name_);
81  ~Impl();
82 };
83 
84 ScenarioRoot::Impl::Impl(
85  const std::string& name_) :
86  name(name_),
87  children() {
88  assert(!name.empty());
89 
90 }
91 
92 ScenarioRoot::Impl::~Impl() {
93 
94 }
95 
97  const std::string& name_) :
98  pimpl(new Impl(name_)) {
99 
100 }
101 
103  odelete(pimpl);
104 }
105 
107  const std::string& name_) {
108  assert(!name_.empty());
109  pimpl->name = name_;
110 }
111 
113  TagsStack& tags_,
114  ScenarioContainerPtr parent_,
115  const RunnerFilter& filter_) const {
116  ScenarioContainerPtr root_(std::make_shared<ScenarioRoot>(pimpl->name));
117  for(auto item_ : pimpl->children) {
118  item_->filterScenario(tags_, root_, filter_);
119  }
120 
121  return root_;
122 }
123 
124 std::pair<std::string, ObjectRepeaterPtr> ScenarioRoot::createRepeater(
125  const Context& context_) const {
126  return {pimpl->name, std::make_shared<ObjectRepeaterRoot>()};
127 }
128 
130  const Context& context_) const noexcept {
131  context_.reporter->enterTest(
132  context_,
133  context_.object_path->getCurrentName(),
134  context_.object_path->getCurrentParameters());
135 }
136 
138  const Context& context_) const noexcept {
139  context_.reporter->leaveTest(
140  context_,
141  context_.object_path->getCurrentName(),
142  context_.object_path->getCurrentParameters(),
143  context_.semantic_stack->top());
144 }
145 
147  return std::make_shared<ScenarioIterContainer>(pimpl->children);
148 }
149 
151  ScenarioPtr scenario_) {
152  assert(scenario_ != nullptr);
153  pimpl->children.push_back(scenario_);
154 }
155 
156 bool ScenarioRoot::isEmpty() const noexcept {
157  return pimpl->children.empty();
158 }
159 
160 } /* -- namespace OTest2 */
OTest2::ScenarioRoot::enterObject
virtual void enterObject(const Context &context_) const noexcept override
Enter the testing object.
Definition: scenarioroot.cpp:129
OTest2::ObjectScenarioPtr
std::shared_ptr< ObjectScenario > ObjectScenarioPtr
Shared pointer of a testing object scheduled by the scenario.
Definition: objectscenarioptr.h:27
OTest2::RunnerFilter
Generic interface of a runner filter.
Definition: runnerfilter.h:35
OTest2::ScenarioRoot::appendScenario
virtual void appendScenario(ScenarioPtr scenario_) override
Append new sub-scenario into the container.
Definition: scenarioroot.cpp:150
reporter.h
OTest2::ScenarioRoot::~ScenarioRoot
virtual ~ScenarioRoot()
Dtor.
Definition: scenarioroot.cpp:102
OTest2::ScenarioRoot::filterScenario
virtual ScenarioPtr filterScenario(TagsStack &tags_, ScenarioContainerPtr parent_, const RunnerFilter &filter_) const override
Filter the scenario.
Definition: scenarioroot.cpp:112
OTest2::ScenarioRoot::ScenarioRoot
ScenarioRoot(const std::string &name_)
Ctor.
Definition: scenarioroot.cpp:96
OTest2::ScenarioRoot::leaveObject
virtual void leaveObject(const Context &context_) const noexcept override
Report leaving of the testing object.
Definition: scenarioroot.cpp:137
OTest2::ScenarioRoot::createRepeater
virtual std::pair< std::string, ObjectRepeaterPtr > createRepeater(const Context &context_) const override
Create repeater object for testing object represented by this scenario object.
Definition: scenarioroot.cpp:124
scenarioroot.h
utils.h
objectpath.h
semanticstack.h
OTest2::ObjectPtr
std::shared_ptr< Object > ObjectPtr
Shared pointer to a testing object.
Definition: objectptr.h:27
OTest2
Definition: assertbean.h:25
OTest2::ScenarioRoot::getChildren
virtual ScenarioIterPtr getChildren() const override
Get iterator of children object.
Definition: scenarioroot.cpp:146
objectrepeateronce.h
OTest2::ScenarioIterPtr
std::shared_ptr< ScenarioIter > ScenarioIterPtr
Shared pointer of the scenario iter interface.
Definition: scenarioiterptr.h:27
OTest2::ScenarioPtr
std::shared_ptr< Scenario > ScenarioPtr
Shared pointer of the scenario object.
Definition: scenarioptr.h:27
scenarioitercontainer.h
testroot.h
OTest2::TagsStack
Stack of assigned tags.
Definition: tagsstack.h:36
context.h
OTest2::Context
OTest2 runtime context.
Definition: context.h:38
OTest2::ScenarioRoot::setName
void setName(const std::string &name_)
Set name of the scenario (the name of the entire test)
Definition: scenarioroot.cpp:106
OTest2::ScenarioRoot::operator=
ScenarioRoot & operator=(const ScenarioRoot &)=delete
OTest2::ScenarioContainerPtr
std::shared_ptr< ScenarioContainer > ScenarioContainerPtr
Shared pointer to a scenario container.
Definition: scenariocontainerptr.h:27
OTest2::odelete
void odelete(T_ *&object_)
Delete a pointer and set it invalid.
Definition: utils.h:34
OTest2::ScenarioRoot::isEmpty
virtual bool isEmpty() const noexcept override
Check whether the container is empty.
Definition: scenarioroot.cpp:156
scenariocontainerptr.h
scenarioptr.h