Smart pascal source code
procedure TestPositive(i: Integer);
require i >= 0; // first check
i > 0: 'must be strictly positive';
begin
WriteLn('TestPositive ' + IntToStr(i));
end;
function TestInc(i: Integer): Integer;
begin
Result := i + 1;
WriteLn('TestInc ' + IntToStr(Result));
ensure Result >= 0; // first check
Result > 10: 'must be above 10, was ' + IntToStr(Result);
end;
TestPositive(10);
try
TestPositive(-10);
except
on e: Exception do
WriteLn(e.Message);
end;
try
TestPositive(0);
except
on e: Exception do
WriteLn(e.Message);
end;
TestInc(10);
try
TestInc(-10);
except
on e: Exception do
WriteLn(e.Message);
end;
try
TestInc(0);
except
on e: Exception do
WriteLn(e.Message);
end;
{<<< RESULT - CONSOLE LOG >>>
-----------------------------
TestPositive 10
Pre-condition failed in TestPositive [line: 3, column: 4], i >= 0
Pre-condition failed in TestPositive [line: 4, column: 4], must be strictly positive
TestInc 11
TestInc -9
Post-condition failed in TestInc [line: 14, column: 4], Result >= 0
TestInc 1
Post-condition failed in TestInc [line: 15, column: 4], must be above 10, was 1
-----------------------------
{<<<<<<<<< THE END >>>>>>>>>}
function $CondFailed(z,m) { throw Exception.Create($New(EAssertionFailed),z+m); }
function $Check(i,z) { if (i) return i; throw Exception.Create($New(Exception),"Object not instantiated"+z); }
function WriteLn(value) {
if (window.console) { window.console.log(value); } };
function TestPositive(i) {
if (!(i>=0)) $CondFailed("Pre-condition failed in TestPositive [line: 4, column: 9], ","i >= 0");
if (!(i>0)) $CondFailed("Pre-condition failed in TestPositive [line: 5, column: 1], ","must be strictly positive");
WriteLn(("TestPositive "+i.toString()));
};
function TestInc(i$1) {
var Result = 0;
Result = i$1+1;
WriteLn(("TestInc "+Result.toString()));
if (!(Result>=0)) $CondFailed("Post-condition failed in TestInc [line: 14, column: 10], ","Result >= 0");
if (!(Result>10)) $CondFailed("Post-condition failed in TestInc [line: 15, column: 3], ",("must be above 10, was "+Result.toString()));
return Result
};
/* <<< main JS >>> */
TestPositive(10);
try {
TestPositive(-10);
} catch ($e) {
var e = $W($e);
WriteLn($Check(e,"").FMessage) }
try {
TestPositive(0);
} catch ($e) {
var e$1 = $W($e);
WriteLn($Check(e$1,"").FMessage) }
TestInc(10);
try {
TestInc(-10);
} catch ($e) {
var e$2 = $W($e);
WriteLn($Check(e$2,"").FMessage) }
try {
TestInc(0);
} catch ($e) {
var e$3 = $W($e);
WriteLn($Check(e$3,"").FMessage) }