tunittest.nim 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. discard """
  2. output: '''[Suite] suite with only teardown
  3. [Suite] suite with only setup
  4. [Suite] suite with none
  5. [Suite] suite with both
  6. [Suite] bug #4494
  7. [Suite] bug #5571
  8. [Suite] bug #5784
  9. [Suite] test suite
  10. [Suite] test name filtering
  11. '''
  12. """
  13. import unittest, sequtils
  14. proc doThings(spuds: var int): int =
  15. spuds = 24
  16. return 99
  17. test "#964":
  18. var spuds = 0
  19. check doThings(spuds) == 99
  20. check spuds == 24
  21. from strutils import toUpperAscii
  22. test "#1384":
  23. check(@["hello", "world"].map(toUpperAscii) == @["HELLO", "WORLD"])
  24. import options
  25. test "unittest typedescs":
  26. check(none(int) == none(int))
  27. check(none(int) != some(1))
  28. test "unittest multiple requires":
  29. require(true)
  30. require(true)
  31. import math, random
  32. from strutils import parseInt
  33. proc defectiveRobot() =
  34. randomize()
  35. case random(1..4)
  36. of 1: raise newException(OSError, "CANNOT COMPUTE!")
  37. of 2: discard parseInt("Hello World!")
  38. of 3: raise newException(IOError, "I can't do that Dave.")
  39. else: assert 2 + 2 == 5
  40. test "unittest expect":
  41. expect IOError, OSError, ValueError, AssertionError:
  42. defectiveRobot()
  43. var
  44. a = 1
  45. b = -1
  46. c = 1
  47. #unittests are sequential right now
  48. suite "suite with only teardown":
  49. teardown:
  50. b = 2
  51. test "unittest with only teardown 1":
  52. check a == c
  53. test "unittest with only teardown 2":
  54. check b > a
  55. suite "suite with only setup":
  56. setup:
  57. var testVar {.used.} = "from setup"
  58. test "unittest with only setup 1":
  59. check testVar == "from setup"
  60. check b > a
  61. b = -1
  62. test "unittest with only setup 2":
  63. check b < a
  64. suite "suite with none":
  65. test "unittest with none":
  66. check b < a
  67. suite "suite with both":
  68. setup:
  69. a = -2
  70. teardown:
  71. c = 2
  72. test "unittest with both 1":
  73. check b > a
  74. test "unittest with both 2":
  75. check c == 2
  76. suite "bug #4494":
  77. test "Uniqueness check":
  78. var tags = @[1, 2, 3, 4, 5]
  79. check:
  80. allIt(0..3, tags[it] != tags[it + 1])
  81. suite "bug #5571":
  82. test "can define gcsafe procs within tests":
  83. proc doTest {.gcsafe.} =
  84. let line = "a"
  85. check: line == "a"
  86. doTest()
  87. suite "bug #5784":
  88. test "`or` should short circuit":
  89. type Obj = ref object
  90. field: int
  91. var obj: Obj
  92. check obj.isNil or obj.field == 0
  93. type
  94. SomeType = object
  95. value: int
  96. children: seq[SomeType]
  97. # bug #5252
  98. proc `==`(a, b: SomeType): bool =
  99. return a.value == b.value
  100. suite "test suite":
  101. test "test":
  102. let a = SomeType(value: 10)
  103. let b = SomeType(value: 10)
  104. check(a == b)
  105. when defined(testing):
  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")