jks
2020-03-09 21:20
Hello, I?ve mostly implemented the ignore-order matcher discussed f(or JsonBodyMatcher) and started writing some tests for the common case and edge cases. One case that I?m having a little trouble with is when _expected_ is smaller than _actual_:
```def '''matching json bodies - return no mismatches - with each like matcher on unequal lists
and ignore-order matching enabled'''() {
given:
matchers.addCategory('body')
.addRule('$.array1', new MinTypeMatcher(1))
.addRule('$.array1', IgnoreOrderMatcher.INSTANCE)
System.setProperty(Matchers.PACT_MATCHING_IGNORE_ORDER, 'true')
expect:
matcher.matchBody(expectedBody, actualBody, true, matchers).empty
where:
actualBody = OptionalBody.body('{"array1": [100, 200, 300, 400]}'.bytes)
expectedBody = OptionalBody.body('{"array1": [200]}'.bytes)
}```
This case is failing with BodyMismatch(expected=[200, 200, 200, 200], actual=[100, 200, 300, 400], mismatch=Expected ignore-order match of [200, 200, 200, 200] and [100, 200, 300, 400], path=$.array1,
I tracked it down to the JsonBodyMatcher.compareListContentUnordered(?) method that I?ve created, which is based on JsonBodyMatcher.compareListContent(?), and is called from JsonBodyMatcher.compareLists:
```...
if (expectedList.isNotEmpty()) {
if (Matchers.ignoreOrderMatcherDefined(path, "body", matchers)) {
result.addAll(compareListContentUnordered(expectedList.padTo(actualValues.size(), expectedValues.first()),
actualList, path, allowUnexpectedKeys, matchers))
} else {
result.addAll(compareListContent(expectedList.padTo(actualValues.size(), expectedValues.first()),
actualList, path, allowUnexpectedKeys, matchers))
}
}
...```
Why are we padding the expectedList before calling compareListContent when expected is less than actual. In the ignore-order case, I suspect that I shouldn?t pad and just compare the values I have in expected to the entire actual list. However, I want to make sure I?m not overlooking something obvious.