-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStepExecutorTest.cpp
More file actions
194 lines (160 loc) · 6.35 KB
/
StepExecutorTest.cpp
File metadata and controls
194 lines (160 loc) · 6.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* cet - Container Environment Test
*
* Copyright (C) 2020-2026 offa
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "StepExecutor.h"
#include <vector>
#include <catch2/catch_test_macros.hpp>
#include <catch2/trompeloeil.hpp>
namespace
{
class StepMock : public cet::TestStep
{
public:
MAKE_MOCK0(execute, cet::Result(), const override);
MAKE_MOCK0(describe, std::string(), const override);
};
class ResultStepMock : public cet::TestStep
{
public:
explicit ResultStepMock(cet::Result r)
: result(r)
{
}
cet::Result execute() const override
{
return result;
}
std::string describe() const override
{
return "";
}
private:
cet::Result result;
};
class IgnoreReporter : public cet::Reporter
{
void printResult([[maybe_unused]] cet::Result result, [[maybe_unused]] const std::string& description) override
{
}
void printError([[maybe_unused]] const std::string& message) override
{
}
};
class ReporterMock : public cet::Reporter
{
MAKE_MOCK2(printResult, void(cet::Result, const std::string&), override);
MAKE_MOCK1(printError, void(const std::string&), override);
};
class ReporterAdapter : public cet::Reporter
{
public:
explicit ReporterAdapter(std::shared_ptr<ReporterMock> mockImpl)
: impl(mockImpl)
{
}
void printResult(cet::Result result, const std::string& description) override
{
impl->printResult(result, description);
}
void printError(const std::string& message) override
{
impl->printError(message);
}
private:
std::shared_ptr<ReporterMock> impl;
};
}
TEST_CASE("Execute executes steps", "[StepExecutorTest]")
{
std::vector<StepMock> steps{2};
REQUIRE_CALL(steps[0], execute()).RETURN(cet::Result::Pass);
REQUIRE_CALL(steps[0], describe()).RETURN("");
REQUIRE_CALL(steps[1], execute()).RETURN(cet::Result::Pass);
REQUIRE_CALL(steps[1], describe()).RETURN("");
cet::StepExecutor se{std::make_unique<IgnoreReporter>()};
se.executeSteps(steps);
}
TEST_CASE("Execute returns successful if all steps are successful", "[StepExecutorTest]")
{
std::vector<StepMock> steps{3};
REQUIRE_CALL(steps[0], execute()).RETURN(cet::Result::Pass);
REQUIRE_CALL(steps[0], describe()).RETURN("");
REQUIRE_CALL(steps[1], execute()).RETURN(cet::Result::Pass);
REQUIRE_CALL(steps[1], describe()).RETURN("");
REQUIRE_CALL(steps[2], execute()).RETURN(cet::Result::Pass);
REQUIRE_CALL(steps[2], describe()).RETURN("");
cet::StepExecutor se{std::make_unique<IgnoreReporter>()};
CHECK(se.executeSteps(steps) == cet::Result::Pass);
}
TEST_CASE("Execute returns failure if one step failed", "[StepExecutorTest]")
{
std::vector<StepMock> steps{3};
REQUIRE_CALL(steps[0], execute()).RETURN(cet::Result::Pass);
REQUIRE_CALL(steps[0], describe()).RETURN("");
REQUIRE_CALL(steps[1], execute()).RETURN(cet::Result::Fail);
REQUIRE_CALL(steps[1], describe()).RETURN("");
REQUIRE_CALL(steps[2], execute()).RETURN(cet::Result::Pass);
REQUIRE_CALL(steps[2], describe()).RETURN("");
cet::StepExecutor se{std::make_unique<IgnoreReporter>()};
CHECK(se.executeSteps(steps) == cet::Result::Fail);
}
TEST_CASE("Execute prints steps with result", "[StepExecutorTest]")
{
std::vector<StepMock> steps{2};
REQUIRE_CALL(steps[0], execute()).RETURN(cet::Result::Pass);
REQUIRE_CALL(steps[0], describe()).RETURN("abc");
REQUIRE_CALL(steps[1], execute()).RETURN(cet::Result::Fail);
REQUIRE_CALL(steps[1], describe()).RETURN("def");
auto reporter = std::make_shared<ReporterMock>();
REQUIRE_CALL(*reporter, printResult(cet::Result::Pass, "abc"));
REQUIRE_CALL(*reporter, printResult(cet::Result::Fail, "def"));
cet::StepExecutor se{std::make_unique<ReporterAdapter>(reporter)};
se.executeSteps(steps);
}
TEST_CASE("Execute catches exception thrown by step", "[StepExecutorTest]")
{
std::vector<StepMock> steps{1};
REQUIRE_CALL(steps[0], execute()).THROW(std::runtime_error{"intentional thrown"});
auto reporter = std::make_shared<ReporterMock>();
REQUIRE_CALL(*reporter, printError("intentional thrown"));
cet::StepExecutor se{std::make_unique<ReporterAdapter>(reporter)};
CHECK(se.executeSteps(steps) == cet::Result::Fail);
}
TEST_CASE("Execute catches exception and continues steps", "[StepExecutorTest]")
{
std::vector<StepMock> steps{3};
REQUIRE_CALL(steps[0], execute()).RETURN(cet::Result::Pass);
REQUIRE_CALL(steps[0], describe()).RETURN("");
REQUIRE_CALL(steps[1], execute()).THROW(std::runtime_error{"intentional"});
REQUIRE_CALL(steps[2], execute()).RETURN(cet::Result::Pass);
REQUIRE_CALL(steps[2], describe()).RETURN("");
cet::StepExecutor se{std::make_unique<IgnoreReporter>()};
CHECK(se.executeSteps(steps) == cet::Result::Fail);
}
TEST_CASE("Execute all collects results of step executions", "[StepExecutorTest]")
{
const std::vector passSteps{ResultStepMock{cet::Result::Pass}, ResultStepMock{cet::Result::Pass}};
const std::vector failSteps{ResultStepMock{cet::Result::Fail}, ResultStepMock{cet::Result::Pass}};
cet::StepExecutor se{std::make_unique<IgnoreReporter>()};
CHECK(executeAll(se, passSteps) == cet::Result::Pass);
CHECK(executeAll(se, passSteps, passSteps) == cet::Result::Pass);
CHECK(executeAll(se, passSteps, failSteps, passSteps) == cet::Result::Fail);
CHECK(executeAll(se, failSteps) == cet::Result::Fail);
CHECK(executeAll(se, passSteps, failSteps) == cet::Result::Fail);
CHECK(executeAll(se, failSteps, passSteps) == cet::Result::Fail);
}