Skip to content

Commit 10b339d

Browse files
committed
StdlibUnittest: improve error messages in checkComparable()
1 parent bfd5942 commit 10b339d

File tree

1 file changed

+58
-15
lines changed

1 file changed

+58
-15
lines changed

stdlib/private/StdlibUnittest/StdlibUnittest.swift.gyb

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1960,7 +1960,8 @@ public func checkEquatable<Instances : Collection>(
19601960
_checkEquatableImpl(
19611961
Array(instances),
19621962
oracle: { oracle(indices[$0], indices[$1]) },
1963-
allowBrokenTransitivity: allowBrokenTransitivity)
1963+
allowBrokenTransitivity: allowBrokenTransitivity,
1964+
${trace})
19641965
}
19651966

19661967
internal func _checkEquatableImpl<Instance : Equatable>(
@@ -1985,18 +1986,23 @@ internal func _checkEquatableImpl<Instance : Equatable>(
19851986
let predictedXY = oracle(i, j)
19861987
expectEqual(
19871988
predictedXY, oracle(j, i),
1988-
"bad oracle: broken symmetry between indices \(i), \(j)")
1989+
"bad oracle: broken symmetry between indices \(i), \(j)",
1990+
stackTrace: ${stackTrace})
19891991

19901992
let isEqualXY = x == y
19911993
expectEqual(
19921994
predictedXY, isEqualXY,
1993-
"lhs (at index \(i)): \(x)\nrhs (at index \(j)): \(y)",
1995+
(predictedXY
1996+
? "expected equal, found not equal\n"
1997+
: "expected not equal, found equal\n") +
1998+
"lhs (at index \(i)): \(String(reflecting: x))\n" +
1999+
"rhs (at index \(j)): \(String(reflecting: y))",
19942000
stackTrace: ${stackTrace})
19952001

19962002
// Not-equal is an inverse of equal.
19972003
expectNotEqual(
19982004
isEqualXY, x != y,
1999-
"lhs (at index \(i)): \(x)\nrhs (at index \(j)): \(y)",
2005+
"lhs (at index \(i)): \(String(reflecting: x))\nrhs (at index \(j)): \(String(reflecting: y))",
20002006
stackTrace: ${stackTrace})
20012007

20022008
if !allowBrokenTransitivity {
@@ -2010,7 +2016,8 @@ internal func _checkEquatableImpl<Instance : Equatable>(
20102016
for k in transitivityScoreboard[i].value {
20112017
expectTrue(
20122018
oracle(j, k),
2013-
"bad oracle: broken transitivity at indices \(i), \(j), \(k)")
2019+
"bad oracle: broken transitivity at indices \(i), \(j), \(k)",
2020+
stackTrace: ${stackTrace})
20142021
// No need to check equality between actual values, we will check
20152022
// them with the checks above.
20162023
}
@@ -2147,11 +2154,28 @@ public func checkComparable<Instances : Collection>(
21472154
for i in instances.indices {
21482155
let x = instances[i]
21492156

2150-
expectFalse(x < x, ${trace})
2151-
expectFalse(x > x, ${trace})
2152-
expectTrue(x <= x, ${trace})
2153-
expectTrue(x >= x, ${trace})
2154-
2157+
expectFalse(
2158+
x < x,
2159+
"found 'x < x'\n" +
2160+
"at index \(i): \(String(reflecting: x))",
2161+
stackTrace: ${stackTrace})
2162+
2163+
expectFalse(
2164+
x > x,
2165+
"found 'x > x'\n" +
2166+
"at index \(i): \(String(reflecting: x))",
2167+
stackTrace: ${stackTrace})
2168+
2169+
expectTrue(x <= x,
2170+
"found 'x <= x' to be false\n" +
2171+
"at index \(i): \(String(reflecting: x))",
2172+
stackTrace: ${stackTrace})
2173+
2174+
expectTrue(x >= x,
2175+
"found 'x >= x' to be false\n" +
2176+
"at index \(i): \(String(reflecting: x))",
2177+
stackTrace: ${stackTrace})
2178+
21552179
for j in instances.indices where i != j {
21562180
let y = instances[j]
21572181

@@ -2163,11 +2187,30 @@ public func checkComparable<Instances : Collection>(
21632187
+ "(\(String(reflecting: i)), \(String(reflecting: j)))",
21642188
stackTrace: ${stackTrace})
21652189

2166-
expectEqual(expected.isLT(), x < y, ${trace})
2167-
expectEqual(expected.isLE(), x <= y, ${trace})
2168-
expectEqual(expected.isGE(), x >= y, ${trace})
2169-
expectEqual(expected.isGT(), x > y, ${trace})
2170-
2190+
expectEqual(expected.isLT(), x < y,
2191+
"x < y\n" +
2192+
"lhs (at index \(i)): \(String(reflecting: x))\n" +
2193+
"rhs (at index \(j)): \(String(reflecting: y))",
2194+
stackTrace: ${stackTrace})
2195+
2196+
expectEqual(expected.isLE(), x <= y,
2197+
"x <= y\n" +
2198+
"lhs (at index \(i)): \(String(reflecting: x))\n" +
2199+
"rhs (at index \(j)): \(String(reflecting: y))",
2200+
stackTrace: ${stackTrace})
2201+
2202+
expectEqual(expected.isGE(), x >= y,
2203+
"x >= y\n" +
2204+
"lhs (at index \(i)): \(String(reflecting: x))\n" +
2205+
"rhs (at index \(j)): \(String(reflecting: y))",
2206+
stackTrace: ${stackTrace})
2207+
2208+
expectEqual(expected.isGT(), x > y,
2209+
"x > y\n" +
2210+
"lhs (at index \(i)): \(String(reflecting: x))\n" +
2211+
"rhs (at index \(j)): \(String(reflecting: y))",
2212+
stackTrace: ${stackTrace})
2213+
21712214
for k in instances.indices {
21722215
let expected2 = oracle(j, k)
21732216
if expected == expected2 {

0 commit comments

Comments
 (0)