Skip to content

Commit a4f5ce8

Browse files
gribozavrMax Moiseev
authored andcommitted
StdlibUnittest: improve error messages in checkComparable()
1 parent 5434ccc commit a4f5ce8

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
@@ -1952,7 +1952,8 @@ public func checkEquatable<Instances : Collection>(
19521952
_checkEquatableImpl(
19531953
Array(instances),
19541954
oracle: { oracle(indices[$0], indices[$1]) },
1955-
allowBrokenTransitivity: allowBrokenTransitivity)
1955+
allowBrokenTransitivity: allowBrokenTransitivity,
1956+
${trace})
19561957
}
19571958

19581959
internal func _checkEquatableImpl<Instance : Equatable>(
@@ -1977,18 +1978,23 @@ internal func _checkEquatableImpl<Instance : Equatable>(
19771978
let predictedXY = oracle(i, j)
19781979
expectEqual(
19791980
predictedXY, oracle(j, i),
1980-
"bad oracle: broken symmetry between indices \(i), \(j)")
1981+
"bad oracle: broken symmetry between indices \(i), \(j)",
1982+
stackTrace: ${stackTrace})
19811983

19821984
let isEqualXY = x == y
19831985
expectEqual(
19841986
predictedXY, isEqualXY,
1985-
"lhs (at index \(i)): \(x)\nrhs (at index \(j)): \(y)",
1987+
(predictedXY
1988+
? "expected equal, found not equal\n"
1989+
: "expected not equal, found equal\n") +
1990+
"lhs (at index \(i)): \(String(reflecting: x))\n" +
1991+
"rhs (at index \(j)): \(String(reflecting: y))",
19861992
stackTrace: ${stackTrace})
19871993

19881994
// Not-equal is an inverse of equal.
19891995
expectNotEqual(
19901996
isEqualXY, x != y,
1991-
"lhs (at index \(i)): \(x)\nrhs (at index \(j)): \(y)",
1997+
"lhs (at index \(i)): \(String(reflecting: x))\nrhs (at index \(j)): \(String(reflecting: y))",
19921998
stackTrace: ${stackTrace})
19931999

19942000
if !allowBrokenTransitivity {
@@ -2002,7 +2008,8 @@ internal func _checkEquatableImpl<Instance : Equatable>(
20022008
for k in transitivityScoreboard[i].value {
20032009
expectTrue(
20042010
oracle(j, k),
2005-
"bad oracle: broken transitivity at indices \(i), \(j), \(k)")
2011+
"bad oracle: broken transitivity at indices \(i), \(j), \(k)",
2012+
stackTrace: ${stackTrace})
20062013
// No need to check equality between actual values, we will check
20072014
// them with the checks above.
20082015
}
@@ -2139,11 +2146,28 @@ public func checkComparable<Instances : Collection>(
21392146
for i in instances.indices {
21402147
let x = instances[i]
21412148

2142-
expectFalse(x < x, ${trace})
2143-
expectFalse(x > x, ${trace})
2144-
expectTrue(x <= x, ${trace})
2145-
expectTrue(x >= x, ${trace})
2146-
2149+
expectFalse(
2150+
x < x,
2151+
"found 'x < x'\n" +
2152+
"at index \(i): \(String(reflecting: x))",
2153+
stackTrace: ${stackTrace})
2154+
2155+
expectFalse(
2156+
x > x,
2157+
"found 'x > x'\n" +
2158+
"at index \(i): \(String(reflecting: x))",
2159+
stackTrace: ${stackTrace})
2160+
2161+
expectTrue(x <= x,
2162+
"found 'x <= x' to be false\n" +
2163+
"at index \(i): \(String(reflecting: x))",
2164+
stackTrace: ${stackTrace})
2165+
2166+
expectTrue(x >= x,
2167+
"found 'x >= x' to be false\n" +
2168+
"at index \(i): \(String(reflecting: x))",
2169+
stackTrace: ${stackTrace})
2170+
21472171
for j in instances.indices where i != j {
21482172
let y = instances[j]
21492173

@@ -2155,11 +2179,30 @@ public func checkComparable<Instances : Collection>(
21552179
+ "(\(String(reflecting: i)), \(String(reflecting: j)))",
21562180
stackTrace: ${stackTrace})
21572181

2158-
expectEqual(expected.isLT(), x < y, ${trace})
2159-
expectEqual(expected.isLE(), x <= y, ${trace})
2160-
expectEqual(expected.isGE(), x >= y, ${trace})
2161-
expectEqual(expected.isGT(), x > y, ${trace})
2162-
2182+
expectEqual(expected.isLT(), x < y,
2183+
"x < y\n" +
2184+
"lhs (at index \(i)): \(String(reflecting: x))\n" +
2185+
"rhs (at index \(j)): \(String(reflecting: y))",
2186+
stackTrace: ${stackTrace})
2187+
2188+
expectEqual(expected.isLE(), x <= y,
2189+
"x <= y\n" +
2190+
"lhs (at index \(i)): \(String(reflecting: x))\n" +
2191+
"rhs (at index \(j)): \(String(reflecting: y))",
2192+
stackTrace: ${stackTrace})
2193+
2194+
expectEqual(expected.isGE(), x >= y,
2195+
"x >= y\n" +
2196+
"lhs (at index \(i)): \(String(reflecting: x))\n" +
2197+
"rhs (at index \(j)): \(String(reflecting: y))",
2198+
stackTrace: ${stackTrace})
2199+
2200+
expectEqual(expected.isGT(), x > y,
2201+
"x > y\n" +
2202+
"lhs (at index \(i)): \(String(reflecting: x))\n" +
2203+
"rhs (at index \(j)): \(String(reflecting: y))",
2204+
stackTrace: ${stackTrace})
2205+
21632206
for k in instances.indices {
21642207
let expected2 = oracle(j, k)
21652208
if expected == expected2 {

0 commit comments

Comments
 (0)