tunittest.nim 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. """
  14. import unittest, sequtils
  15. proc doThings(spuds: var int): int =
  16. spuds = 24
  17. return 99
  18. test "#964":
  19. var spuds = 0
  20. check doThings(spuds) == 99
  21. check spuds == 24
  22. from strutils import toUpperAscii
  23. test "#1384":
  24. check(@["hello", "world"].map(toUpperAscii) == @["HELLO", "WORLD"])
  25. import options
  26. test "unittest typedescs":
  27. check(none(int) == none(int))
  28. check(none(int) != some(1))
  29. test "unittest multiple requires":
  30. require(true)
  31. require(true)
  32. import random
  33. from strutils import parseInt
  34. proc defectiveRobot() =
  35. case rand(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, AssertionDefect:
  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. suite "test name filtering":
  106. test "test name":
  107. check matchFilter("suite1", "foo", "")
  108. check matchFilter("suite1", "foo", "foo")
  109. check matchFilter("suite1", "foo", "::")
  110. check matchFilter("suite1", "foo", "*")
  111. check matchFilter("suite1", "foo", "::foo")
  112. check matchFilter("suite1", "::foo", "::foo")
  113. test "test name - glob":
  114. check matchFilter("suite1", "foo", "f*")
  115. check matchFilter("suite1", "foo", "*oo")
  116. check matchFilter("suite1", "12345", "12*345")
  117. check matchFilter("suite1", "q*wefoo", "q*wefoo")
  118. check false == matchFilter("suite1", "foo", "::x")
  119. check false == matchFilter("suite1", "foo", "::x*")
  120. check false == matchFilter("suite1", "foo", "::*x")
  121. # overlap
  122. check false == matchFilter("suite1", "12345", "123*345")
  123. check matchFilter("suite1", "ab*c::d*e::f", "ab*c::d*e::f")
  124. test "suite name":
  125. check matchFilter("suite1", "foo", "suite1::")
  126. check false == matchFilter("suite1", "foo", "suite2::")
  127. check matchFilter("suite1", "qwe::foo", "qwe::foo")
  128. check matchFilter("suite1", "qwe::foo", "suite1::qwe::foo")
  129. test "suite name - glob":
  130. check matchFilter("suite1", "foo", "::*")
  131. check matchFilter("suite1", "foo", "*::*")
  132. check matchFilter("suite1", "foo", "*::foo")
  133. check false == matchFilter("suite1", "foo", "*ite2::")
  134. check matchFilter("suite1", "q**we::foo", "q**we::foo")
  135. check matchFilter("suite1", "a::b*c::d*e", "a::b*c::d*e")