OTest2
A C++ testing framework
scenariosuite.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 
20 #include <scenariosuite.h>
21 
22 #include <assert.h>
23 #include <map>
24 #include <memory>
25 #include <vector>
26 
27 #include <context.h>
28 #include <objectpath.h>
29 #include <objectrepeater.h>
30 #include <objectrepeaterfactory.h>
31 #include <reporter.h>
32 #include "scenarioitercontainer.h"
33 #include <semanticstack.h>
34 #include <tags.h>
35 #include <tagsstack.h>
36 #include <utils.h>
37 
38 namespace OTest2 {
39 
40 struct ScenarioSuite::Impl {
41  std::string name;
42  Tags tags;
43  ObjectRepeaterFactoryPtr repeater_factory;
44  typedef std::vector<ScenarioPtr> Children;
45  Children children;
46 
47  /* -- avoid copying */
48  Impl(
49  const Impl&) = delete;
50  Impl& operator = (
51  const Impl&) = delete;
52 
53  explicit Impl(
54  const std::string& name_,
55  const Tags& tags_,
56  ObjectRepeaterFactoryPtr repeater_factory_);
57  ~Impl();
58 };
59 
60 ScenarioSuite::Impl::Impl(
61  const std::string& name_,
62  const Tags& tags_,
63  ObjectRepeaterFactoryPtr repeater_factory_) :
64  name(name_),
65  tags(tags_),
66  repeater_factory(repeater_factory_),
67  children() {
68  assert(!name_.empty() && repeater_factory != nullptr);
69 
70 }
71 
72 ScenarioSuite::Impl::~Impl() {
73 
74 }
75 
77  const std::string& name_,
78  const Tags& tags_,
79  ObjectRepeaterFactoryPtr repeater_factory_) :
80  pimpl(new Impl(name_, tags_, repeater_factory_)) {
81 
82 }
83 
85  odelete(pimpl);
86 }
87 
89  TagsStack& tags_,
90  ScenarioContainerPtr parent_,
91  const RunnerFilter& filter_) const {
92  /* -- add myself into the object path */
93  tags_.pushTags(pimpl->name, pimpl->tags);
94 
95  /* -- create new suite container and filter children */
96  ScenarioContainerPtr suite_(std::make_shared<ScenarioSuite>(
97  pimpl->name, pimpl->tags, pimpl->repeater_factory));
98  for(auto iter_ : pimpl->children) {
99  iter_->filterScenario(tags_, suite_, filter_);
100  }
101 
102  /* -- append myself if there are some not-filtered children */
103  if(!suite_->isEmpty()) {
104  parent_->appendScenario(suite_);
105  }
106 
107  /* -- remove myself from the object path */
108  tags_.popTags();
109 
110  return parent_;
111 }
112 
113 std::pair<std::string, ObjectRepeaterPtr> ScenarioSuite::createRepeater(
114  const Context& context_) const {
115  return {pimpl->name, pimpl->repeater_factory->createRepeater(context_, std::string())};
116 }
117 
119  const Context& context_) const noexcept {
120  context_.reporter->enterSuite(
121  context_,
122  context_.object_path->getCurrentName(),
123  context_.object_path->getCurrentParameters());
124 }
125 
127  const Context& context_) const noexcept {
128  context_.reporter->leaveSuite(
129  context_,
130  context_.object_path->getCurrentName(),
131  context_.object_path->getCurrentParameters(),
132  context_.semantic_stack->top());
133 }
134 
136  return std::make_shared<ScenarioIterContainer>(pimpl->children);
137 }
138 
140  ScenarioPtr scenario_) {
141  assert(scenario_ != nullptr);
142  pimpl->children.push_back(scenario_);
143 }
144 
145 bool ScenarioSuite::isEmpty() const noexcept {
146  return pimpl->children.empty();
147 }
148 
149 } /* -- namespace OTest2 */
objectrepeater.h
scenariosuite.h
OTest2::RunnerFilter
Generic interface of a runner filter.
Definition: runnerfilter.h:35
tags.h
tagsstack.h
reporter.h
OTest2::TagsStack::popTags
void popTags()
Pop tags at the top of the stack.
Definition: tagsstack.cpp:67
OTest2::ScenarioSuite::~ScenarioSuite
virtual ~ScenarioSuite()
Dtor.
Definition: scenariosuite.cpp:84
OTest2::TagsStack::pushTags
void pushTags(const std::string &name_, const Tags &tags_)
Push tags.
Definition: tagsstack.cpp:61
OTest2::ScenarioSuite::appendScenario
virtual void appendScenario(ScenarioPtr scenario_) override
Append new sub-scenario into the container.
Definition: scenariosuite.cpp:139
utils.h
OTest2::ScenarioSuite::filterScenario
virtual ScenarioPtr filterScenario(TagsStack &tags_, ScenarioContainerPtr parent_, const RunnerFilter &filter_) const override
Filter the scenario.
Definition: scenariosuite.cpp:88
OTest2::ScenarioSuite::operator=
ScenarioSuite & operator=(const ScenarioSuite &)=delete
OTest2::ScenarioSuite::getChildren
virtual ScenarioIterPtr getChildren() const override
Get iterator of children object.
Definition: scenariosuite.cpp:135
OTest2::ScenarioSuite::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: scenariosuite.cpp:113
objectpath.h
semanticstack.h
OTest2::ObjectRepeaterFactoryPtr
std::shared_ptr< ObjectRepeaterFactory > ObjectRepeaterFactoryPtr
Shared pointer of the object repeater factories.
Definition: objectrepeaterfactoryptr.h:27
OTest2
Definition: assertbean.h:25
OTest2::ScenarioSuite::ScenarioSuite
ScenarioSuite(const std::string &name_, const Tags &tags_, ObjectRepeaterFactoryPtr repeater_factory_)
Create scenario object of a test suite.
Definition: scenariosuite.cpp:76
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
OTest2::ScenarioSuite::enterObject
virtual void enterObject(const Context &context_) const noexcept override
Enter the testing object.
Definition: scenariosuite.cpp:118
OTest2::TagsStack
Stack of assigned tags.
Definition: tagsstack.h:36
context.h
OTest2::Tags
List of tags assigned to a testing object.
Definition: tags.h:32
OTest2::Context
OTest2 runtime context.
Definition: context.h:38
OTest2::ScenarioSuite::leaveObject
virtual void leaveObject(const Context &context_) const noexcept override
Report leaving of the testing object.
Definition: scenariosuite.cpp:126
objectrepeaterfactory.h
OTest2::ScenarioSuite::isEmpty
virtual bool isEmpty() const noexcept
Check whether the container is empty.
Definition: scenariosuite.cpp:145
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