# 1 assert

The <assert> is very important and it allows to detect failures in the program or in the XSQL Script Runner interpreter, because it causes the abortion of the program execution if the evaluation of the examined condition is not accomplished. The execution of the program stops showing the exception because the assert tag evaluates expressions such that if they are false it is that a situation that should never have occurred has been reached.

This tag allows to debug the program code and even detect 'bugs' of the XSQL Script Runner interpreter.

Then, it will be showed an example:

Copy
<xsql-script name='test_assert'>

<body>
<set name='a'>5</set>                       <!-- a=5 -->
<assert><eq><a/>5</eq></assert>

<set name='b'><mul><a/>2</mul></set>        <!-- b=10 -->

<set name='a'>5</set>                       <!-- a=5 -->
<println>The variable 'a' has the value '<a/>'</println>
<assert><eq><a/>4</eq></assert>

<set name='b'><mul><a/>2</mul></set>        <!-- ABORT !!! Stops the execution.  -->

</body>
</xsql-script>

Returns an error because it was aborted after it was determined that 'a' should be equal to 4.

This simple example tries to explain the operation and the objective of the tag assert. In this way, we can complicated, as much as desired, the condition to evaluate a more complex expression and with a sense within the program.

The following example tries to, with more sense than the previous, detect the failures in the XSQL Script Runner interpreter.

Copy
<xsql-script name='test_assert1'>

<body>

<set name='a'>5</set>                       <!-- a=5 -->
<println><assert><eq><variable.typeof><a/></variable.typeof>java.lang.Integer</eq></assert></println>

</body>
</xsql-script>

The result is:

Copy
true

If the XSQL Script Runner interpreter had a 'bug' in the <set> function, I would have ABORTED in the first instance. In this way, for example a failure in the automation of data types would be detected, because if the data type is not specified, the engine detects it automatically.

In this 'while' loop the use of the assert tag is clearly reflected.

Copy
<xsql-script name='test_assert2'>
<body>
<set name='a'>0</set>                           <!-- a=0 -->
<while>
<expr>
<lt><a/>100</lt>                        <!-- a<100 ?  -->
</expr>
<do>
<println>A=<a/></println>
<if>
<expr>
<gt>15<a/></gt>                 <!-- a>15 ?  -->
</expr>
<then>
<while.exit/>                   <!-- Si a>15 EXIT WHILE  -->
<assert><false/></assert>       <!-- If it was not done EXIT WHILE, it ABORTS.  -->
</then>
</if>

<println>a=<a/></println>
<assert><gt>15<a/></gt></assert>        <!-- If >15 ABORTS. It should have been done EXIT WHILE  -->

<while.continue/>                       <!-- EXIT CONTINUE -->
<println>After continue it should never appear </println>
<assert><false/></assert>               <!-- It should have been done EXIT CONTINUE. -->
</do>
</while>
</body>
</xsql-script>

The result is:

Copy
A=0