C.1 Simplified Unit Testing
Testing your code is very important. Because of Sheerpower's extremely fast
compile and link times (sub-second is typical), it is practical to add new
code and test it every few lines of code written. Sheerpower also makes it
practical to write the unit testing
code at the same time.
Sheerpower makes unit testing
quick and easy using just one statement
and two directives:
- option test on -- enable test mode
- %test some_statement to compile only if in test mode
- %test_ignore some_statement to ignore if in test mode
option test on
%test print 'We are in test mode'
age = 15
%test age = 25
debug show age
Let's say there is an include file filled with testing code. You could
conditionally compile and run that testing code by:
option test on
tax_rate = 6.5/100
%test %include 'safe_unit_tests.spinc'
Here is what safe_unit_tests.spinc looks like. Notice the use of the
statement. You tell assert
expected and it produces an
exception if the assert fails:
print 'Testing the tax_rate of '; tax_rate
assert tax_rate > 0 and tax_rate <= 1.00, 'Tax rate is out of bounds'
print 'Tax rate is fine'
For clarity in your code, you can explicitly turn off test mode by:
option test off
By default, test mode is turned off.
Your code can contain any number of option test on
or option test
statements. However, for easier code maintenance, just having one at
the top of your program is best. Your code can also contain any number of