Skip to content

Commit 5b3e5ff

Browse files
committed
refactored scenario pipeline, updated results
1 parent a09228a commit 5b3e5ff

4 files changed

Lines changed: 70 additions & 61 deletions

File tree

src/test_results.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ void new_feature(const cuke::ast::feature_node& current)
277277
result.description = cuke::internal::to_string(current.description());
278278
test_results().data().push_back(result);
279279
}
280-
void new_scenario(const cuke::ast::scenario_node& current)
280+
scenario& new_scenario(const cuke::ast::scenario_node& current)
281281
{
282282
scenario result;
283283
result.id = current.id();
@@ -287,7 +287,9 @@ void new_scenario(const cuke::ast::scenario_node& current)
287287
result.keyword = current.keyword();
288288
result.tags = current.tags();
289289
test_results().back().scenarios.push_back(result);
290+
return test_results().back().scenarios.back();
290291
}
292+
void remove_last_scenario() { test_results().back().scenarios.pop_back(); }
291293
step& new_step(const cuke::ast::step_node& current)
292294
{
293295
step result;

src/test_results.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,9 @@ class test_result
107107
}
108108

109109
void new_feature(const cuke::ast::feature_node& current);
110-
void new_scenario(const cuke::ast::scenario_node& current);
111-
step& new_step(const cuke::ast::step_node& current);
110+
[[nodiscard]] scenario& new_scenario(const cuke::ast::scenario_node& current);
111+
[[nodiscard]] step& new_step(const cuke::ast::step_node& current);
112+
void remove_last_scenario();
112113
void set_source_location(const std::string& location);
113114
void set_feature_to(test_status status);
114115
void set_scenario_to(test_status status);

src/test_runner.cpp

Lines changed: 62 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,6 @@ namespace cuke
1717

1818
namespace
1919
{
20-
void verbose_start(const ast::scenario_node& scenario)
21-
{
22-
log::verbose("[ VERBOSE ] ----------------------------------",
23-
log::new_line);
24-
log::verbose(std::format("[ VERBOSE ] Scenario Start '{}' - File: {}:{}",
25-
scenario.name(), scenario.file(), scenario.line()),
26-
log::new_line);
27-
}
2820
void verbose_end()
2921
{
3022
log::verbose("[ VERBOSE ] Scenario end", log::new_line);
@@ -59,16 +51,18 @@ void verbose_ignore()
5951
log::new_line);
6052
}
6153

62-
[[nodiscard]] bool tags_valid(const ast::scenario_node& scenario,
63-
const internal::tag_expression& tag_expression)
54+
[[nodiscard]] bool tags_valid(const scenario_pipeline_context& context)
6455
{
65-
if (tag_expression.empty())
56+
if (context.tag_expression.empty())
6657
{
6758
verbose_no_tags();
6859
return true;
6960
}
70-
bool tag_evaluation = tag_expression.evaluate(scenario.tags());
71-
verbose_evaluate_tags(scenario, tag_evaluation, tag_expression.expression());
61+
bool tag_evaluation =
62+
context.tag_expression.evaluate(context.scenario.tags());
63+
64+
verbose_evaluate_tags(context.scenario, tag_evaluation,
65+
context.tag_expression.expression());
7266

7367
return tag_evaluation;
7468
}
@@ -156,54 +150,61 @@ void log_helper(const cuke::ast::step_node& step, results::test_status status)
156150
return false;
157151
}
158152

159-
void update_scenario_status(std::string_view name, std::string_view file,
160-
std::size_t line, bool skipped)
153+
[[nodiscard]] bool has_undefined_steps(const std::vector<results::step>& steps)
161154
{
162-
const auto& steps = results::scenarios_back().steps;
155+
return std::any_of(
156+
steps.begin(), steps.end(), [](const auto& step)
157+
{ return step.status == results::test_status::undefined; });
158+
}
163159

164-
if (skipped)
160+
[[nodiscard]] bool has_failed_or_undefined_steps(
161+
const std::vector<results::step>& steps)
162+
{
163+
return std::any_of(steps.begin(), steps.end(),
164+
[](const auto& step)
165+
{
166+
return step.status == results::test_status::failed ||
167+
step.status == results::test_status::undefined;
168+
});
169+
}
170+
171+
void update_scenario_status(scenario_pipeline_context& context)
172+
{
173+
const auto& steps = results::scenarios_back().steps;
174+
if (context.skip_scenario)
165175
{
166176
#ifdef UNDEFINED_STEPS_ARE_A_FAILURE
167-
if (std::any_of(steps.begin(), steps.end(), [](const auto& step)
168-
{ return step.status == results::test_status::undefined; }))
177+
if (has_undefined_steps(steps))
169178
{
170-
results::scenarios_back().status = results::test_status::failed;
179+
context.result.status = results::test_status::failed;
171180
}
172181
else
173182
#endif // UNDEFINED_STEPS_ARE_A_FAILURE
174-
results::scenarios_back().status = results::test_status::skipped;
183+
context.result.status = results::test_status::skipped;
175184
}
176185
else if (internal::get_runtime_options().fail_scenario().is_set)
177186
{
178187
const std::string& msg =
179188
internal::get_runtime_options().fail_scenario().msg;
180189
log::error(msg, log::new_line);
181-
results::scenarios_back().status = results::test_status::failed;
182-
std::for_each(results::scenarios_back().steps.begin(),
183-
results::scenarios_back().steps.end(),
184-
[&msg](results::step& step) { step.error_msg = msg; });
190+
context.result.status = results::test_status::failed;
191+
for (results::step& step : context.result.steps)
192+
{
193+
step.error_msg = msg;
194+
}
185195
}
186196
else
187197
{
188-
if (std::any_of(steps.begin(), steps.end(),
189-
[](const auto& step)
190-
{
191-
return step.status == results::test_status::failed ||
192-
step.status == results::test_status::undefined;
193-
}))
198+
if (has_failed_or_undefined_steps(steps))
194199
{
195-
{
196-
results::scenarios_back().status = results::test_status::failed;
197-
}
200+
context.result.status = results::test_status::failed;
198201
}
199202
}
200203

201204
internal::get_runtime_options().reset_fail_scenario();
202-
results::test_results().add_scenario(results::scenarios_back().status);
205+
results::test_results().add_scenario(context.result.status);
203206
}
204207

205-
} // namespace
206-
207208
void skip_step(step_pipeline_context& context)
208209
{
209210
const bool continue_on_failure_or_prev_step_failed = []()
@@ -308,39 +309,40 @@ void run_step(const ast::step_node& step, bool scenario_already_skpped)
308309
}
309310
}
310311

311-
void setup_scenario(scenario_pipeline_context& context)
312+
void verbose_start_print(scenario_pipeline_context& context)
312313
{
313-
verbose_start(context.scenario);
314+
log::verbose("[ VERBOSE ] ----------------------------------",
315+
log::new_line);
316+
log::verbose(std::format("[ VERBOSE ] Scenario Start '{}' - File: {}:{}",
317+
context.scenario.name(), context.scenario.file(),
318+
context.scenario.line()),
319+
log::new_line);
314320
}
315321
void hook_before_scenario(scenario_pipeline_context& context)
316322
{
317323
cuke::registry().run_hook_before(context.scenario.tags());
318324
}
319325
void is_scenario_ignored(scenario_pipeline_context& context)
320326
{
321-
if (ignore_flag() || !tags_valid(context.scenario, context.tag_expression))
327+
if (ignore_flag() || !tags_valid(context))
322328
{
323329
verbose_ignore();
324330
verbose_end();
325331
internal::get_runtime_options().skip_scenario(false);
326332
context.ignore = true;
333+
results::remove_last_scenario();
327334
}
328335
}
329336
void is_scenario_skipped(scenario_pipeline_context& context)
330337
{
331338
context.skip_scenario =
332339
skip_flag() || program_arguments().get_options().dry_run;
333-
}
334-
void init_scenario(scenario_pipeline_context& context)
335-
{
336-
results::new_scenario(context.scenario);
337340

338341
if (context.skip_scenario)
339342
{
340343
verbose_skip();
341-
results::scenarios_back().status = results::test_status::skipped;
344+
context.result.status = results::test_status::skipped;
342345
}
343-
344346
log_helper(context.scenario);
345347
}
346348
void run_background(scenario_pipeline_context& context)
@@ -368,30 +370,33 @@ void hook_after_scenario(scenario_pipeline_context& context)
368370
cuke::registry().run_hook_after(context.scenario.tags());
369371
}
370372
}
371-
void teardown_scenario(scenario_pipeline_context& context)
373+
void reset_user_context(scenario_pipeline_context&)
372374
{
373-
update_scenario_status(context.scenario.name(), context.scenario.file(),
374-
context.scenario.line(), context.skip_scenario);
375375
cuke::internal::reset_context();
376-
376+
}
377+
void verbose_end_print(scenario_pipeline_context& context)
378+
{
377379
verbose_end();
378380
log::info(log::new_line);
379381
}
380382

381383
// clang-format off
382384
std::vector<void (*)(scenario_pipeline_context&)> scenario_pipeline = {
383-
setup_scenario,
385+
verbose_start_print,
384386
hook_before_scenario,
385387
is_scenario_ignored,
386388
is_scenario_skipped,
387-
init_scenario,
388389
run_background,
389-
run_all_steps,
390-
hook_after_scenario,
391-
teardown_scenario
390+
run_all_steps,
391+
hook_after_scenario,
392+
update_scenario_status,
393+
reset_user_context,
394+
verbose_end_print
392395
};
393396
// clang-format on
394397

398+
} // namespace
399+
395400
void test_runner::setup() const { cuke::registry().run_hook_before_all(); }
396401
void test_runner::teardown() const { cuke::registry().run_hook_after_all(); }
397402
void test_runner::run() const
@@ -440,7 +445,8 @@ void test_runner::visit(
440445
void test_runner::run_scenario(const ast::scenario_node& scenario) const
441446
{
442447
scenario_pipeline_context context{.scenario = scenario,
443-
.tag_expression = m_tag_expression};
448+
.tag_expression = m_tag_expression,
449+
.result = results::new_scenario(scenario)};
444450
for (const auto& pipeline_step : scenario_pipeline)
445451
{
446452
pipeline_step(context);

src/test_runner.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace cuke
1212
struct step_pipeline_context
1313
{
1414
const ast::step_node& step;
15-
cuke::results::step& result;
15+
results::step& result;
1616
const bool scenario_already_skpped;
1717
};
1818

@@ -22,7 +22,7 @@ struct scenario_pipeline_context
2222
const internal::tag_expression& tag_expression;
2323
bool skip_scenario = false;
2424
bool ignore = false;
25-
results::test_status status = results::test_status::passed;
25+
results::scenario& result;
2626
};
2727

2828
class test_runner

0 commit comments

Comments
 (0)