Method Contracts



Method Contracts | require | ensure
Smart pascal source code
type TBase = class procedure Check(i: Integer); virtual; end; type TChild = class(TBase) end; type TSubChild = class(TBase) procedure Check(i: Integer); override; end; procedure TBase.Check(i: Integer); require i > 0; begin WriteLn('base ' + IntToStr(i)); ensure i < 10; end; procedure TSubChild.Check(i: Integer); begin WriteLn('subchild ' + IntToStr(i)); ensure i < 5: 'was ' + IntToStr(i); end; var b = TBase.Create; WriteLn('TBase'); b.Check(1); try b.Check(-1); except on E: Exception do WriteLn(E.Message); end; b.Check(7); try b.Check(10); except on E: Exception do WriteLn(E.Message); end; var c = TChild.Create; WriteLn('TChild'); c.Check(1); try c.Check(-1); except on E: Exception do WriteLn(E.Message); end; c.Check(7); try c.Check(10); except on E: Exception do WriteLn(E.Message); end; var s = TSubChild.Create; WriteLn('TSubChild'); s.Check(1); try s.Check(-1); except on E: Exception do WriteLn(E.Message); end; s.Check(4); try s.Check(7); except on E: Exception do WriteLn(E.Message); end; try s.Check(10); except on E: Exception do WriteLn(E.Message); end; {<<< RESULT - CONSOLE LOG >>> ----------------------------- TBase base 1 Pre-condition failed in TBase.Check [line: 15, column: 4], i > 0 base 7 base 10 Post-condition failed in TBase.Check [line: 19, column: 4], i < 10 TChild base 1 Pre-condition failed in TBase.Check [line: 15, column: 4], i > 0 base 7 base 10 Post-condition failed in TBase.Check [line: 19, column: 4], i < 10 TSubChild subchild 1 Pre-condition failed in TBase.Check [line: 15, column: 4], i > 0 subchild 4 subchild 7 Post-condition failed in TSubChild.Check [line: 26, column: 4], was 7 subchild 10 Post-condition failed in TSubChild.Check [line: 26, column: 4], was 10 ----------------------------- {<<<<<<<<< THE END >>>>>>>>>}