summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorTimothee Cour <timothee.cour2@gmail.com>2019-01-12 15:51:44 -0800
committerTimothee Cour <timothee.cour2@gmail.com>2019-01-14 17:00:54 -0800
commite17321aa2429c6bed97bef28a149fd21166b90a2 (patch)
treebbc3cff97147320bbbb00b54afb9d169f0794904
parentdd1f23f6fc9c3d57f5d3e2a44d2abe4dc1b57a2a (diff)
downloadNim-e17321aa2429c6bed97bef28a149fd21166b90a2.tar.gz
improve formatting in assertEquals
-rw-r--r--compiler/unittest_light.nim15
-rw-r--r--tests/compiler/tunittest_light.nim22
2 files changed, 19 insertions, 18 deletions
diff --git a/compiler/unittest_light.nim b/compiler/unittest_light.nim
index bcba6f7c7..d9842b399 100644
--- a/compiler/unittest_light.nim
+++ b/compiler/unittest_light.nim
@@ -14,8 +14,8 @@ proc mismatch*[T](lhs: T, rhs: T): string =
   proc quoted(s: string): string = result.addQuoted s
 
   result.add "\n"
-  result.add "lhs:{\n" & replaceInvisible(
-      $lhs) & "}\nrhs:{\n" & replaceInvisible($rhs) & "}\n"
+  result.add "lhs:{" & replaceInvisible(
+      $lhs) & "}\nrhs:{" & replaceInvisible($rhs) & "}\n"
   when compiles(lhs.len):
     if lhs.len != rhs.len:
       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
       result.add "first mismatch index: " & $i & "\n"
       if i < lhs.len and i < rhs.len:
-        result.add "lhs[i]: {" & quoted($lhs[i]) & "} rhs[i]: {" & quoted(
-            $rhs[i]) & "}"
-      result.add "lhs[0..<i]:{\n" & replaceInvisible($lhs[
-          0..<i]) & "}\nrhs[0..<i]:{\n" & replaceInvisible($rhs[0..<i]) & "}"
+        result.add "lhs[i]: {" & quoted($lhs[i]) & "}\nrhs[i]: {" & quoted(
+            $rhs[i]) & "}\n"
+      result.add "lhs[0..<i]:{" & replaceInvisible($lhs[
+          0..<i]) & "}"
 
 proc assertEquals*[T](lhs: T, rhs: T) =
   when false: # can be useful for debugging to see all that's fed to this.
     echo "----" & $lhs
-  doAssert lhs==rhs, mismatch(lhs, rhs)
+  if lhs!=rhs:
+    doAssert false, mismatch(lhs, rhs)
diff --git a/tests/compiler/tunittest_light.nim b/tests/compiler/tunittest_light.nim
index d20293d5b..422474002 100644
--- a/tests/compiler/tunittest_light.nim
+++ b/tests/compiler/tunittest_light.nim
@@ -24,17 +24,16 @@ proc testMismatch() =
 
 """
 
-  doAssert mismatch(a, b) == """
+  let output = mismatch(a, b)
+  let expected = """
 
-lhs:{
-  some test with space at the end of lines    \n
+lhs:{  some test with space at the end of lines    \n
 \n
   can be hard to spot differences when diffing in a terminal   \n
   without this helper function\n
 \n
 }
-rhs:{
-  some test with space at the end of lines    \n
+rhs:{  some test with space at the end of lines    \n
 \n
   can be hard to spot differences when diffing in a terminal  \n
   without this helper function\n
@@ -42,14 +41,15 @@ rhs:{
 }
 lhs.len: 144 rhs.len: 143
 first mismatch index: 110
-lhs[i]: {" "} rhs[i]: {"\n"}lhs[0..<i]:{
-  some test with space at the end of lines    \n
-\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
+lhs[i]: {" "}
+rhs[i]: {"\n"}
+lhs[0..<i]:{  some test with space at the end of lines    \n
 \n
   can be hard to spot differences when diffing in a terminal  }"""
 
+  if output != expected:
+    echo output
+    doAssert false
+
 testMismatch()
 testAssertEquals()