tdefine.nim 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. discard """
  2. joinable: false
  3. cmd: "nim c $options -d:booldef -d:booldef2=false -d:intdef=2 -d:strdef=foobar -d:namespaced.define=false -d:double.namespaced.define -r $file"
  4. matrix: "; -d:useGenericDefine"
  5. """
  6. when defined(useGenericDefine):
  7. {.pragma: booldefine2, define.}
  8. {.pragma: intdefine2, define.}
  9. {.pragma: strdefine2, define.}
  10. else:
  11. {.pragma: booldefine2, booldefine.}
  12. {.pragma: intdefine2, intdefine.}
  13. {.pragma: strdefine2, strdefine.}
  14. const booldef {.booldefine2.} = false
  15. const booldef2 {.booldefine2.} = true
  16. const intdef {.intdefine2.} = 0
  17. const strdef {.strdefine2.} = ""
  18. doAssert defined(booldef)
  19. doAssert defined(booldef2)
  20. doAssert defined(intdef)
  21. doAssert defined(strdef)
  22. doAssert booldef
  23. doAssert not booldef2
  24. doAssert intdef == 2
  25. doAssert strdef == "foobar"
  26. when defined(useGenericDefine):
  27. block:
  28. const uintdef {.define: "intdef".}: uint = 17
  29. doAssert intdef == int(uintdef)
  30. const cstrdef {.define: "strdef".}: cstring = "not strdef"
  31. doAssert $cstrdef == strdef
  32. type FooBar = enum foo, bar, foobar
  33. const enumdef {.define: "strdef".} = foo
  34. doAssert $enumdef == strdef
  35. doAssert enumdef == foobar
  36. # Intentionally not defined from command line
  37. const booldef3 {.booldefine2.} = true
  38. const intdef2 {.intdefine2.} = 1
  39. const strdef2 {.strdefine2.} = "abc"
  40. type T = object
  41. when booldef3:
  42. field1: int
  43. when intdef2 == 1:
  44. field2: int
  45. when strdef2 == "abc":
  46. field3: int
  47. doAssert not defined(booldef3)
  48. doAssert not defined(intdef2)
  49. doAssert not defined(strdef2)
  50. discard T(field1: 1, field2: 2, field3: 3)
  51. doAssert defined(namespaced.define)
  52. const `namespaced.define` {.booldefine2.} = true
  53. doAssert not `namespaced.define`
  54. when defined(useGenericDefine):
  55. const aliasToNamespacedDefine {.define: "namespaced.define".} = not `namespaced.define`
  56. else:
  57. const aliasToNamespacedDefine {.booldefine: "namespaced.define".} = not `namespaced.define`
  58. doAssert aliasToNamespacedDefine == `namespaced.define`
  59. doAssert defined(double.namespaced.define)
  60. const `double.namespaced.define` {.booldefine2.} = false
  61. doAssert `double.namespaced.define`
  62. when defined(useGenericDefine):
  63. const aliasToDoubleNamespacedDefine {.define: "double.namespaced.define".} = not `double.namespaced.define`
  64. else:
  65. const aliasToDoubleNamespacedDefine {.booldefine: "double.namespaced.define".} = not `double.namespaced.define`
  66. doAssert aliasToDoubleNamespacedDefine == `double.namespaced.define`
  67. doAssert not defined(namespaced.butnotdefined)