summary refs log tree commit diff stats
path: root/tests/pragmas/tcustom_pragma.nim
diff options
context:
space:
mode:
Diffstat (limited to 'tests/pragmas/tcustom_pragma.nim')
-rw-r--r--tests/pragmas/tcustom_pragma.nim43
1 files changed, 43 insertions, 0 deletions
diff --git a/tests/pragmas/tcustom_pragma.nim b/tests/pragmas/tcustom_pragma.nim
new file mode 100644
index 000000000..a2380522f
--- /dev/null
+++ b/tests/pragmas/tcustom_pragma.nim
@@ -0,0 +1,43 @@
+import macros
+ 
+block:
+  template myAttr() {.pragma.}
+
+  proc myProc():int {.myAttr.} = 2
+  const myAttrIdx = myProc.hasCustomPragma(myAttr)
+  static: 
+    assert(myAttrIdx)
+
+block:
+  template myAttr(a: string) {.pragma.}
+
+  type MyObj = object
+    myField1, myField2 {.myAttr: "hi".}: int
+  var o: MyObj
+  static: 
+    assert o.myField2.hasCustomPragma(myAttr)
+    assert(not o.myField1.hasCustomPragma(myAttr))
+
+import custom_pragma 
+block: # A bit more advanced case
+  type 
+    Subfield = object
+      c {.serializationKey: "cc".}: float
+
+    MySerializable = object
+      a {.serializationKey"asdf", defaultValue: 5.} : int
+      b {.custom_pragma.defaultValue"hello".} : int
+      field: Subfield
+      d {.alternativeKey("df", 5).}: float
+      e {.alternativeKey(V = 5).}: seq[bool] 
+
+  var s: MySerializable
+
+  const aDefVal = s.a.getCustomPragmaVal(defaultValue)
+  static: assert(aDefVal == 5)
+
+  const aSerKey = s.a.getCustomPragmaVal(serializationKey)
+  static: assert(aSerKey == "asdf")
+
+  const cSerKey = getCustomPragmaVal(s.field.c, serializationKey)
+  static: assert(cSerKey == "cc")