Ensure (keyword)

From RemObjects Software

Jump to: navigation, search

This is a Oxygene Keyword topic
Feel free to add your notes to this topic below.



The "ensure" post condition

Post conditions are a part of Class Contracts and describe conditions that are required to be true when after a method exits. They can be used to check for the validity of input parameters, results, or for the state of the object required by the method.

The ensure section consists of a list of Boolean statements, separated by semicolons.

Example

method MyObject.Add(aItem: ListItem);
begin
  InternalList.Add(aItem);
ensure
  Count > 0;
End;

There are times when you want to check the effect of the method on a field and the old keyword is provided for this purpose:

method MyObject.Add(aItem: ListItem);
begin
  InternalList.Add(aItem);
ensure
  Count = old Count +1;
End;

Notes

old is only supported for integers and value types.


Ensure Failure

If any of the boolean conditions in the ensure section fail, an assertion is raised containing information about one or more source lines. Consider the following code (a modification to the ASync sample shipped):

Image:Ensure1.png

The highlighted lines have been added and the requirement must fail. When the project is run, the following error is displayed:

Image:Ensure2.png

Note the line numbers. They show the line after the failed test plus the method's caller.


See Also


Product: RemObjects Oxygene (formerly known as Chrome)
Current version: 3.0 Previous Versions: 'Joyride' (2.0), 'Floorshow' (1.5), 'Adrenochrome' (1.0)

GlossaryKeywordsLanguage FeaturesPlatform FeaturesSamplesArticlesHow ToIssues

Personal tools