improve formatting in assertEquals

This commit is contained in:
Timothee Cour 2019-01-12 15:51:44 -08:00
commit e17321aa24
2 changed files with 19 additions and 18 deletions

View file

@ -14,8 +14,8 @@ proc mismatch*[T](lhs: T, rhs: T): string =
proc quoted(s: string): string = result.addQuoted s proc quoted(s: string): string = result.addQuoted s
result.add "\n" result.add "\n"
result.add "lhs:{\n" & replaceInvisible( result.add "lhs:{" & replaceInvisible(
$lhs) & "}\nrhs:{\n" & replaceInvisible($rhs) & "}\n" $lhs) & "}\nrhs:{" & replaceInvisible($rhs) & "}\n"
when compiles(lhs.len): when compiles(lhs.len):
if lhs.len != rhs.len: if lhs.len != rhs.len:
result.add "lhs.len: " & $lhs.len & " rhs.len: " & $rhs.len & "\n" result.add "lhs.len: " & $lhs.len & " rhs.len: " & $rhs.len & "\n"
@ -26,12 +26,13 @@ proc mismatch*[T](lhs: T, rhs: T): string =
i.inc i.inc
result.add "first mismatch index: " & $i & "\n" result.add "first mismatch index: " & $i & "\n"
if i < lhs.len and i < rhs.len: if i < lhs.len and i < rhs.len:
result.add "lhs[i]: {" & quoted($lhs[i]) & "} rhs[i]: {" & quoted( result.add "lhs[i]: {" & quoted($lhs[i]) & "}\nrhs[i]: {" & quoted(
$rhs[i]) & "}" $rhs[i]) & "}\n"
result.add "lhs[0..<i]:{\n" & replaceInvisible($lhs[ result.add "lhs[0..<i]:{" & replaceInvisible($lhs[
0..<i]) & "}\nrhs[0..<i]:{\n" & replaceInvisible($rhs[0..<i]) & "}" 0..<i]) & "}"
proc assertEquals*[T](lhs: T, rhs: T) = proc assertEquals*[T](lhs: T, rhs: T) =
when false: # can be useful for debugging to see all that's fed to this. when false: # can be useful for debugging to see all that's fed to this.
echo "----" & $lhs echo "----" & $lhs
doAssert lhs==rhs, mismatch(lhs, rhs) if lhs!=rhs:
doAssert false, mismatch(lhs, rhs)

View file

@ -24,17 +24,16 @@ proc testMismatch() =
""" """
doAssert mismatch(a, b) == """ let output = mismatch(a, b)
let expected = """
lhs:{ lhs:{ some test with space at the end of lines \n
some test with space at the end of lines \n
\n \n
can be hard to spot differences when diffing in a terminal \n can be hard to spot differences when diffing in a terminal \n
without this helper function\n without this helper function\n
\n \n
} }
rhs:{ rhs:{ some test with space at the end of lines \n
some test with space at the end of lines \n
\n \n
can be hard to spot differences when diffing in a terminal \n can be hard to spot differences when diffing in a terminal \n
without this helper function\n without this helper function\n
@ -42,14 +41,15 @@ rhs:{
} }
lhs.len: 144 rhs.len: 143 lhs.len: 144 rhs.len: 143
first mismatch index: 110 first mismatch index: 110
lhs[i]: {" "} rhs[i]: {"\n"}lhs[0..<i]:{ lhs[i]: {" "}
some test with space at the end of lines \n rhs[i]: {"\n"}
\n lhs[0..<i]:{ some test with space at the end of lines \n
can be hard to spot differences when diffing in a terminal }
rhs[0..<i]:{
some test with space at the end of lines \n
\n \n
can be hard to spot differences when diffing in a terminal }""" can be hard to spot differences when diffing in a terminal }"""
if output != expected:
echo output
doAssert false
testMismatch() testMismatch()
testAssertEquals() testAssertEquals()