OTest2
A C++ testing framework
runnerordinary.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 <runnerordinary.h>
21 
22 #include <assert.h>
23 #include <string>
24 
25 #include <cmdnextobject.h>
26 #include <commandptr.h>
27 #include <commandstack.h>
28 #include <context.h>
29 #include <objectpath.h>
30 #include <semanticstack.h>
31 #include <utils.h>
32 
33 namespace OTest2 {
34 
35 struct RunnerOrdinary::Impl {
36  public:
37  RunnerOrdinary* owner;
38 
39  CommandStack command_stack;
40  SemanticStack semantic_stack;
41  ObjectPath object_path;
42  Context context;
43 
44  /* -- avoid copying */
45  Impl(
46  const Impl&) = delete;
47  Impl& operator =(
48  const Impl&) = delete;
49 
50  explicit Impl(
51  RunnerOrdinary* owner_,
52  TimeSource* time_source_,
53  ExcCatcher* exc_catcher_,
54  Reporter* reporter_,
55  TestMarkFactory* test_mark_factory_,
56  TestMarkStorage* test_mark_storage_,
57  UserData* user_data_,
58  ScenarioIterPtr test_scenario_);
59  ~Impl();
60 };
61 
62 RunnerOrdinary::Impl::Impl(
63  RunnerOrdinary* owner_,
64  TimeSource* time_source_,
65  ExcCatcher* exc_catcher_,
66  Reporter* reporter_,
67  TestMarkFactory* test_mark_factory_,
68  TestMarkStorage* test_mark_storage_,
69  UserData* user_data_,
70  ScenarioIterPtr test_scenario_) :
71  owner(owner_),
72  command_stack(),
73  semantic_stack(),
74  object_path(),
75  context(
76  &command_stack,
77  &semantic_stack,
78  &object_path,
79  time_source_,
80  exc_catcher_,
81  reporter_,
82  test_mark_factory_,
83  test_mark_storage_,
84  user_data_) {
85  assert(context.reporter != nullptr);
86  assert(context.exception_catcher != nullptr);
87 
88  /* -- prepare the context */
89  semantic_stack.push(true); /* -- test passes by default */
90 
91  /* -- schedule start of the test */
92  command_stack.pushCommand(std::make_shared<CmdNextObject>(test_scenario_, nullptr));
93 }
94 
95 RunnerOrdinary::Impl::~Impl() {
96 
97 }
98 
100  TimeSource* time_source_,
101  ExcCatcher* exc_catcher_,
102  Reporter* reporter_,
103  TestMarkFactory* test_mark_factory_,
104  TestMarkStorage* test_mark_storage_,
105  UserData* user_data_,
106  ScenarioIterPtr test_scenario_) :
107  pimpl(new Impl(
108  this,
109  time_source_,
110  exc_catcher_,
111  reporter_,
112  test_mark_factory_,
113  test_mark_storage_,
114  user_data_,
115  test_scenario_)) {
116 
117 }
118 
120  odelete(pimpl);
121 }
122 
124  bool first_command_(true);
125  while(!pimpl->command_stack.empty()) {
126  CommandPtr cmd_(pimpl->command_stack.topCommand());
127 
128  /* -- check whether we should get back into the main loop */
129  int delay_(0);
130  if(!first_command_ && cmd_->shouldWait(pimpl->context, delay_)) {
131  assert(delay_ >= 0);
132  return RunnerResult(true, false, delay_);
133  }
134 
135  /* -- run the command */
136  first_command_ = false;
137  pimpl->command_stack.popCommand();
138  cmd_->run(pimpl->context);
139  }
140 
141  /* -- check whether the stack is finished - there is only one value
142  * meant the result of the test. */
143  assert(pimpl->semantic_stack.isFinished());
144 
145  /* -- no other work - get the test result and stop */
146  return RunnerResult(false, pimpl->semantic_stack.top(), -1);
147 }
148 
149 } /* namespace OTest2 */
OTest2::RunnerOrdinary::runNext
virtual RunnerResult runNext() override
Run next pack of work.
Definition: runnerordinary.cpp:123
OTest2::Reporter
Generic test reporter.
Definition: reporter.h:34
OTest2::TimeSource
Generic interface for getting of current time.
Definition: timesource.h:30
OTest2::UserData
An object keeping user data passed from user's custom main function.
Definition: userdata.h:120
commandstack.h
utils.h
objectpath.h
semanticstack.h
OTest2
Definition: assertbean.h:25
runnerordinary.h
OTest2::TestMarkStorage
Storage of test marks.
Definition: testmarkstorage.h:34
OTest2::ScenarioIterPtr
std::shared_ptr< ScenarioIter > ScenarioIterPtr
Shared pointer of the scenario iter interface.
Definition: scenarioiterptr.h:27
OTest2::RunnerOrdinary::~RunnerOrdinary
virtual ~RunnerOrdinary()
Dtor.
Definition: runnerordinary.cpp:119
OTest2::RunnerOrdinary::RunnerOrdinary
RunnerOrdinary(const RunnerOrdinary &)=delete
OTest2::RunnerResult
Result of test run.
Definition: runner.h:40
OTest2::RunnerOrdinary::operator=
RunnerOrdinary & operator=(const RunnerOrdinary &)=delete
OTest2::ExcCatcher
Generic exception catcher interface.
Definition: exccatcher.h:39
context.h
OTest2::CommandPtr
std::shared_ptr< Command > CommandPtr
Definition: commandptr.h:27
OTest2::TestMarkFactory
A factory of testmark objects used for deserialization.
Definition: testmarkfactory.h:45
commandptr.h
cmdnextobject.h
OTest2::odelete
void odelete(T_ *&object_)
Delete a pointer and set it invalid.
Definition: utils.h:34