summary refs log tree commit diff stats
path: root/tests/specialops/tcallprecedence.nim
diff options
context:
space:
mode:
authorhlaaftana <10591326+hlaaftana@users.noreply.github.com>2021-02-09 14:18:16 +0300
committerGitHub <noreply@github.com>2021-02-09 12:18:16 +0100
commitaac8f675732086e9573851765d6e1c5b9ff38c28 (patch)
treedc934d291fba2d73370723d1b21c8b53d03a3ca1 /tests/specialops/tcallprecedence.nim
parentd1210a3bb9fb381c42e3f6070f1011dcf7178d29 (diff)
downloadNim-aac8f675732086e9573851765d6e1c5b9ff38c28.tar.gz
tests and docs for call operator (#16980)
* tests and docs for call operator

* fix leftover

* add extra dot test
Diffstat (limited to 'tests/specialops/tcallprecedence.nim')
-rw-r--r--tests/specialops/tcallprecedence.nim44
1 files changed, 44 insertions, 0 deletions
diff --git a/tests/specialops/tcallprecedence.nim b/tests/specialops/tcallprecedence.nim
new file mode 100644
index 000000000..6116f83d5
--- /dev/null
+++ b/tests/specialops/tcallprecedence.nim
@@ -0,0 +1,44 @@
+import macros
+
+{.experimental: "dotOperators".}
+{.experimental: "callOperator".}
+
+block:
+  template `.()`(foo: int, args: varargs[untyped]): untyped {.used.} =
+    ".()"
+
+  template `()`(foo: int, args: varargs[untyped]): untyped =
+    "()"
+
+  let a = (b: 1)
+  let c = 3
+
+  doAssert a.b(c) == "()"
+  doAssert not compiles(a(c))
+  doAssert (a.b)(c) == "()"
+
+macro `()`(args: varargs[typed]): untyped =
+  result = newLit("() " & args.treeRepr)
+
+macro `.`(args: varargs[typed]): untyped =
+  result = newLit(". " & args.treeRepr)
+
+block:
+  let a = 1
+  let b = 2
+  doAssert a.b == `()`(b, a)
+
+block:
+  let a = 1
+  proc b(): int {.used.} = 2
+  doAssert a.b == `.`(a, b)
+
+block:
+  let a = 1
+  proc b(x: int): int = x + 1
+  let c = 3
+
+  doAssert a.b(c) == `.`(a, b, c)
+  doAssert a(b) == `()`(a, b)
+  doAssert (a.b)(c) == `()`(a.b, c)
+  doAssert a.b == b(a)