The following diagram shows the classes used in the Calculator example of the use of the JADE unit testing framework.
The main classes in the Calculator example are:
Calculator, which is a class with calculator-style functionality
TestCalculator, which is the unit test class for testing Calculator functionality
Listener, which is a listener class that outputs unit test results to a file on disk
The Calculator class, which has an attribute of type
add(n: Integer) updating; begin result := result + n; end; clear() updating; begin // Cleans the result result := 0; end; divide(n: Integer) updating; begin result := result div n; end; getResult(): Integer; begin return result; end; multiply(n: Integer) updating; begin // not implemented yet end; square() updating; begin result := result * result; end; squareRoot() updating; begin //Bug: loops indefinitely while true do endwhile; end; subtract(n: Integer) updating; begin //Bug: should be result = result - n result := result - 1; end; switchOff(); begin write "bye bye"; // Display "bye bye", beep, switch off the screen end; switchOn() updating; begin // Switch on the screen, display "hello", beep // and do other things that calculators do nowadays write "hello"; result := 0; end;
The TestCalculator class, which is a subclass of the
add() unitTest; begin calculator.add(1); calculator.add(1); assertEquals(calculator.getResult(), 2); end; clearBeforeTest() unitTestBefore; begin calculator.clear; end; divide() unitTest; begin // BUG 8 div 2 is 4 not 5 calculator.add(8); calculator.divide(2); assertEquals(calculator.getResult(), 5); end; divideByZero() unitTest; begin expectedException(4030); calculator.divide(0); end; multiply() unitTestIgnore; // not ready yet begin calculator.add(10); calculator.multiply(10); assertEquals(calculator.getResult(), 100); end; square() unitTest; begin calculator.add(10); calculator.square(); assertEquals(calculator.getResult(), 100); end; subtract() unitTest; begin calculator.add(10); calculator.subtract(2); assertEquals(calculator.getResult(), 8); end; turnOnCalculator() unitTestBeforeClass, updating; begin create calculator transient; calculator.switchOn; end; turnoffCalculator() unitTestAfterClass, updating; begin calculator.switchOff; delete calculator; end;
The Listener class, which implements the
finish(elapsedTime: Time; testsFailed: Integer; testsSkipped: Integer; testsSucceeded: Integer); begin file.writeLine(Tab & "Elapsed time=" & elapsedTime.String & " failed=" & testsFailed.String & " skipped=" & testsSkipped.String & " succeeded=" & testsSucceeded.String); end; message(messageText: String); begin // not implemented end; start(numberOfTestMethods: Integer); vars ts: TimeStamp; begin file.writeLine("UNIT TEST RUN " & ts.String & " (" & numberOfTestMethods.String & " tests)"); end; testFailure(testMethodName: String; callStack: String; failureReason: String); begin file.writeLine(Tab & "Fail" & Tab & testMethodName & " (" & failureReason & ")"); file.writeLine(callStack); end; testSkipped(testMethodName: String); begin file.writeLine(Tab & "Skip" & Tab & testMethodName); end; testSuccess(testMethodName: String); begin file.writeLine(Tab & "Success" & Tab testMethodName); end;
The following JadeScript method runs the calculator unit tests and appends the results to a file C:\UnitTests\results.txt.
captureToFile(); vars tests : ObjectArray; listener : Listener; jtr : JadeTestRunner; begin create listener transient; create listener.file transient; listener.file.mode := File.Mode_Append; listener.file.fileName := "C:\UnitTests\results.txt"; create tests transient; tests.add(TestCalculator); create jtr transient; jtr.setTestListener(listener); jtr.runTests(tests); epilog delete tests; delete listener; delete listener.file; delete jtr; end;