Here is a script (%unittest.r) that can be used to create
simple unit tests. It is included with its own test script
(%unittest-test.r).
Stan Silver
REBOL [
Title: {Unit Test}
File: %unittest.r
Date: 2-Mar-2000
Author: {Stan Silver}
Purpose: {Simple unit test framework}
Notes: {
unit-test takes a single block as an argument, with
alternating desired values and expressions. If all
expressions evaluate to the desired values, true is returned;
else false.
The /errors refinement causes a list all mismatched
[desired actual] pairs to be returned instead of true or false.
The /show refinement causes all [desired actual] pairs to be
printed.
One possible convention is to have a unit test script named
%xxx-test.r for each script %xxx.r.
}
Examples: {
unit-test [
1 first [1 2 3]
3 last [1 2 3]
]
unit-test/errors [
1
first [1 2 3]
0
last [1 2 3]
]
unit-test/show [
1
first [1 2 3]
0
last [1 2 3]
]
}
]
unit-test: func [
cases-block [block!]
/errors
/show
/local result error-block cases actual desired
][
result: true
error-block: copy []
cases: reduce cases-block
forskip cases 2 [
desired: first cases
actual: second cases
if not equal? desired actual [
append/only error-block reduce [desired actual]
result: false
]
]
if show [
cases: head cases
forskip cases 2 [
print remold [first cases second cases]
]
print ""
]
return either errors [
error-block
][
result
]
]
REBOL [
Title: "Unit Test Test"
File: %unittest-test.r
Date: 2-Mar-2000
Purpose: {Unit test of unit test}
]
do %unittest.r
print unit-test/show [
true
unit-test [1 1 2 2 3 1 + 2]
true
unit-test [
1 first [1 2 3 4 5]
5 last [1 2 3 4 5]
]
true
do [
f: func [x][x * x]
unit-test [
1 f 1
4 f 2
9 f 3
]
]
false
unit-test [1 1 2 2 3 0]
false
unit-test [
1 first [1 2 3 4 5]
1 last [1 2 3 4 5]
]
[[1 2] [1 5]]
unit-test/errors [
1 first [1 2 3 4 5]
1 second [1 2 3 4 5]
1 last [1 2 3 4 5]
]
[]
unit-test/errors [
1 first [1 2 3 4 5]
2 second [1 2 3 4 5]
5 last [1 2 3 4 5]
]
]
break
______________________________________________________
Get Your Private, Free Email at http://www.hotmail.com