|
| 1 | +Rebol [ |
| 2 | + Title: "Rebol simple testing framework" |
| 3 | + Author: ["Peter W A Wood" "Oldes"] |
| 4 | + File: %quick-test-module.r3 |
| 5 | + Version: 0.3.0 |
| 6 | + Rights: "Copyright (C) 2012-2018 Peter W A Wood, Oldes. All rights reserved." |
| 7 | + License: "BSD-3 - https://github.com/red/red/blob/master/BSD-3-License.txt" |
| 8 | + History: [ |
| 9 | + 0.3.0 "Oldes: modified to be as a Rebol3's module" |
| 10 | + 0.2.0 "Peter's version, which he was using in Red development with some modifications by Oldes fo use with R3" |
| 11 | + ] |
| 12 | + Name: Quick-test |
| 13 | + Type: module |
| 14 | + Exports: [ |
| 15 | + ***start-run*** |
| 16 | + ~~~start-file~~~ |
| 17 | + ===start-group=== |
| 18 | + --test-- |
| 19 | + --red-- |
| 20 | + --assert |
| 21 | + --assertf~= |
| 22 | + ===end-group=== |
| 23 | + ~~~end-file~~~ |
| 24 | + ***end-run*** |
| 25 | + ] |
| 26 | +] |
| 27 | + |
| 28 | +;- counters |
| 29 | +qt-run-tests: 0 |
| 30 | +qt-run-asserts: 0 |
| 31 | +qt-run-passes: 0 |
| 32 | +qt-run-failures: 0 |
| 33 | +qt-run-incompatible: 0 |
| 34 | +qt-file-tests: 0 |
| 35 | +qt-file-asserts: 0 |
| 36 | +qt-file-passes: 0 |
| 37 | +qt-file-failures: 0 |
| 38 | +qt-file-incompatible: 0 |
| 39 | + |
| 40 | +;- names |
| 41 | +qt-run-name: |
| 42 | +qt-file-name: |
| 43 | +qt-group-name: |
| 44 | +qt-test-name: none |
| 45 | + |
| 46 | +;- group switches |
| 47 | +qt-group-name-not-printed: true |
| 48 | +qt-group?: false |
| 49 | +qt-red-only: false |
| 50 | + |
| 51 | +;-state |
| 52 | +was-quiet: false |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | +qt-init-group: does [ |
| 57 | + qt-group-name-not-printed: true |
| 58 | + qt-group?: false |
| 59 | + qt-group-name: "" |
| 60 | +] |
| 61 | + |
| 62 | +qt-init-run: does [ |
| 63 | + qt-run-tests: |
| 64 | + qt-run-asserts: |
| 65 | + qt-run-passes: |
| 66 | + qt-run-failures: |
| 67 | + qt-run-incompatible: 0 |
| 68 | + qt-init-group |
| 69 | +] |
| 70 | + |
| 71 | +qt-init-file: does [ |
| 72 | + qt-file-tests: |
| 73 | + qt-file-asserts: |
| 74 | + qt-file-passes: |
| 75 | + qt-file-failures: |
| 76 | + qt-file-incompatible: 0 |
| 77 | + qt-init-group |
| 78 | +] |
| 79 | + |
| 80 | +start-run: func[ |
| 81 | + title [string!] |
| 82 | +][ |
| 83 | + was-quiet: system/options/quiet |
| 84 | + system/options/quiet: true |
| 85 | + qt-init-run |
| 86 | + qt-run-name: title |
| 87 | + prin "^[[7m***Starting***^[[0m " |
| 88 | + print title |
| 89 | +] |
| 90 | + |
| 91 | +start-file: func [ |
| 92 | + title [string!] |
| 93 | +][ |
| 94 | + qt-init-file |
| 95 | + prin "^[[7m~~~started test~~~^[[0m " |
| 96 | + print title |
| 97 | + qt-file-name: title |
| 98 | + qt-group?: false |
| 99 | +] |
| 100 | + |
| 101 | +start-group: func [ |
| 102 | + title [string!] |
| 103 | +][ |
| 104 | + qt-group-name: title |
| 105 | + qt-group?: true |
| 106 | +] |
| 107 | + |
| 108 | +start-test: func [ |
| 109 | + title [string!] |
| 110 | +][ |
| 111 | + qt-test-name: title |
| 112 | + qt-file-tests: qt-file-tests + 1 |
| 113 | + qt-red-only: false |
| 114 | +] |
| 115 | + |
| 116 | +as-red-only: does [ |
| 117 | + qt-red-only: true |
| 118 | +] |
| 119 | + |
| 120 | +assert: func [ |
| 121 | + assertion [logic!] |
| 122 | +][ |
| 123 | + qt-file-asserts: qt-file-asserts + 1 |
| 124 | + |
| 125 | + either assertion [ |
| 126 | + qt-file-passes: qt-file-passes + 1 |
| 127 | + ][ |
| 128 | + either qt-red-only [ |
| 129 | + qt-file-incompatible: qt-file-incompatible + 1 |
| 130 | + ][ qt-file-failures: qt-file-failures + 1] |
| 131 | + if qt-group? [ |
| 132 | + if qt-group-name-not-printed [ |
| 133 | + prin "===group=== " |
| 134 | + print qt-group-name |
| 135 | + qt-group-name-not-printed: false |
| 136 | + ] |
| 137 | + ] |
| 138 | + prin "--test-- " |
| 139 | + prin qt-test-name |
| 140 | + print either qt-red-only [ |
| 141 | + " not like Red********" |
| 142 | + ][ " FAILED**************"] |
| 143 | + ] |
| 144 | +] |
| 145 | + |
| 146 | +assertf~=: func[ |
| 147 | + x [decimal!] |
| 148 | + y [decimal!] |
| 149 | + e [decimal!] |
| 150 | + /local |
| 151 | + diff [decimal!] |
| 152 | + e1 [decimal!] |
| 153 | + e2 [decimal!] |
| 154 | +][ |
| 155 | + ;; calculate tolerance to use |
| 156 | + ;; as e * max (1, x, y) |
| 157 | + e1: either x > 0.0 [ x * e ][ e1: -1.0 * x * e ] |
| 158 | + e2: either y > 0.0 [ y * e ][ e2: -1.0 * y * e ] |
| 159 | + if e > e1 [e1: e] |
| 160 | + if e1 > e2 [e2: e1] |
| 161 | + |
| 162 | + ;; perform almost equal check |
| 163 | + diff: either x > y [ x - y ][ y - x ] |
| 164 | + assert diff < e2 |
| 165 | +] |
| 166 | + |
| 167 | +end-group: does[ |
| 168 | + qt-init-group |
| 169 | +] |
| 170 | + |
| 171 | +end-file: func [] [ |
| 172 | + print ["^[[7m~~~finished test~~~^[[0m " qt-file-name] |
| 173 | + print-totals qt-file-tests |
| 174 | + qt-file-asserts |
| 175 | + qt-file-passes |
| 176 | + qt-file-failures |
| 177 | + qt-file-incompatible |
| 178 | + print "" |
| 179 | + |
| 180 | + ;; update run totals |
| 181 | + qt-run-passes: qt-run-passes + qt-file-passes |
| 182 | + qt-run-asserts: qt-run-asserts + qt-file-asserts |
| 183 | + qt-run-failures: qt-run-failures + qt-file-failures |
| 184 | + qt-run-tests: qt-run-tests + qt-file-tests |
| 185 | + qt-run-incompatible: qt-run-incompatible + qt-file-incompatible |
| 186 | +] |
| 187 | + |
| 188 | +end-run: func [][ |
| 189 | + system/options/quiet: was-quiet |
| 190 | + |
| 191 | + prin "^[[7m***Finished***^[[0m " |
| 192 | + print qt-run-name |
| 193 | + print-totals qt-run-tests |
| 194 | + qt-run-asserts |
| 195 | + qt-run-passes |
| 196 | + qt-run-failures |
| 197 | + qt-run-incompatible |
| 198 | +] |
| 199 | + |
| 200 | +print-totals: func [ |
| 201 | + tests [integer!] |
| 202 | + asserts [integer!] |
| 203 | + passes [integer!] |
| 204 | + failures [integer!] |
| 205 | + incompat [integer!] |
| 206 | +][ |
| 207 | + prin " Number of Tests Performed: " |
| 208 | + print tests |
| 209 | + prin " Number of Assertions Performed: " |
| 210 | + print asserts |
| 211 | + prin " Number of Assertions Passed: " |
| 212 | + print passes |
| 213 | + prin " Number of Assertions Failed: " |
| 214 | + print failures |
| 215 | + prin " Number of Assertions Red-diff: " |
| 216 | + print incompat |
| 217 | + if failures <> 0 [ |
| 218 | + print "****************TEST FAILURES****************" |
| 219 | + ] |
| 220 | +] |
| 221 | + |
| 222 | +;- exported functions used as a "test dialect" |
| 223 | +***start-run***: :start-run |
| 224 | +~~~start-file~~~: :start-file |
| 225 | +===start-group===: :start-group |
| 226 | +--test--: :start-test |
| 227 | +--red--: :as-red-only |
| 228 | +--assert: :assert |
| 229 | +--assertf~=: :assertf~= |
| 230 | +===end-group===: :end-group |
| 231 | +~~~end-file~~~: :end-file |
| 232 | +***end-run***: :end-run |
| 233 | + |
0 commit comments