forked from substrait-io/substrait-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParseResultMatchers.cpp
More file actions
440 lines (377 loc) · 13 KB
/
ParseResultMatchers.cpp
File metadata and controls
440 lines (377 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/* SPDX-License-Identifier: Apache-2.0 */
#include "substrait/textplan/tests/ParseResultMatchers.h"
#include <gmock/gmock-matchers.h>
#include <gtest/gtest.h>
#include <memory>
#include <set>
#include <string>
#include <utility>
#include "substrait/proto/plan.pb.h"
#include "substrait/textplan/ParseResult.h"
#include "substrait/textplan/SymbolTable.h"
#include "substrait/textplan/SymbolTablePrinter.h"
// NOLINTBEGIN(readability-identifier-naming)
namespace io::substrait::textplan {
namespace {
std::vector<std::string> symbolNames(
const std::vector<std::shared_ptr<SymbolInfo>>& symbols) {
std::vector<std::string> names;
for (const auto& symbol : symbols) {
names.push_back(symbol->name);
}
return names;
}
bool stringEqSquashingWhitespace(
const std::string& have,
const std::string& expected,
std::ostream* listener) {
auto atHave = have.begin();
auto atExpected = expected.begin();
while (atHave != have.end() && atExpected != expected.end()) {
if (isspace(*atExpected)) {
if (!isspace(*atHave)) {
return false;
}
// Have a match, consume all remaining space.
do {
atExpected++;
} while (atExpected != expected.end() && isspace(*atExpected));
do {
atHave++;
} while (atHave != have.end() && isspace(*atHave));
continue;
}
if (*atHave != *atExpected) {
if (listener) {
*listener << "first mismatch at character #"
<< std::to_string(atHave - have.begin()) << " |"
<< have.substr(atHave - have.begin(), 20) << "|"
<< " should be |"
<< expected.substr(atExpected - expected.begin(), 20) << "|"
<< std::endl;
}
return false;
}
atHave++;
atExpected++;
}
// For convenience consume any trailing whitespace on both sides.
if (atExpected != expected.end()) {
do {
atExpected++;
} while (atExpected != expected.end() && isspace(*atExpected));
}
if (atHave != have.end()) {
do {
atHave++;
} while (atHave != have.end() && isspace(*atHave));
}
return atHave == have.end() && atExpected == expected.end();
}
std::vector<std::string> symbolNamesWithTypes(
const std::vector<std::shared_ptr<SymbolInfo>>& symbols,
const std::set<SymbolType>& types) {
std::vector<std::string> names;
for (const auto& symbol : symbols) {
if (types.find(symbol->type) != types.end()) {
names.push_back(symbol->name);
}
}
return names;
}
std::vector<std::string> orderedSetDifference(
const std::vector<std::string>& haystack,
const std::vector<std::string>& source) {
std::vector<std::string> found(haystack.size());
std::vector<std::string> sortedHaystack = haystack;
std::vector<std::string> sortedSource = source;
std::sort(sortedHaystack.begin(), sortedHaystack.end());
std::sort(sortedSource.begin(), sortedSource.end());
auto end = std::set_difference(
sortedHaystack.begin(),
sortedHaystack.end(),
sortedSource.begin(),
sortedSource.end(),
found.begin());
found.resize(end - found.begin());
std::set<std::string> items;
items.insert(found.begin(), found.end());
std::vector<std::string> result;
for (const auto& item : haystack) {
if (items.find(item) != items.end()) {
result.push_back(item);
}
}
return result;
}
} // namespace
class ParsesOkMatcher {
public:
using is_gtest_matcher = void;
static bool MatchAndExplain(
const ParseResult& result,
std::ostream* /* listener */) {
return result.successful();
}
static void DescribeTo(std::ostream* os) {
*os << "parses successfully";
}
static void DescribeNegationTo(std::ostream* os) {
*os << "does not parse successfully";
}
};
[[maybe_unused]] ::testing::Matcher<const ParseResult&> ParsesOk() { // NOLINT
return ParsesOkMatcher();
}
class HasSymbolsMatcher {
public:
using is_gtest_matcher = void;
explicit HasSymbolsMatcher(const std::vector<std::string>& expectedSymbols)
: expectedSymbols_(std::move(expectedSymbols)) {}
bool MatchAndExplain(const ParseResult& result, std::ostream* listener)
const {
// Note: Need set or sorted vector for set_difference.
auto actualSymbolsSorted =
symbolNames(result.getSymbolTable().getSymbols());
std::sort(actualSymbolsSorted.begin(), actualSymbolsSorted.end());
std::vector<std::string> extraSymbols;
auto expectedSymbolsSorted = expectedSymbols_;
std::sort(expectedSymbolsSorted.begin(), expectedSymbolsSorted.end());
if (listener != nullptr) {
auto end = std::set_difference(
actualSymbolsSorted.begin(),
actualSymbolsSorted.end(),
expectedSymbolsSorted.begin(),
expectedSymbolsSorted.end(),
std::back_inserter(extraSymbols));
if (!extraSymbols.empty()) {
*listener << std::endl << " with extra symbols: ";
for (const auto& symbol : extraSymbols) {
*listener << " \"" << symbol << "\"";
}
}
std::vector<std::string> missingSymbols;
end = std::set_difference(
expectedSymbolsSorted.begin(),
expectedSymbolsSorted.end(),
actualSymbolsSorted.begin(),
actualSymbolsSorted.end(),
std::back_inserter(missingSymbols));
if (!missingSymbols.empty()) {
if (!extraSymbols.empty()) {
*listener << ", and missing symbols: ";
} else {
*listener << " with missing symbols: ";
}
for (const auto& symbol : missingSymbols) {
*listener << " \"" << symbol << "\"";
}
}
}
return actualSymbolsSorted == expectedSymbolsSorted;
}
void DescribeTo(std::ostream* os) const {
*os << "has exactly these symbols: "
<< ::testing::PrintToString(expectedSymbols_);
}
void DescribeNegationTo(std::ostream* os) const {
*os << "does not have exactly these symbols: "
<< ::testing::PrintToString(expectedSymbols_);
}
private:
const std::vector<std::string> expectedSymbols_;
};
::testing::Matcher<const ParseResult&> HasSymbols(
const std::vector<std::string>& expectedSymbols) {
return HasSymbolsMatcher(std::move(expectedSymbols));
}
class WhenSerializedMatcher {
public:
using is_gtest_matcher = void;
explicit WhenSerializedMatcher(
::testing::Matcher<const std::string&> stringMatcher)
: stringMatcher_(std::move(stringMatcher)) {}
bool MatchAndExplain(
const ParseResult& result,
::testing::MatchResultListener* listener) const {
SubstraitErrorListener errorListener;
std::string outputText = SymbolTablePrinter::outputToText(
result.getSymbolTable(), &errorListener);
// Ignores errors as other matchers check for that.
return MatchPrintAndExplain(outputText, stringMatcher_, listener);
}
void DescribeTo(::std::ostream* os) const {
*os << "matches after serializing ";
stringMatcher_.DescribeTo(os);
}
void DescribeNegationTo(::std::ostream* os) const {
*os << "does not match after serializing ";
stringMatcher_.DescribeTo(os);
}
private:
::testing::Matcher<const std::string&> stringMatcher_;
};
::testing::Matcher<const ParseResult&> WhenSerialized(
::testing::Matcher<const std::string&> stringMatcher) {
return WhenSerializedMatcher(std::move(stringMatcher));
}
class AsBinaryPlanMatcher {
public:
using is_gtest_matcher = void;
explicit AsBinaryPlanMatcher(
::testing::Matcher<const ::substrait::proto::Plan&> proto_matcher)
: proto_matcher_(std::move(proto_matcher)) {}
bool MatchAndExplain(
const ParseResult& result,
::testing::MatchResultListener* listener) const {
auto outputPlan =
SymbolTablePrinter::outputToBinaryPlan(result.getSymbolTable());
return MatchPrintAndExplain(outputPlan, proto_matcher_, listener);
}
void DescribeTo(::std::ostream* os) const {
*os << "matches after converting to a binary plan ";
proto_matcher_.DescribeTo(os);
}
void DescribeNegationTo(::std::ostream* os) const {
*os << "does not match after converting to a binary plan ";
proto_matcher_.DescribeTo(os);
}
private:
::testing::Matcher<const ::substrait::proto::Plan&> proto_matcher_;
};
::testing::Matcher<const ParseResult&> AsBinaryPlan(
::testing::Matcher<const ::substrait::proto::Plan&> proto_matcher) {
return AsBinaryPlanMatcher(std::move(proto_matcher));
}
class HasSymbolsWithTypesMatcher {
public:
using is_gtest_matcher = void;
HasSymbolsWithTypesMatcher(
const std::vector<std::string>& expected_symbols,
const std::vector<SymbolType>& interesting_types)
: expectedSymbols_(std::move(expected_symbols)) {
interestingTypes_.insert(
interesting_types.begin(), interesting_types.end());
}
void DescribeTypes(std::ostream* os) const {
*os << " of types ";
bool hasPreviousOutput = false;
for (const auto& type : interestingTypes_) {
if (hasPreviousOutput) {
*os << ", ";
}
*os << symbolTypeName(type);
hasPreviousOutput = true;
}
}
bool MatchAndExplain(const ParseResult& result, std::ostream* listener)
const {
auto actualSymbols = symbolNamesWithTypes(
result.getSymbolTable().getSymbols(), interestingTypes_);
if (listener != nullptr) {
std::vector<std::string> extraSymbols =
orderedSetDifference(actualSymbols, expectedSymbols_);
if (!extraSymbols.empty()) {
*listener << std::endl << " with extra symbols";
DescribeTypes(listener);
*listener << ": ";
for (const auto& symbol : extraSymbols) {
*listener << " \"" << symbol << "\"";
}
}
std::vector<std::string> missingSymbols =
orderedSetDifference(expectedSymbols_, actualSymbols);
if (!missingSymbols.empty()) {
if (!extraSymbols.empty()) {
*listener << ", and missing symbols";
DescribeTypes(listener);
*listener << ": ";
} else {
*listener << " with missing symbols";
DescribeTypes(listener);
*listener << ": ";
}
for (const auto& symbol : missingSymbols) {
*listener << " \"" << symbol << "\"";
}
}
}
return actualSymbols == expectedSymbols_;
}
void DescribeTo(std::ostream* os) const {
*os << "has exactly these symbols";
DescribeTypes(os);
*os << ": " << ::testing::PrintToString(expectedSymbols_);
}
void DescribeNegationTo(std::ostream* os) const {
*os << "does not have exactly these symbols";
DescribeTypes(os);
*os << ": " << ::testing::PrintToString(expectedSymbols_);
}
private:
const std::vector<std::string> expectedSymbols_;
std::set<SymbolType> interestingTypes_;
};
::testing::Matcher<const ParseResult&> HasSymbolsWithTypes(
const std::vector<std::string>& expected_symbols,
const std::vector<SymbolType>& interesting_types) {
return HasSymbolsWithTypesMatcher(
std::move(expected_symbols), std::move(interesting_types));
}
class HasErrorsMatcher {
public:
using is_gtest_matcher = void;
explicit HasErrorsMatcher(const std::vector<std::string>& expectedErrors)
: expectedErrors_(std::move(expectedErrors)) {}
bool MatchAndExplain(const ParseResult& result, std::ostream* listener)
const {
::testing::Matcher<std::vector<std::string>> matcher =
::testing::ContainerEq(expectedErrors_);
::testing::StringMatchResultListener strListener;
std::vector<std::string> errors = result.getAllErrors();
bool match = MatchPrintAndExplain(errors, matcher, &strListener);
if (listener) {
*listener << strListener.str();
}
return match;
}
void DescribeTo(std::ostream* os) const {
*os << "has exactly these errors: "
<< ::testing::PrintToString(expectedErrors_);
}
void DescribeNegationTo(std::ostream* os) const {
*os << "does not have exactly these errors: "
<< ::testing::PrintToString(expectedErrors_);
}
private:
const std::vector<std::string> expectedErrors_;
};
::testing::Matcher<const ParseResult&> HasErrors(
const std::vector<std::string>& expectedErrors) {
return HasErrorsMatcher(std::move(expectedErrors));
}
class EqSquashingWhitespaceMatcher {
public:
using is_gtest_matcher = void;
explicit EqSquashingWhitespaceMatcher(const std::string& expectedString)
: expectedString_(std::move(expectedString)) {}
bool MatchAndExplain(const std::string& str, std::ostream* listener) const {
return stringEqSquashingWhitespace(str, expectedString_, listener);
}
void DescribeTo(std::ostream* os) const {
*os << "equals squashing whitespace "
<< ::testing::PrintToString(expectedString_);
}
void DescribeNegationTo(std::ostream* os) const {
*os << "does not equal squashing whitespace "
<< ::testing::PrintToString(expectedString_);
}
private:
std::string expectedString_;
};
::testing::Matcher<const std::string&> EqSquashingWhitespace(
const std::string& expectedString) {
return EqSquashingWhitespaceMatcher(std::move(expectedString));
}
// NOLINTEND(readability-identifier-naming)
} // namespace io::substrait::textplan