diff options
author | Dankr4d <dude569@freenet.de> | 2021-08-25 19:27:00 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-25 17:27:00 +0000 |
commit | c70e4040bd69e1ee32b6c676dbfc763c7cf4df37 (patch) | |
tree | a0265f944fac5a46d14cac1a65808d540ffefb98 /tests | |
parent | 3aa16c1de00c723d48e48fe3fdf07a276d1b4b6a (diff) | |
download | Nim-c70e4040bd69e1ee32b6c676dbfc763c7cf4df37.tar.gz |
fixes #14511 [backport:1.4] (#18732)
* fixes #14511 [backport:1.4] Signed-off-by: Dankr4d <dude569@freenet.de> * Replaced fix with code from alaviss, for better readability, with small changes. Signed-off-by: Dankr4d <dude569@freenet.de> * - Specified output in test. Signed-off-by: Dankr4d <dude569@freenet.de> * Replaced case in nnkRecCase with a simpler version, which just adds the last son. Signed-off-by: Dankr4d <dude569@freenet.de> * Update tests/macros/t14511.nim * Update tests/macros/t14511.nim Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/macros/t14511.nim | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/tests/macros/t14511.nim b/tests/macros/t14511.nim new file mode 100644 index 000000000..f3b1e2894 --- /dev/null +++ b/tests/macros/t14511.nim @@ -0,0 +1,54 @@ +discard """ + output: "true\n(y: XInt, a: 5)\n(y: XString, b: \"abc\")" +""" + +import macros + +block TEST_1: + # https://github.com/nim-lang/Nim/issues/14511 + + template myPragma() {.pragma.} + + type + XType = enum + XInt, + XString, + XUnused + X = object + case y {.myPragma.}: XType + of XInt, XUnused: + a: int + else: # <-- Else case caused the "Error: index 1 not in 0 .. 0" error + b: string + + var x: X = X(y: XInt, a: 5) + echo x.y.hasCustomPragma(myPragma) + echo x + echo X(y: XString, b: "abc") + + +block TEST_2: + template myDevice(val: string) {.pragma.} + template myKey(val: string) {.pragma.} + template myMouse(val: string) {.pragma.} + + type + Device {.pure.} = enum Keyboard, Mouse + Key = enum Key1, Key2 + Mouse = enum Mouse1, Mouse2 + + type + Obj = object of RootObj + case device {.myDevice: "MyDevicePragmaStr".}: Device + of Device.Keyboard: + key {.myKey: "MyKeyPragmaStr".}: Key + else: # <-- Else case caused the "Error: index 1 not in 0 .. 0" error + mouse {.myMouse: "MyMousePragmaStr".}: Mouse + + var obj: Obj + assert obj.device.hasCustomPragma(myDevice) == true + assert obj.key.hasCustomPragma(myKey) == true + assert obj.mouse.hasCustomPragma(myMouse) == true + assert obj.device.getCustomPragmaVal(myDevice) == "MyDevicePragmaStr" + assert obj.key.getCustomPragmaVal(myKey) == "MyKeyPragmaStr" + assert obj.mouse.getCustomPragmaVal(myMouse) == "MyMousePragmaStr" \ No newline at end of file |