Skip to content

Commit b812f35

Browse files
committed
add tests
1 parent b2c77d0 commit b812f35

File tree

3 files changed

+57
-19
lines changed

3 files changed

+57
-19
lines changed

cucumber_cpp/library/Application.cpp

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ namespace cucumber_cpp::library
117117
runCommand->add_option("--outputfolder", options.outputfolder, "Specifies the output folder for generated report files")->group("report generation");
118118
runCommand->add_option("--reportfile", options.reportfile, "Specifies the output name for generated report files")->group("report generation");
119119
runCommand->add_flag("--dry", options.dryrun, "Generate report without running tests");
120-
runCommand->add_flag("--unused", options.printStepsNotUsed, "Show step definitions that were not used")->default_val(false);
120+
runCommand->add_flag("--unused", options.printStepsNotUsed, "Show step definitions that were not used");
121121

122122
reporters.Add("console", std::make_unique<report::StdOutReport>());
123123
reporters.Add("junit-xml", std::make_unique<report::JunitReport>(options.outputfolder, options.reportfile));
@@ -217,26 +217,31 @@ namespace cucumber_cpp::library
217217
testRunner.Run(GetFeatureTree(featureTreeFactory, tagExpression));
218218

219219
if (options.printStepsNotUsed)
220-
{
221-
auto isUsed = [](const StepRegistry::EntryView& entry)
222-
{
223-
return entry.used > 0;
224-
};
225-
auto unusedSteps = stepRegistry.List() | std::views::filter(std::not_fn(isUsed));
226-
if (std::ranges::begin(unusedSteps) == std::ranges::end(unusedSteps))
227-
std::cout << "\nAll steps have been used.";
228-
else
229-
{
230-
std::cout << "\nThe following steps have not been used:";
231-
for (const auto& entry : unusedSteps)
232-
std::cout << "\n - " << std::visit(cucumber_expression::PatternVisitor{}, entry.stepRegex);
233-
}
234-
}
220+
PrintStepsNotUsed(stepRegistry);
235221

236222
std::cout << '\n'
237223
<< std::flush;
238224
}
239225

226+
void Application::PrintStepsNotUsed(StepRegistry& stepRegistry)
227+
{
228+
auto isUnused = [](const StepRegistry::EntryView& entry)
229+
{
230+
return entry.used == 0;
231+
};
232+
233+
auto unusedSteps = stepRegistry.List() | std::views::filter(isUnused);
234+
235+
if (std::ranges::empty(unusedSteps))
236+
std::cout << "\nAll steps have been used.";
237+
else
238+
{
239+
std::cout << "\nThe following steps have not been used:";
240+
for (const auto& entry : unusedSteps)
241+
std::cout << "\n - " << std::visit(cucumber_expression::SourceVisitor{}, entry.stepRegex);
242+
}
243+
}
244+
240245
std::vector<std::unique_ptr<engine::FeatureInfo>> Application::GetFeatureTree(const engine::FeatureTreeFactory& featureTreeFactory, std::string_view tagExpression)
241246
{
242247

cucumber_cpp/library/Application.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "cucumber/gherkin/app.hpp"
88
#include "cucumber_cpp/library/Context.hpp"
9+
#include "cucumber_cpp/library/StepRegistry.hpp"
910
#include "cucumber_cpp/library/cucumber_expression/ParameterRegistry.hpp"
1011
#include "cucumber_cpp/library/engine/ContextManager.hpp"
1112
#include "cucumber_cpp/library/engine/FeatureFactory.hpp"
@@ -54,12 +55,14 @@ namespace cucumber_cpp::library
5455
void AddReportHandler(const std::string& name, std::unique_ptr<report::ReportHandlerV2>&& reporter);
5556

5657
private:
57-
[[nodiscard]] int GetExitCode() const;
58-
[[nodiscard]] int GetExitCode(engine::Result result) const;
5958
void DryRunFeatures();
6059
void RunFeatures();
61-
[[nodiscard]] std::vector<std::unique_ptr<engine::FeatureInfo>> GetFeatureTree(const engine::FeatureTreeFactory& featureTreeFactory, std::string_view tagExpression);
6260
[[nodiscard]] engine::Result RunFeature(const std::filesystem::path& path, std::string_view tagExpression, report::ReportHandlerV2& reportHandler);
61+
void PrintStepsNotUsed(StepRegistry& stepRegistry);
62+
[[nodiscard]] std::vector<std::unique_ptr<engine::FeatureInfo>> GetFeatureTree(const engine::FeatureTreeFactory& featureTreeFactory, std::string_view tagExpression);
63+
64+
[[nodiscard]] int GetExitCode() const;
65+
[[nodiscard]] int GetExitCode(engine::Result result) const;
6366

6467
Options options;
6568
CLI::App cli;

cucumber_cpp/library/test/TestApplication.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "cucumber_cpp/CucumberCpp.hpp"
22
#include "cucumber_cpp/library/Application.hpp"
33
#include "cucumber_cpp/library/engine/test_helper/TemporaryFile.hpp"
4+
#include "gmock/gmock.h"
45
#include <CLI/Error.hpp>
56
#include <array>
67
#include <cstddef>
@@ -110,4 +111,33 @@ namespace cucumber_cpp::library
110111
{
111112
EXPECT_THAT(&Application{}.ParameterRegistration(), testing::NotNull());
112113
}
114+
115+
TEST_F(TestApplication, UnusedParameters)
116+
{
117+
auto tmp = engine::test_helper::TemporaryFile{ "tmpfile.feature" };
118+
const auto path = tmp.Path().string();
119+
120+
tmp << "Feature: Test feature\n"
121+
" Rule: Test rule\n"
122+
" Scenario: Test scenario1\n"
123+
" Given 5 and 5 are equal\n"
124+
" And This is a GIVEN step\n"
125+
" And This is a WHEN step\n"
126+
" And This is a THEN step\n"
127+
" And This is a STEP step\n";
128+
129+
const std::array args{ "application", "run", "--feature", path.c_str(), "--report", "console", "--unused" };
130+
131+
std::string stdoutString = RunWithArgs(args, static_cast<std::underlying_type_t<CLI::ExitCodes>>(CLI::ExitCodes::Success));
132+
133+
EXPECT_THAT(stdoutString, testing::HasSubstr("The following steps have not been used:"));
134+
EXPECT_THAT(stdoutString, testing::HasSubstr("^This is a step with a ([0-9]+)s delay$"));
135+
EXPECT_THAT(stdoutString, testing::HasSubstr("Step with cucumber expression syntax {float} {string} {int}"));
136+
137+
EXPECT_THAT(stdoutString, testing::Not(testing::HasSubstr("{int} and {int} are equal")));
138+
EXPECT_THAT(stdoutString, testing::Not(testing::HasSubstr("And This is a GIVEN step")));
139+
EXPECT_THAT(stdoutString, testing::Not(testing::HasSubstr("And This is a WHEN step")));
140+
EXPECT_THAT(stdoutString, testing::Not(testing::HasSubstr("And This is a THEN step")));
141+
EXPECT_THAT(stdoutString, testing::Not(testing::HasSubstr("And This is a STEP step")));
142+
}
113143
}

0 commit comments

Comments
 (0)