Skip to content

Commit 45a4380

Browse files
committed
sanitize and dump actual and expected json
1 parent 4054607 commit 45a4380

File tree

2 files changed

+66
-24
lines changed

2 files changed

+66
-24
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@
33
.xwin-cache
44
.vs/
55
megalinter-reports/
6+
7+
# compatibility generated files
8+
actual.ndjson
9+
expected.ndjson

compatibility/compatibility.cpp

Lines changed: 62 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include "library/support/Duration.hpp"
99
#include "nlohmann/json.hpp"
1010
#include "nlohmann/json_fwd.hpp"
11+
#include "gmock/gmock.h"
12+
#include "gtest/gtest.h"
1113
#include <algorithm>
1214
#include <chrono>
1315
#include <cstddef>
@@ -63,12 +65,27 @@ namespace compatibility
6365
for (auto& [key, value] : json.items())
6466
{
6567
if (value.is_object())
68+
{
6669
SanitizeExpectedJson(value);
70+
if (value.size() == 0)
71+
json.erase(key);
72+
}
6773
else if (value.is_array())
6874
{
75+
auto idx = 0;
6976
for (auto& item : value)
77+
{
7078
if (item.is_object())
7179
SanitizeExpectedJson(item);
80+
81+
if (item.size() == 0)
82+
value.erase(idx);
83+
84+
++idx;
85+
}
86+
87+
if (value.size() == 0)
88+
json.erase(key);
7289
}
7390
else if (key == "uri")
7491
{
@@ -87,12 +104,27 @@ namespace compatibility
87104
for (auto& [key, value] : json.items())
88105
{
89106
if (value.is_object())
107+
{
90108
SanitizeActualJson(value);
109+
if (value.size() == 0)
110+
json.erase(key);
111+
}
91112
else if (value.is_array())
92113
{
114+
auto idx = 0;
93115
for (auto& item : value)
116+
{
94117
if (item.is_object())
95118
SanitizeActualJson(item);
119+
120+
if (item.size() == 0)
121+
value.erase(idx);
122+
123+
++idx;
124+
}
125+
126+
if (value.size() == 0)
127+
json.erase(key);
96128
}
97129
else if (key == "line")
98130
json.erase(key);
@@ -101,13 +133,14 @@ namespace compatibility
101133

102134
struct BroadcastListener
103135
{
104-
BroadcastListener(std::filesystem::path ndjsonin, std::filesystem::path ndout, cucumber_cpp::library::util::Broadcaster& broadcaster)
136+
BroadcastListener(std::filesystem::path ndjsonin, std::filesystem::path expectedndjson, std::filesystem::path ndout, cucumber_cpp::library::util::Broadcaster& broadcaster)
105137
: listener(broadcaster, [this](const cucumber::messages::envelope& envelope)
106138
{
107139
OnEvent(envelope);
108140
})
109141
, ndjsonin{ std::move(ndjsonin) }
110-
, ndout(std::move(ndout))
142+
, expectedndjson{ std::move(expectedndjson) }
143+
, actualndjson(std::move(ndout))
111144
{
112145
while (!ifs.eof())
113146
{
@@ -133,8 +166,6 @@ namespace compatibility
133166
nlohmann::json actualJson{};
134167
to_json(actualJson, envelope);
135168

136-
ofs << envelope.to_json() << "\n";
137-
138169
if (expectedEnvelopes.empty())
139170
{
140171
std::cerr << "Unexpected envelope: " << actualJson.dump() << "\n\n";
@@ -146,36 +177,43 @@ namespace compatibility
146177
const auto expectedJson = expectedEnvelopes.front();
147178
expectedEnvelopes.pop_front();
148179

180+
expectedOfs << expectedJson.dump() << "\n";
181+
actualOfs << actualJson.dump() << "\n";
182+
149183
const auto expectedMessage = expectedJson.items().begin().key();
150184

151185
const auto diff = nlohmann::json::diff(expectedJson, actualJson);
152186

153-
if (actualJson.contains(expectedMessage))
154-
{
155-
if (actualJson[expectedMessage] == expectedJson[expectedMessage])
156-
{
157-
std::cout << "matching!!!! " << expectedMessage << "\n\n";
158-
}
159-
else
160-
{
161-
std::cerr << std::format("Mismatch {}: {}\n", expectedMessage, diff.dump());
162-
std::cerr << "expected: " << expectedJson[expectedMessage] << "\n";
163-
std::cerr << "actual : " << actualJson[expectedMessage] << "\n\n";
164-
}
165-
}
166-
else
167-
{
168-
std::cerr << std::format("Missing {}: {}\n", expectedMessage, diff.dump());
169-
}
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+
// }
170205
}
171206

172207
private:
173208
cucumber_cpp::library::util::Listener listener;
174209
std::filesystem::path ndjsonin;
175210
std::ifstream ifs{ ndjsonin };
176211

177-
std::filesystem::path ndout;
178-
std::ofstream ofs{ ndout };
212+
std::filesystem::path expectedndjson;
213+
std::ofstream expectedOfs{ expectedndjson };
214+
215+
std::filesystem::path actualndjson;
216+
std::ofstream actualOfs{ actualndjson };
179217

180218
std::list<nlohmann::json> expectedEnvelopes;
181219
};
@@ -248,7 +286,7 @@ namespace compatibility
248286

249287
cucumber_cpp::library::util::Broadcaster broadcaster;
250288

251-
BroadcastListener broadcastListener{ devkit.ndjsonFile, devkit.ndjsonFile.parent_path() / "out.ndjson", broadcaster };
289+
BroadcastListener broadcastListener{ devkit.ndjsonFile, devkit.ndjsonFile.parent_path() / "expected.ndjson", devkit.ndjsonFile.parent_path() / "actual.ndjson", broadcaster };
252290

253291
cucumber_cpp::library::api::RunCucumber(runOptions, parameterRegistry, *programContext, broadcaster);
254292
// EXPECT_NONFATAL_FAILURE(cucumber_cpp::library::api::RunCucumber(runOptions, parameterRegistry, *programContext, broadcaster), "");

0 commit comments

Comments
 (0)