Skip to content

Commit 7287b4c

Browse files
committed
refactor: update HookExecutor and TestExecution to use optional scopes for feature and scenario starts
1 parent 739cc17 commit 7287b4c

File tree

7 files changed

+26
-16
lines changed

7 files changed

+26
-16
lines changed

cucumber_cpp/acceptance_test/test.bats

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ teardown() {
183183
assert_failure
184184
assert_output --partial "skipped Given a given step"
185185
refute_output --partial "should not be executed"
186-
# assert_output --partial "tests : 0/1 passed"
186+
assert_output --partial "tests : 0/1 passed"
187187
}
188188

189189
@test "Test unicode" {

cucumber_cpp/library/engine/ContextManager.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ namespace cucumber_cpp::library::engine
5757
NestedContext(RunnerContext& parent, const T& info)
5858
: RunnerContext{ &parent }
5959
, info{ info }
60-
{}
60+
{
61+
if (parent.InheritedExecutionStatus() != Result::passed)
62+
ExecutionStatus(Result::skipped);
63+
}
6164

6265
const T& info;
6366
};

cucumber_cpp/library/engine/HookExecutor.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "cucumber_cpp/library/engine/ScenarioInfo.hpp"
77
#include "cucumber_cpp/library/engine/StepInfo.hpp"
88
#include <functional>
9+
#include <optional>
910
#include <set>
1011
#include <stdexcept>
1112
#include <string>
@@ -72,14 +73,18 @@ namespace cucumber_cpp::library::engine
7273
return ProgramScope{ contextManager };
7374
}
7475

75-
HookExecutor::FeatureScope HookExecutorImpl::FeatureStart()
76+
std::optional<HookExecutor::FeatureScope> HookExecutorImpl::FeatureStart(cucumber_cpp::library::engine::ContextManager& contextManager)
7677
{
77-
return FeatureScope{ contextManager };
78+
if (contextManager.FeatureContext().InheritedExecutionStatus() != Result::passed)
79+
return std::nullopt;
80+
return std::make_optional<FeatureScope>(contextManager);
7881
}
7982

80-
HookExecutor::ScenarioScope HookExecutorImpl::ScenarioStart()
83+
std::optional<HookExecutor::ScenarioScope> HookExecutorImpl::ScenarioStart(cucumber_cpp::library::engine::ContextManager& contextManager)
8184
{
82-
return ScenarioScope{ contextManager };
85+
if (contextManager.ScenarioContext().InheritedExecutionStatus() != Result::passed)
86+
return std::nullopt;
87+
return std::make_optional<ScenarioScope>(contextManager);
8388
}
8489

8590
HookExecutor::StepScope HookExecutorImpl::StepStart()

cucumber_cpp/library/engine/HookExecutor.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "cucumber_cpp/library/engine/ContextManager.hpp"
66
#include "cucumber_cpp/library/util/Immoveable.hpp"
77
#include <functional>
8+
#include <optional>
89
#include <set>
910
#include <string>
1011

@@ -23,8 +24,8 @@ namespace cucumber_cpp::library::engine
2324
struct StepScope;
2425

2526
[[nodiscard]] virtual ProgramScope BeforeAll() = 0;
26-
[[nodiscard]] virtual FeatureScope FeatureStart() = 0;
27-
[[nodiscard]] virtual ScenarioScope ScenarioStart() = 0;
27+
[[nodiscard]] virtual std::optional<FeatureScope> FeatureStart(cucumber_cpp::library::engine::ContextManager& contextManager) = 0;
28+
[[nodiscard]] virtual std::optional<ScenarioScope> ScenarioStart(cucumber_cpp::library::engine::ContextManager& contextManager) = 0;
2829
[[nodiscard]] virtual StepScope StepStart() = 0;
2930

3031
private:
@@ -75,8 +76,8 @@ namespace cucumber_cpp::library::engine
7576

7677
[[nodiscard]] ProgramScope
7778
BeforeAll() override;
78-
[[nodiscard]] FeatureScope FeatureStart() override;
79-
[[nodiscard]] ScenarioScope ScenarioStart() override;
79+
[[nodiscard]] std::optional<FeatureScope> FeatureStart(cucumber_cpp::library::engine::ContextManager& contextManager) override;
80+
[[nodiscard]] std::optional<ScenarioScope> ScenarioStart(cucumber_cpp::library::engine::ContextManager& contextManager) override;
8081
[[nodiscard]] StepScope StepStart() override;
8182

8283
private:

cucumber_cpp/library/engine/TestExecution.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace cucumber_cpp::library::engine
2525
TestExecution::FeatureScope::FeatureScope(cucumber_cpp::library::engine::ContextManager& contextManager, report::ReportForwarder& reportHandler, HookExecutor& hookExecution, const cucumber_cpp::library::engine::FeatureInfo& featureInfo)
2626
: scopedFeatureContext{ contextManager.CreateFeatureContext(featureInfo) }
2727
, scopedFeatureReport{ reportHandler.FeatureStart() }
28-
, scopedFeatureHook{ hookExecution.FeatureStart() }
28+
, scopedFeatureHook{ hookExecution.FeatureStart(contextManager) }
2929
{
3030
}
3131

@@ -38,7 +38,7 @@ namespace cucumber_cpp::library::engine
3838
TestExecution::ScenarioScope::ScenarioScope(cucumber_cpp::library::engine::ContextManager& contextManager, report::ReportForwarder& reportHandler, HookExecutor& hookExecution, const cucumber_cpp::library::engine::ScenarioInfo& scenarioInfo)
3939
: scopedScenarioContext{ contextManager.CreateScenarioContext(scenarioInfo) }
4040
, scopedScenarioReport{ reportHandler.ScenarioStart() }
41-
, scopedScenarioHook{ hookExecution.ScenarioStart() }
41+
, scopedScenarioHook{ hookExecution.ScenarioStart(contextManager) }
4242
{
4343
}
4444

cucumber_cpp/library/engine/TestExecution.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "cucumber_cpp/library/engine/StepInfo.hpp"
1212
#include "cucumber_cpp/library/report/Report.hpp"
1313
#include "cucumber_cpp/library/util/Immoveable.hpp"
14+
#include <optional>
1415
#include <variant>
1516
#include <vector>
1617

@@ -53,7 +54,7 @@ namespace cucumber_cpp::library::engine
5354
private:
5455
cucumber_cpp::library::engine::ContextManager::ScopedFeatureContext scopedFeatureContext;
5556
report::ReportForwarder::FeatureScope scopedFeatureReport;
56-
HookExecutor::FeatureScope scopedFeatureHook;
57+
std::optional<HookExecutor::FeatureScope> scopedFeatureHook;
5758
};
5859

5960
struct TestExecution::RuleScope : library::util::Immoveable
@@ -72,7 +73,7 @@ namespace cucumber_cpp::library::engine
7273
private:
7374
cucumber_cpp::library::engine::ContextManager::ScopedScenarioContext scopedScenarioContext;
7475
report::ReportForwarder::ScenarioScope scopedScenarioReport;
75-
HookExecutor::ScenarioScope scopedScenarioHook;
76+
std::optional<HookExecutor::ScenarioScope> scopedScenarioHook;
7677
};
7778

7879
struct TestExecution::Policy

cucumber_cpp/library/engine/test/TestHookExecutor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ namespace cucumber_cpp::library::engine
4141
ASSERT_FALSE(contextManagerInstance->FeatureContext().Contains("hookAfterFeature"));
4242

4343
{
44-
auto scope = hookExecutor->FeatureStart();
44+
auto scope = hookExecutor->FeatureStart(*contextManagerInstance);
4545
EXPECT_TRUE(contextManagerInstance->FeatureContext().Contains("hookBeforeFeature"));
4646
}
4747
EXPECT_TRUE(contextManagerInstance->FeatureContext().Contains("hookAfterFeature"));
@@ -53,7 +53,7 @@ namespace cucumber_cpp::library::engine
5353
ASSERT_FALSE(contextManagerInstance->ScenarioContext().Contains("hookAfterScenario"));
5454

5555
{
56-
auto scope = hookExecutor->ScenarioStart();
56+
auto scope = hookExecutor->ScenarioStart(*contextManagerInstance);
5757
EXPECT_TRUE(contextManagerInstance->ScenarioContext().Contains("hookBeforeScenario"));
5858
}
5959
EXPECT_TRUE(contextManagerInstance->ScenarioContext().Contains("hookAfterScenario"));

0 commit comments

Comments
 (0)