timportutils.nim 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import std/[importutils, assertions]
  2. import stdtest/testutils
  3. import mimportutils
  4. template main =
  5. block: # privateAccess
  6. assertAll:
  7. var a: A
  8. var b = initB() # B is private
  9. compiles(a.a0)
  10. compiles(b.b0)
  11. not compiles(a.ha1)
  12. not compiles(b.hb1)
  13. block:
  14. assertAll:
  15. privateAccess A
  16. compiles(a.ha1)
  17. a.ha1 == 0.0
  18. not compiles(a.hb1)
  19. privateAccess b.typeof
  20. b.hb1 = 3
  21. type B2 = b.typeof
  22. let b2 = B2(b0: 4, hb1: 5)
  23. b.hb1 == 3
  24. b2 == B2(b0: 4, hb1: 5)
  25. assertAll:
  26. not compiles(a.ha1)
  27. not compiles(b.hb1)
  28. block:
  29. assertAll:
  30. not compiles(C(c0: 1, hc1: 2))
  31. privateAccess C
  32. let c = C(c0: 1, hc1: 2)
  33. c.hc1 == 2
  34. block:
  35. assertAll:
  36. not compiles(E[int](he1: 1))
  37. privateAccess E[int]
  38. var e = E[int](he1: 1)
  39. e.he1 == 1
  40. e.he1 = 2
  41. e.he1 == 2
  42. e.he1 += 3
  43. e.he1 == 5
  44. # xxx caveat: this currently compiles but in future, we may want
  45. # to make `privateAccess E[int]` only affect a specific instantiation;
  46. # note that `privateAccess E` does work to cover all instantiations.
  47. var e2 = E[float](he1: 1)
  48. block:
  49. assertAll:
  50. not compiles(E[int](he1: 1))
  51. privateAccess E
  52. var e = E[int](he1: 1)
  53. e.he1 == 1
  54. block:
  55. assertAll:
  56. not compiles(F[int, int](h3: 1))
  57. privateAccess F[int, int]
  58. var e = F[int, int](h3: 1)
  59. e.h3 == 1
  60. block:
  61. assertAll:
  62. not compiles(F[int, int](h3: 1))
  63. privateAccess F[int, int].default[].typeof
  64. var e = F[int, int](h3: 1)
  65. e.h3 == 1
  66. block:
  67. assertAll:
  68. var a = G[int]()
  69. var b = a.addr
  70. privateAccess b.type
  71. discard b.he1
  72. discard b[][].he1
  73. block:
  74. assertAll:
  75. privateAccess H[int]
  76. var a = H[int](h5: 2)
  77. block:
  78. assertAll:
  79. privateAccess PA
  80. var pa = PA(a0: 1, ha1: 2)
  81. pa.ha1 == 2
  82. pa.ha1 = 3
  83. pa.ha1 == 3
  84. block:
  85. assertAll:
  86. var b = BAalias()
  87. not compiles(b.hb1)
  88. privateAccess BAalias
  89. discard b.hb1
  90. block:
  91. assertAll:
  92. var a = A(a0: 1)
  93. var a2 = a.addr
  94. not compiles(a2.ha1)
  95. privateAccess PtA
  96. a2.type is PtA
  97. a2.ha1 = 2
  98. a2.ha1 == 2
  99. a.ha1 = 3
  100. a2.ha1 == 3
  101. block:
  102. disableVm:
  103. assertAll:
  104. var a = A.create()
  105. defer: dealloc(a)
  106. a is PtA
  107. a.typeof is PtA
  108. not compiles(a.ha1)
  109. privateAccess a.typeof
  110. a.ha1 = 2
  111. a.ha1 == 2
  112. a[].ha1 = 3
  113. a.ha1 == 3
  114. block:
  115. disableVm:
  116. assertAll:
  117. var a = A.create()
  118. defer: dealloc(a)
  119. privateAccess PtA
  120. a.ha1 == 0
  121. block:
  122. privateAccess PityRef
  123. let x = PityRef[int](a: 1) # works
  124. doAssert x.a == 1
  125. privateAccess Hope
  126. let y = Hope[int](a: 1)
  127. doAssert y.a == 1
  128. static: main()
  129. main()