tmacros.nim 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #[
  2. xxx macros tests need to be reorganized to makes sure each API is tested once
  3. See also:
  4. tests/macros/tdumpast.nim for treeRepr + friends
  5. ]#
  6. import std/macros
  7. import std/assertions
  8. block: # hasArgOfName
  9. macro m(u: untyped): untyped =
  10. for name in ["s","i","j","k","b","xs","ys"]:
  11. doAssert hasArgOfName(params u,name)
  12. doAssert not hasArgOfName(params u,"nonexistent")
  13. proc p(s: string; i,j,k: int; b: bool; xs,ys: seq[int] = @[]) {.m.} = discard
  14. block: # bug #17454
  15. proc f(v: NimNode): string {.raises: [].} = $v
  16. block: # unpackVarargs
  17. block:
  18. proc bar1(a: varargs[int]): string =
  19. for ai in a: result.add " " & $ai
  20. proc bar2(a: varargs[int]) =
  21. let s1 = bar1(a)
  22. let s2 = unpackVarargs(bar1, a) # `unpackVarargs` makes no difference here
  23. doAssert s1 == s2
  24. bar2(1, 2, 3)
  25. bar2(1)
  26. bar2()
  27. block:
  28. template call1(fun: typed; args: varargs[untyped]): untyped =
  29. unpackVarargs(fun, args)
  30. template call2(fun: typed; args: varargs[untyped]): untyped =
  31. # fun(args) # works except for last case with empty `args`, pending bug #9996
  32. when varargsLen(args) > 0: fun(args)
  33. else: fun()
  34. proc fn1(a = 0, b = 1) = discard (a, b)
  35. call1(fn1)
  36. call1(fn1, 10)
  37. call1(fn1, 10, 11)
  38. call2(fn1)
  39. call2(fn1, 10)
  40. call2(fn1, 10, 11)
  41. block:
  42. template call1(fun: typed; args: varargs[typed]): untyped =
  43. unpackVarargs(fun, args)
  44. template call2(fun: typed; args: varargs[typed]): untyped =
  45. # xxx this would give a confusing error message:
  46. # required type for a: varargs[typed] [varargs] but expression '[10]' is of type: varargs[typed] [varargs]
  47. when varargsLen(args) > 0: fun(args)
  48. else: fun()
  49. macro toString(a: varargs[typed, `$`]): string =
  50. var msg = genSym(nskVar, "msg")
  51. result = newStmtList()
  52. result.add quote do:
  53. var `msg` = ""
  54. for ai in a:
  55. result.add quote do: `msg`.add $`ai`
  56. result.add quote do: `msg`
  57. doAssert call1(toString) == ""
  58. doAssert call1(toString, 10) == "10"
  59. doAssert call1(toString, 10, 11) == "1011"
  60. block: # SameType
  61. type
  62. A = int
  63. B = distinct int
  64. C = object
  65. Generic[T, Y] = object
  66. macro isSameType(a, b: typed): untyped =
  67. newLit(sameType(a, b))
  68. static:
  69. assert Generic[int, int].isSameType(Generic[int, int])
  70. assert Generic[A, string].isSameType(Generic[int, string])
  71. assert not Generic[A, string].isSameType(Generic[B, string])
  72. assert not Generic[int, string].isSameType(Generic[int, int])
  73. assert isSameType(int, A)
  74. assert isSameType(10, 20)
  75. assert isSameType("Hello", "world")
  76. assert not isSameType("Hello", cstring"world")
  77. assert not isSameType(int, B)
  78. assert not isSameType(int, Generic[int, int])
  79. assert not isSameType(C, string)
  80. assert not isSameType(C, int)
  81. #[
  82. # compiler sameType fails for the following, read more in `types.nim`'s `sameTypeAux`.
  83. type
  84. D[T] = C
  85. G[T] = T
  86. static:
  87. assert isSameType(D[int], C)
  88. assert isSameType(D[int], D[float])
  89. assert isSameType(G[float](1.0), float(1.0))
  90. assert isSameType(float(1.0), G[float](1.0))
  91. ]#
  92. type Tensor[T] = object
  93. data: T
  94. macro testTensorInt(x: typed): untyped =
  95. let
  96. tensorIntType = getTypeInst(Tensor[int])[1]
  97. xTyp = x.getTypeInst
  98. newLit(xTyp.sameType(tensorIntType))
  99. var
  100. x: Tensor[int]
  101. x1 = Tensor[float]()
  102. x2 = Tensor[A]()
  103. x3 = Tensor[B]()
  104. static:
  105. assert testTensorInt(x)
  106. assert not testTensorInt(x1)
  107. assert testTensorInt(x2)
  108. assert not testTensorInt(x3)
  109. block: # extractDocCommentsAndRunnables
  110. macro checkRunnables(prc: untyped) =
  111. let runnables = prc.body.extractDocCommentsAndRunnables()
  112. doAssert runnables[0][0].eqIdent("runnableExamples")
  113. macro checkComments(comment: static[string], prc: untyped) =
  114. let comments = prc.body.extractDocCommentsAndRunnables()
  115. doAssert comments[0].strVal == comment
  116. proc a() {.checkRunnables.} =
  117. runnableExamples: discard
  118. discard
  119. proc b() {.checkRunnables.} =
  120. runnableExamples "-d:ssl": discard
  121. discard
  122. proc c() {.checkComments("Hello world").} =
  123. ## Hello world
  124. block: # bug #19020
  125. type
  126. foo = object
  127. template typ(T:typedesc) {.pragma.}
  128. proc bar() {.typ: foo.} = discard
  129. static:
  130. doAssert $bar.getCustomPragmaVal(typ) == "foo"
  131. doAssert $bar.getCustomPragmaVal(typ) == "foo"