summary refs log tree commit diff stats
path: root/tests/pragmas
diff options
context:
space:
mode:
authorRSDuck <RSDuck@users.noreply.github.com>2018-04-14 08:33:36 +0200
committerAndreas Rumpf <rumpf_a@web.de>2018-04-14 08:33:36 +0200
commit3d1d163efff446bd420a150114b7914c7b34798e (patch)
tree52f6fcdc1d054371c6dd81e81a99547728cf87ec /tests/pragmas
parent116e984a6bddc15598867d0f2132f7bede70c4d5 (diff)
downloadNim-3d1d163efff446bd420a150114b7914c7b34798e.tar.gz
fixes #7451 (#7575)
Diffstat (limited to 'tests/pragmas')
-rw-r--r--tests/pragmas/tcustom_pragma.nim101
1 files changed, 90 insertions, 11 deletions
diff --git a/tests/pragmas/tcustom_pragma.nim b/tests/pragmas/tcustom_pragma.nim
index 415ae6a32..28a8713ce 100644
--- a/tests/pragmas/tcustom_pragma.nim
+++ b/tests/pragmas/tcustom_pragma.nim
@@ -1,12 +1,12 @@
 import macros
- 
+
 block:
   template myAttr() {.pragma.}
 
   proc myProc():int {.myAttr.} = 2
-  const myAttrIdx = myProc.hasCustomPragma(myAttr)
-  static: 
-    assert(myAttrIdx)
+  const hasMyAttr = myProc.hasCustomPragma(myAttr)
+  static:
+    assert(hasMyAttr)
 
 block:
   template myAttr(a: string) {.pragma.}
@@ -14,14 +14,14 @@ block:
   type MyObj = object
     myField1, myField2 {.myAttr: "hi".}: int
   var o: MyObj
-  static: 
+  static:
     assert o.myField2.hasCustomPragma(myAttr)
     assert(not o.myField1.hasCustomPragma(myAttr))
 
-import custom_pragma 
+import custom_pragma
 block: # A bit more advanced case
-  type 
-    Subfield = object
+  type
+    Subfield {.defaultValue: "catman".} = object
       c {.serializationKey: "cc".}: float
 
     MySerializable = object
@@ -29,10 +29,9 @@ block: # A bit more advanced case
       b {.custom_pragma.defaultValue"hello".} : int
       field: Subfield
       d {.alternativeKey("df", 5).}: float
-      e {.alternativeKey(V = 5).}: seq[bool] 
-
+      e {.alternativeKey(V = 5).}: seq[bool]
 
-  proc myproc(x: int, s: string) {.alternativeKey(V = 5), serializationKey"myprocSS".} = 
+  proc myproc(x: int, s: string) {.alternativeKey(V = 5), serializationKey"myprocSS".} =
     echo x, s
 
 
@@ -51,3 +50,83 @@ block: # A bit more advanced case
   static: assert(procSerKey == "myprocSS")
 
   static: assert(hasCustomPragma(myproc, alternativeKey))
+
+  # pragma on an object
+  static:
+    assert Subfield.hasCustomPragma(defaultValue)
+    assert(Subfield.getCustomPragmaVal(defaultValue) == "catman")
+
+    assert hasCustomPragma(type(s.field), defaultValue)
+
+block: # ref types
+  type
+    Node = object of RootObj
+      left {.serializationKey:"l".}, right {.serializationKey:"r".}: NodeRef
+    NodeRef = ref Node
+    NodePtr = ptr Node
+
+    SpecialNodeRef = ref object of NodeRef
+      data {.defaultValue"none".}: string
+
+    MyFile {.defaultValue: "closed".} = ref object
+      path {.defaultValue: "invalid".}: string
+
+  var s = NodeRef()
+
+  const
+    leftSerKey = getCustomPragmaVal(s.left, serializationKey)
+    rightSerKey = getCustomPragmaVal(s.right, serializationKey)
+  static:
+    assert leftSerKey == "l"
+    assert rightSerKey == "r"
+
+  var specS = SpecialNodeRef()
+
+  const
+    dataDefVal = hasCustomPragma(specS.data, defaultValue)
+    specLeftSerKey = hasCustomPragma(specS.left, serializationKey)
+  static:
+    assert dataDefVal == true
+    assert specLeftSerKey == true
+
+  var ptrS = NodePtr(nil)
+  const
+    ptrRightSerKey = getCustomPragmaVal(s.right, serializationKey)
+  static:
+    assert ptrRightSerKey == "r"
+
+  var f = MyFile()
+  const
+    fileDefVal = f.getCustomPragmaVal(defaultValue)
+    filePathDefVal = f.path.getCustomPragmaVal(defaultValue)
+  static:
+    assert fileDefVal == "closed"
+    assert filePathDefVal == "invalid"
+
+block:
+  type
+    VariantKind = enum
+      variInt,
+      variFloat
+      variString
+      variNestedCase
+    Variant = object
+      case kind: VariantKind
+      of variInt: integer {.serializationKey: "int".}: BiggestInt
+      of variFloat: floatp: BiggestFloat
+      of variString: str {.serializationKey: "string".}: string
+      of variNestedCase:
+        case nestedKind: VariantKind
+        of variInt..variNestedCase: nestedItem {.defaultValue: "Nimmers of the world, unite!".}: int
+
+  let vari = Variant(kind: variInt)
+
+  const
+    hasIntSerKey = vari.integer.hasCustomPragma(serializationKey)
+    strSerKey = vari.str.getCustomPragmaVal(serializationKey)
+    nestedItemDefVal = vari.nestedItem.getCustomPragmaVal(defaultValue)
+
+  static:
+    assert hasIntSerKey
+    assert strSerKey == "string"
+    assert nestedItemDefVal == "Nimmers of the world, unite!"
\ No newline at end of file