diff options
author | Timothee Cour <timothee.cour2@gmail.com> | 2019-12-23 09:42:32 +0100 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2019-12-23 09:42:32 +0100 |
commit | a0980c88c934dcbefb17099ca68f0d775aef44cd (patch) | |
tree | 61ba3b50ca28554650e93209e9f320a7c969f76d /tests/system/tlenvarargs.nim | |
parent | 2e7c9eb6a8dea7e4ca6b268020058b46f6b16580 (diff) | |
download | Nim-a0980c88c934dcbefb17099ca68f0d775aef44cd.tar.gz |
lenVarargs: number of varargs elements (#12907)
Diffstat (limited to 'tests/system/tlenvarargs.nim')
-rw-r--r-- | tests/system/tlenvarargs.nim | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/tests/system/tlenvarargs.nim b/tests/system/tlenvarargs.nim new file mode 100644 index 000000000..ca12c8171 --- /dev/null +++ b/tests/system/tlenvarargs.nim @@ -0,0 +1,59 @@ +discard """ + output: ''' +tlenvarargs.nim:35:9 (1, 2) +tlenvarargs.nim:36:9 12 +tlenvarargs.nim:37:9 1 +tlenvarargs.nim:38:8 +done +''' +""" +## line 10 + +template myecho*(a: varargs[untyped]) = + ## shows a useful debugging echo-like proc that is dependency-free (no dependency + ## on macros.nim) so can be used in more contexts + const info = instantiationInfo(-1, false) + const loc = info.filename & ":" & $info.line & ":" & $info.column & " " + when lenVarargs(a) > 0: + echo(loc, a) + else: + echo(loc) + +template fun*(a: varargs[untyped]): untyped = + lenVarargs(a) + +template fun2*(a: varargs[typed]): untyped = + a.lenVarargs + +template fun3*(a: varargs[int]): untyped = + a.lenVarargs + +template fun4*(a: varargs[untyped]): untyped = + len(a) + +proc main()= + myecho (1, 2) + myecho 1, 2 + myecho 1 + myecho() + + doAssert fun() == 0 + doAssert fun('a') == 1 + doAssert fun("asdf", 1) == 2 + + doAssert fun2() == 0 + doAssert fun2('a') == 1 + doAssert fun2("asdf", 1) == 2 + + doAssert fun3() == 0 + doAssert fun3(10) == 1 + doAssert fun3(10, 11) == 2 + + ## shows why `lenVarargs` can't be named `len` + doAssert fun4("abcdef") == len("abcdef") + + ## workaround for BUG:D20191218T171447 whereby if testament expected output ends + ## in space, testament strips it from expected output but not actual output, + ## which leads to a mismatch when running test via megatest + echo "done" +main() |