OTest2
A C++ testing framework
registry.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2018 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 <registry.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 <objectrepeateronce.h>
30 #include <reporter.h>
31 #include <semanticstack.h>
32 #include <scenario.h>
33 #include <scenarioiter.h>
34 #include <scenarioroot.h>
35 #include <tagsstack.h>
36 #include <utils.h>
37 
38 namespace OTest2 {
39 
40 namespace {
41 
42 class RootIter : public ScenarioIter {
43  private:
44  ScenarioPtr root;
45 
46  public:
47  explicit RootIter(
48  ScenarioPtr root_);
49  virtual ~RootIter() = default;
50 
51  /* -- avoid copying */
52  RootIter(
53  const RootIter&) = delete;
54  RootIter& operator = (
55  const RootIter&) = delete;
56 
57  /* -- scenario iterator */
58  virtual bool isValid() const noexcept override;
59  virtual ScenarioPtr getScenario() const noexcept override;
60  virtual void next() noexcept override;
61 };
62 
63 RootIter::RootIter(
64  ScenarioPtr root_) :
65  root(root_) {
66  assert(root != nullptr);
67 
68 }
69 
70 bool RootIter::isValid() const noexcept {
71  return root != nullptr;
72 }
73 
74 ScenarioPtr RootIter::getScenario() const noexcept {
75  assert(root != nullptr);
76  return root;
77 }
78 
79 void RootIter::next() noexcept {
80  root = nullptr;
81 }
82 
83 } /* -- namespace */
84 
85 struct Registry::Impl {
86  public:
87  Registry* owner;
88  ScenarioContainerPtr scenario_root;
89 
90  /* -- avoid copying */
91  Impl(
92  const Impl&) = delete;
93  Impl& operator =(
94  const Impl&) = delete;
95 
96  explicit Impl(
97  Registry* owner_);
98  ~Impl();
99 };
100 
101 Registry::Impl::Impl(
102  Registry* owner_) :
103  owner(owner_),
104  scenario_root(std::make_shared<ScenarioRoot>("test")) {
105 
106 }
107 
108 Registry::Impl::~Impl() {
109 
110 }
111 
112 Registry::Registry() :
113  pimpl(new Impl(this)) {
114 
115 }
116 
118  odelete(pimpl);
119 }
120 
122  ScenarioPtr scenario_) {
123  pimpl->scenario_root->appendScenario(scenario_);
124 }
125 
127  const std::string& name_) {
128  assert(!name_.empty());
129  std::static_pointer_cast<ScenarioRoot>(pimpl->scenario_root)->setName(name_);
130 }
131 
133  const RunnerFilter& filter_) const {
134  /* -- filter the scenario */
135  TagsStack tags_stack_;
136  ScenarioPtr filtered_root_(
137  pimpl->scenario_root->filterScenario(tags_stack_, nullptr, filter_));
138 
139  /* -- return the iterator */
140  return std::make_shared<RootIter>(filtered_root_);
141 }
142 
144  const std::string& domain_) {
145  typedef std::map<std::string, Registry> TestDomains;
146  static TestDomains domains_;
147  auto iter_(domains_.find(domain_));
148  if(iter_ == domains_.end()) {
149  auto insert_ret_(domains_.emplace(
150  std::piecewise_construct,
151  std::forward_as_tuple(domain_),
152  std::forward_as_tuple()));
153  assert(insert_ret_.second);
154  iter_ = insert_ret_.first;
155  }
156  return (*iter_).second;
157 }
158 
159 } /* namespace OTest2 */
OTest2::Registry::operator=
Registry & operator=(const Registry &)=delete
OTest2::Registry::registerScenario
void registerScenario(ScenarioPtr scenario_)
Register a scenario object.
Definition: registry.cpp:121
OTest2::RunnerFilter
Generic interface of a runner filter.
Definition: runnerfilter.h:35
tagsstack.h
reporter.h
scenarioroot.h
utils.h
objectpath.h
semanticstack.h
OTest2
Definition: assertbean.h:25
objectrepeateronce.h
OTest2::ScenarioIterPtr
std::shared_ptr< ScenarioIter > ScenarioIterPtr
Shared pointer of the scenario iter interface.
Definition: scenarioiterptr.h:27
registry.h
OTest2::ScenarioPtr
std::shared_ptr< Scenario > ScenarioPtr
Shared pointer of the scenario object.
Definition: scenarioptr.h:27
scenarioiter.h
OTest2::Registry::instance
static Registry & instance(const std::string &domain_)
Access of the global instances.
Definition: registry.cpp:143
OTest2::Registry
Test registry.
Definition: registry.h:38
OTest2::Registry::~Registry
~Registry()
Dtor.
Definition: registry.cpp:117
scenario.h
OTest2::TagsStack
Stack of assigned tags.
Definition: tagsstack.h:36
OTest2::Registry::Registry
Registry()
Ctor.
Definition: registry.cpp:112
context.h
OTest2::ScenarioContainerPtr
std::shared_ptr< ScenarioContainer > ScenarioContainerPtr
Shared pointer to a scenario container.
Definition: scenariocontainerptr.h:27
OTest2::Registry::setTestName
void setTestName(const std::string &name_)
Set test name.
Definition: registry.cpp:126
OTest2::odelete
void odelete(T_ *&object_)
Delete a pointer and set it invalid.
Definition: utils.h:34
OTest2::Registry::getTests
ScenarioIterPtr getTests(const RunnerFilter &filter_) const
Get iterator of test roots.
Definition: registry.cpp:132