2022-07-18 11:02:40 +00:00
|
|
|
# Copyright (c) 2020-2022 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license: http://opensource.org/licenses/MIT
|
|
|
|
# * Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
{.used.}
|
|
|
|
|
2020-05-26 15:42:33 +00:00
|
|
|
import
|
2022-07-08 09:34:21 +00:00
|
|
|
unittest2,
|
2020-05-26 15:42:33 +00:00
|
|
|
../stew/shims/macros
|
|
|
|
|
2021-02-10 13:10:08 +00:00
|
|
|
template unknown() {.pragma.}
|
|
|
|
template zero() {.pragma.}
|
|
|
|
template one(one: string) {.pragma.}
|
|
|
|
template two(one: string, two: string) {.pragma.}
|
|
|
|
|
2020-05-26 15:42:33 +00:00
|
|
|
type
|
2021-02-10 13:10:08 +00:00
|
|
|
MyType[T] = object
|
|
|
|
myField {.zero, one("foo"), two("foo", "bar")}: string
|
|
|
|
myGeneric {.zero.}: T
|
|
|
|
case kind {.zero.}: bool
|
|
|
|
of true:
|
|
|
|
first {.zero.}: string
|
|
|
|
else:
|
|
|
|
second {.zero.}: string
|
|
|
|
|
2020-05-26 15:42:33 +00:00
|
|
|
FieldKind = enum
|
|
|
|
KindA
|
|
|
|
KindB
|
|
|
|
|
|
|
|
BaseType = object of RootObj
|
|
|
|
baseField: int
|
|
|
|
case baseCaseField: FieldKind
|
|
|
|
of KindA:
|
|
|
|
baseA: int
|
|
|
|
of KindB:
|
|
|
|
discard
|
|
|
|
|
|
|
|
DerivedType = ref object of BaseType
|
|
|
|
derivedField: int
|
|
|
|
|
2020-05-27 08:30:56 +00:00
|
|
|
DerivedFromRefType = ref object of DerivedType
|
|
|
|
anotherDerivedField: string
|
|
|
|
|
2022-03-03 15:36:16 +00:00
|
|
|
EmptyObject = object
|
|
|
|
EmptyRefObject = ref object
|
|
|
|
|
2020-05-26 15:42:33 +00:00
|
|
|
macro getFieldsLists(T: type): untyped =
|
|
|
|
result = newTree(nnkBracket)
|
|
|
|
|
2022-03-03 15:36:16 +00:00
|
|
|
var resolvedType = skipPtr skipRef getType(T)[1]
|
2020-05-26 15:42:33 +00:00
|
|
|
doAssert resolvedType.kind == nnkSym
|
|
|
|
var objectType = getImpl(resolvedType)
|
|
|
|
doAssert objectType.kind == nnkTypeDef
|
|
|
|
|
|
|
|
for f in recordFields(objectType):
|
|
|
|
result.add newLit($f.name)
|
|
|
|
|
|
|
|
static:
|
2020-05-27 08:30:56 +00:00
|
|
|
doAssert getFieldsLists(DerivedFromRefType) == [
|
2020-05-26 15:42:33 +00:00
|
|
|
"baseField",
|
|
|
|
"baseCaseField",
|
|
|
|
"baseA",
|
2020-05-27 08:30:56 +00:00
|
|
|
"derivedField",
|
|
|
|
"anotherDerivedField"
|
2020-05-26 15:42:33 +00:00
|
|
|
]
|
|
|
|
|
2022-03-03 15:36:16 +00:00
|
|
|
doAssert getFieldsLists(EmptyObject).len == 0
|
|
|
|
doAssert getFieldsLists(EmptyRefObject).len == 0
|
|
|
|
|
2021-02-10 13:10:08 +00:00
|
|
|
let myType = MyType[string](myField: "test", myGeneric: "test", kind: true, first: "test")
|
|
|
|
|
|
|
|
suite "Macros":
|
|
|
|
test "hasCustomPragmaFixed":
|
|
|
|
check:
|
|
|
|
not myType.type.hasCustomPragmaFixed("myField", unknown)
|
|
|
|
myType.type.hasCustomPragmaFixed("myField", zero)
|
|
|
|
myType.type.hasCustomPragmaFixed("myField", one)
|
|
|
|
myType.type.hasCustomPragmaFixed("myField", two)
|
|
|
|
|
|
|
|
myType.type.hasCustomPragmaFixed("myGeneric", zero)
|
|
|
|
myType.type.hasCustomPragmaFixed("kind", zero)
|
|
|
|
myType.type.hasCustomPragmaFixed("first", zero)
|
|
|
|
myType.type.hasCustomPragmaFixed("second", zero)
|
|
|
|
|
|
|
|
test "getCustomPragmaFixed":
|
|
|
|
check:
|
|
|
|
myType.type.getCustomPragmaFixed("myField", unknown).isNil
|
|
|
|
myType.type.getCustomPragmaFixed("myField", zero).isNil
|
|
|
|
myType.type.getCustomPragmaFixed("myField", one) is string
|
|
|
|
myType.type.getCustomPragmaFixed("myField", two) is tuple[one: string, two: string]
|
|
|
|
|
|
|
|
myType.type.getCustomPragmaFixed("myGeneric", zero).isNil
|
|
|
|
myType.type.getCustomPragmaFixed("kind", zero).isNil
|
|
|
|
myType.type.getCustomPragmaFixed("first", zero).isNil
|
|
|
|
myType.type.getCustomPragmaFixed("second", zero).isNil
|