Statement Language¶
The Statement Language defines single Statements, for example, control flow statements. The most general element of the Statement layer is a Block. A Block is composed of zero or more successive statements.
Statement Language |
---|
Statements |
Assert Statements |
If Statements |
For Statements |
For Loops With Maps |
Blocks and Scopes |
Statements ¶
There are five types of Statements:
Statement Type | Example | Remark |
---|---|---|
Assignment | x := 2 |
Assigns an expression to a variable. |
Return statement | return "bonjour" |
Returns an expression from a function. |
Assert | assert not isWindows()! "The system is running on Windows" |
Throws an assert error and an optional string representation in the log if the expression returns a false statement. |
If statement | if x > 2 then y := 4 else y := 1 end |
Evaluates the given condition and performs operations accordingly. |
For statement | for i in [1, 2, 3] x := x + i end |
Performs a Block of statements multiple times. |
Assert Statements ¶
An Assert Statement message will appear in the intaQt Studio log if an "assert" expression evaluates to false
.
Syntax
1 | assert expression ! <assertionMessage String> |
-
expression - Any valid expression which returns a boolean statement
true
orfalse
-
assertionMessage - An optional string representation that will be logged if the assertion expression evaluates to
false
Feature File Example
1 2 3 | Feature: AssertMessage Scenario: AssertMessageTesting And test assert without message false |
The example below creates the booleanVar
parameter, which is set as false
in the Feature File.
The assert
Statement expression inside the function assertWithoutMessage
then creates an assertion error message.
Stepdef Example
1 2 3 4 5 6 7 8 9 | stepdef "test assert without message {}" / booleanVar / MyModel.assertWithoutMessage(booleanVar) end model MyModel func assertWithoutMessage(booleanVar) assert booleanVar end end |
Assert with Additional String Representation¶
The example below uses an exclamation mark (!) to indicate that an Additional String Representation will be added to the Assertion Statement message.
Feature File Example
1 2 3 | Feature: AssertMessage Scenario: AssertMessageTesting And test assert with message false |
Stepdef Example
1 2 3 4 5 6 7 8 9 | stepdef "test assert with message {}" / booleanVar / MyModel.assertWithMessage(booleanVar) end model MyModel func assertWithMessage(booleanVar) assert booleanVar ! "booleanVar is false" end end |
If Statements ¶
In the example below, expressions in square brackets ([]
) represent optional parts of the statement. A GUARD
evaluates to a Boolean expression (true
or false
), which indicates if the associated Block of statements is executed.
Example
1 2 3 4 5 6 7 8 9 | if GUARD-1 then BLOCK-1 [elif GUARD-2 then BLOCK-2] ... [elif GUARD-(n-1) then BLOCK-(n-1)] [else BLOCK-n] end |
-
If
GUARD-x
returnstrue
, thenBLOCK-x
is executed and theif
Statement is considered finished. -
If
GUARD-x
returnsfalse
, thenBLOCK-x
is skipped and the statement continues to check onGUARD-(x+1)
. -
If there is no matching guard,
BLOCK-n
, then theelse
clause is executed. Ifelse
is not present, theif
Statement is skipped.
Note:
If there are multiple matching GUARD
expressions, in other words, returning true, then the elif
(else if
) clause ensures that only the Block of the first matching guard is executed.
For Statements ¶
For
Statements provide simple loops with a value range given by a predefined list.
Example
1 2 3 | for [INDEX,] VALUE in RANGE BLOCK end |
INDEX
and VALUE
are Identifiers for loop variables. INDEX
represents an incrementing integer index and VALUE
represents the value in RANGE
, the list, at the given position. RANGE
can be a one-dimensional array or a list.
For Loop Example
1 2 3 4 | sum := 0 for ix, val in [2,4,6,8] println("index: "+ix+" value: "+val) end |
In the example above, the loop will iterate over each element specified in range (between the []
).
Parameters
-
ix - The index, therefore, elements can be accessed via element
[ix]
-
val - The value in the range at index
ix
The output for this loop should appear as:
1 2 3 4 | index: 0 value: 2 index: 1 value: 4 index: 2 value: 6 index: 3 value: 8 |
To use this as a standard for loop
(execute n
times), it should appear as:
1 | for val in range(from,to) //e.g. range(0, 100) |
In the example above, val
will take values from 0
to 100
(inclusive) with an increment of the default step size of 1
.
A different step size can be specified as follows:
1 | for val in range (from,to, step size) //e.g. range(0,100,2) |
val
will take values from 0
to 100
(inclusive), incrementing by 2
for each iteration.
Note:
The index variable (such as, ix
) of the for
loop specification is optional. If it is omitted, the range's element positions will not be available in the for
loop Block.
For Loops with Maps ¶
When using For
loops with Maps, the index variable specifies the keys. The following example uses simple Map called myMap
, which will be iterated over.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 | myMap := { "A" : 1, "B" : 2, "C" : 3 } s := "" n := 0 for key, val in myMap s := s + key n := n + val end |
This results in s
holding the string ABC
and n
holding the number 6
.
Blocks and Scopes ¶
An executed Block has an associated Scope which holds all of its accessible variables. Scopes are nested. For example, each time a subblock is about to be executed, the current Scope is enlarged to provide a new empty local Scope for the Block. When subblock execution is complete, the topmost local Scope layer is dumped. If the subblock applies any changes to variables defined outside the local Scope, the changes are visible even after the subblock's lifetime has passed.
In the illustration above, the subblock changes the x
variable that is located in the parent Scope. Even after the subscope has been dumped, the change is visible. The z
variable that has been created in the subblock is only accessible in the Block-local Scope: its lifetime ends when the subblock finishes executing.
Example
1 2 3 4 5 6 | x := 7 y := 10 if x > 5 then y := 12 z := 1 end |
Local scoping in Blocks applies to all Blocks ending with an end
. The above if
statement results in a scope that contains the variable y
holding 12
, while z
no longer exists.