procedure contracts



procedure_contracts
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 >>>>>>>>>}