Skip to content

Commit eaff96c

Browse files
authored
Merge pull request #97 from ThoSe1990/refactor/test_runner
Refactor: Test Runner
2 parents abf961c + 9326abe commit eaff96c

11 files changed

Lines changed: 590 additions & 422 deletions

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919

2020
- Internal field: `test_results::feature::status` ([94](https://github.com/ThoSe1990/cwt-cucumber/pull/94))
2121

22+
### Changed
23+
24+
- Reorganized / refactored `test_runner`, no changes to behavior or public API's ([97](https://github.com/ThoSe1990/cwt-cucumber/pull/97))
25+
2226
## [2.7] 2025-06-16
2327

2428
### Added

src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ set(cucumber-src
1414
${CMAKE_CURRENT_SOURCE_DIR}/scanner.cpp
1515
${CMAKE_CURRENT_SOURCE_DIR}/options.cpp
1616
${CMAKE_CURRENT_SOURCE_DIR}/lexer.cpp
17+
${CMAKE_CURRENT_SOURCE_DIR}/log_util.cpp
1718
${CMAKE_CURRENT_SOURCE_DIR}/report.cpp
1819
${CMAKE_CURRENT_SOURCE_DIR}/step_finder.cpp
1920
${CMAKE_CURRENT_SOURCE_DIR}/step.cpp
2021
${CMAKE_CURRENT_SOURCE_DIR}/table.cpp
22+
${CMAKE_CURRENT_SOURCE_DIR}/test_runner.cpp
2123
${CMAKE_CURRENT_SOURCE_DIR}/tags.cpp
2224
)
2325

src/asserts.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ inline void internal_assert(bool condition, std::string_view error_msg)
1515
{
1616
cuke::results::set_step_to(cuke::results::test_status::failed);
1717
cuke::results::steps_back().error_msg = error_msg;
18-
cuke::log::info(cuke::log::red, error_msg, cuke::log::reset_color,
19-
cuke::log::new_line);
18+
cuke::log::info(log::color::red());
19+
cuke::log::info(error_msg);
20+
cuke::log::info(log::color::reset());
21+
cuke::log::info(cuke::log::new_line);
2022
}
2123
}
2224

src/cucumber.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ void print_failed_scenarios()
2727
log::always("Failed Scenarios:", log::new_line);
2828
first = false;
2929
}
30-
if (log::colors_enabled()) log::always(log::red);
30+
log::always(log::color::red());
3131
log::always(scenario.name);
32-
if (log::colors_enabled()) log::always(log::black);
32+
log::always(log::color::black());
3333
log::always(" ", feature.file, ':', scenario.line);
34-
if (log::colors_enabled()) log::always(log::reset_color);
34+
log::always(log::color::reset());
3535
log::always(log::new_line);
3636
}
3737
});

src/log.hpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,33 @@ void info(Args&&... args)
133133
logger::instance().info(std::forward<Args>(args)...);
134134
}
135135

136-
inline bool colors_enabled() { return logger::instance().colors_enabled(); }
136+
namespace color
137+
{
138+
139+
inline const char* reset()
140+
{
141+
return logger::instance().colors_enabled() ? log::reset_color : "";
142+
}
143+
inline const char* green()
144+
{
145+
return logger::instance().colors_enabled() ? log::green : "";
146+
}
147+
inline const char* yellow()
148+
{
149+
return logger::instance().colors_enabled() ? log::yellow : "";
150+
}
151+
inline const char* red()
152+
{
153+
return logger::instance().colors_enabled() ? log::red : "";
154+
}
155+
inline const char* blue()
156+
{
157+
return logger::instance().colors_enabled() ? log::blue : "";
158+
}
159+
inline const char* black()
160+
{
161+
return logger::instance().colors_enabled() ? log::black : "";
162+
}
163+
} // namespace color
137164

138165
} // namespace cuke::log

src/log_util.cpp

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#include "log_util.hpp"
2+
3+
#include "util.hpp"
4+
#include "util_regex.hpp"
5+
6+
namespace cuke::log
7+
{
8+
void verbose_end()
9+
{
10+
verbose("[ VERBOSE ] Scenario end", log::new_line);
11+
verbose("[ VERBOSE ] ----------------------------------", new_line,
12+
log::new_line);
13+
}
14+
void verbose_no_tags()
15+
{
16+
verbose("[ VERBOSE ] No tags given, continuing", log::new_line);
17+
}
18+
void verbose_evaluate_tags(const ast::scenario_node& scenario,
19+
bool tag_evaluation, const std::string& expression)
20+
{
21+
verbose(std::format("[ VERBOSE ] Scenario tags '{}'",
22+
internal::to_string(scenario.tags())),
23+
new_line);
24+
verbose(
25+
std::format(" checked against tag expression '{}' -> {}",
26+
expression,
27+
tag_evaluation ? "'True', continuing with scenario"
28+
: "'False', stopping scenario"),
29+
new_line);
30+
}
31+
void verbose_skip()
32+
{
33+
verbose("[ VERBOSE ] Scenario skipped with 'skip_scenario'", new_line);
34+
}
35+
void verbose_ignore()
36+
{
37+
verbose("[ VERBOSE ] Scenario ignored with 'ignore_scenario'", new_line);
38+
}
39+
40+
void info(const cuke::ast::feature_node& feature)
41+
{
42+
info(feature.keyword(), ": ", feature.name());
43+
info(log::color::black());
44+
info(" ", feature.file(), ':', feature.line());
45+
info(log::color::reset());
46+
info(log::new_line, log::new_line);
47+
}
48+
void info(const cuke::ast::scenario_node& scenario)
49+
{
50+
info(scenario.keyword(), ": ", scenario.name());
51+
info(log::color::black());
52+
info(" ", scenario.file(), ':', scenario.line());
53+
info(log::color::reset());
54+
info(log::new_line);
55+
}
56+
void info(const cuke::ast::scenario_outline_node& scenario_outline,
57+
const table::row& row)
58+
{
59+
info(scenario_outline.keyword(), ": ",
60+
cuke::internal::replace_variables(scenario_outline.name(), row));
61+
info(color::black());
62+
info(" ", scenario_outline.file(), ':', scenario_outline.line());
63+
info(color::reset());
64+
info(new_line);
65+
}
66+
67+
void info(const std::vector<std::string>& doc_string)
68+
{
69+
info("\"\"\"", new_line);
70+
for (const std::string& line : doc_string)
71+
{
72+
info(line, new_line);
73+
}
74+
info("\"\"\"", new_line);
75+
}
76+
void info(const cuke::table& t)
77+
{
78+
for (const std::string& row : t.to_string_array())
79+
{
80+
info(" ", row, new_line);
81+
}
82+
}
83+
void info(const cuke::ast::step_node& step, results::test_status status)
84+
{
85+
info(results::to_color(status));
86+
info(results::step_prefix(status), step.keyword(), ' ', step.name());
87+
info(color::reset());
88+
89+
info(color::black());
90+
info(" ", step.file(), ':', step.line());
91+
info(color::reset());
92+
info(new_line);
93+
94+
if (!step.data_table().empty())
95+
{
96+
info(step.data_table());
97+
}
98+
if (!step.doc_string().empty())
99+
{
100+
info(step.doc_string());
101+
}
102+
}
103+
104+
} // namespace cuke::log

src/log_util.hpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "ast.hpp"
2+
#include "test_results.hpp"
3+
4+
namespace cuke::log
5+
{
6+
void verbose_end();
7+
void verbose_no_tags();
8+
void verbose_evaluate_tags(const ast::scenario_node& scenario,
9+
bool tag_evaluation, const std::string& expression);
10+
void verbose_skip();
11+
void verbose_ignore();
12+
void info(const cuke::ast::feature_node& feature);
13+
void info(const cuke::ast::scenario_node& scenario);
14+
void info(const cuke::ast::scenario_outline_node& scenario_outline,
15+
const table::row& row);
16+
void info(const std::vector<std::string>& doc_string);
17+
void info(const cuke::table& t);
18+
void info(const cuke::ast::step_node& step, results::test_status status);
19+
20+
} // namespace cuke::log

src/test_results.cpp

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#include "ast.hpp"
33
#include "log.hpp"
44
#include "util.hpp"
5-
#include "util_regex.hpp"
65

76
namespace cuke::results
87
{
@@ -117,10 +116,10 @@ std::string scenarios_to_string()
117116
bool add_comma = false;
118117
if (results::test_results().scenarios_failed() > 0)
119118
{
120-
if (log::colors_enabled()) str.append(log::red);
119+
str.append(log::color::red());
121120
str.append(std::to_string(results::test_results().scenarios_failed()));
122121
str.append(" failed");
123-
if (log::colors_enabled()) str.append(log::reset_color);
122+
str.append(log::color::reset());
124123
add_comma = true;
125124
}
126125

@@ -130,10 +129,10 @@ std::string scenarios_to_string()
130129
{
131130
str.append(", ");
132131
}
133-
if (log::colors_enabled()) str.append(log::blue);
132+
str.append(log::color::blue());
134133
str.append(std::to_string(results::test_results().scenarios_skipped()));
135134
str.append(" skipped");
136-
if (log::colors_enabled()) str.append(log::reset_color);
135+
str.append(log::color::reset());
137136
add_comma = true;
138137
}
139138

@@ -143,10 +142,10 @@ std::string scenarios_to_string()
143142
{
144143
str.append(", ");
145144
}
146-
if (log::colors_enabled()) str.append(log::green);
145+
str.append(log::color::green());
147146
str.append(std::to_string(results::test_results().scenarios_passed()));
148147
str.append(" passed");
149-
if (log::colors_enabled()) str.append(log::reset_color);
148+
str.append(log::color::reset());
150149
}
151150

152151
str += ')';
@@ -166,10 +165,10 @@ std::string steps_to_string()
166165
bool add_comma = false;
167166
if (results::test_results().steps_failed() > 0)
168167
{
169-
if (log::colors_enabled()) str.append(log::red);
168+
str.append(log::color::red());
170169
str.append(std::to_string(results::test_results().steps_failed()));
171170
str.append(" failed");
172-
if (log::colors_enabled()) str.append(log::reset_color);
171+
str.append(log::color::reset());
173172
add_comma = true;
174173
}
175174

@@ -179,10 +178,10 @@ std::string steps_to_string()
179178
{
180179
str.append(", ");
181180
}
182-
if (log::colors_enabled()) str.append(log::yellow);
181+
str.append(log::color::yellow());
183182
str.append(std::to_string(results::test_results().steps_undefined()));
184183
str.append(" undefined");
185-
if (log::colors_enabled()) str.append(log::reset_color);
184+
str.append(log::color::reset());
186185
add_comma = true;
187186
}
188187

@@ -192,10 +191,10 @@ std::string steps_to_string()
192191
{
193192
str.append(", ");
194193
}
195-
if (log::colors_enabled()) str.append(log::blue);
194+
str.append(log::color::blue());
196195
str.append(std::to_string(results::test_results().steps_skipped()));
197196
str.append(" skipped");
198-
if (log::colors_enabled()) str.append(log::reset_color);
197+
str.append(log::color::reset());
199198
add_comma = true;
200199
}
201200

@@ -205,10 +204,10 @@ std::string steps_to_string()
205204
{
206205
str.append(", ");
207206
}
208-
if (log::colors_enabled()) str.append(log::green);
207+
str.append(log::color::green());
209208
str.append(std::to_string(results::test_results().steps_passed()));
210209
str.append(" passed");
211-
if (log::colors_enabled()) str.append(log::reset_color);
210+
str.append(log::color::reset());
212211
}
213212

214213
str += ')';
@@ -220,15 +219,15 @@ const char* to_color(test_status status)
220219
switch (status)
221220
{
222221
case cuke::results::test_status::passed:
223-
return log::green;
222+
return log::color::green();
224223
case cuke::results::test_status::failed:
225-
return log::red;
224+
return log::color::red();
226225
case cuke::results::test_status::skipped:
227-
return log::blue;
226+
return log::color::blue();
228227
case cuke::results::test_status::undefined:
229-
return log::yellow;
228+
return log::color::yellow();
230229
default:
231-
return log::reset_color;
230+
return log::color::reset();
232231
}
233232
}
234233
std::string to_string(test_status status)
@@ -277,7 +276,7 @@ void new_feature(const cuke::ast::feature_node& current)
277276
result.description = cuke::internal::to_string(current.description());
278277
test_results().data().push_back(result);
279278
}
280-
void new_scenario(const cuke::ast::scenario_node& current)
279+
scenario& new_scenario(const cuke::ast::scenario_node& current)
281280
{
282281
scenario result;
283282
result.id = current.id();
@@ -287,8 +286,10 @@ void new_scenario(const cuke::ast::scenario_node& current)
287286
result.keyword = current.keyword();
288287
result.tags = current.tags();
289288
test_results().back().scenarios.push_back(result);
289+
return test_results().back().scenarios.back();
290290
}
291-
void new_step(const cuke::ast::step_node& current)
291+
void remove_last_scenario() { test_results().back().scenarios.pop_back(); }
292+
step& new_step(const cuke::ast::step_node& current)
292293
{
293294
step result;
294295
result.line = current.line();
@@ -300,19 +301,32 @@ void new_step(const cuke::ast::step_node& current)
300301
result.table = current.data_table();
301302

302303
test_results().back().scenarios.back().steps.push_back(result);
304+
return test_results().back().scenarios.back().steps.back();
303305
}
304306

305-
void set_source_location(const std::string& location)
307+
test_status final_result()
306308
{
307-
test_results().back().scenarios.back().steps.back().source_location =
308-
location;
309-
}
310-
void set_scenario_to(test_status status)
311-
{
312-
test_results().back().scenarios.back().status = status;
309+
if (test_results().data().empty())
310+
{
311+
return test_status::passed;
312+
}
313+
314+
if (test_results().scenarios_failed() == 0)
315+
{
316+
return test_status::passed;
317+
}
318+
return test_status::failed;
313319
}
320+
314321
void set_step_to(test_status status)
315322
{
316323
test_results().back().scenarios.back().steps.back().status = status;
317324
}
325+
326+
feature& features_back() { return test_results().back(); }
327+
scenario& scenarios_back() { return test_results().back().scenarios.back(); }
328+
step& steps_back()
329+
{
330+
return test_results().back().scenarios.back().steps.back();
331+
}
318332
} // namespace cuke::results

0 commit comments

Comments
 (0)