test.sa
Generated by gen_html_sa_files from ICSI. Contact gomes@icsi.berkeley.edu for details
class TEST
class TEST is
-- This class is an auxiliary class used for testing library facilities.
-- It is to be included by a testing class. The test routine should
-- start with the call `class_name("CLASS_FOO") ;'. It should then have calls
-- to `test' or `unchecked_test' which actually perform the test. It should
-- end with `finish' to print out the results. The results are sent to both
-- `stderr' and `stdout' so output may be redirected to a file and the user
-- may still see whether all tests were passed. The routine being tested must
-- return a string. The basic classes all have routines named `str' to
-- produce text string representation encodings of themselves.
-- A typical test might look something like :
-- test("sum", (1+1).str, "2");
-- The tests are numbered and the failures are summarized at the end.
-- NOTE It is IMPORTANT to note that, as written, this class is NOT
-- portable to other cultural environments than the one in which
-- the code is compiled -- and then there is a limitation to cultures
-- wherein the character repertoire in use includes all English
-- characters and it is possible to interpret the visible output
-- produced in the English language.
-- Version 1.0 Nov 96. Copyright K Hopper,U of Waikato
-- Development History
-- -------------------
-- Date Who By Detail
-- ---- ------ ------
-- 8 Nov 96 kh Original from Sather Sys library, etc.
shared class_name_str : STR ; -- The name of the tested class.
shared failures : FLIST{CARD} ; -- The tests which failed.
shared failure_docs : FLIST{STR} ; -- Documentation of failures.
shared test_number : CARD ; -- Which test.
shared checked_tests : CARD ;
class_name(
name : STR
) is
-- This routine specifies the name of the class being tested.
-- It must be called first.
class_name_str := name ;
test_number := 1 ;
checked_tests := 0 ;
#OUT + "Test of class " + name + ":\n\n"
end ;
test(doc_ds,
does_ds,
should_ds : $STR
) is
-- This routine performs the test with the description `doc',
-- return value `does', and desired return value `should'.
-- It also keeps track of failures.
doc : STR := doc_ds.str ;
does : STR := does_ds.str ;
should : STR := should_ds.str ;
if (void(class_name_str)) then
#ERR + "Error in TEST class: \"class_name\" not called\n" ;
class_name_str := "!!!UNKNOWN_CLASS_NAME!!!" ;
test_number := 1 ;
checked_tests := 0
end ;
#OUT + class_name_str + " Test " ;
#OUT + test_number.str + " (" + doc + ") " ;
if does.is_eq(should).not then
failures := failures.push(test_number) ;
failure_docs := failure_docs.push(doc) ;
#OUT + "FAILED!!\n"
else
#OUT + "passed.\n"
end ;
if should.size > 0 then
#OUT + " Expected result is '" + should + "'\n"
else
#OUT + " Expected result is ''\n"
end ;
if does.size > 0 then
#OUT + " - actually is '" + does + "'\n"
else
#OUT + " - actually is ''\n"
end ;
(#OUT + "\n").flush ;
test_number := test_number + 1 ;
checked_tests := checked_tests + 1
end ;
raise_test(
testname : $STR,
testrout : ROUT : STR,
should : STR
) is
-- This routine carries out the test with the description 'doc'.
-- The routine 'testrout' is expected to raise an exception with the string
-- representation 'should'.
protect
test( testname, testrout.call, "raise \"" + should.str + "\"." )
when $STR then
test(testname, exception, should)
else
raise exception
end
end ;
unchecked_test(
doc_ds,
does_ds,
should_ds : $STR
) is
-- This routine performs the test with the description `doc',
-- return value `does', and desired return value `should'. It does not keep
-- track of failures.
doc : STR := doc_ds.str ;
does : STR := does_ds.str ;
should : STR := should_ds.str ;
#OUT + " " + class_name_str + " Test " + test_number.str +
" (" + doc + ")\n" ;
#OUT + " Value is " + should + "\n" ;
#OUT + " Expected " + does +"\n\n" ;
test_number := test_number + 1
end ;
finish is
-- This routine carries out the actions necessary in completing
-- the testing of the current class. This sends to the error channel only
-- the name of the class being tested, to the standard output channel all
-- of the test details.
if failures.is_empty then
if checked_tests = 0 then
#OUT + "Class " + class_name_str + " had no checkable tests.\n"
else
#OUT + "Class " + class_name_str +
" passed all " + checked_tests.str + " checkable tests.\n"
end
else
loop
f1 : STR := failures.elt!.str ;
f2 : STR := failure_docs.elt! ;
msg : STR := "Class " + class_name_str + " failed test " +
f1 + " (" + f2 + ")\n" ;
#ERR + "\n" + msg.copy ;
#OUT + msg
end
end
end ;
end ; -- TEST
class TEST_TEST
class TEST_TEST is
-- This is the test class for the class TEST!
include TEST ;
raising : STR is
raise "an exception"
end ;
main is
class_name("TEST") ;
test("A good test", "good", "good") ;
-- The following test should FAIL - it is NOT an error!
test("A bad test", "good", "bad") ;
raise_test("A raise test", bind(raising), "an exception") ;
unchecked_test("Unchecked test", "anything", "anything") ;
finish
end ;
end ; -- TEST_TEST