You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: testing-with-tap/forth.md
+36-36
Original file line number
Diff line number
Diff line change
@@ -25,61 +25,61 @@ You may not be familiar with [Forth](https://en.wikipedia.org/wiki/Forth_(progra
25
25
It's recommended that whenever possible your TAP should start with a plan. The plan says how many tests are expected to run and looks like this:
26
26
27
27
```
28
-
1..10
28
+
1..10
29
29
```
30
30
31
31
The plan always takes the form 1..*number* of tests.
32
32
In Forth we can output a plan like this:
33
33
34
34
```
35
-
." 1..10" cr
35
+
." 1..10" cr
36
36
```
37
37
38
38
Or you can define the word plan like this:
39
39
40
40
```
41
-
: plan ( n -- )
42
-
." 1.." . cr
43
-
;
41
+
: plan ( n -- )
42
+
." 1.." . cr
43
+
;
44
44
```
45
45
46
46
and use it like this:
47
47
48
48
```
49
-
10 plan
49
+
10 plan
50
50
```
51
51
52
52
### Test results
53
53
54
54
The general form of a test result is either
55
55
56
56
```
57
-
ok test-number description
57
+
ok test-number description
58
58
```
59
59
60
60
or
61
61
62
62
```
63
-
not ok test-number description
63
+
not ok test-number description
64
64
```
65
65
66
66
The description is optional so we'll omit it for now and concentrate on generating results that a TAP parser can process.
67
67
We need a variable to hold the number of the next test. In Forth that looks like this:
68
68
69
69
```
70
-
Variable test#
71
-
0 test# !
70
+
Variable test#
71
+
0 test# !
72
72
```
73
73
74
74
And we need something to output the result:
75
75
76
76
```
77
-
: ok ( f -- )
78
-
0= if ." not " then
79
-
." ok "
80
-
test# dup @ 1+ dup . swap !
81
-
cr
82
-
;
77
+
: ok ( f -- )
78
+
0= if ." not " then
79
+
." ok "
80
+
test# dup @ 1+ dup . swap !
81
+
cr
82
+
;
83
83
```
84
84
85
85
Now we have a word called ok which checks the top value on the stack and outputs 'ok' or 'not ok' depending on whether it's true (non-zero) or false (zero). With that we have everything necessary to start TAP based testing.
@@ -90,37 +90,37 @@ Now we have a word called ok which checks the top value on the stack and outputs
90
90
Here's a simple test of addition and subtraction using the words we just defined:
91
91
92
92
```
93
-
#! /usr/local/bin/gforth
94
-
95
-
require tap.fs
96
-
97
-
2 plan
98
-
1 1 + 2 = ok
99
-
2 1 - 1 = ok
100
-
101
-
bye
93
+
#! /usr/local/bin/gforth
94
+
95
+
require tap.fs
96
+
97
+
2 plan
98
+
1 1 + 2 = ok
99
+
2 1 - 1 = ok
100
+
101
+
bye
102
102
```
103
103
104
104
It assumes that we saved our TAP generating code in a file called tap.fs. When run it will output
105
105
106
106
```
107
-
1..2
108
-
ok 1
109
-
ok 2
107
+
1..2
108
+
ok 1
109
+
ok 2
110
110
```
111
111
112
112
### Analysing the results
113
113
114
114
If you have the Perl prove utility (which is part of [Test::Harness](http://search.cpan.org/dist/Test-Harness/) you can use it to run this test script and analyse the results:
0 commit comments