Add tests for recordFields being used with empty objects

This commit is contained in:
Zahary Karadjov 2022-03-03 17:36:16 +02:00
parent bb705bf17b
commit b87fd80b0f
No known key found for this signature in database
GPG Key ID: C8936F8A3073D609
2 changed files with 16 additions and 4 deletions

View File

@ -76,9 +76,13 @@ macro isTuple*(T: type): untyped =
proc skipRef*(T: NimNode): NimNode =
result = T
if T.kind == nnkBracketExpr:
if eqIdent(T[0], bindSym"ref"):
result = T[1]
if T.kind == nnkBracketExpr and eqIdent(T[0], "ref"):
result = T[1]
proc skipPtr*(T: NimNode): NimNode =
result = T
if T.kind == nnkBracketExpr and eqIdent(T[0], "ptr"):
result = T[1]
template readPragma*(field: FieldDescription, pragmaName: static string): NimNode =
let p = findPragma(field.pragmas, bindSym(pragmaName))

View File

@ -36,10 +36,14 @@ type
DerivedFromRefType = ref object of DerivedType
anotherDerivedField: string
EmptyObject = object
EmptyRefObject = ref object
EmptyPtrObject = ptr object
macro getFieldsLists(T: type): untyped =
result = newTree(nnkBracket)
var resolvedType = skipRef getType(T)[1]
var resolvedType = skipPtr skipRef getType(T)[1]
doAssert resolvedType.kind == nnkSym
var objectType = getImpl(resolvedType)
doAssert objectType.kind == nnkTypeDef
@ -56,6 +60,10 @@ static:
"anotherDerivedField"
]
doAssert getFieldsLists(EmptyObject).len == 0
doAssert getFieldsLists(EmptyRefObject).len == 0
doAssert getFieldsLists(EmptyPtrObject).len == 0
let myType = MyType[string](myField: "test", myGeneric: "test", kind: true, first: "test")
suite "Macros":