Skip to content

Commit 6f1f4c8

Browse files
committed
Enable the following compatibility tests: ambiguous, backgrounds, cdata, doc-strings, empty, examples-tables and minimal
1 parent ece0b62 commit 6f1f4c8

File tree

8 files changed

+157
-66
lines changed

8 files changed

+157
-66
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "cucumber_cpp/CucumberCpp.hpp"
2+
#include <string>
3+
4+
STEP(R"(^a (.*?) with (.*?)$)", (const std::string& arg1, const std::string& arg2))
5+
{
6+
// no-op
7+
}
8+
9+
STEP(R"(^a step with (.*)$)", (const std::string& arg1))
10+
{
11+
// no-op
12+
}
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 <string>
3+
4+
STEP(R"(an order for {string})", ([[maybe_unused]] const std::string& item))
5+
{
6+
// no-op
7+
}
8+
9+
STEP(R"(an action)")
10+
{
11+
// no-op
12+
}
13+
14+
STEP(R"(an outcome)")
15+
{
16+
// no-op
17+
}

compatibility/cdata/cdata.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "cucumber_cpp/CucumberCpp.hpp"
2+
#include <cstdint>
3+
4+
GIVEN(R"(I have {int} <![CDATA[cukes]]> in my belly)", ([[maybe_unused]] std::int32_t number))
5+
{}

compatibility/compatibility.cpp

Lines changed: 82 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <filesystem>
1717
#include <format>
1818
#include <fstream>
19+
#include <functional>
1920
#include <gtest/gtest-spi.h>
2021
#include <gtest/gtest.h>
2122
#include <iostream>
@@ -62,72 +63,107 @@ namespace compatibility
6263

6364
void SanitizeExpectedJson(nlohmann::json& json)
6465
{
65-
for (auto& [key, value] : json.items())
66+
for (auto jsonIter = json.begin(); jsonIter != json.end();)
6667
{
67-
if (value.is_object())
68+
auto& key = jsonIter.key();
69+
auto& value = jsonIter.value();
70+
71+
if (key == "parameterTypeName" && value.get<std::string>().empty())
72+
jsonIter = json.erase(jsonIter);
73+
else if (key == "exception")
74+
jsonIter = json.erase(jsonIter);
75+
else if (key == "message")
76+
jsonIter = json.erase(jsonIter);
77+
else if (key == "line")
78+
jsonIter = json.erase(jsonIter);
79+
else if (key == "start")
80+
jsonIter = json.erase(jsonIter);
81+
else if (value.is_object())
6882
{
6983
SanitizeExpectedJson(value);
7084
if (value.size() == 0)
71-
json.erase(key);
85+
jsonIter = json.erase(jsonIter);
86+
else
87+
++jsonIter;
7288
}
7389
else if (value.is_array())
7490
{
7591
auto idx = 0;
76-
for (auto& item : value)
92+
for (auto valueIter = value.begin(); valueIter != value.end();)
7793
{
94+
auto& item = *valueIter;
95+
7896
if (item.is_object())
7997
SanitizeExpectedJson(item);
8098

8199
if (item.size() == 0)
82-
value.erase(idx);
83-
84-
++idx;
100+
valueIter = value.erase(valueIter);
101+
else
102+
++valueIter;
85103
}
86104

87105
if (value.size() == 0)
88-
json.erase(key);
106+
jsonIter = json.erase(jsonIter);
107+
else
108+
++jsonIter;
89109
}
90110
else if (key == "uri")
91111
{
92112
json[key] = std::regex_replace(value.get<std::string>(), std::regex(R"(samples\/[^\/]+)"), KIT_FOLDER);
93113
json[key] = std::regex_replace(value.get<std::string>(), std::regex(R"(\.ts$)"), ".cpp");
114+
++jsonIter;
94115
}
95-
else if (key == "line")
96-
json.erase(key);
97-
else if (key == "start")
98-
json.erase(key);
116+
else
117+
++jsonIter;
99118
}
100119
}
101120

102121
void SanitizeActualJson(nlohmann::json& json)
103122
{
104-
for (auto& [key, value] : json.items())
123+
for (auto jsonIter = json.begin(); jsonIter != json.end();)
105124
{
106-
if (value.is_object())
125+
auto& key = jsonIter.key();
126+
auto& value = jsonIter.value();
127+
128+
if (key == "parameterTypeName" && value.get<std::string>().empty())
129+
jsonIter = json.erase(jsonIter);
130+
else if (key == "exception")
131+
jsonIter = json.erase(jsonIter);
132+
else if (key == "message")
133+
jsonIter = json.erase(jsonIter);
134+
else if (key == "line")
135+
jsonIter = json.erase(jsonIter);
136+
else if (value.is_object())
107137
{
108138
SanitizeActualJson(value);
109139
if (value.size() == 0)
110-
json.erase(key);
140+
jsonIter = json.erase(jsonIter);
141+
else
142+
++jsonIter;
111143
}
112144
else if (value.is_array())
113145
{
114146
auto idx = 0;
115-
for (auto& item : value)
147+
for (auto valueIter = value.begin(); valueIter != value.end();)
116148
{
149+
auto& item = *valueIter;
150+
117151
if (item.is_object())
118152
SanitizeActualJson(item);
119153

120154
if (item.size() == 0)
121-
value.erase(idx);
122-
123-
++idx;
155+
valueIter = value.erase(valueIter);
156+
else
157+
++valueIter;
124158
}
125159

126160
if (value.size() == 0)
127-
json.erase(key);
161+
jsonIter = json.erase(jsonIter);
162+
else
163+
++jsonIter;
128164
}
129-
else if (key == "line")
130-
json.erase(key);
165+
else
166+
++jsonIter;
131167
}
132168
}
133169

@@ -155,8 +191,6 @@ namespace compatibility
155191
if (json.contains("meta"))
156192
continue;
157193

158-
SanitizeExpectedJson(json);
159-
160194
expectedEnvelopes.emplace_back(std::move(json));
161195
}
162196
}
@@ -166,42 +200,29 @@ namespace compatibility
166200
nlohmann::json actualJson{};
167201
to_json(actualJson, envelope);
168202

169-
if (expectedEnvelopes.empty())
203+
actualEnvelopes.emplace_back(std::move(actualJson));
204+
}
205+
206+
void CompareEnvelopes()
207+
{
208+
ASSERT_THAT(actualEnvelopes.size(), testing::Eq(expectedEnvelopes.size()));
209+
210+
while (!actualEnvelopes.empty() && !expectedEnvelopes.empty())
170211
{
171-
std::cerr << "Unexpected envelope: " << actualJson.dump() << "\n\n";
172-
return;
173-
}
212+
auto actualJson = actualEnvelopes.front();
213+
actualEnvelopes.pop_front();
174214

175-
SanitizeActualJson(actualJson);
176-
177-
const auto expectedJson = expectedEnvelopes.front();
178-
expectedEnvelopes.pop_front();
179-
180-
expectedOfs << expectedJson.dump() << "\n";
181-
actualOfs << actualJson.dump() << "\n";
182-
183-
const auto expectedMessage = expectedJson.items().begin().key();
184-
185-
const auto diff = nlohmann::json::diff(expectedJson, actualJson);
186-
187-
EXPECT_THAT(actualJson, testing::Eq(expectedJson));
188-
// if (actualJson.contains(expectedMessage))
189-
// {
190-
// if (actualJson[expectedMessage] == expectedJson[expectedMessage])
191-
// {
192-
// std::cout << "matching!!!! " << expectedMessage << "\n\n";
193-
// }
194-
// else
195-
// {
196-
// std::cerr << std::format("Mismatch {}: {}\n", expectedMessage, diff.dump());
197-
// std::cerr << "expected: " << expectedJson[expectedMessage] << "\n";
198-
// std::cerr << "actual : " << actualJson[expectedMessage] << "\n\n";
199-
// }
200-
// }
201-
// else
202-
// {
203-
// std::cerr << std::format("Missing {}: {}\n", expectedMessage, diff.dump());
204-
// }
215+
auto expectedJson = expectedEnvelopes.front();
216+
expectedEnvelopes.pop_front();
217+
218+
SanitizeActualJson(actualJson);
219+
SanitizeExpectedJson(expectedJson);
220+
221+
expectedOfs << expectedJson.dump() << "\n";
222+
actualOfs << actualJson.dump() << "\n";
223+
224+
EXPECT_THAT(actualJson, testing::Eq(expectedJson));
225+
}
205226
}
206227

207228
private:
@@ -216,6 +237,7 @@ namespace compatibility
216237
std::ofstream actualOfs{ actualndjson };
217238

218239
std::list<nlohmann::json> expectedEnvelopes;
240+
std::list<nlohmann::json> actualEnvelopes;
219241
};
220242

221243
bool IsFeatureFile(const std::filesystem::directory_entry& entry)
@@ -289,8 +311,8 @@ namespace compatibility
289311
BroadcastListener broadcastListener{ devkit.ndjsonFile, devkit.ndjsonFile.parent_path() / "expected.ndjson", devkit.ndjsonFile.parent_path() / "actual.ndjson", broadcaster };
290312

291313
cucumber_cpp::library::api::RunCucumber(runOptions, parameterRegistry, *programContext, broadcaster);
292-
// EXPECT_NONFATAL_FAILURE(cucumber_cpp::library::api::RunCucumber(runOptions, parameterRegistry, *programContext, broadcaster), "");
293-
// EXPECT_FATAL_FAILURE(cucumber_cpp::library::api::RunCucumber(runOptions, parameterRegistry, *programContext, broadcaster), "");
314+
315+
broadcastListener.CompareEnvelopes();
294316
}
295317
}
296318
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include "cucumber_cpp/CucumberCpp.hpp"
2+
#include "gmock/gmock.h"
3+
#include <string>
4+
5+
STEP(R"(a doc string:)")
6+
{
7+
ASSERT_THAT(docString, testing::IsTrue());
8+
ASSERT_THAT(docString->content, testing::Not(testing::IsEmpty()));
9+
}

compatibility/empty/empty.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +0,0 @@
1-
#include <filesystem>
2-
#include <source_location>
3-
4-
namespace cucumber_cpp::devkit
5-
{
6-
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "cucumber_cpp/CucumberCpp.hpp"
2+
#include "gmock/gmock.h"
3+
#include "gtest/gtest.h"
4+
#include <cstdint>
5+
#include <string>
6+
7+
GIVEN(R"(there are {int} cucumbers)", (std::int32_t cucumbers))
8+
{
9+
context.InsertAt("cucumbers", cucumbers);
10+
}
11+
12+
GIVEN(R"(there are {int} friends)", (std::int32_t friends))
13+
{
14+
context.InsertAt("friends", friends);
15+
}
16+
17+
WHEN(R"(I eat {int} cucumbers)", (std::int32_t eatCount))
18+
{
19+
context.Get<std::int32_t>("cucumbers") -= eatCount;
20+
}
21+
22+
THEN(R"(I should have {int} cucumbers)", (std::int32_t expectedCount))
23+
{
24+
ASSERT_THAT(context.Get<std::int32_t>("cucumbers"), testing::Eq(expectedCount));
25+
}
26+
27+
THEN(R"(each person can eat {int} cucumbers)", (std::int32_t expectedShare))
28+
{
29+
const auto share = context.Get<std::int32_t>("cucumbers") / (1 + context.Get<std::int32_t>("friends"));
30+
ASSERT_THAT(share, testing::Eq(expectedShare));
31+
}

compatibility/minimal/minimal.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "cucumber_cpp/CucumberCpp.hpp"
22
#include <cstdint>
3+
34
STEP(R"(I have {int} cukes in my belly)", (std::int32_t number))
45
{
56
// no-op

0 commit comments

Comments
 (0)