Skip to content

Commit

Permalink
chore: Update error messages to match the compatibility-suite
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Jul 26, 2023
1 parent 4967b5f commit 6c1f850
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class Generators {
UUID.fromString(element.toString())
}
case 'boolean' -> {
assert element.type() ==~ /True|False/
assert element.type() == 'Boolean'
}
default -> throw new AssertionError("Invalid type: $type")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void provider2Fails() throws IOException {
mockTestProvider2.validateResultWith((result, t) -> {
assertThat(t, is(instanceOf(AssertionError.class)));
assertThat(t.getMessage(), is("The following mismatched requests occurred:\n" +
"body - $.name: Expected 'larry' (String) but received 'farry' (String)"));
"body - $.name: Expected 'farry' (String) to be equal to 'larry' (String)"));
assertThat(result, is(instanceOf(PactVerificationResult.Mismatches.class)));
PactVerificationResult.Mismatches error = (PactVerificationResult.Mismatches) result;
assertThat(error.getMismatches(), hasSize(1));
Expand Down Expand Up @@ -152,7 +152,7 @@ public void bothprovidersFail() throws IOException {
mockTestProvider2.validateResultWith((result, t) -> {
assertThat(t, is(instanceOf(AssertionError.class)));
assertThat(t.getMessage(), is("The following mismatched requests occurred:\n" +
"body - $.name: Expected 'larry' (String) but received 'farry' (String)"));
"body - $.name: Expected 'farry' (String) to be equal to 'larry' (String)"));
assertThat(result, is(instanceOf(PactVerificationResult.Mismatches.class)));
PactVerificationResult.Mismatches error = (PactVerificationResult.Mismatches) result;
assertThat(error.getMismatches(), hasSize(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ object HeaderMatcher : KLogging() {
val expectedParameters = parseParameters(expectedValues.drop(1))
val actualParameters = parseParameters(actualValues.drop(1))
val headerMismatch = HeaderMismatch(headerKey, expected, actual,
"Expected header $headerKey to have value '$expected' but was '$actual'")
"Expected header '$headerKey' to have value '$expected' but was '$actual'")

return if (expectedValue.equals(actualValue, ignoreCase = true)) {
expectedParameters.map { entry ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ object JsonContentMatcher : ContentMatcher, KLogging() {
expected is JsonValue.Object && actual !is JsonValue.Object ||
expected is JsonValue.Array && actual !is JsonValue.Array ->
listOf(BodyItemMatchResult(constructPath(path),
listOf(BodyMismatch(expected, actual, "Type mismatch: Expected ${typeOf(expected)} " +
"${valueOf(expected)} but received ${typeOf(actual)} ${valueOf(actual)}", constructPath(path),
listOf(BodyMismatch(expected, actual, "Type mismatch: Expected ${typeOf(actual)} " +
"${valueOf(actual)} to be equal to ${typeOf(expected)} ${valueOf(expected)}", constructPath(path),
generateJsonDiff(expected, actual)))))
else -> compareValues(path, expected, actual, context)
}
Expand Down Expand Up @@ -178,8 +178,8 @@ object JsonContentMatcher : ContentMatcher, KLogging() {
listOf(BodyItemMatchResult(constructPath(path), emptyList()))
} else {
listOf(BodyItemMatchResult(constructPath(path),
listOf(BodyMismatch(expected, actual, "Expected ${valueOf(expected)} (${typeOf(expected)}) " +
"but received ${valueOf(actual)} (${typeOf(actual)})", constructPath(path)))))
listOf(BodyMismatch(expected, actual, "Expected ${valueOf(actual)} (${typeOf(actual)}) " +
"to be equal to ${valueOf(expected)} (${typeOf(expected)})", constructPath(path)))))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ fun <M : Mismatch> matchEquality(
emptyList()
} else {
listOf(mismatchFactory.create(expected, actual,
"Expected ${valueOf(actual)} (${typeOf(actual)}) to equal ${valueOf(expected)} (${typeOf(expected)})", path))
"Expected ${valueOf(actual)} (${typeOf(actual)}) to be equal to " +
"${valueOf(expected)} (${typeOf(expected)})", path))
}
}

Expand Down Expand Up @@ -226,7 +227,8 @@ fun <M : Mismatch> matchType(
allowEmpty: Boolean
): List<M> {
logger.debug {
"comparing type of [$actual] (${actual?.javaClass?.simpleName}) to [$expected] (${expected?.javaClass?.simpleName}) at $path"
"comparing type of [$actual] (${actual?.javaClass?.simpleName}) to " +
"[$expected] (${expected?.javaClass?.simpleName}) at $path"
}
return if (expected is Number && actual is Number ||
expected is Boolean && actual is Boolean ||
Expand All @@ -251,7 +253,8 @@ fun <M : Mismatch> matchType(
else -> false
}
if (empty) {
listOf(mismatchFactory.create(expected, actual, "Expected ${valueOf(actual)} to not be empty", path))
listOf(mismatchFactory.create(expected, actual,
"Expected ${valueOf(actual)} (${typeOf(actual)}) to not be empty", path))
} else {
emptyList()
}
Expand All @@ -265,7 +268,8 @@ fun <M : Mismatch> matchType(
if (actual == null || actual is JsonValue.Null) {
emptyList()
} else {
listOf(mismatchFactory.create(expected, actual, "Expected ${valueOf(actual)} to be null", path))
listOf(mismatchFactory.create(expected, actual,
"Expected ${valueOf(actual)} (${typeOf(actual)}) to be a null value", path))
}
} else {
listOf(mismatchFactory.create(expected, actual,
Expand All @@ -283,7 +287,8 @@ fun <M : Mismatch> matchNumber(
mismatchFactory: MismatchFactory<M>
): List<M> {
if (expected == null && actual != null) {
return listOf(mismatchFactory.create(expected, actual, "Expected ${valueOf(actual)} to be null", path))
return listOf(mismatchFactory.create(expected, actual,
"Expected ${valueOf(actual)} (${typeOf(actual)}) to be a null value", path))
}
when (numberType) {
NumberTypeMatcher.NumberType.NUMBER -> {
Expand Down Expand Up @@ -358,7 +363,8 @@ fun <M : Mismatch> matchBoolean(
mismatchFactory: MismatchFactory<M>
): List<M> {
if (expected == null && actual != null) {
return listOf(mismatchFactory.create(expected, actual, "Expected ${valueOf(actual)} to be null", path))
return listOf(mismatchFactory.create(expected, actual,
"Expected ${valueOf(actual)} (${typeOf(actual)}) to be a null value", path))
}
logger.debug { "comparing type of ${valueOf(actual)} (${typeOf(actual)}) to match a boolean at $path" }
return when {
Expand Down Expand Up @@ -476,21 +482,24 @@ fun <M : Mismatch> matchMinType(
when (actual) {
is List<*> -> {
if (actual.size < min) {
listOf(mismatchFactory.create(expected, actual, "Expected ${valueOf(actual)} (size ${actual.size}) to have minimum size of $min", path))
listOf(mismatchFactory.create(expected, actual, "Expected ${valueOf(actual)} (size ${actual.size})" +
" to have minimum size of $min", path))
} else {
emptyList()
}
}
is JsonValue.Array -> {
if (actual.size < min) {
listOf(mismatchFactory.create(expected, actual, "Expected ${valueOf(actual)} (size ${actual.size}) to have minimum size of $min", path))
listOf(mismatchFactory.create(expected, actual, "Expected ${valueOf(actual)} (size ${actual.size})" +
" to have minimum size of $min", path))
} else {
emptyList()
}
}
is Element -> {
if (actual.childNodes.length < min) {
listOf(mismatchFactory.create(expected, actual, "Expected ${valueOf(actual)} (size ${actual.childNodes.length}) to have minimum size of $min", path))
listOf(mismatchFactory.create(expected, actual, "Expected ${valueOf(actual)} " +
"(size ${actual.childNodes.length}) to have minimum size of $min", path))
} else {
emptyList()
}
Expand All @@ -515,21 +524,24 @@ fun <M : Mismatch> matchMaxType(
when (actual) {
is List<*> -> {
if (actual.size > max) {
listOf(mismatchFactory.create(expected, actual, "Expected ${valueOf(actual)} (size ${actual.size}) to have maximum size of $max", path))
listOf(mismatchFactory.create(expected, actual, "Expected ${valueOf(actual)} (size ${actual.size})" +
" to have maximum size of $max", path))
} else {
emptyList()
}
}
is JsonValue.Array -> {
if (actual.size > max) {
listOf(mismatchFactory.create(expected, actual, "Expected ${valueOf(actual)} (size ${actual.size}) to have maximum size of $max", path))
listOf(mismatchFactory.create(expected, actual, "Expected ${valueOf(actual)} (size ${actual.size})" +
" to have maximum size of $max", path))
} else {
emptyList()
}
}
is Element -> {
if (actual.childNodes.length > max) {
listOf(mismatchFactory.create(expected, actual, "Expected ${valueOf(actual)} (size ${actual.childNodes.length}) to have maximum size of $max", path))
listOf(mismatchFactory.create(expected, actual, "Expected ${valueOf(actual)} " +
"(size ${actual.childNodes.length}) to have maximum size of $max", path))
} else {
emptyList()
}
Expand Down Expand Up @@ -586,19 +598,22 @@ fun <M : Mismatch> matchMinEqualsIgnoreOrder(
logger.debug { "comparing ${valueOf(actual)} with minimum $min at $path" }
return if (actual is List<*>) {
if (actual.size < min) {
listOf(mismatchFactory.create(expected, actual, "Expected ${valueOf(actual)} (size ${actual.size}) to have minimum size of $min", path))
listOf(mismatchFactory.create(expected, actual, "Expected ${valueOf(actual)} (size ${actual.size})" +
" to have minimum size of $min", path))
} else {
emptyList()
}
} else if (actual is JsonValue.Array) {
if (actual.size() < min) {
listOf(mismatchFactory.create(expected, actual, "Expected ${valueOf(actual)} (size ${actual.size}) to have minimum size of $min", path))
listOf(mismatchFactory.create(expected, actual, "Expected ${valueOf(actual)} (size ${actual.size})" +
" to have minimum size of $min", path))
} else {
emptyList()
}
} else if (actual is Element) {
if (actual.childNodes.length < min) {
listOf(mismatchFactory.create(expected, actual, "Expected ${valueOf(actual)} (size ${actual.childNodes.length}) to have minimum size of $min", path))
listOf(mismatchFactory.create(expected, actual, "Expected ${valueOf(actual)} " +
"(size ${actual.childNodes.length}) to have minimum size of $min", path))
} else {
emptyList()
}
Expand All @@ -617,19 +632,22 @@ fun <M : Mismatch> matchMaxEqualsIgnoreOrder(
logger.debug { "comparing ${valueOf(actual)} with maximum $max at $path" }
return if (actual is List<*>) {
if (actual.size > max) {
listOf(mismatchFactory.create(expected, actual, "Expected ${valueOf(actual)} (size ${actual.size}) to have maximum size of $max", path))
listOf(mismatchFactory.create(expected, actual, "Expected ${valueOf(actual)} (size ${actual.size})" +
" to have maximum size of $max", path))
} else {
emptyList()
}
} else if (actual is JsonValue.Array) {
if (actual.size() > max) {
listOf(mismatchFactory.create(expected, actual, "Expected ${valueOf(actual)} (size ${actual.size}) to have maximum size of $max", path))
listOf(mismatchFactory.create(expected, actual, "Expected ${valueOf(actual)} (size ${actual.size})" +
" to have maximum size of $max", path))
} else {
emptyList()
}
} else if (actual is Element) {
if (actual.childNodes.length > max) {
listOf(mismatchFactory.create(expected, actual, "Expected ${valueOf(actual)} (size ${actual.childNodes.length}) to have maximum size of $max", path))
listOf(mismatchFactory.create(expected, actual, "Expected ${valueOf(actual)} " +
"(size ${actual.childNodes.length}) to have maximum size of $max", path))
} else {
emptyList()
}
Expand All @@ -644,7 +662,8 @@ fun <M : Mismatch> matchNull(path: List<String>, actual: Any?, mismatchFactory:
return if (matches) {
emptyList()
} else {
listOf(mismatchFactory.create(null, actual, "Expected ${valueOf(actual)} to be null", path))
listOf(mismatchFactory.create(null, actual,
"Expected ${valueOf(actual)} (${typeOf(actual)}) to be a null value", path))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ class JsonContentMatcherSpec extends Specification {
expect:
matcher.matchBody(expectedBody, actualBody, context).mismatches.find {
it instanceof BodyMismatch &&
it.mismatch.contains('Expected 100 (Integer) but received 101 (Integer)')
it.mismatch.contains('Expected 101 (Integer) to be equal to 100 (Integer)')
}

where:
Expand All @@ -220,8 +220,8 @@ class JsonContentMatcherSpec extends Specification {
expect:
matcher.matchBody(expectedBody, actualBody, context).mismatches.find {
it instanceof BodyMismatch &&
it.mismatch.contains('Type mismatch: Expected Map {"something":100,"somethingElse":100} ' +
'but received List [100,100]')
it.mismatch.contains(
'Type mismatch: Expected List [100,100] to be equal to Map {"something":100,"somethingElse":100}')
}

where:
Expand All @@ -234,7 +234,7 @@ class JsonContentMatcherSpec extends Specification {
expect:
matcher.matchBody(expectedBody, actualBody, context).mismatches.find {
it instanceof BodyMismatch &&
it.mismatch.contains('Type mismatch: Expected List [100,100] but received Integer 100')
it.mismatch.contains('Type mismatch: Expected Integer 100 to be equal to List [100,100]')
}

where:
Expand Down Expand Up @@ -490,7 +490,7 @@ class JsonContentMatcherSpec extends Specification {
then:
mismatches.size() == 2
mismatches*.mismatch[0].matches(/Expected \[(.*)\] to match \[(.*)\] ignoring order of elements/)
mismatches*.path == ['$', '$.2']
mismatches*.path == ['$', '$[2]']

where:

Expand Down Expand Up @@ -529,7 +529,7 @@ class JsonContentMatcherSpec extends Specification {
then:
mismatches.size() == 1 + 4
mismatches*.mismatch[0].matches(/Expected \[(.*)\] to match \[(.*)\] ignoring order of elements/)
mismatches*.path == ['$'] + ['$.0'] * 4
mismatches*.path == ['$'] + ['$[0]'] * 4

where:

Expand Down Expand Up @@ -562,7 +562,7 @@ class JsonContentMatcherSpec extends Specification {
expect:
matcher.matchBody(expectedBody, actualBody, context)
.bodyResults.collectMany { it.result }.find {
it instanceof BodyMismatch && it.mismatch.contains('Expected 1 (Integer) but received 2 (Integer)')
it instanceof BodyMismatch && it.mismatch.contains('Expected 2 (Integer) to be equal to 1 (Integer)')
}

where:
Expand Down Expand Up @@ -618,7 +618,7 @@ class JsonContentMatcherSpec extends Specification {
!mismatches.empty
mismatches*.mismatch == ['Expected [red, blue] to match [blue, seven] ignoring order of elements',
"Expected 'seven' to match 'red|blue'"]
mismatches*.path == ['$', '$.1']
mismatches*.path == ['$', '$[1]']
}

@Unroll
Expand Down Expand Up @@ -730,14 +730,14 @@ class JsonContentMatcherSpec extends Specification {
then:
[ //[path, expected, [...actual]]
['$', expected, [actual]],
['$.0', '[1, 2, 3]', ['[6, 4, 5]', '[2, 3, 1]']],
['$.0.0', '1', ['6', '2']],
['$.0.1', '2', ['4', '3']],
['$.0.2', '3', ['5', '1']],
['$.1', '[4, 5, 6]', ['[2, 3, 1]']],
['$.1.0', '4', ['2', '3', '1']],
['$.1.1', '5', ['2', '3', '1']],
['$.1.2', '6', ['2', '3', '1']]
['$[0]', '[1, 2, 3]', ['[6, 4, 5]', '[2, 3, 1]']],
['$[0][0]', '1', ['6', '2']],
['$[0][1]', '2', ['4', '3']],
['$[0][2]', '3', ['5', '1']],
['$[1]', '[4, 5, 6]', ['[2, 3, 1]']],
['$[1][0]', '4', ['2', '3', '1']],
['$[1][1]', '5', ['2', '3', '1']],
['$[1][2]', '6', ['2', '3', '1']]
].eachWithIndex { expectedResult, i ->
assert expectedResult == results[i]
}
Expand Down
Loading

0 comments on commit 6c1f850

Please sign in to comment.