Debugging Scripts with Assertions


Assertions are a debugging technique that helps the developer detect incorrect assumptions as soon as they occur, rather than hundreds of lines later when something finally fails.

An assertion can be a simple check to confirm if a condition is true. Simple assertions can be used to verify input values, output results, or current states.

An assertion says:

“At this point in the script, I expect this condition to be true.”

If it isn’t, something has gone wrong. When an assertion fails, the script should inform the developer. At that point there may be options to stop the script, pause to check other values, or ignore the assertion and continue.

Assertion structure

We can add an assertion to an existing script like this:


# --- script steps before the assertion
# assertion tests the condition
If [ ( Get ( LayoutName ) = "Email Form" ) ]
 # assertion passed
Else
 Show Custom Dialog [ 
       "Assertion Failed" ;
       "Incorrect layout." ]
 Set Variable [ $choice = Get(LastMessageChoice) ]
 If [ $choice = 1 // stop ]
   Halt Script
 Else If [ $choice = 2 // pause ]
   Pause/Resume Script [ Indefinitely ]
 Else If [ $choice = 3 // ignore ]
   # continue script
 End If
End If
# --- script steps after the assertion

In this example, we are saying "At this point in the script, I expect the layout to be Email Form." If that is true, there is no further action. If false, there is a dialog showing a message of what failed and three buttons – Stop, Pause and Ignore.

Stop will halt the script and all other scripts in the stack – stop it dead in its tracks.

Pause allows the developer to check other values and states using the Data Viewer on the Current and Watch tabs. Then to cancel or continue as needed.

Ignore allows the developer to note the issue but continue the script anyway.

The issue with this method is that we have to add a dozen script steps for each assertion we add to the script.

There must be a better way. Tomorrow.


Are you coming to Reconnect.Christchurch, 15-16 October?
Get all the details and purchase tickets at Reconnect.Christchurch.

Are you late to the ScriptLogic party? If you have missed out on some of these emails, catch up here: Email Archive

If you know someone else who you think might like this email, you can send them this link to subscribe: Subscribe to ScriptLogic Daily