Skip to content

Commit 319758a

Browse files
committed
refactor: integrate StepRegistry and FeatureTreeFactory into Application and TestRunner implementations
1 parent c24cc46 commit 319758a

File tree

12 files changed

+147
-76
lines changed

12 files changed

+147
-76
lines changed

cucumber_cpp/library/Application.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "cucumber_cpp/library/Application.hpp"
22
#include "cucumber_cpp/library/Context.hpp"
3+
#include "cucumber_cpp/library/StepRegistry.hpp"
34
#include "cucumber_cpp/library/engine/ContextManager.hpp"
45
#include "cucumber_cpp/library/engine/FeatureFactory.hpp"
56
#include "cucumber_cpp/library/engine/FeatureInfo.hpp"
@@ -202,16 +203,20 @@ namespace cucumber_cpp::library
202203
return engine::executeRunPolicy;
203204
}() };
204205

205-
engine::TestRunnerImpl testRunner{ testExecution };
206+
StepRegistry stepRegistry{ parameterRegistry };
207+
engine::FeatureTreeFactory featureTreeFactory{ stepRegistry };
206208

207-
testRunner.Run(GetFeatureTree(tagExpression));
209+
engine::TestRunnerImpl testRunner{ featureTreeFactory, testExecution };
210+
211+
testRunner.Run(GetFeatureTree(featureTreeFactory, tagExpression));
208212

209213
std::cout << '\n'
210214
<< std::flush;
211215
}
212216

213-
std::vector<std::unique_ptr<engine::FeatureInfo>> Application::GetFeatureTree(std::string_view tagExpression)
217+
std::vector<std::unique_ptr<engine::FeatureInfo>> Application::GetFeatureTree(engine::FeatureTreeFactory& featureTreeFactory, std::string_view tagExpression)
214218
{
219+
215220
const auto featureFiles = GetFeatureFiles(options);
216221
std::vector<std::unique_ptr<engine::FeatureInfo>> vec;
217222
vec.reserve(featureFiles.size());

cucumber_cpp/library/Application.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#include "cucumber/gherkin/app.hpp"
55
#include "cucumber_cpp/library/Context.hpp"
6+
#include "cucumber_cpp/library/StepRegistry.hpp"
7+
#include "cucumber_cpp/library/cucumber_expression/ParameterRegistry.hpp"
68
#include "cucumber_cpp/library/engine/ContextManager.hpp"
79
#include "cucumber_cpp/library/engine/FeatureFactory.hpp"
810
#include "cucumber_cpp/library/engine/FeatureInfo.hpp"
@@ -64,7 +66,7 @@ namespace cucumber_cpp::library
6466
[[nodiscard]] int GetExitCode() const;
6567
void DryRunFeatures();
6668
void RunFeatures();
67-
[[nodiscard]] std::vector<std::unique_ptr<engine::FeatureInfo>> GetFeatureTree(std::string_view tagExpression);
69+
[[nodiscard]] std::vector<std::unique_ptr<engine::FeatureInfo>> GetFeatureTree(engine::FeatureTreeFactory& featureTreeFactory, std::string_view tagExpression);
6870
[[nodiscard]] engine::Result RunFeature(const std::filesystem::path& path, std::string_view tagExpression, report::ReportHandlerV2& reportHandler);
6971

7072
Options options;
@@ -78,7 +80,7 @@ namespace cucumber_cpp::library
7880

7981
cucumber::gherkin::app gherkin;
8082

81-
engine::FeatureTreeFactory featureTreeFactory{};
83+
cucumber_expression::ParameterRegistry parameterRegistry;
8284
};
8385
}
8486

cucumber_cpp/library/StepRegistry.cpp

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
#include "cucumber_cpp/library/StepRegistry.hpp"
2+
#include "cucumber_cpp/library/Body.hpp"
3+
#include "cucumber_cpp/library/Context.hpp"
4+
#include "cucumber_cpp/library/cucumber_expression/Expression.hpp"
25
#include "cucumber_cpp/library/cucumber_expression/Matcher.hpp"
6+
#include "cucumber_cpp/library/cucumber_expression/ParameterRegistry.hpp"
7+
#include "cucumber_cpp/library/cucumber_expression/RegularExpression.hpp"
38
#include "cucumber_cpp/library/engine/StepType.hpp"
9+
#include "cucumber_cpp/library/engine/Table.hpp"
410
#include <algorithm>
511
#include <cstddef>
12+
#include <memory>
613
#include <ranges>
14+
#include <span>
715
#include <string>
816
#include <utility>
917
#include <variant>
@@ -22,13 +30,14 @@ namespace cucumber_cpp::library
2230
};
2331
}
2432

25-
StepRegistry& StepRegistry::Instance()
33+
StepRegistry::StepRegistry(cucumber_expression::ParameterRegistry& parameterRegistry)
34+
: parameterRegistry{ parameterRegistry }
2635
{
27-
static StepRegistry instance;
28-
return instance;
36+
for (const auto& matcher : StepStringRegistration::Instance().GetEntries())
37+
Register(matcher.regex, matcher.type, matcher.factory);
2938
}
3039

31-
StepMatch StepRegistryBase::Query(engine::StepType stepType, const std::string& expression)
40+
StepMatch StepRegistry::Query(engine::StepType stepType, const std::string& expression)
3241
{
3342
std::vector<StepMatch> matches;
3443

@@ -51,19 +60,19 @@ namespace cucumber_cpp::library
5160
return std::move(matches.front());
5261
}
5362

54-
std::size_t StepRegistryBase::Size() const
63+
std::size_t StepRegistry::Size() const
5564
{
5665
return registry.size();
5766
}
5867

59-
std::size_t StepRegistryBase::Size(engine::StepType stepType) const
68+
std::size_t StepRegistry::Size(engine::StepType stepType) const
6069
{
6170
return std::ranges::count(registry, stepType, &Entry::type);
6271
}
6372

64-
std::vector<StepRegistryBase::EntryView> StepRegistryBase::List() const
73+
std::vector<StepRegistry::EntryView> StepRegistry::List() const
6574
{
66-
std::vector<StepRegistryBase::EntryView> list;
75+
std::vector<StepRegistry::EntryView> list;
6776

6877
list.reserve(registry.size());
6978

@@ -72,4 +81,28 @@ namespace cucumber_cpp::library
7281

7382
return list;
7483
}
84+
85+
void StepRegistry::Register(const std::string& matcher, engine::StepType stepType, std::unique_ptr<Body> (&factory)(Context& context, const engine::Table& table))
86+
{
87+
if (matcher.starts_with('^') || matcher.ends_with('$'))
88+
registry.emplace_back(stepType, cucumber_expression::Matcher{ std::in_place_type<cucumber_expression::RegularExpression>, matcher }, factory);
89+
else
90+
registry.emplace_back(stepType, cucumber_expression::Matcher{ std::in_place_type<cucumber_expression::Expression>, matcher, parameterRegistry }, factory);
91+
}
92+
93+
StepStringRegistration& StepStringRegistration::Instance()
94+
{
95+
static StepStringRegistration instance;
96+
return instance;
97+
}
98+
99+
std::span<StepStringRegistration::Entry> StepStringRegistration::GetEntries()
100+
{
101+
return registry;
102+
}
103+
104+
std::span<const StepStringRegistration::Entry> StepStringRegistration::GetEntries() const
105+
{
106+
return registry;
107+
}
75108
}

cucumber_cpp/library/StepRegistry.hpp

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@
33

44
#include "cucumber_cpp/library/Body.hpp"
55
#include "cucumber_cpp/library/Context.hpp"
6-
#include "cucumber_cpp/library/cucumber_expression/Expression.hpp"
76
#include "cucumber_cpp/library/cucumber_expression/Matcher.hpp"
87
#include "cucumber_cpp/library/cucumber_expression/ParameterRegistry.hpp"
9-
#include "cucumber_cpp/library/cucumber_expression/RegularExpression.hpp"
108
#include "cucumber_cpp/library/engine/StepType.hpp"
119
#include "cucumber_cpp/library/engine/Table.hpp"
1210
#include <any>
1311
#include <cstddef>
1412
#include <cstdint>
1513
#include <exception>
1614
#include <memory>
15+
#include <span>
1716
#include <string>
1817
#include <string_view>
1918
#include <utility>
@@ -22,6 +21,12 @@
2221

2322
namespace cucumber_cpp::library
2423
{
24+
template<class T>
25+
std::unique_ptr<Body> StepBodyFactory(Context& context, const engine::Table& table)
26+
{
27+
return std::make_unique<T>(context, table);
28+
}
29+
2530
struct StepMatch
2631
{
2732
StepMatch(std::unique_ptr<Body> (&factory)(Context& context, const engine::Table& table), std::variant<std::vector<std::string>, std::vector<std::any>> matches, std::string_view stepRegexStr)
@@ -35,7 +40,7 @@ namespace cucumber_cpp::library
3540
std::string_view stepRegexStr{};
3641
};
3742

38-
struct StepRegistryBase
43+
struct StepRegistry
3944
{
4045
struct StepNotFoundError : std::exception
4146
{
@@ -77,62 +82,63 @@ namespace cucumber_cpp::library
7782
const std::uint32_t& used;
7883
};
7984

85+
explicit StepRegistry(cucumber_expression::ParameterRegistry& parameterRegistry);
86+
8087
[[nodiscard]] StepMatch Query(engine::StepType stepType, const std::string& expression);
8188

8289
[[nodiscard]] std::size_t Size() const;
8390
[[nodiscard]] std::size_t Size(engine::StepType stepType) const;
8491

8592
[[nodiscard]] std::vector<EntryView> List() const;
8693

87-
protected:
88-
template<class T>
89-
std::size_t
90-
Register(const std::string& matcher, engine::StepType stepType);
91-
9294
private:
93-
template<class T>
94-
static std::unique_ptr<Body> Construct(Context& context, const engine::Table& table);
95+
void Register(const std::string& matcher, engine::StepType stepType, std::unique_ptr<Body> (&factory)(Context& context, const engine::Table& table));
9596

9697
std::vector<Entry> registry;
97-
cucumber_expression::ParameterRegistry parameterRegistry;
98+
cucumber_expression::ParameterRegistry& parameterRegistry;
9899
};
99100

100-
struct StepRegistry : StepRegistryBase
101+
struct StepStringRegistration
101102
{
102103
private:
103-
StepRegistry() = default;
104+
StepStringRegistration() = default;
104105

105106
public:
106-
static StepRegistry& Instance();
107+
static StepStringRegistration& Instance();
108+
109+
struct Entry
110+
{
111+
Entry(engine::StepType type, std::string regex, std::unique_ptr<Body> (&factory)(Context& context, const engine::Table& table))
112+
: type(type)
113+
, regex(std::move(regex))
114+
, factory(factory)
115+
{}
116+
117+
engine::StepType type{};
118+
std::string regex;
119+
std::unique_ptr<Body> (&factory)(Context& context, const engine::Table& table);
120+
};
107121

108122
template<class T>
109123
static std::size_t Register(const std::string& matcher, engine::StepType stepType);
124+
125+
std::span<Entry> GetEntries();
126+
[[nodiscard]] std::span<const Entry> GetEntries() const;
127+
128+
private:
129+
std::vector<Entry> registry;
110130
};
111131

112132
//////////////////////////
113133
// implementation //
114134
//////////////////////////
115135

116136
template<class T>
117-
std::size_t StepRegistryBase::Register(const std::string& matcher, engine::StepType stepType)
137+
std::size_t StepStringRegistration::Register(const std::string& matcher, engine::StepType stepType)
118138
{
119-
if (matcher.starts_with('^') || matcher.ends_with('$'))
120-
registry.emplace_back(stepType, cucumber_expression::Matcher{ std::in_place_type<cucumber_expression::RegularExpression>, matcher }, Construct<T>);
121-
else
122-
registry.emplace_back(stepType, cucumber_expression::Matcher{ std::in_place_type<cucumber_expression::Expression>, matcher, parameterRegistry }, Construct<T>);
123-
return registry.size();
124-
}
125-
126-
template<class T>
127-
std::unique_ptr<Body> StepRegistryBase::Construct(Context& context, const engine::Table& table)
128-
{
129-
return std::make_unique<T>(context, table);
130-
}
139+
Instance().registry.emplace_back(stepType, matcher, StepBodyFactory<T>);
131140

132-
template<class T>
133-
std::size_t StepRegistry::Register(const std::string& matcher, engine::StepType stepType)
134-
{
135-
return Instance().StepRegistryBase::Register<T>(matcher, stepType);
141+
return Instance().registry.size();
136142
}
137143
}
138144

cucumber_cpp/library/Steps.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include "cucumber_cpp/library/StepRegistry.hpp"
66
#include "cucumber_cpp/library/engine/Step.hpp"
77

8-
#define STEP_(matcher, type, args, fixture) BODY(matcher, type, args, cucumber_cpp::library::StepRegistry::Register, fixture)
8+
#define STEP_(matcher, type, args, fixture) BODY(matcher, type, args, cucumber_cpp::library::StepStringRegistration::Register, fixture)
99

1010
#define STEP_TYPE_(fixture, type, ...) \
1111
STEP_( \

0 commit comments

Comments
 (0)