Testing and Debugging

This chapter discusses how to:

Click to jump to parent topicUsing the Test Feature

When developing an SQR program, you frequently test it by running it and examining its output. Often, you are interested only in the first few pages of the report.

To speed up the cycle of running and viewing a few pages, use the -T command-line flag. The -T flag enables reports to finish more quickly because all BEGIN-SELECT ORDER BY clauses are ignored. The database does not sort the data, and the first set of records are selected sooner. Enter the appropriate number of test pages following the -T flag. For example, -T6 causes the program to stop after six pages of output have been created.

Note. If the program contains break logic, the breaks can occur in unexpected locations because the ORDER BY clause is ignored.

To test a report file called customer.sqr, enter the following command:

sqr customer username/password -T3

The -T3 flag specifies that the program stops running after three pages have been produced.

When the test finishes successfully, check it by displaying the output file on the screen or by printing it. The default name of the output file is the same as the program file with the .LIS extension. For example, if the report is named customer.sqr, the output file is named customer.lis.

When you finish developing the program, run it without the -T flag. The program processes all ORDER BY clauses and runs to completion. If the program creates more than one report, the -T flag restriction applies only to the first report.

Click to jump to parent topicUsing the #DEBUG Command

When debugging a program, you should:

SQR provides the #DEBUG command to help you make temporary changes to the code. Use the #DEBUG command to conditionally process portions of the program.

Precede the command with #DEBUG, as shown in the following example:

#debug display $s

When the #DEBUG precedes a command, that command is processed only if the -DEBUG flag is specified on the SQR command line. In this example, the value of $s is displayed only when you run the program with -DEBUG.

You can obtain multiple debug commands by using up to 10 letters or digits to differentiate between them. Indicate which command is to be debugged on the -DEBUG flag, as shown in the following example:

sqr myreport username/password -DEBUGabc

In this example, commands that are preceded by #DEBUG, #DEBUGa, #DEBUGb, or #DEBUGc are compiled when the program is run. Commands that are preceded with #DEBUGd are not compiled because d was not specified in the -DEBUG command-line flag.

Click to jump to parent topicUsing Compiler Directives for Debugging

You can conditionally compile entire sections of a program by using the five compiler directives:

Use the value of a substitution variable, declared by a #DEFINE command, to activate or deactivate a set of statements, as shown in the following example:

#define DEBUG_SESSION Y #if DEBUG_SESSION = 'Y' begin-procedure dump_array let #i = 0 while #i < #counter ! Get data from the array get $state $city $name $phone from customer_array(#i) print $state (,1) print $city (,7) print $name (,24) print $phone (,55) position (+1) add 1 to #i end-while end-procedure ! dump_array #end-if

The dump_array procedure is used only for debugging. Because DEBUG_SESSION is defined as Y, the dump_array procedure is included in the program. Later, you can change DEBUG_SESSION to N and exclude the dump_array procedure from the program.

Click to jump to parent topicAvoiding Common Programming Errors

The most common programming error when using SQR is misspelling variable names. Because SQR does not require variables to be declared, it does not issue an error message when variable names are misspelled. Instead, SQR considers the misspelled variable as if it is another variable.

For example:

let #customer_access_code = 55 print #customer_acess_code ()

This example does not print 55 because the variable name is misspelled. One c in access in the PRINT command is missing.

A related problem involves global versus local variables. If you refer to a global variable in a local procedure without preceding it with an underscore, SQR does not issue an error message. Instead, it is taken as a new local variable name. For example:

begin-procedure main let $area = 'North' do proc end-procedure ! main begin-procedure proc local print $area () ! Should be $_area end-procedure

In this example, the proc local procedure prints the value of the local $area variable and not the global $area variable. It prints nothing because the local $area variable did not receive a value. To refer to the global variable, use $_area.

Such small errors are difficult to detect because SQR considers #customer_acess_code as just another variable with a value of zero.