diff options
Diffstat (limited to 'tests/whenstmt')
-rw-r--r-- | tests/whenstmt/t12517.nim | 21 | ||||
-rw-r--r-- | tests/whenstmt/t19426.nim | 16 | ||||
-rw-r--r-- | tests/whenstmt/twhen.nim | 47 | ||||
-rw-r--r-- | tests/whenstmt/twhen_macro.nim | 18 |
4 files changed, 102 insertions, 0 deletions
diff --git a/tests/whenstmt/t12517.nim b/tests/whenstmt/t12517.nim new file mode 100644 index 000000000..8be0171e1 --- /dev/null +++ b/tests/whenstmt/t12517.nim @@ -0,0 +1,21 @@ +# Test based on issue #12517 + +discard """ + nimout: ''' +nimvm +both +''' + output: ''' +both +''' +""" + +proc test() = + when nimvm: + echo "nimvm" + echo "both" + +static: + test() +test() + diff --git a/tests/whenstmt/t19426.nim b/tests/whenstmt/t19426.nim new file mode 100644 index 000000000..95fb54a9e --- /dev/null +++ b/tests/whenstmt/t19426.nim @@ -0,0 +1,16 @@ +type + MyInt = object + bitWidth: int + +template toRealType*(t: MyInt): typedesc = + when t.bitWidth == 32: int32 + elif t.bitWidth == 64: int64 + else: {.error.} + +proc doFail(T: typedesc): T = default(T) + +proc test = + const myInt = MyInt(bitWidth:32) + discard doFail(toRealType(myInt)) + +test() \ No newline at end of file diff --git a/tests/whenstmt/twhen.nim b/tests/whenstmt/twhen.nim new file mode 100644 index 000000000..b155ddcfb --- /dev/null +++ b/tests/whenstmt/twhen.nim @@ -0,0 +1,47 @@ +discard """ + nimout: ''' +nimvm - when +nimvm - whenElif +nimvm - whenElse +''' + output: ''' +when +whenElif +whenElse +''' +""" + +# test both when and when nimvm to ensure proper evaluation + +proc compileOrRuntimeProc(s: string) = + when nimvm: + echo "nimvm - " & s + else: + echo s + +template output(s: string) = + static: + compileOrRuntimeProc(s) + compileOrRuntimeProc(s) + +when compiles(1): + output("when") +elif compiles(2): + output("fail - whenElif") +else: + output("fail - whenElse") + +when compiles(nonexistent): + output("fail - when") +elif compiles(1): + output("whenElif") +else: + output("fail - whenElse") + +when compiles(nonexistent): + output("fail - when") +elif compiles(nonexistent): + output("fail - whenElif") +else: + output("whenElse") + diff --git a/tests/whenstmt/twhen_macro.nim b/tests/whenstmt/twhen_macro.nim new file mode 100644 index 000000000..deb1dddc9 --- /dev/null +++ b/tests/whenstmt/twhen_macro.nim @@ -0,0 +1,18 @@ +import macros + +# test that when stmt works from within a macro + +macro output(s: string, xs: varargs[untyped]): auto = + result = quote do: + when compiles(`s`): + "when - " & `s` + elif compiles(`s`): + "elif - " & `s` + # should never get here so this should not break + broken.xs + else: + "else - " & `s` + # should never get here so this should not break + more.broken.xs + +doAssert output("test") == "when - test" \ No newline at end of file |