tunittest.nim 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 random
  32. from strutils import parseInt
  33. proc defectiveRobot() =
  34. case rand(1..4)
  35. of 1: raise newException(OSError, "CANNOT COMPUTE!")
  36. of 2: discard parseInt("Hello World!")
  37. of 3: raise newException(IOError, "I can't do that Dave.")
  38. else: assert 2 + 2 == 5
  39. test "unittest expect":
  40. expect IOError, OSError, ValueError, AssertionDefect:
  41. defectiveRobot()
  42. var
  43. a = 1
  44. b = -1
  45. c = 1
  46. #unittests are sequential right now
  47. suite "suite with only teardown":
  48. teardown:
  49. b = 2
  50. test "unittest with only teardown 1":
  51. check a == c
  52. test "unittest with only teardown 2":
  53. check b > a
  54. suite "suite with only setup":
  55. setup:
  56. var testVar {.used.} = "from setup"
  57. test "unittest with only setup 1":
  58. check testVar == "from setup"
  59. check b > a
  60. b = -1
  61. test "unittest with only setup 2":
  62. check b < a
  63. suite "suite with none":
  64. test "unittest with none":
  65. check b < a
  66. suite "suite with both":
  67. setup:
  68. a = -2
  69. teardown:
  70. c = 2
  71. test "unittest with both 1":
  72. check b > a
  73. test "unittest with both 2":
  74. check c == 2
  75. suite "bug #4494":
  76. test "Uniqueness check":
  77. var tags = @[1, 2, 3, 4, 5]
  78. check:
  79. allIt(0..3, tags[it] != tags[it + 1])
  80. suite "bug #5571":
  81. test "can define gcsafe procs within tests":
  82. proc doTest {.gcsafe.} =
  83. let line = "a"
  84. check: line == "a"
  85. doTest()
  86. suite "bug #5784":
  87. test "`or` should short circuit":
  88. type Obj = ref object
  89. field: int
  90. var obj: Obj
  91. check obj.isNil or obj.field == 0
  92. type
  93. SomeType = object
  94. value: int
  95. children: seq[SomeType]
  96. # bug #5252
  97. proc `==`(a, b: SomeType): bool =
  98. return a.value == b.value
  99. suite "test suite":
  100. test "test":
  101. let a = SomeType(value: 10)
  102. let b = SomeType(value: 10)
  103. check(a == b)
  104. when defined(testing):
  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")