-
Notifications
You must be signed in to change notification settings - Fork 3
/
Tests.Mod
346 lines (302 loc) · 9.41 KB
/
Tests.Mod
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
(** Tests.Mod - A module providing simple test support for Oberon-7.
Copyright (C) 2021 R. S. Doiel
Released under The 3-Clause BSD License.
See https://opensource.org/licenses/BSD-3-Clause
*)
MODULE Tests; (** portable *)
IMPORT Out;
CONST
MAXSET = 31; (* Oberon-7 doesn't have MAX(SET), use 0 to 31 *)
MAXSTR* = 1024; (* Maximum string length used in tests *)
TYPE
(** TestProc is the signature of a test procedure. It is simple.
if a test successeds it returns true, otherwise it returns false.
The procedure Test counts the number of test run and results
updating variable parameters of success and errors. In turn
these are passed to Summarize to return a summary report. *)
TestProc* = PROCEDURE () : BOOLEAN;
TestSet* = POINTER TO TestSetDesc;
TestSetDesc* = RECORD
title* : ARRAY MAXSTR OF CHAR;
fn* : TestProc;
next* : TestSet
END;
PROCEDURE Length(source : ARRAY OF CHAR) : INTEGER;
VAR i, l : INTEGER;
BEGIN i := 0; l := LEN(source);
WHILE (i < l) & (source[i] # 0X) DO INC(i); END;
RETURN i
END Length;
(** DisplayString display quoted ARRAY OF CHAR with prefixed by msg *)
PROCEDURE DisplayString*(msg: ARRAY OF CHAR; source : ARRAY OF CHAR);
BEGIN
Out.String(msg);Out.String(" '"); Out.String(source); Out.String("'");
Out.Ln();
END DisplayString;
(* displayStrings display expected (s1) and calculated (s2) strings
and if they don't match display error message and strings. *)
PROCEDURE displayStrings(s1, s2, msg: ARRAY OF CHAR);
BEGIN
Out.String("Expected '");Out.String(s1);
Out.String("', got '"); Out.String(s2); Out.String("' ");
Out.String(msg);Out.Ln();
END displayStrings;
(** ExpectedInt compares to int display msg on error and updates test to
FALSE if they don'y match *)
PROCEDURE ExpectedInt*(expected, got : INTEGER; msg : ARRAY OF CHAR; VAR test : BOOLEAN);
BEGIN
IF expected # got THEN
Out.String("Expected ");Out.Int(expected, 1);
Out.String(", got ");Out.Int(got, 1);
Out.String(" "); Out.String(msg); Out.Ln();
test := FALSE;
END;
END ExpectedInt;
(** ExpectedReal compares to REAL display msg on error and updates test to
FALSE if they don'y match *)
PROCEDURE ExpectedReal*(expected, got : REAL; msg : ARRAY OF CHAR; VAR test : BOOLEAN);
BEGIN
IF expected # got THEN
Out.String("Expected ");Out.Real(expected, 1);
Out.String(", got ");Out.Real(got, 1);
Out.String(" "); Out.String(msg); Out.Ln();
test := FALSE;
END;
END ExpectedReal;
(** ExpectedString compare two ARRAY OF CHAR, set test to FALSE
if they don't match and display msg *)
PROCEDURE ExpectedString*(s1, s2, msg : ARRAY OF CHAR; VAR test : BOOLEAN);
VAR i, lengthS1, lengthS2 : INTEGER; ok : BOOLEAN;
BEGIN ok := TRUE;
lengthS1 := Length(s1);
lengthS2 := Length(s2);
IF lengthS1 # lengthS2 THEN
displayStrings(s1, s2, msg); test := FALSE;
ELSE
i := 0;
ok := TRUE;
WHILE ok & (i < lengthS1) DO
IF s1[i] # s2[i] THEN
displayStrings(msg, s1, s2); test := FALSE;
ok := FALSE;
END;
INC(i);
END;
END;
END ExpectedString;
(** ExpectedChar compare two CHAR, set test to FALSE if they don't
match and display msg *)
PROCEDURE ExpectedChar*(expected, got : CHAR; msg : ARRAY OF CHAR; VAR test : BOOLEAN);
BEGIN
IF expected # got THEN
test := FALSE;
Out.String("Expected '");Out.Char(expected); Out.String("', got '");
Out.Char(got); Out.String("' ");
Out.String(msg);
Out.Ln;
END;
END ExpectedChar;
(** ExpectedBool compare to BOOLEAN values, set test to FALSE if they
don't match and display message *)
PROCEDURE ExpectedBool*(expected, got : BOOLEAN; msg : ARRAY OF CHAR; VAR test : BOOLEAN);
PROCEDURE displayBool( val : BOOLEAN);
BEGIN
IF val THEN
Out.String("TRUE");
ELSE
Out.String("FALSE");
END;
END displayBool;
BEGIN
(* NOTE: Odd behavior. I would expect to that the following should NOT display
output when both "expected" and "got" are TRUE. Compiler is OBNC 0.16.1.
IF expected # got THEN
...
This also exhibits the same behavior.
IF (expected # got) THEN
...
IF (expected # got) = TRUE THEN
...
However checking explicitly for value of "expected" and "got" and making explicit
tests does work.
IF ((expected = TRUE) & (got = FALSE)) & ((expected = FALSE) & (got = TRUE)) THEN
...
Is this a subtitly in the translation of Oberon-07 to C combined with lang-1000.11.45.5
on Darin 17.7.0?
*)
IF ((expected = TRUE) & (got = FALSE)) OR ((expected = FALSE) & (got = TRUE)) THEN
Out.String("Expected ");
displayBool(expected);
Out.String(", got ");
displayBool(got);
Out.String(" "); Out.String(msg); Out.Ln;
test := FALSE;
END;
END ExpectedBool;
(** ExpectedBytes compares the first N values to two array of byte *)
PROCEDURE ExpectedBytes*(expected, got : ARRAY OF BYTE; n : INTEGER; msg : ARRAY OF CHAR; VAR test: BOOLEAN);
VAR i, l : INTEGER;
PROCEDURE minimum(a, b : INTEGER) : INTEGER;
VAR res : INTEGER;
BEGIN
IF a < b THEN res := a ELSE res := b END;
RETURN res
END minimum;
BEGIN
l := minimum(LEN(expected), LEN(got));
IF n <= l THEN
i := 0;
WHILE test & (i < (n - 1)) DO
IF expected[i] # got[i] THEN
test := FALSE;
Out.String("Expected (pos: ");Out.Int(i, 1);Out.String(") ");
Out.Int(expected[i], 1);Out.String(", got ");Out.Int(got[i], 1);
Out.String(" ");
Out.String(msg); Out.Ln();
END;
INC(i);
END;
ELSE
test := FALSE;
Out.String("Expected length N (");Out.Int(n, 1);Out.String("), got length (");Out.Int(l, 1); Out.String(") ");
Out.String(msg); Out.Ln();
END;
END ExpectedBytes;
PROCEDURE displaySet(s : SET);
VAR i : INTEGER;
BEGIN
Out.Char("{");
FOR i := 0 TO MAXSET DO
IF i > 0 THEN
Out.String(", ");
END;
Out.Int(i, 1);
END;
Out.Char("}");
END displaySet;
(**ExpectedSet compares two sets, display message if they don't match and
set the value of test to FALSE *)
PROCEDURE ExpectedSet*(expected, got : SET; msg : ARRAY OF CHAR; VAR test : BOOLEAN);
BEGIN
IF expected = got THEN
Out.String("Expected set ");displaySet(expected);
Out.String(" got ");displaySet(got); Out.String(" ");
Out.String(msg);Out.Ln();
test := FALSE;
END;
END ExpectedSet;
(** ShowTitle displays the title in standard out and underlined with '=' *)
PROCEDURE ShowTitle*(s : ARRAY OF CHAR);
VAR i, l : INTEGER;
BEGIN l := Length(s) - 1;
Out.Ln();
Out.String(s); Out.Ln();
FOR i := 0 TO l DO
Out.String("=");
END;
Out.Ln();
END ShowTitle;
(** Test -- run the test method and update the success and error variables
provided. *)
PROCEDURE Test*(fn : TestProc; VAR success : INTEGER; VAR errors : INTEGER);
BEGIN
IF fn() THEN
success := success + 1;
ELSE
errors := errors + 1;
END
END Test;
(** Summarize -- sumarize the results using the test title, success
and error counts. *)
PROCEDURE Summarize*(title : ARRAY OF CHAR; successes, errors : INTEGER);
BEGIN
IF errors > 0 THEN
Out.Ln();
Out.String("Success: ");Out.Int(successes, 5);Out.Ln();
Out.String(" Errors: ");Out.Int(errors, 5);Out.Ln();
Out.String("-------------------------------------------");Out.Ln();
Out.String(" Total: ");Out.Int(successes + errors, 5);Out.Ln();
Out.Ln();
Out.String(title);Out.String(" failed.");Out.Ln();
ASSERT(FALSE);
ELSE
Out.String("OK, ");Out.String(title); Out.Ln();
END;
END Summarize;
(** New initializes a new TestSet with a title *)
PROCEDURE Init*(VAR ts: TestSet; title : ARRAY OF CHAR);
BEGIN
IF ts = NIL THEN
NEW(ts);
END;
ts.title := title;
ts.fn := NIL;
ts.next := NIL;
END Init;
PROCEDURE Add*(VAR ts : TestSet; fn : TestProc);
VAR cur : TestSet;
BEGIN
cur := ts;
IF cur.fn # NIL THEN
(* Move to end of list *)
WHILE cur.next # NIL DO cur := cur.next END;
NEW(cur.next);
cur := cur.next;
END;
cur.fn := fn;
cur.next := NIL;
END Add;
PROCEDURE Run*(ts : TestSet) : BOOLEAN;
VAR title : ARRAY MAXSTR OF CHAR;
i, successes, errors : INTEGER;
ok, result : BOOLEAN;
BEGIN
successes := 0; errors := 0;
FOR i := 0 TO LEN(ts.title) - 1 DO
title[i] := ts.title[i];
END;
ok := TRUE;
WHILE ts # NIL DO
IF ts.fn # NIL THEN
result := ts.fn();
IF result THEN
INC(successes);
ELSE
INC(errors);
ok := FALSE;
END;
END;
ts := ts.next;
END;
IF ok THEN
Out.String("OK, "); Out.String(title); Out.Ln();
ELSE
ShowTitle(title);
Summarize(title, successes, errors);
END;
RETURN ok
END Run;
END Tests.
{
"title": "Tests",
"description": "An Oberon-7 simple test module."
"byline: "R. S. Doiel, 2020-11-09",
"pubDate": "2020-11-09",
"keywords": [ "oberon", "testing" ],
"license": "http://www.gnu.org/licenses/agpl-3.0.html"
}
Tests
=====
This module provide a few simple method for implementing module
testing of exported methods and variables.
Test
----
This procedure provides a simple way to run a test method
and track the success or failure of the test. Test proceedures
hare parameterless returning a TRUE on success and FALSE if
an error in encountered.
Summarize
---------
This procedure takes a title, success and error counts and returns
the results. If error count is greater than zero then program
exists with an error by calling `ASSERT(FALSE);`.