Skip to content

Commit cbb2f0c

Browse files
committed
Added global-hooks-afterall-error, global-hooks-attachments, global-hooks-beforeall-error, hooks, hooks-attachment and hooks-conditional compatibility tests
1 parent 640986f commit cbb2f0c

File tree

19 files changed

+321
-139
lines changed

19 files changed

+321
-139
lines changed

compatibility/compatibility.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,19 @@ namespace compatibility
209209

210210
void CompareEnvelopes()
211211
{
212-
ASSERT_THAT(actualEnvelopes.size(), testing::Eq(expectedEnvelopes.size()));
212+
EXPECT_THAT(actualEnvelopes.size(), testing::Eq(expectedEnvelopes.size()));
213+
214+
for (auto& json : actualEnvelopes)
215+
{
216+
SanitizeActualJson(json);
217+
actualOfs << json.dump() << "\n";
218+
}
219+
220+
for (auto& json : expectedEnvelopes)
221+
{
222+
SanitizeExpectedJson(json);
223+
expectedOfs << json.dump() << "\n";
224+
}
213225

214226
while (!actualEnvelopes.empty() && !expectedEnvelopes.empty())
215227
{
@@ -219,12 +231,6 @@ namespace compatibility
219231
auto expectedJson = expectedEnvelopes.front();
220232
expectedEnvelopes.pop_front();
221233

222-
SanitizeActualJson(actualJson);
223-
SanitizeExpectedJson(expectedJson);
224-
225-
expectedOfs << expectedJson.dump() << "\n";
226-
actualOfs << actualJson.dump() << "\n";
227-
228234
EXPECT_THAT(actualJson, testing::Eq(expectedJson));
229235
}
230236
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "cucumber_cpp/CucumberCpp.hpp"
2+
#include <gmock/gmock.h>
3+
4+
HOOK_BEFORE_ALL()
5+
{
6+
// no-op
7+
}
8+
9+
HOOK_BEFORE_ALL()
10+
{
11+
// no-op
12+
}
13+
14+
WHEN(R"(a step passes)")
15+
{
16+
// no-op
17+
}
18+
19+
HOOK_AFTER_ALL()
20+
{
21+
// no-op
22+
}
23+
24+
HOOK_AFTER_ALL()
25+
{
26+
ASSERT_THAT(true, false);
27+
}
28+
29+
HOOK_AFTER_ALL()
30+
{
31+
// no-op
32+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include "cucumber_cpp/CucumberCpp.hpp"
2+
#include <gmock/gmock.h>
3+
4+
HOOK_BEFORE_ALL()
5+
{
6+
Attach("Attachment from BeforeAll hook", "text/plain");
7+
}
8+
9+
WHEN(R"(a step passes)")
10+
{
11+
// no-op
12+
}
13+
14+
HOOK_AFTER_ALL()
15+
{
16+
Attach("Attachment from AfterAll hook", "text/plain");
17+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "cucumber_cpp/CucumberCpp.hpp"
2+
#include <gmock/gmock.h>
3+
4+
HOOK_BEFORE_ALL()
5+
{
6+
// no-op
7+
}
8+
9+
HOOK_BEFORE_ALL()
10+
{
11+
ASSERT_THAT(true, false);
12+
}
13+
14+
HOOK_BEFORE_ALL()
15+
{
16+
// no-op
17+
}
18+
19+
WHEN(R"(a step passes)")
20+
{
21+
// no-op
22+
}
23+
24+
HOOK_AFTER_ALL()
25+
{
26+
// no-op
27+
}
28+
29+
HOOK_AFTER_ALL()
30+
{
31+
// no-op
32+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "cucumber_cpp/CucumberCpp.hpp"
2+
#include <filesystem>
3+
#include <fstream>
4+
#include <gmock/gmock.h>
5+
#include <ios>
6+
#include <source_location>
7+
8+
namespace
9+
{
10+
const std::filesystem::path currentCompileDir = std::filesystem::path{ std::source_location::current().file_name() }.parent_path();
11+
}
12+
13+
HOOK_BEFORE_SCENARIO()
14+
{
15+
std::ifstream svgFile{ currentCompileDir / "cucumber.svg", std::ios::binary };
16+
Attach(svgFile, "image/svg+xml");
17+
}
18+
19+
WHEN(R"(a step passes)")
20+
{
21+
// no-op
22+
}
23+
24+
HOOK_AFTER_SCENARIO()
25+
{
26+
std::ifstream svgFile{ currentCompileDir / "cucumber.svg", std::ios::binary };
27+
Attach(svgFile, "image/svg+xml");
28+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "cucumber_cpp/CucumberCpp.hpp"
2+
#include <gmock/gmock.h>
3+
4+
HOOK_BEFORE_SCENARIO("@passing-hook")
5+
{
6+
// no-op
7+
}
8+
9+
HOOK_BEFORE_SCENARIO("@fail-before")
10+
{
11+
ASSERT_THAT(true, false);
12+
}
13+
14+
WHEN(R"(a step passes)")
15+
{
16+
// no-op
17+
}
18+
19+
HOOK_AFTER_SCENARIO("@fail-after")
20+
{
21+
ASSERT_THAT(true, false);
22+
}
23+
24+
HOOK_AFTER_SCENARIO("@passing-hook")
25+
{
26+
// no-op
27+
}

compatibility/hooks/hooks.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "cucumber_cpp/CucumberCpp.hpp"
2+
#include <gmock/gmock.h>
3+
4+
HOOK_BEFORE_SCENARIO()
5+
{
6+
// no-op
7+
}
8+
9+
WHEN(R"(a step passes)")
10+
{
11+
// no-op
12+
}
13+
14+
WHEN(R"(a step fails)")
15+
{
16+
ASSERT_THAT(true, false);
17+
}
18+
19+
HOOK_AFTER_SCENARIO()
20+
{
21+
// no-op
22+
}

cucumber_cpp/library/HookRegistry.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
#include "cucumber/messages/source_reference.hpp"
88
#include "cucumber/messages/tag.hpp"
99
#include "cucumber_cpp/library/Context.hpp"
10+
#include "cucumber_cpp/library/engine/ExecutionContext.hpp"
1011
#include "cucumber_cpp/library/support/SupportCodeLibrary.hpp"
1112
#include "cucumber_cpp/library/tag_expression/Parser.hpp"
13+
#include "cucumber_cpp/library/util/Broadcaster.hpp"
1214
#include <algorithm>
1315
#include <cstddef>
1416
#include <map>
@@ -61,8 +63,8 @@ namespace cucumber_cpp::library
6163
};
6264
}
6365

64-
HookBase::HookBase(Context& context)
65-
: context{ context }
66+
HookBase::HookBase(util::Broadcaster& broadCaster, Context& context, engine::StepOrHookStarted stepOrHookStarted)
67+
: engine::ExecutionContext{ broadCaster, context, stepOrHookStarted }
6668
{}
6769

6870
HookRegistry::Definition::Definition(std::string id, HookType type, std::string_view expression, HookFactory factory, std::source_location sourceLocation)
@@ -89,9 +91,7 @@ namespace cucumber_cpp::library
8991

9092
void HookRegistry::LoadHooks()
9193
{
92-
9394
for (const auto& matcher : support::DefinitionRegistration::Instance().GetHooks())
94-
// for (const auto& matcher : HookRegistration::Instance().GetEntries())
9595
Register(matcher.id, matcher.type, matcher.expression, matcher.factory, matcher.sourceLocation);
9696
}
9797

cucumber_cpp/library/HookRegistry.hpp

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,17 @@
33

44
#include "cucumber/gherkin/id_generator.hpp"
55
#include "cucumber/messages/hook.hpp"
6-
#include "cucumber/messages/hook_type.hpp"
7-
#include "cucumber/messages/location.hpp"
86
#include "cucumber/messages/pickle_tag.hpp"
9-
#include "cucumber/messages/source_reference.hpp"
107
#include "cucumber/messages/tag.hpp"
118
#include "cucumber_cpp/library/Body.hpp"
129
#include "cucumber_cpp/library/Context.hpp"
10+
#include "cucumber_cpp/library/engine/ExecutionContext.hpp"
1311
#include "cucumber_cpp/library/tag_expression/Model.hpp"
14-
#include "cucumber_cpp/library/tag_expression/Parser.hpp"
12+
#include "cucumber_cpp/library/util/Broadcaster.hpp"
1513
#include <cstddef>
1614
#include <cstdint>
1715
#include <map>
1816
#include <memory>
19-
#include <optional>
2017
#include <ranges>
2118
#include <source_location>
2219
#include <span>
@@ -27,12 +24,6 @@
2724
namespace cucumber_cpp::library
2825
{
2926

30-
template<class T>
31-
std::unique_ptr<Body> HookBodyFactory(Context& context)
32-
{
33-
return std::make_unique<T>(context);
34-
}
35-
3627
enum struct HookType
3728
{
3829
beforeAll,
@@ -45,9 +36,9 @@ namespace cucumber_cpp::library
4536
afterStep,
4637
};
4738

48-
struct HookBase
39+
struct HookBase : engine::ExecutionContext
4940
{
50-
explicit HookBase(Context& context);
41+
HookBase(util::Broadcaster& broadCaster, Context& context, engine::StepOrHookStarted stepOrHookStarted);
5142

5243
virtual ~HookBase() = default;
5344

@@ -60,12 +51,15 @@ namespace cucumber_cpp::library
6051
{
6152
/* nothing to do */
6253
}
63-
64-
protected:
65-
Context& context;
6654
};
6755

68-
using HookFactory = std::unique_ptr<Body> (&)(Context& context);
56+
using HookFactory = std::unique_ptr<Body> (&)(util::Broadcaster& broadCaster, Context& context, engine::StepOrHookStarted stepOrHookStarted);
57+
58+
template<class T>
59+
std::unique_ptr<Body> HookBodyFactory(util::Broadcaster& broadCaster, Context& context, engine::StepOrHookStarted stepOrHookStarted)
60+
{
61+
return std::make_unique<T>(broadCaster, context, stepOrHookStarted);
62+
}
6963

7064
struct HookMatch
7165
{

0 commit comments

Comments
 (0)