test.hoon 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. :: testing utilities meant to be directly used from files in %/tests
  2. ::
  3. |%
  4. :: +expect-eq: compares :expected and :actual and pretty-prints the result
  5. ::
  6. ++ expect-eq
  7. |= [expected=vase actual=vase]
  8. ^- tang
  9. ::
  10. =| result=tang
  11. ::
  12. =? result !=(q.expected q.actual)
  13. %+ weld result
  14. ^- tang
  15. :~ [%palm [": " ~ ~ ~] [leaf+"expected" (sell expected) ~]]
  16. [%palm [": " ~ ~ ~] [leaf+"actual " (sell actual) ~]]
  17. ==
  18. ::
  19. =? result !(~(nest ut p.actual) | p.expected)
  20. %+ weld result
  21. ^- tang
  22. :~ :+ %palm [": " ~ ~ ~]
  23. :~ [%leaf "failed to nest"]
  24. (~(dunk ut p.actual) %actual)
  25. (~(dunk ut p.expected) %expected)
  26. == ==
  27. result
  28. :: +expect: compares :actual to %.y and pretty-prints anything else
  29. ::
  30. ++ expect
  31. |= actual=vase
  32. (expect-eq !>(%.y) actual)
  33. :: +expect-fail: kicks a trap, expecting crash. pretty-prints if succeeds
  34. ::
  35. ++ expect-fail
  36. |= [a=(trap) err=(unit tape)]
  37. ^- tang
  38. =/ b (mule a)
  39. ?: ?=(%& -.b)
  40. =- (welp - ~[(sell !>(p.b))])
  41. ~['expected crash, got: ']
  42. ?~ err
  43. %. ~
  44. (%*(. slog pri 1) ['caught expected failure: ' p.b])
  45. =/ found=(unit tank)
  46. (find-tank p.b u.err)
  47. ?: ?=(^ found)
  48. %. ~
  49. (%*(. slog pri 1) ['caught expected failure: ' p.b])
  50. %+ weld
  51. ^- tang
  52. :~ [%palm [": " ~ ~ ~] [leaf+"expected" leaf+u.err ~]]
  53. [%palm [": " ~ ~ ~] [leaf+"actual " ~]]
  54. ==
  55. p.b
  56. ::
  57. :: +expect-runs: kicks a trap, expecting success; returns trace on failure
  58. ::
  59. ++ expect-success
  60. |= a=(trap)
  61. ^- tang
  62. =/ b (mule a)
  63. ?- -.b
  64. %& ~
  65. %| ['expected success - failed' ((slog p.b) p.b)]
  66. ==
  67. :: $a-test-chain: a sequence of tests to be run
  68. ::
  69. :: NB: arms shouldn't start with `test-` so that `-test % ~` runs
  70. ::
  71. +$ a-test-chain
  72. $_
  73. |?
  74. ?: =(0 0)
  75. [%& p=*tang]
  76. [%| p=[tang=*tang next=^?(..$)]]
  77. :: +run-chain: run a sequence of tests, stopping at first failure
  78. ::
  79. ++ run-chain
  80. |= seq=a-test-chain
  81. ^- tang
  82. =/ res $:seq
  83. ?- -.res
  84. %& p.res
  85. %| ?. =(~ tang.p.res)
  86. tang.p.res
  87. $(seq next.p.res)
  88. ==
  89. :: +category: prepends a name to an error result; passes successes unchanged
  90. ::
  91. ++ category
  92. |= [a=tape b=tang] ^- tang
  93. ~& > "category: {a}"
  94. ?: =(~ b) ~ :: test OK
  95. :- leaf+"in: '{a}'"
  96. (turn b |=(c=tank rose+[~ " " ~]^~[c]))
  97. :: +give-result: runs a test, pretty-prints the result
  98. ::
  99. ++ give-result
  100. |= [name=tape test=(trap tang)]
  101. ^- [ok=? =tang]
  102. =+ run=(mule test)
  103. ?- -.run
  104. %| |+(welp p.run leaf+"CRASHED {name}" ~)
  105. %& ?: =(~ p.run)
  106. &+[leaf+"OK {name}"]~
  107. |+(flop `tang`[leaf+"FAILED {name}" p.run])
  108. ==
  109. ++ find-tank
  110. |= [=tang =tape]
  111. ^- (unit tank)
  112. ?~ tang ~
  113. :: %- (slog i.tang ~)
  114. ?. ?=(%leaf -.i.tang) $(tang t.tang)
  115. ?: ?=(^ (find tape i.tang))
  116. `i.tang
  117. $(tang t.tang)
  118. ::
  119. ::
  120. :: Convenience functions for roswell testing modules
  121. ::
  122. +$ test-arm [name=term func=test-func]
  123. +$ test-func (trap tang)
  124. ++ succeed
  125. |= res=(list [ok=? =tang])
  126. ^- ?
  127. %+ roll res
  128. |= [[ok=? =tang] pass=?]
  129. %- (slog (flop tang))
  130. &(pass ok)
  131. ::
  132. ++ run-tests
  133. |= test-arms=(list test-arm)
  134. ^- (list [ok=? =tang])
  135. %+ turn test-arms
  136. |= =test-arm
  137. (run-test test-arm)
  138. ::
  139. ++ run-test
  140. |= =test-arm
  141. ^- [ok=? =tang]
  142. =+ name=(trip name.test-arm)
  143. ~& test-name+name
  144. =+ run=(mule func.test-arm)
  145. ?- -.run
  146. %| [| `tang`(welp p.run leaf+"CRASHED {name}" ~)]
  147. %& ?: =(~ p.run)
  148. [& `tang`[leaf+"OK {name}"]~]
  149. [| (flop `tang`[leaf+"FAILED {name}" p.run])]
  150. ==
  151. ::
  152. ++ get-test-arms
  153. |= tests-core=vase
  154. ^- (list test-arm)
  155. (get-prefix-arms 'test-' tests-core)
  156. ::
  157. :: +get-prefix-arms: produce arms that begin with .prefix
  158. ++ get-prefix-arms
  159. |= [prefix=term tests-core=vase]
  160. ^- (list test-arm)
  161. |^
  162. =/ arms=(list @tas) (sloe p:tests-core)
  163. %+ turn (skim arms has-prefix)
  164. |= name=term
  165. ^- test-arm
  166. =/ fire-arm=nock
  167. ~| [%failed-to-compile-test-arm name]
  168. q:(~(mint ut p:tests-core) p:!>(*tang) [%limb name])
  169. :- name
  170. |.(;;(tang ~>(%bout.[1 name] .*(q:tests-core fire-arm))))
  171. ::
  172. ++ has-prefix
  173. |= a=term ^- ?
  174. =((end [3 (met 3 prefix)] a) prefix)
  175. --
  176. --