OTest2
A C++ testing framework
stategenerated.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 <stategenerated.h>
21 
22 #include <assert.h>
23 #include <string>
24 
25 #include <caseordinaryptr.h>
26 #include <cmdstate.h>
27 #include <commandstack.h>
28 #include <context.h>
29 #include <objectpath.h>
30 #include "runcode.h"
31 #include <utils.h>
32 
33 namespace OTest2 {
34 
35 struct StateGenerated::Impl {
36  public:
37  StateGenerated* owner;
38 
39  const Context* context;
40  std::string name;
41  ObjectPath section;
42 
43  CaseOrdinaryPtr parent;
44 
45  /* -- avoid copying */
46  Impl(
47  const Impl&) = delete;
48  Impl& operator =(
49  const Impl&) = delete;
50 
51  explicit Impl(
52  StateGenerated* owner_,
53  const Context& context_,
54  const std::string& name_,
55  const std::string& section_path_);
56  ~Impl();
57 };
58 
59 StateGenerated::Impl::Impl(
60  StateGenerated* owner_,
61  const Context& context_,
62  const std::string& name_,
63  const std::string& section_path_) :
64  owner(owner_),
65  context(&context_),
66  name(name_),
67  section(section_path_),
68  parent() {
69 
70 }
71 
72 StateGenerated::Impl::~Impl() {
73 
74 }
75 
77  const Context& context_,
78  const std::string& name_,
79  const std::string& section_path_) :
80  StateOrdinary(context_),
81  pimpl(new Impl(this, context_, name_, section_path_)) {
82 
83 }
84 
86  odelete(pimpl);
87 }
88 
89 std::string StateGenerated::getName() const {
90  return pimpl->name;
91 }
92 
94  const Context& context_,
95  CaseOrdinaryPtr parent_) {
96  assert(parent_ != nullptr);
97 
98  pimpl->parent = parent_;
99  runUserCode(context_, [this](const Context& context_) {
100  runState(context_);
101  });
102  pimpl->parent = CaseOrdinaryPtr();
103 }
104 
106  return *pimpl->context;
107 }
108 
110  const Context& context_,
111  const std::string& name_,
112  int delay_) {
113  assert(pimpl->parent != nullptr && !name_.empty() && delay_ >= 0);
114 
115  /* -- schedule the commands */
116  context_.command_stack->replaceCommand(
117  std::make_shared<CmdState>(pimpl->parent, name_, delay_));
118 }
119 
121  const Context& context_,
122  const std::string& section_path_) {
123  const ObjectPath path_(section_path_);
124  return path_.isPrefixOf(pimpl->section);
125 }
126 
127 } /* -- namespace OTest2 */
caseordinaryptr.h
OTest2::StateGenerated::StateGenerated
StateGenerated(const StateGenerated &)=delete
OTest2::ObjectPath
This is a simple object keeping path to current testing object.
Definition: objectpath.h:32
OTest2::StateGenerated::~StateGenerated
virtual ~StateGenerated()
Dtor.
Definition: stategenerated.cpp:85
OTest2::StateGenerated::isTestSectionActive
bool isTestSectionActive(const Context &context_, const std::string &section_path_)
Check whether a test section is active.
Definition: stategenerated.cpp:120
commandstack.h
utils.h
cmdstate.h
OTest2::CaseOrdinaryPtr
std::shared_ptr< CaseOrdinary > CaseOrdinaryPtr
Shared pointer of the ordinary cases.
Definition: caseordinaryptr.h:27
objectpath.h
OTest2::Context::command_stack
CommandStack *const command_stack
Definition: context.h:40
OTest2
Definition: assertbean.h:25
OTest2::StateGenerated::switchState
void switchState(const Context &context_, const std::string &name_, int delay_)
Switch test states.
Definition: stategenerated.cpp:109
OTest2::runUserCode
bool runUserCode(const Context &context_, std::function< void(const Context &)> ftor_) noexcept
Definition: runcode.cpp:33
OTest2::CommandStack::replaceCommand
void replaceCommand(CommandPtr command_)
Replace a command at the top of the stack.
Definition: commandstack.cpp:73
OTest2::StateOrdinary
Ordinary state class.
Definition: stateordinary.h:33
stategenerated.h
OTest2::StateGenerated::getName
virtual std::string getName() const
Get object's name.
Definition: stategenerated.cpp:89
runcode.h
OTest2::StateGenerated::otest2Context
virtual const Context & otest2Context() const
Get the OTest2 context.
Definition: stategenerated.cpp:105
context.h
OTest2::Context
OTest2 runtime context.
Definition: context.h:38
OTest2::odelete
void odelete(T_ *&object_)
Delete a pointer and set it invalid.
Definition: utils.h:34
OTest2::StateGenerated::operator=
StateGenerated & operator=(const StateGenerated &)=delete
OTest2::ObjectPath::isPrefixOf
bool isPrefixOf(const ObjectPath &path_) const noexcept
Tell us whether this object is a prefix of specified path_.
Definition: objectpath.cpp:143
OTest2::StateGenerated::executeState
virtual void executeState(const Context &context_, CaseOrdinaryPtr parent_)
Run the state.
Definition: stategenerated.cpp:93