% $Id: TestingNoStop.oz,v 1.6 2011/09/02 20:06:14 leavens Exp leavens $ % % Assertion and testing procedures for Oz that do not stop % (you have to check the output) % % AUTHOR: Gary T. Leavens, using ideas from Jeff Balogh declare local %% Total number of failures so far, DoneTesting prints and resets this TotalFailures = {NewCell 0} %% A procedure that handles reporting, customize by using SetReporter Reporter = {NewCell System.showInfo} in %% Set the reporting Procedure to be the given one proc {SetReporter ReportProc} Reporter := ReportProc end %% Report only to the emulator window (only) proc {EmulatorReporter VS} {System.showInfo VS} end %% Report only to the Browser (only) proc {BrowserReporter VS} {Browse {VirtualString.toAtom VS}} end %% Report messages both to the Emulator window and the Browser proc {BothReporter VS} {EmulatorReporter VS} {BrowserReporter VS} end %% The default reporting just goes to the emulator window Reporter := EmulatorReporter %% Print out a message (about something that is not a failure) proc {ReportAbout VS} {@Reporter VS} end %% Notify the user about test failures proc {NotifyAbout VS} TotalFailures := @TotalFailures+1 {ReportAbout VS} end %% Assert that the argument is true. proc {Assert B} if {Not B} then {NotifyAbout 'ASSERTION FAILED!'} end end %% Assert that the second argument is true. proc {AssertTrue Message B} if {Not B} then {NotifyAbout 'ASSERTION FAILED: ' # Message} end end %% Assert that the second argument is false. proc {AssertFalse Message B} {AssertTrue Message {Not B}} end %% Mark an assumption that the argument is true. proc {Assume B} if {Not B} then {NotifyAbout 'ASSUMPTION FAILED!'} end end %% Mark an assumption that the second argument is true. proc {AssumeTrue Message B} if {Not B} then {NotifyAbout 'ASSUMPTION FAILED: ' # Message} end end %% Mark an assumption that the second argument is false. proc {AssumeFalse Message B} {AssumeTrue Message {Not B}} end %% Print a newline and a message that testing is beginning. proc {StartTesting Name} {ReportAbout ""} {ReportAbout 'Testing ' # Name # '...'} end %% How many failures so far? fun {GetFailures} @TotalFailures end %% Print a message about how many failures were seen. proc {DoneTesting} {ReportAbout 'Finished with ' # @TotalFailures # if @TotalFailures == 0 then ' failures!' elseif @TotalFailures == 1 then ' failure' else ' failures' end} TotalFailures := 0 end %% Test if Actual == Expected. %% If so, print a message, otherwise output a failure message. proc {Test Actual Connective Expected} if Actual == Expected then {ReportAbout {Value.toVirtualString Actual 5 20} # ' ' # Connective # ' ' # {Value.toVirtualString Expected 5 20}} else {NotifyAbout 'TEST FAILURE: ' # {Value.toVirtualString Actual 5 20} # ' ' # Connective # ' ' # {Value.toVirtualString Expected 5 20} } end end %% Test if Actual == Expected. %% If so, print a message, otherwise output a failure message. %% This assumes that Expected is a String. proc {TestString Actual Connective Expected} if Actual == Expected then %% they are equal, so Actual is also a String {ReportAbout Actual # ' ' # Connective # ' ' # Expected} else if {IsString Actual} then {NotifyAbout 'TEST FAILURE: ' # Actual # ' ' # Connective # ' ' # Expected} else {NotifyAbout 'TEST FAILURE: ' # {Value.toVirtualString Actual 5 20} # ' ' # Connective # ' ' # Expected} end end end %% Test if Actual == Expected. %% If so, print a message, otherwise output a failure message. %% This assumes that Expected is a . proc {TestLOS Actual Connective Expected} fun {Virtualize LOS} if LOS == nil then "n"#"il" else "[" # {FoldR LOS fun {$ X Res} '"'#X#'" '#Res end ""} # "]" end end in if Actual == Expected then %% they are equal, so Actual is also a {ReportAbout {Virtualize Actual} # ' ' # Connective # ' ' # {Virtualize Expected}} else if {IsList Actual} andthen {All Actual IsString} then {NotifyAbout 'TEST FAILURE: ' # {Virtualize Actual} # ' ' # Connective # ' ' # {Virtualize Expected}} else {NotifyAbout 'TEST FAILURE: ' # {Value.toVirtualString Actual 5 20} # ' ' # Connective # ' ' # {Virtualize Expected}} end end end end