-- Overture STANDARD LIBRARY: VDM-Unit -- -------------------------------------------- -- Version 2.1.0 -- -- Standard library for the Overture Interpreter. When the interpreter -- evaluates the preliminary functions/operations in this file, -- corresponding internal functions is called instead of issuing a run -- time error. Signatures should not be changed, as well as name of -- module (VDM-SL) or class (VDM++). Pre/post conditions is -- fully user customisable. -- Dont care's may NOT be used in the parameter lists. -- -- -- define VDM++ equivalents to the JUnit 3.8.2 classes -- --=========================================================================== -- Using VDMUnit -- -- VDM Unit generally supports the same features as JUnit for Java -- -- Approach: -- * Define one Test Case per class which should be tested -- ** Specify all test *operations* in the test case -- * Define a TestResult and TestSuite to execute the tests -- -- Running the tests: -- Option 1. -- Use the automated reflection search to find Test classes. -- new TestRunner().run() -- -- Option 2. -- Use the automated reflection search to find test operations. -- All operations must begin with "test". -- Usage: -- let tests : set of Test = {new TestCase1(), new TestCase2()}, -- ts : TestSuite = new TestSuite(tests), -- result : TestResult = new TestResult() -- in -- ( -- ts.run(result); -- IO`print(result.toString()); -- ); -- -- Option 3. -- Use explicit definition to call tests. -- All test cases must *override* the operation -- "protected runTest : () ==> ()" -- This means that only a single test can be performed in each test case -- -- Usage: -- let ts : TestSuite = new TestSuite(), -- result = new TestResult() -- in -- ( -- ts.addTest(new TestCase1()); -- ts.addTest(new TestCase2()); -- ts.run(result); -- IO`println(result.toString()); -- ) -- --===========================================================================
-- forward definition of java.lang.Throwable -- do not code generate (but import from Java library) class Throwable end Throwable
-- forward definition of java.lang.Error -- do not code generate (but import from Java library) classErrorissubclassof Throwable
-- -- forward definition of junit.framework.TestCase --
class TestCase issubclassof Test
operations
-- /** -- * A convenience method to run this test, collecting the results with a -- * default TestResult object. -- * -- * @see TestResult -- */ public run :() ==> TestResult
run() ==
( letresult : TestResult = createResult() in (
run(result); returnresult);
); -- /** -- * Runs the test case and collects the results in TestResult. -- */ public run : TestResult ==> ()
run(result) ==
( result.run(self);
);
public runOnly: seqofchar * TestResult ==> ()
runOnly (name,result) == if name = getName() then result.run(self);
-- -- This method might be overriden in a sub class or it will -- use reflection to call the test method as specified in the name -- protected runTest: () ==> ()
runTest () == reflectionRunTest(self,getName());
trap exc :Throwable with
exception := exc in
(
runTest();
);
trap exc :Throwable with if exception = nilthen
exception := exc in
(
tearDown();
);
if exception <> nilthenexit exception;
);
public tearDown: () ==> ()
tearDown () == skip;
end TestCase
-- -- forward definition of junit.framework.TestSuite --
class TestSuite issubclassof Test
instancevariables private fTests : seqof Test := []
operations public TestSuite: seqofchar ==> TestSuite
TestSuite (name) == setName(name);
public TestSuite: () ==> TestSuite
TestSuite () == setName("");
public TestSuite: setof Test * seqofchar ==> TestSuite
TestSuite (tests,name) ==
(
setName(name); forall test inset tests do
( forall t insetelems createTests(test) do
(
addTest(t);
);
);
);
public TestSuite: setof Test ==> TestSuite
TestSuite (tests) ==
(
setName(""); forall test inset tests do
( forall t insetelems createTests(test) do
(
addTest(t);
);
);
);
public TestSuite: Test ==> TestSuite
TestSuite (test) ==
(
setName(""); forall t insetelems createTests(test) do
(
addTest(t);
);
);
public TestSuite: Test * seqofchar ==> TestSuite
TestSuite (test,name) ==
(
setName(name); forall t insetelems createTests(test) do
(
addTest(t);
);
);
public addTest: Test ==> ()
addTest (ptst) == fTests := fTests ^ [ptst];
-- implementing the abstract run operation public run: TestResult ==> ()
run (ptr) ==for ptst in fTests do ptst.run(ptr);
public runOnly: seqofchar * TestResult ==> ()
runOnly (name,ptr) == for ptst in fTests do if ptst.getName() = name then
ptst.run(ptr);
public run: Test * TestResult ==> ()
run (test, ptr) == test.run(ptr);
public tests : () ==> seqof Test
tests()==return fTests;
public testCount : () ==> int
testCount()== returnlen fTests;
public testAt : int ==> Test
testAt(index) == return fTests(index) pre index >0 and index < len fTests;
private getTestMethodNamed : Test ==> seqofseqofchar
getTestMethodNamed(test)== isnotyetspecified;
public createTests : Test ==> seqof Test
createTests(test)== isnotyetspecified;
end TestSuite
-- -- forward definition of junit.framework.TestListener --
class TestListener
operations public initListener: () ==> ()
initListener () == issubclassresponsibility;
public exitListener: () ==> ()
exitListener () == issubclassresponsibility;
public addFailure: Test * AssertionFailedError ==> ()
addFailure (-, -) == issubclassresponsibility;
public addError: Test * Throwable ==> ()
addError (-, -) == issubclassresponsibility;
public startTest: Test ==> ()
startTest (-) == issubclassresponsibility;
public endTest: Test ==> ()
endTest (-) == issubclassresponsibility;
end TestListener
-- -- forward definition of junit.framework.TestResult --
-- count the number of executed tests private fRunTests : nat := 0;
-- the list of interested listeners private fListeners : setof TestListener := {}
operations public addListener: TestListener ==> ()
addListener (ptl) ==
( -- first add the listener to the set
fListeners := fListeners union {ptl}; -- execute the initializer for the listener
ptl.initListener() ) pre ptl notinset fListeners;
public removeListener: TestListener ==> ()
removeListener (ptl) ==
( -- execute the exit operation for the listener
ptl.exitListener(); -- remove the listener from the set
fListeners := fListeners \ {ptl} ) pre ptl inset fListeners;
public addFailure: Test * AssertionFailedError ==> ()
addFailure (ptst, pafe) ==
( -- first handle the failed assertion locally if pafe.hasMessage() then fFailures := fFailures ^ [pafe] elselet str = "Assertion failed in test "^ptst.getName() in
fFailures := fFailures ^ [new AssertionFailedError(str)]; -- now inform all listeners forall listener inset fListeners do
listener.addFailure(ptst, pafe) );
public addError: Test * Throwable ==> ()
addError (ptst, perr) ==
( -- add the error to the list of exceptions caught so far
fErrors := fErrors ^ [mk_InternalError(ptst.getName(), perr)]; -- now inform all listeners forall listener inset fListeners do
listener.addError(ptst, perr) );
public startTest: Test ==> ()
startTest (ptst) ==
( -- first increase the local run test counter
fRunTests := fRunTests + 1; -- now inform all listeners forall listener inset fListeners do
listener.startTest(ptst) );
public endTest: Test ==> ()
endTest (ptst) == forall listener inset fListeners do
listener.endTest(ptst);
public run : TestCase ==> ()
run(test)==
(
startTest(test);
runProtected(test);
endTest(test);
); public runProtected : TestCase ==> ()
runProtected(test) ==
( trap exc :Throwable with ifisofclass(AssertionFailedError,exc) then addFailure(test, exc) elseifisofbaseclass(Throwable, exc) then addError(test, exc) elseerror-- We hope this never happens in (
test.runBare();
);
);
public runCount: () ==> nat
runCount () == return fRunTests;
public failureCount: () ==> nat
failureCount () == returnlen fFailures;
public errorCount: () ==> nat
errorCount () == returnlen fErrors;
public wasSuccessful: () ==> bool
wasSuccessful () == return failureCount() = 0 and errorCount() = 0;
if wasSuccessful() then
(
tmp := tmp^"| SUCCESS |";
) else
(
tmp := tmp^"| FAILURE |";
);
tmp := tmp^"\n|______________________________________|"; return tmp;
); functions ------------------------------------- -- Convert a int to a String ------------------------------------- publicstatic ToStringInt : int | nat1 | nat -> seqofchar
ToStringInt(val) == letresult : int = val mod 10,
rest : int = val div 10 inif rest > 0 then
ToStringInt(rest) ^ GetStringFromNum(result) else GetStringFromNum(result) pre val >= 0 measure ToStringIntMeasure;
------------------------------------- -- Convert a int < 10 to a String ------------------------------------- publicstatic GetStringFromNum : int -> seqofchar--| nat | real
GetStringFromNum(val) == ["0123456789"(val+1)] pre val < 10;
end TestResult
class TestRunner
operations public run : () ==> ()
run() == let tests : setof Test = collectTests(new TestRunner()),
ts : TestSuite = new TestSuite(tests), result : TestResult = new TestResult() in
(
ts.run(result);
IO`print(result.toString());
);
collectTests : TestRunner ==> setof Test
collectTests(obj) == isnotyetspecified;
end TestRunner
¤ Dauer der Verarbeitung: 0.15 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.