tunittest.nim 4.2 KB

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