summary refs log tree commit diff stats
path: root/tests/misc
diff options
context:
space:
mode:
authorTimothee Cour <timothee.cour2@gmail.com>2021-08-20 01:13:03 -0700
committerGitHub <noreply@github.com>2021-08-20 10:13:03 +0200
commitf2910077ac41a614154f3851f5bffc02b06124a2 (patch)
treed22b376683ff02dc3ad685335a3d67ec1f6dc0c7 /tests/misc
parent13b97291835046eb1368ff4cd240cdcc7d0a2899 (diff)
downloadNim-f2910077ac41a614154f3851f5bffc02b06124a2.tar.gz
sigmatch: support optional params with last block arg(s) (#18631)
* sigmatch: support optional params with last block arg
* add tests
* works with multiple block args
* cleanup
* address comment
Diffstat (limited to 'tests/misc')
-rw-r--r--tests/misc/trfc405.nim81
1 files changed, 81 insertions, 0 deletions
diff --git a/tests/misc/trfc405.nim b/tests/misc/trfc405.nim
new file mode 100644
index 000000000..adc39b1df
--- /dev/null
+++ b/tests/misc/trfc405.nim
@@ -0,0 +1,81 @@
+# https://github.com/nim-lang/RFCs/issues/405
+
+template main =
+  template fn1(a = 1, b = 2, body): auto = (a, b, astToStr(body))
+  let a1 = fn1(10, 20):
+    foo
+  doAssert a1 == (10, 20, "\nfoo")
+
+  template fn2(a = 1, b = 2, body): auto = (a, b, astToStr(body))
+  let a2 = fn2(a = 10): foo
+  doAssert a2 == (10, 2, "\nfoo")
+  let a2b = fn2(b = 20): foo
+  doAssert a2b == (1, 20, "\nfoo")
+
+  template fn3(x: int, a = 1, b = 2, body): auto = (a, b, astToStr(body))
+  let a3 = fn3(3, 10, 20): foo
+  doAssert a3 == (10, 20, "\nfoo")
+  let a3b = fn3(3, a = 10): foo
+  doAssert a3b == (10, 2, "\nfoo")
+
+  template fn4(x: int, y: int, body): auto = (x, y, astToStr(body))
+  let a4 = fn4(1, 2): foo
+  doAssert a4 == (1, 2, "\nfoo")
+
+  template fn5(x = 1, y = 2, body: untyped = 3): auto = (x, y, astToStr(body))
+  doAssert compiles(fn5(1, 2, foo))
+  doAssert not compiles(fn5(1, foo))
+
+  block:
+    # with an overload
+    var witness = 0
+    template fn6() = discard
+    template fn6(procname: string, body: untyped): untyped = witness.inc
+    fn6("abc"): discard
+    assert witness == 1
+
+  block:
+    # with overloads
+    var witness = 0
+    template fn6() = discard
+    template fn6(a: int) = discard
+    template fn6(procname: string, body: untyped): untyped = witness.inc
+    fn6("abc"): discard
+    assert witness == 1
+
+    template fn6(b = 1.5, body: untyped): untyped = witness.inc
+    fn6(1.3): discard
+    assert witness == 2
+
+  block:
+    var witness = 0
+    template fn6(a: int) = discard
+    template fn6(a: string) = discard
+    template fn6(ignore: string, b = 1.5, body: untyped): untyped = witness.inc
+    fn6(""):
+      foobar1
+      foobar2
+    doAssert witness == 1
+    fn6(""): discard
+    doAssert witness == 2
+
+  block: # multi block args
+    template fn8(a = 1, b = 2, body1: untyped, body2: untyped): auto = (a, b, astToStr(body1), astToStr(body2))
+    let a1 = fn8():
+      foobar1
+      foobar2
+    do:
+      foobar3
+      foobar4
+    doAssert a1 == (1, 2, "\nfoobar1\nfoobar2", "\nfoobar3\nfoobar4")
+
+    let a2 = fn8(b = 20):
+      foobar1
+      foobar2
+    do:
+      foobar3
+      foobar4
+    doAssert a2 == (1, 20, "\nfoobar1\nfoobar2", "\nfoobar3\nfoobar4")
+
+static: main()
+main()