Modula-2 unit testing
last modified November 27, 2026 Modula-2
A unit test calls a procedure with known arguments and compares the result
with the value it should have. Neither PIM nor ISO Modula-2 comes with a
testing library, and none is needed: a few procedures that count the
passed and failed checks are enough, and the exit status from the
environment and processes
article tells the shell or make whether everything passed.
The article tests a small Dates module whose first version
has a bug, and later the Text and Numbers modules
from earlier articles. All examples are compiled with -fiso.
A first test
The module under test answers two questions about the calendar: whether a year is a leap year and how many days a month has.
DEFINITION MODULE Dates; PROCEDURE IsLeap(year: CARDINAL): BOOLEAN; PROCEDURE DaysIn(month, year: CARDINAL): CARDINAL; END Dates.
IMPLEMENTATION MODULE Dates;
PROCEDURE IsLeap(year: CARDINAL): BOOLEAN;
BEGIN
RETURN year MOD 4 = 0
END IsLeap;
PROCEDURE DaysIn(month, year: CARDINAL): CARDINAL;
BEGIN
CASE month OF
2: IF IsLeap(year) THEN RETURN 29 ELSE RETURN 28 END
| 4, 6, 9, 11: RETURN 30
ELSE
RETURN 31
END
END DaysIn;
END Dates.
IsLeap treats every fourth year as a leap year, and
DaysIn uses it for February. A test program imports the
module like any other client.
MODULE first;
FROM STextIO IMPORT WriteString, WriteLn;
FROM Dates IMPORT IsLeap;
BEGIN
IF IsLeap(2024) THEN
WriteString("pass: 2024 is a leap year")
ELSE
WriteString("FAIL: 2024 is a leap year")
END;
WriteLn
END first.
The test calls IsLeap with a year whose answer is known and
prints pass or FAIL. A test describes what it
checks, so that a failure message says what went wrong without a look at
the source.
$ gm2 -fiso -c Dates.mod $ gm2 -fiso first.mod Dates.o -o first $ ./first pass: 2024 is a leap year
Counting passes and failures
One IF per test does not scale. A procedure that takes the
description and the result of the check keeps each test to one line and
counts the results.
MODULE counting;
FROM STextIO IMPORT WriteString, WriteLn;
FROM SWholeIO IMPORT WriteCard;
FROM Dates IMPORT IsLeap;
VAR
passed, failed: CARDINAL;
PROCEDURE Check(name: ARRAY OF CHAR; ok: BOOLEAN);
BEGIN
IF ok THEN
INC(passed)
ELSE
INC(failed);
WriteString("FAIL: "); WriteString(name); WriteLn
END
END Check;
BEGIN
passed := 0;
failed := 0;
Check("2024 is a leap year", IsLeap(2024));
Check("2023 is not a leap year", NOT IsLeap(2023));
Check("2000 is a leap year", IsLeap(2000));
Check("1900 is not a leap year", NOT IsLeap(1900));
Check("2100 is not a leap year", NOT IsLeap(2100));
WriteCard(passed, 0); WriteString(" passed, ");
WriteCard(failed, 0); WriteString(" failed"); WriteLn
END counting.
Check stays quiet for a passed test and prints the
description of a failed one, so that failures stand out. The summary
counts both. Two tests fail: years divisible by 100 are leap years only
when they are also divisible by 400, which IsLeap ignores.
The tests that pass are needed too, because a fix for the failing ones
must not break them.
$ gm2 -fiso counting.mod Dates.o -o counting $ ./counting FAIL: 1900 is not a leap year FAIL: 2100 is not a leap year 3 passed, 2 failed
Expected and actual values
A failed Check reports only that a condition was false. When
the result is a number, the test can compare it with the expected value
itself and print both.
MODULE expected;
FROM STextIO IMPORT WriteString, WriteLn;
FROM SWholeIO IMPORT WriteCard;
FROM Dates IMPORT DaysIn;
VAR
passed, failed: CARDINAL;
PROCEDURE CheckCard(name: ARRAY OF CHAR; actual, wanted: CARDINAL);
BEGIN
IF actual = wanted THEN
INC(passed)
ELSE
INC(failed);
WriteString("FAIL: "); WriteString(name);
WriteString(": expected "); WriteCard(wanted, 0);
WriteString(", got "); WriteCard(actual, 0); WriteLn
END
END CheckCard;
BEGIN
passed := 0;
failed := 0;
CheckCard("January", DaysIn(1, 2023), 31);
CheckCard("April", DaysIn(4, 2023), 30);
CheckCard("February 2023", DaysIn(2, 2023), 28);
CheckCard("February 2024", DaysIn(2, 2024), 29);
CheckCard("February 1900", DaysIn(2, 1900), 28);
CheckCard("December", DaysIn(12, 2023), 31);
WriteCard(passed, 0); WriteString(" passed, ");
WriteCard(failed, 0); WriteString(" failed"); WriteLn
END expected.
CheckCard takes the actual value first and the expected one
second, and on a mismatch prints both. The message for February 1900 shows
that DaysIn returns 29 instead of 28; the bug in
IsLeap shows up in the procedure that uses it.
$ gm2 -fiso expected.mod Dates.o -o expected $ ./expected FAIL: February 1900: expected 28, got 29 5 passed, 1 failed
The exit status
The programs so far end with status 0 even when tests fail, so a script
cannot tell success from failure without reading the output. The test
program datestest is the previous program with three more
lines at the end of its body.
BEGIN
passed := 0;
failed := 0;
CheckCard("January", DaysIn(1, 2023), 31);
CheckCard("April", DaysIn(4, 2023), 30);
CheckCard("February 2023", DaysIn(2, 2023), 28);
CheckCard("February 2024", DaysIn(2, 2024), 29);
CheckCard("February 1900", DaysIn(2, 1900), 28);
CheckCard("December", DaysIn(12, 2023), 31);
WriteCard(passed, 0); WriteString(" passed, ");
WriteCard(failed, 0); WriteString(" failed"); WriteLn;
IF failed > 0 THEN
HALT(1)
END
END datestest.
HALT(1) ends the program with status 1 when a test has
failed. The shell operator && runs the second command
only after a successful first one, so the message is not printed. Build
scripts and continuous integration servers work the same way: a non-zero
status stops the build.
$ gm2 -fiso datestest.mod Dates.o -o datestest $ ./datestest FAIL: February 1900: expected 28, got 29 5 passed, 1 failed $ echo $? 1 $ ./datestest && echo "ready to release" FAIL: February 1900: expected 28, got 29 5 passed, 1 failed
Fixing the bug
With the tests in place, the fix can be checked at once. A year is a leap year when it is divisible by 4, except for years divisible by 100 that are not divisible by 400.
PROCEDURE IsLeap(year: CARDINAL): BOOLEAN;
BEGIN
RETURN (year MOD 4 = 0) AND
((year MOD 100 # 0) OR (year MOD 400 = 0))
END IsLeap;
Only IsLeap changes. The implementation module is compiled
again, the test program linked with the new object file, and all six
tests pass; the status is 0. The definition module did not change, so the
clients do not have to be recompiled.
$ gm2 -fiso -c Dates.mod $ gm2 -fiso datestest.mod Dates.o -o datestest $ ./datestest 6 passed, 0 failed $ echo $? 0 $ ./datestest && echo "ready to release" 6 passed, 0 failed ready to release
A test module
Every test program needs the counters, the check procedures and the summary. They belong in a module of their own.
DEFINITION MODULE Tests; PROCEDURE Check(name: ARRAY OF CHAR; ok: BOOLEAN); PROCEDURE CheckInt(name: ARRAY OF CHAR; actual, wanted: INTEGER); PROCEDURE CheckStr(name, actual, wanted: ARRAY OF CHAR); PROCEDURE Finish; END Tests.
The module exports a check for a condition, for integers and for strings,
and Finish, which prints the summary and sets the exit
status. The counters stay hidden in the implementation.
IMPLEMENTATION MODULE Tests;
FROM STextIO IMPORT WriteString, WriteLn;
FROM SWholeIO IMPORT WriteInt, WriteCard;
FROM Strings IMPORT Equal;
VAR
passed, failed: CARDINAL;
PROCEDURE Fail(name: ARRAY OF CHAR);
BEGIN
INC(failed);
WriteString("FAIL: "); WriteString(name)
END Fail;
PROCEDURE Check(name: ARRAY OF CHAR; ok: BOOLEAN);
BEGIN
IF ok THEN INC(passed) ELSE Fail(name); WriteLn END
END Check;
PROCEDURE CheckInt(name: ARRAY OF CHAR; actual, wanted: INTEGER);
BEGIN
IF actual = wanted THEN
INC(passed)
ELSE
Fail(name);
WriteString(": expected "); WriteInt(wanted, 0);
WriteString(", got "); WriteInt(actual, 0); WriteLn
END
END CheckInt;
PROCEDURE CheckStr(name, actual, wanted: ARRAY OF CHAR);
BEGIN
IF Equal(actual, wanted) THEN
INC(passed)
ELSE
Fail(name);
WriteString(': expected "'); WriteString(wanted);
WriteString('", got "'); WriteString(actual); WriteString('"'); WriteLn
END
END CheckStr;
PROCEDURE Finish;
BEGIN
WriteCard(passed, 0); WriteString(" passed, ");
WriteCard(failed, 0); WriteString(" failed"); WriteLn;
IF failed > 0 THEN
HALT(1)
END
END Finish;
BEGIN
passed := 0;
failed := 0
END Tests.
Fail counts a failure and starts its message; each check adds
its own details. CheckStr compares with Equal
from the ISO Strings module and puts quotes around the
strings, so that leading and trailing spaces are visible. The module body
sets the counters before the test program starts.
MODULE texttest;
FROM Tests IMPORT Check, CheckInt, CheckStr, Finish;
FROM Text IMPORT Field, Trim, Split;
VAR
s: ARRAY [0..31] OF CHAR;
f: ARRAY [0..3] OF Field;
n: CARDINAL;
BEGIN
s := " padded ";
Trim(s);
CheckStr("Trim both ends", s, "padded");
s := " ";
Trim(s);
CheckStr("Trim blanks only", s, "");
Split("Bratislava, 475000, SK", ',', f, n);
CheckInt("Split count", n, 3);
CheckStr("Split first", f[0], "Bratislava");
CheckStr("Split last", f[2], "SK");
Split("a,,b", ',', f, n);
Check("Split keeps empty field", (n = 3) AND (f[1][0] = 0C));
Finish
END texttest.
The test program checks Trim and Split from the
Text module of the
string processing article,
including the edge cases: a string of blanks only and an empty field
between two separators. With the helpers in a module, a test program
contains nothing but tests.
$ gm2 -fiso -c Tests.mod $ gm2 -fiso -c Text.mod $ gm2 -fiso texttest.mod Tests.o Text.o -o texttest $ ./texttest 6 passed, 0 failed
Table-driven tests
Many tests of one procedure differ only in their data. A table-driven test
writes the test logic once and lists the cases as rows. Here each row is
one call of a small procedure; the procedure under test is
ToInt from the Numbers module of the
reading input article.
MODULE numberstest;
FROM Tests IMPORT Check, CheckInt, Finish;
FROM Numbers IMPORT ToInt;
PROCEDURE Good(input: ARRAY OF CHAR; wanted: INTEGER);
VAR
n: INTEGER;
BEGIN
IF ToInt(input, n) THEN
CheckInt(input, n, wanted)
ELSE
Check(input, FALSE)
END
END Good;
PROCEDURE Bad(input: ARRAY OF CHAR);
VAR
n: INTEGER;
BEGIN
Check(input, NOT ToInt(input, n))
END Bad;
BEGIN
Good("42", 42);
Good("-17", -17);
Good("+5", 5);
Good(" 8 ", 8);
Good("0", 0);
Good("2147483647", MAX(INTEGER));
Good("-2147483648", MIN(INTEGER));
Bad("");
Bad("abc");
Bad("8x");
Bad("1 2");
Bad("-");
Bad("2147483648");
Finish
END numberstest.
Good expects the input to convert to the given value, and
Bad expects the conversion to fail. The input doubles as the
name of the test. Adding a case takes one line, which makes it easy to try
the limits, and the limits find a bug: ToInt rejects
-2147483648, the smallest INTEGER. It builds the
number as a positive value and negates it at the end, and 2147483648 does
not fit into an INTEGER. The other articles never read such a
number, so the bug stays, and the failing test documents it.
$ gm2 -fiso -c Numbers.mod $ gm2 -fiso numberstest.mod Tests.o Numbers.o -o numberstest $ ./numberstest FAIL: -2147483648 12 passed, 1 failed $ echo $? 1
Testing for an exception
An error case is part of the behaviour of a procedure and deserves a test
too. The final version of DaysIn raises an exception with the
EXCEPTIONS module, as shown in the
error handling article, when the
month does not exist.
PROCEDURE DaysIn(month, year: CARDINAL): CARDINAL;
BEGIN
CASE month OF
2: IF IsLeap(year) THEN RETURN 29 ELSE RETURN 28 END
| 4, 6, 9, 11: RETURN 30
| 1, 3, 5, 7, 8, 10, 12: RETURN 31
ELSE
RAISE(dates, 1, "no such month")
END
END DaysIn;
The ELSE branch now handles only invalid months; the
months with 31 days get a label of their own. The module allocates the
exception source dates in its body.
MODULE raisetest;
FROM Tests IMPORT Check, CheckInt, Finish;
FROM Dates IMPORT DaysIn;
VAR
days: CARDINAL;
PROCEDURE Raises(test: PROC): BOOLEAN;
BEGIN
test;
RETURN FALSE
EXCEPT
RETURN TRUE
END Raises;
PROCEDURE MonthZero;
BEGIN
days := DaysIn(0, 2024)
END MonthZero;
PROCEDURE Month13;
BEGIN
days := DaysIn(13, 2024)
END Month13;
PROCEDURE May;
BEGIN
days := DaysIn(5, 2024)
END May;
BEGIN
Check("month 0 raises", Raises(MonthZero));
Check("month 13 raises", Raises(Month13));
Check("May does not raise", NOT Raises(May));
CheckInt("May has 31 days", days, 31);
Finish
END raisetest.
Raises takes a parameterless procedure of the standard type
PROC, calls it and reports whether it raised an exception.
If the call returns normally, Raises returns
FALSE; if an exception leaves the call, the handler returns
TRUE. Each case is a small procedure that stores its result
in days, so the test also checks that a valid month still
works.
$ gm2 -fiso -c Dates.mod $ gm2 -fiso raisetest.mod Tests.o Dates.o -o raisetest $ ./raisetest 4 passed, 0 failed
Running the tests with make
A project with several test programs needs one command that builds and
runs them all. The compiling
and debugging article builds a program with make; here a
test target runs the tests as well.
GM2 = gm2 -fiso -Wall TESTS = datestest texttest numberstest raisetest .PHONY: test clean test: $(addprefix run-,$(TESTS)) run-%: % ./$< %test: %test.mod $(GM2) $< $(filter %.o,$^) -o $@ datestest: Dates.o texttest: Tests.o Text.o numberstest: Tests.o Numbers.o raisetest: Tests.o Dates.o %.o: %.mod %.def $(GM2) -c $< clean: rm -f $(TESTS) *.o
test depends on one run- target per test
program, and the pattern rule run-% builds the program and
runs it. A test program is linked from its source and the object files
listed in its dependency line; $(filter %.o,$^) picks them
out of the prerequisites. make stops at the first command
with a non-zero status, so the failing numberstest stops the
run and raisetest is never built. make -k
(keep going) runs the remaining targets and reports the failure at the
end. In both cases make itself exits with status 2.
$ make test gm2 -fiso -Wall -c Dates.mod gm2 -fiso -Wall datestest.mod Dates.o -o datestest ./datestest 6 passed, 0 failed gm2 -fiso -Wall -c Tests.mod gm2 -fiso -Wall -c Text.mod gm2 -fiso -Wall texttest.mod Tests.o Text.o -o texttest ./texttest 6 passed, 0 failed gm2 -fiso -Wall -c Numbers.mod gm2 -fiso -Wall numberstest.mod Tests.o Numbers.o -o numberstest ./numberstest FAIL: -2147483648 12 passed, 1 failed make: *** [Makefile:8: run-numberstest] Error 1 $ make -k test ./datestest 6 passed, 0 failed ./texttest 6 passed, 0 failed ./numberstest FAIL: -2147483648 12 passed, 1 failed make: *** [Makefile:8: run-numberstest] Error 1 gm2 -fiso -Wall raisetest.mod Tests.o Dates.o -o raisetest ./raisetest 4 passed, 0 failed make: Target 'test' not remade because of errors. $ echo $? 2
Source
In this article we have written checks that count passes and failures,
reported expected and actual values, set the exit status with
HALT, fixed a bug that the tests found, moved the helpers into
a Tests module, written table-driven tests, tested that a
procedure raises an exception and run all tests with make.
The error handling article covers
the exceptions used here, and the modules
article the separate compilation behind the test programs.
Author
List all Modula-2 tutorials.