/quick-test/quick-test.red

http://github.com/dockimbel/Red · Unknown · 186 lines · 163 code · 23 blank · 0 comment · 0 complexity · 95f7ac4a49bdb99b0d621af635fa9f4b MD5 · raw file

  1. Red [
  2. Title: "Red simple testing framework"
  3. Author: "Peter W A Wood"
  4. File: %quick-test.red
  5. Version: "0.2.0"
  6. Rights: "Copyright (C) 2012-2015 Peter W A Wood. All rights reserved."
  7. License: "BSD-3 - https://github.com/red/red/blob/master/BSD-3-License.txt"
  8. ]
  9. ;; counters
  10. qt-run-tests: 0
  11. qt-run-asserts: 0
  12. qt-run-passes: 0
  13. qt-run-failures: 0
  14. qt-file-tests: 0
  15. qt-file-asserts: 0
  16. qt-file-passes: 0
  17. qt-file-failures: 0
  18. qt-file-name: none
  19. ;; group switches
  20. qt-group-name-not-printed: true
  21. qt-group?: false
  22. _qt-init-group: func [] [
  23. qt-group-name-not-printed: true
  24. qt-group?: false
  25. qt-group-name: ""
  26. ]
  27. qt-init-run: func [] [
  28. qt-run-tests: 0
  29. qt-run-asserts: 0
  30. qt-run-passes: 0
  31. qt-run-failures: 0
  32. _qt-init-group
  33. ]
  34. qt-init-file: func [] [
  35. qt-file-tests: 0
  36. qt-file-asserts: 0
  37. qt-file-passes: 0
  38. qt-file-failures: 0
  39. _qt-init-group
  40. ]
  41. ***start-run***: func[
  42. title [string!]
  43. ][
  44. qt-init-run
  45. qt-run-name: title
  46. prin "***Starting*** "
  47. print title
  48. ]
  49. ~~~start-file~~~: func [
  50. title [string!]
  51. ][
  52. qt-init-file
  53. prin "~~~started test~~~ "
  54. print title
  55. qt-file-name: title
  56. qt-group?: false
  57. ]
  58. ===start-group===: func [
  59. title [string!]
  60. ][
  61. qt-group-name: title
  62. qt-group?: true
  63. ]
  64. --test--: func [
  65. title [string!]
  66. ][
  67. qt-test-name: title
  68. qt-file-tests: qt-file-tests + 1
  69. ]
  70. --assert: func [
  71. assertion [logic!]
  72. ][
  73. qt-file-asserts: qt-file-asserts + 1
  74. either assertion [
  75. qt-file-passes: qt-file-passes + 1
  76. ][
  77. qt-file-failures: qt-file-failures + 1
  78. if qt-group? [
  79. if qt-group-name-not-printed [
  80. prin "===group=== "
  81. print qt-group-name
  82. qt-group-name-not-printed: false
  83. ]
  84. ]
  85. prin "--test-- "
  86. prin qt-test-name
  87. print " FAILED**************"
  88. ]
  89. ]
  90. --assertf~=: func[
  91. x [float!]
  92. y [float!]
  93. e [float!]
  94. /local
  95. diff [float!]
  96. e1 [float!]
  97. e2 [float!]
  98. ][
  99. ;; calculate tolerance to use
  100. ;; as e * max (1, x, y)
  101. either x > 0.0 [
  102. e1: x * e
  103. ][
  104. e1: -1.0 * x * e
  105. ]
  106. if e > e1 [e1: e]
  107. either y > 0.0 [
  108. e2: y * e
  109. ][
  110. e2: -1.0 * y * e
  111. ]
  112. if e1 > e2 [e2: e1]
  113. ;; perform almost equal check
  114. either x > y [
  115. diff: x - y
  116. ][
  117. diff: y - x
  118. ]
  119. either diff > e2 [
  120. --assert false
  121. ][
  122. --assert true
  123. ]
  124. ]
  125. ===end-group===: func [] [
  126. _qt-init-group
  127. ]
  128. qt-print-totals: func [
  129. tests [integer!]
  130. asserts [integer!]
  131. passes [integer!]
  132. failures [integer!]
  133. ][
  134. prin " Number of Tests Performed: "
  135. print tests
  136. prin " Number of Assertions Performed: "
  137. print asserts
  138. prin " Number of Assertions Passed: "
  139. print passes
  140. prin " Number of Assertions Failed: "
  141. print failures
  142. if failures <> 0 [
  143. print "****************TEST FAILURES****************"
  144. ]
  145. ]
  146. ~~~end-file~~~: func [] [
  147. print ["~~~finished test~~~ " qt-file-name]
  148. qt-print-totals qt-file-tests qt-file-asserts qt-file-passes qt-file-failures
  149. print ""
  150. ;; update run totals
  151. qt-run-passes: qt-run-passes + qt-file-passes
  152. qt-run-asserts: qt-run-asserts + qt-file-asserts
  153. qt-run-failures: qt-run-failures + qt-file-failures
  154. qt-run-tests: qt-run-tests + qt-file-tests
  155. ]
  156. ***end-run***: func [][
  157. prin "***Finished*** "
  158. print qt-run-name
  159. qt-print-totals qt-run-tests
  160. qt-run-asserts
  161. qt-run-passes
  162. qt-run-failures
  163. ]