OTest2
A C++ testing framework
casegenerated.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 <casegenerated.h>
21 
22 #include <assert.h>
23 #include <memory>
24 #include <string>
25 #include <vector>
26 
27 #include <cmdfirststate.h>
28 #include <commandstack.h>
29 #include <context.h>
30 #include <fcemarshaler.h>
31 #include "runcode.h"
32 #include <stateptr.h>
33 #include <stateregistry.h>
34 #include <utils.h>
35 
36 namespace OTest2 {
37 
38 struct CaseGenerated::Impl {
39  public:
40  CaseGenerated* owner;
41 
42  const Context* context;
43  std::string name;
44  StateRegistry state_registry;
45  std::string entering_state;
46  std::vector<FceMarshalerPtr> start_ups;
47  std::vector<FceMarshalerPtr> tear_downs;
48 
49  /* -- avoid copying */
50  Impl(
51  const Impl&) = delete;
52  Impl& operator =(
53  const Impl&) = delete;
54 
55  explicit Impl(
56  CaseGenerated* owner_,
57  const Context& context_,
58  const std::string& name_);
59  ~Impl();
60 };
61 
62 CaseGenerated::Impl::Impl(
63  CaseGenerated* owner_,
64  const Context& context_,
65  const std::string& name_) :
66  owner(owner_),
67  context(&context_),
68  name(name_),
69  state_registry(),
70  entering_state(),
71  start_ups(),
72  tear_downs() {
73 
74 }
75 
76 CaseGenerated::Impl::~Impl() {
77 
78 }
79 
81  const Context& context_,
82  const std::string& name_) :
83  CaseOrdinary(context_),
84  pimpl(new Impl(this, context_, name_)) {
85 
86 }
87 
89  odelete(pimpl);
90 }
91 
92 std::string CaseGenerated::getName() const {
93  return pimpl->name;
94 }
95 
97  const Context& context_,
98  int index_) {
99  if(index_ >= 0 && index_ < pimpl->start_ups.size()) {
100  runUserCode(context_, [this, index_](const Context& context_) {
101  pimpl->start_ups[index_]->runFunction(context_);
102  });
103  return true;
104  }
105  else
106  return false;
107 }
108 
110  const Context& context_,
111  ScenarioPtr scenario_,
112  ObjectPtr me_) {
113  assert(me_.get() == this);
114 
115  context_.command_stack->pushCommand(
116  std::make_shared<CmdFirstState>(
117  std::static_pointer_cast<CaseOrdinary>(me_)));
118 }
119 
121  const Context& context_,
122  int index_) {
123  assert(index_ >= 0 && index_ < pimpl->tear_downs.size());
124 
125  runUserCode(context_, [this, index_](const Context& context_) {
126  pimpl->tear_downs[index_]->runFunction(context_);
127  });
128 }
129 
131  return pimpl->state_registry.getState(pimpl->entering_state);
132 }
133 
135  const std::string& name_) const {
136  return pimpl->state_registry.getState(name_);
137 }
138 
140  return *pimpl->context;
141 }
142 
144  const std::string& name_,
145  StatePtr state_) {
146  pimpl->state_registry.registerState(name_, state_);
147 }
148 
150  const std::string& name_) {
151  pimpl->entering_state = name_;
152 }
153 
155  FceMarshalerPtr start_up_,
156  FceMarshalerPtr tear_down_) {
157  assert(start_up_ != nullptr && tear_down_ != nullptr);
158 
159  pimpl->start_ups.push_back(start_up_);
160  pimpl->tear_downs.push_back(tear_down_);
161 }
162 
163 } /* -- namespace OTest2 */
OTest2::CaseGenerated::getName
virtual std::string getName() const
Get object's name.
Definition: casegenerated.cpp:92
fcemarshaler.h
OTest2::CaseGenerated::operator=
CaseGenerated & operator=(const CaseGenerated &)=delete
OTest2::CaseGenerated::tearDownObject
virtual void tearDownObject(const Context &context_, int index_) override
Execute tear-down function at specified index.
Definition: casegenerated.cpp:120
OTest2::CaseGenerated::otest2Context
virtual const Context & otest2Context() const
Get the OTest2 context.
Definition: casegenerated.cpp:139
OTest2::CaseGenerated::setEnteringState
void setEnteringState(const std::string &name_)
Set name of the entering (first) state.
Definition: casegenerated.cpp:149
OTest2::CaseGenerated::~CaseGenerated
virtual ~CaseGenerated()
Dtor.
Definition: casegenerated.cpp:88
casegenerated.h
OTest2::CommandStack::pushCommand
void pushCommand(CommandPtr command_)
Push a command into the stack.
Definition: commandstack.cpp:67
commandstack.h
utils.h
OTest2::FceMarshalerPtr
std::shared_ptr< FceMarshaler > FceMarshalerPtr
Definition: fcemarshalerptr.h:27
OTest2::ObjectPtr
std::shared_ptr< Object > ObjectPtr
Shared pointer to a testing object.
Definition: objectptr.h:27
OTest2::CaseGenerated::getState
virtual StatePtr getState(const std::string &name_) const override
Get state with specified name_.
Definition: casegenerated.cpp:134
OTest2::Context::command_stack
CommandStack *const command_stack
Definition: context.h:40
OTest2
Definition: assertbean.h:25
OTest2::CaseGenerated::startUpObject
virtual bool startUpObject(const Context &context_, int index_) override
Execute start-up function at specified index.
Definition: casegenerated.cpp:96
OTest2::runUserCode
bool runUserCode(const Context &context_, std::function< void(const Context &)> ftor_) noexcept
Definition: runcode.cpp:33
OTest2::ScenarioPtr
std::shared_ptr< Scenario > ScenarioPtr
Shared pointer of the scenario object.
Definition: scenarioptr.h:27
OTest2::CaseGenerated::registerFixture
void registerFixture(FceMarshalerPtr start_up_, FceMarshalerPtr tear_down_)
Register a pair of start-up and tear-down functions.
Definition: casegenerated.cpp:154
OTest2::StatePtr
std::shared_ptr< State > StatePtr
Pointer to test state objects.
Definition: stateptr.h:27
OTest2::CaseOrdinary
An ordinary case object scheduled by the CmdFirstState, CmdRunState and CmdState commands.
Definition: caseordinary.h:37
runcode.h
OTest2::CaseGenerated::getFirstState
virtual StatePtr getFirstState() const override
Get the initial state.
Definition: casegenerated.cpp:130
OTest2::CaseGenerated::CaseGenerated
CaseGenerated(const CaseGenerated &)=delete
context.h
stateregistry.h
OTest2::CaseGenerated::registerState
void registerState(const std::string &name_, StatePtr state_)
Register new test state.
Definition: casegenerated.cpp:143
stateptr.h
OTest2::Context
OTest2 runtime context.
Definition: context.h:38
cmdfirststate.h
OTest2::CaseGenerated::scheduleBody
virtual void scheduleBody(const Context &context_, ScenarioPtr scenario_, ObjectPtr me_) override
Schedule body of the testing object.
Definition: casegenerated.cpp:109
OTest2::odelete
void odelete(T_ *&object_)
Delete a pointer and set it invalid.
Definition: utils.h:34