OTest2
A C++ testing framework
cmdrunstate.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 <cmdrunstate.h>
21 
22 #include <assert.h>
23 #include <memory>
24 
25 #include <cmddummy.h>
26 #include <commandstack.h>
27 #include <context.h>
28 #include <reporter.h>
29 #include <semanticstack.h>
30 #include <stateordinary.h>
31 
32 namespace OTest2 {
33 
35  CaseOrdinaryPtr parent_,
36  StateOrdinaryPtr state_,
37  bool wait_before_,
38  int delay_) :
39  parent(parent_),
40  state(state_),
41  wait_before(wait_before_),
42  delay(delay_) {
43  assert(parent != nullptr && state != nullptr && (!wait_before || delay >= 0));
44 
45 }
46 
48 
49 }
50 
52  const Context& context_,
53  int& delay_) {
54  if(wait_before) {
55  delay_ = delay;
56  return true;
57  }
58  else
59  return false;
60 }
61 
63  const Context& context_) {
64  /* -- report the state entrance */
65  context_.reporter->enterState(context_, state->getName());
66 
67  /* -- Prepare a dummy command. The test state can replace it by
68  * a command switching another state. */
69  context_.command_stack->pushCommand(std::make_shared<CmdDummy>());
70  /* -- prepare the return value of the state */
71  context_.semantic_stack->push(true);
72  /* -- execute the state */
73  state->executeState(context_, parent);
74 
75  /* -- report end of the state */
76  context_.reporter->leaveState(
77  context_, state->getName(), context_.semantic_stack->top());
78 
79  /* -- return the value */
80  context_.semantic_stack->popAnd();
81 }
82 
83 } /* namespace OTest2 */
OTest2::CmdRunState::CmdRunState
CmdRunState(const CmdRunState &)=delete
cmdrunstate.h
OTest2::Reporter::enterState
virtual void enterState(const Context &context_, const std::string &name_)=0
Enter a state.
OTest2::SemanticStack::popAnd
void popAnd()
AND last two values and replace them by the result.
Definition: semanticstack.cpp:86
OTest2::StateOrdinaryPtr
std::shared_ptr< StateOrdinary > StateOrdinaryPtr
Shared pointer of ordinary states.
Definition: stateordinaryptr.h:27
OTest2::Context::reporter
Reporter *const reporter
Definition: context.h:45
reporter.h
OTest2::CommandStack::pushCommand
void pushCommand(CommandPtr command_)
Push a command into the stack.
Definition: commandstack.cpp:67
commandstack.h
OTest2::SemanticStack::push
void push(bool value_)
Push a value.
Definition: semanticstack.cpp:65
cmddummy.h
OTest2::CmdRunState::shouldWait
virtual bool shouldWait(const Context &context_, int &delay_)
Say whether the framework should wait before running the command.
Definition: cmdrunstate.cpp:51
OTest2::CaseOrdinaryPtr
std::shared_ptr< CaseOrdinary > CaseOrdinaryPtr
Shared pointer of the ordinary cases.
Definition: caseordinaryptr.h:27
semanticstack.h
OTest2::CmdRunState::run
virtual void run(const Context &context_)
Run the command.
Definition: cmdrunstate.cpp:62
OTest2::Context::command_stack
CommandStack *const command_stack
Definition: context.h:40
OTest2
Definition: assertbean.h:25
stateordinary.h
OTest2::Reporter::leaveState
virtual void leaveState(const Context &context_, const std::string &name_, bool result_)=0
Leave a state.
OTest2::Context::semantic_stack
SemanticStack *const semantic_stack
Definition: context.h:41
context.h
OTest2::Context
OTest2 runtime context.
Definition: context.h:38
OTest2::CmdRunState::~CmdRunState
virtual ~CmdRunState()
Dtor.
Definition: cmdrunstate.cpp:47
OTest2::SemanticStack::top
bool top() const
Get top value.
Definition: semanticstack.cpp:70