summary refs log tree commit diff stats
path: root/tests/stdlib/trepr.nim
diff options
context:
space:
mode:
Diffstat (limited to 'tests/stdlib/trepr.nim')
-rw-r--r--tests/stdlib/trepr.nim203
1 files changed, 191 insertions, 12 deletions
diff --git a/tests/stdlib/trepr.nim b/tests/stdlib/trepr.nim
index 357854d67..3956b98f9 100644
--- a/tests/stdlib/trepr.nim
+++ b/tests/stdlib/trepr.nim
@@ -1,13 +1,16 @@
 discard """
   targets: "c cpp js"
-  matrix: ";--gc:arc"
+  matrix: "--mm:refc;--mm:arc"
 """
 
 # if excessive, could remove 'cpp' from targets
 
-from strutils import endsWith, contains
+from strutils import endsWith, contains, strip
 from std/macros import newLit
-macro deb(a): string = newLit a.repr
+import std/assertions
+
+macro deb(a): string = newLit a.repr.strip
+macro debTyped(a: typed): string = newLit a.repr.strip
 
 template main() =
   doAssert repr({3,5}) == "{3, 5}"
@@ -37,7 +40,7 @@ template main() =
   #[
   BUG:
   --gc:arc returns `"abc"`
-  regular gc returns with address, e.g. 0x1068aae60"abc", but only 
+  regular gc returns with address, e.g. 0x1068aae60"abc", but only
   for c,cpp backends (not js, vm)
   ]#
   block:
@@ -62,22 +65,21 @@ template main() =
   doAssert repr(arr) == "[1, 2, 3]"
 
   block: # bug #7878
-    proc reprOpenarray(variable: var openarray[int]): string = repr(variable)
+    proc reprOpenarray(variable: var openArray[int]): string = repr(variable)
     when defined(js): discard # BUG: doesn't work
     else:
       doAssert reprOpenarray(arr) == "[1, 2, 3]"
 
-  block: # bug #17292
+  block: # bug #17292 repr with `do`
     template foo(a, b, c, d) = discard
     block:
       let a = deb:
         foo(1, 2, 3, 4)
-      doAssert a == "\nfoo(1, 2, 3, 4)"
+      doAssert a == "foo(1, 2, 3, 4)"
     block:
       let a = deb:
         foo(1, 2, 3): 4
       doAssert a == """
-
 foo(1, 2, 3):
   4"""
 
@@ -86,7 +88,6 @@ foo(1, 2, 3):
         foo(1, 2): 3
         do: 4
       doAssert a == """
-
 foo(1, 2):
   3
 do:
@@ -98,7 +99,6 @@ do:
         do: 3
         do: 4
       doAssert a == """
-
 foo(1):
   3
 do:
@@ -118,7 +118,6 @@ do:
           4
 
       doAssert a == """
-
 foo(1):
   3
 do:
@@ -135,7 +134,6 @@ do:
         do: 3
         do: 4
       doAssert a == """
-
 foo:
   1
 do:
@@ -145,5 +143,186 @@ do:
 do:
   4"""
 
+  block: # bug #17292 repr with `(discard)` (`discard` would result in illegal code)
+    let a = deb:
+      let f {.inject.} = () => (discard)
+    doAssert a == """
+let f {.inject.} = () =>
+    (discard )"""
+
+    let a2 = deb:
+      block:
+        discard
+      discard
+
+      block:
+        when true: discard
+
+      # let a = b => discard # illegal
+      discard b => (discard) # legal
+
+      block:
+        return
+    doAssert a2 == """
+block:
+  discard
+discard
+block:
+  when true:
+    discard
+discard b =>
+    (discard )
+block:
+  return"""
+
+  block: # bug #17292 (bug 4)
+    let a = deb:
+      proc `=destroy`() = discard
+      proc `'foo`(): int = discard
+      proc `foo bar baz`(): int = discard
+    let a2 = """
+proc `=destroy`() =
+  discard
+
+proc `'foo`(): int =
+  discard
+
+proc `foo bar baz`(): int =
+  discard"""
+    doAssert a2 == a
+
+  block: # setters: `foo=`
+    let a = deb:
+      proc `foo=`() = discard
+    doAssert a == """
+proc `foo=`() =
+  discard"""
+
+  block: # bug #14850
+    block:
+      let a = deb:
+        template bar(): untyped =
+          foo1:
+            discard
+            4
+          foo2(1):
+            discard
+            4
+          foo3(1):
+            discard
+            4
+          do: 1
+          do: 2
+          x.add foo4
+          x.add: foo5: 3
+          x.add foo6 do: 4
+          a.add(foo7 do:
+            echo "baz"
+            4)
+
+      doAssert a == """
+template bar(): untyped =
+  foo1:
+    discard
+    4
+  foo2(1):
+    discard
+    4
+  foo3(1):
+    discard
+    4
+  do:
+    1
+  do:
+    2
+  x.add foo4
+  x.add:
+    foo5:
+      3
+  x.add foo6 do:
+    4
+  a.add(foo7 do:
+    echo "baz"
+    4)"""
+
+  block: # one liner doc comments
+    let a1 = deb:
+      func fn1(): int = 1  ## comment
+      func fn2(): int = 1
+        ## comment
+    let a2 = debTyped:
+      func fn1(): int = 1  ## comment
+      func fn2(): int = 1
+        ## comment
+    doAssert a1 == """
+func fn1(): int =
+  ## comment
+  1
+
+func fn2(): int =
+  ## comment
+  1"""
+    doAssert a2 == """
+func fn1(): int =
+  ## comment
+  result = 1
+
+func fn2(): int =
+  ## comment
+  result = 1"""
+
+  block: # block calls
+    let a = deb:
+      foo(a, b, (c, d)):
+        e
+        f
+      do: g
+      of h: i
+      elif j: k
+      except m: n
+      do () -> u: v
+      finally: o
+
+      a + b:
+        c
+        d
+      do:
+        e
+        f
+      else: g
+
+      *a: b
+      do: c
+
+    doAssert a == """foo(a, b, (c, d)):
+  e
+  f
+do:
+  g
+of h:
+  i
+elif j:
+  k
+except m:
+  n
+do -> u:
+  v
+finally:
+  o
+a + b:
+  c
+  d
+do:
+  e
+  f
+else:
+  g
+*a:
+  b
+do:
+  c"""
+
+  doAssert repr(1..2) == "1 .. 2"
+
 static: main()
 main()