tunittest.nim 4.1 KB

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