tunittest.nim 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. discard """
  2. output: '''
  3. [Suite] suite with only teardown
  4. [Suite] suite with only setup
  5. [Suite] suite with none
  6. [Suite] suite with both
  7. [Suite] bug #4494
  8. [Suite] bug #5571
  9. [Suite] bug #5784
  10. [Suite] test suite
  11. [Suite] test name filtering
  12. '''
  13. matrix: "--mm:refc; --mm:orc"
  14. targets: "c js"
  15. """
  16. import std/[unittest, sequtils, assertions]
  17. from std/unittest {.all.} import matchFilter
  18. proc doThings(spuds: var int): int =
  19. spuds = 24
  20. return 99
  21. test "#964":
  22. var spuds = 0
  23. check doThings(spuds) == 99
  24. check spuds == 24
  25. from std/strutils import toUpperAscii
  26. test "#1384":
  27. check(@["hello", "world"].map(toUpperAscii) == @["HELLO", "WORLD"])
  28. import std/options
  29. test "unittest typedescs":
  30. check(none(int) == none(int))
  31. check(none(int) != some(1))
  32. test "unittest multiple requires":
  33. require(true)
  34. require(true)
  35. import std/random
  36. from std/strutils import parseInt
  37. proc defectiveRobot() =
  38. case rand(1..4)
  39. of 1: raise newException(OSError, "CANNOT COMPUTE!")
  40. of 2: discard parseInt("Hello World!")
  41. of 3: raise newException(IOError, "I can't do that Dave.")
  42. else: doAssert 2 + 2 == 5
  43. test "unittest expect":
  44. expect IOError, OSError, ValueError, AssertionDefect:
  45. defectiveRobot()
  46. var
  47. a = 1
  48. b = -1
  49. c = 1
  50. #unittests are sequential right now
  51. suite "suite with only teardown":
  52. teardown:
  53. b = 2
  54. test "unittest with only teardown 1":
  55. check a == c
  56. test "unittest with only teardown 2":
  57. check b > a
  58. suite "suite with only setup":
  59. setup:
  60. var testVar {.used.} = "from setup"
  61. test "unittest with only setup 1":
  62. check testVar == "from setup"
  63. check b > a
  64. b = -1
  65. test "unittest with only setup 2":
  66. check b < a
  67. suite "suite with none":
  68. test "unittest with none":
  69. check b < a
  70. suite "suite with both":
  71. setup:
  72. a = -2
  73. teardown:
  74. c = 2
  75. test "unittest with both 1":
  76. check b > a
  77. test "unittest with both 2":
  78. check c == 2
  79. suite "bug #4494":
  80. test "Uniqueness check":
  81. var tags = @[1, 2, 3, 4, 5]
  82. check:
  83. allIt(0..3, tags[it] != tags[it + 1])
  84. suite "bug #5571":
  85. test "can define gcsafe procs within tests":
  86. proc doTest {.gcsafe.} =
  87. let line = "a"
  88. check: line == "a"
  89. doTest()
  90. suite "bug #5784":
  91. test "`or` should short circuit":
  92. type Obj = ref object
  93. field: int
  94. var obj: Obj
  95. check obj.isNil or obj.field == 0
  96. type
  97. SomeType = object
  98. value: int
  99. children: seq[SomeType]
  100. # bug #5252
  101. proc `==`(a, b: SomeType): bool =
  102. return a.value == b.value
  103. suite "test suite":
  104. test "test":
  105. let a = SomeType(value: 10)
  106. let b = SomeType(value: 10)
  107. check(a == b)
  108. suite "test name filtering":
  109. test "test name":
  110. check matchFilter("suite1", "foo", "")
  111. check matchFilter("suite1", "foo", "foo")
  112. check matchFilter("suite1", "foo", "::")
  113. check matchFilter("suite1", "foo", "*")
  114. check matchFilter("suite1", "foo", "::foo")
  115. check matchFilter("suite1", "::foo", "::foo")
  116. test "test name - glob":
  117. check matchFilter("suite1", "foo", "f*")
  118. check matchFilter("suite1", "foo", "*oo")
  119. check matchFilter("suite1", "12345", "12*345")
  120. check matchFilter("suite1", "q*wefoo", "q*wefoo")
  121. check false == matchFilter("suite1", "foo", "::x")
  122. check false == matchFilter("suite1", "foo", "::x*")
  123. check false == matchFilter("suite1", "foo", "::*x")
  124. # overlap
  125. check false == matchFilter("suite1", "12345", "123*345")
  126. check matchFilter("suite1", "ab*c::d*e::f", "ab*c::d*e::f")
  127. test "suite name":
  128. check matchFilter("suite1", "foo", "suite1::")
  129. check false == matchFilter("suite1", "foo", "suite2::")
  130. check matchFilter("suite1", "qwe::foo", "qwe::foo")
  131. check matchFilter("suite1", "qwe::foo", "suite1::qwe::foo")
  132. test "suite name - glob":
  133. check matchFilter("suite1", "foo", "::*")
  134. check matchFilter("suite1", "foo", "*::*")
  135. check matchFilter("suite1", "foo", "*::foo")
  136. check false == matchFilter("suite1", "foo", "*ite2::")
  137. check matchFilter("suite1", "q**we::foo", "q**we::foo")
  138. check matchFilter("suite1", "a::b*c::d*e", "a::b*c::d*e")
  139. block:
  140. type MyFoo = object
  141. var obj = MyFoo()
  142. let check = 1
  143. check(obj == obj)
  144. block:
  145. let check = 123
  146. var a = 1
  147. var b = 1
  148. check(a == b)