REQUIRE
C++ Test Script Language
Syntax
REQUIRE <native expression>
Location
Description
The REQUIRE statement describes a method pre-condition. It can be used in a WRAP, STUB or PROC block.
Note The information below pertains to the use of REQUIRE within a WRAP block. For more information about using the REQUIRE and ENSURE statement within a STUB or PROC block, please refer to the STUB and PROC.
<native expression> is a C++ Boolean expression (or an expression that can be converted into a Boolean), which can use:
-
Any of the public or protected class members.
-
The method parameters (with the names used in the signature or in the method definition).
-
Any of the global variables declared in the file where the method is defined.
The following symbols cannot be used in the <native expression> parameter of the REQUIRE statement:
-
Local variables
-
Macros
Evaluation
The <native expression> parameter of the REQUIRE statement is evaluated before any code of the method is executed (local variables are not pushed yet).
Warning: you can call methods in <native expression>, but you must ensure that these calls do not modify the object's state by writing to any field. You can ensure this by calling const methods only.
Example
C++ source code example:
class Stack {
int count;
Stack () : count(0) {}
void push (void *);
void *pop ();
};
OTC code example:
CLASS Stack {
WRAP pop
REQUIRE (count > 0)
}