source: project/wiki/assertions @ 37088

Last change on this file since 37088 was 34216, checked in by svnwiki, 2 years ago

Anonymous wiki edit for IP [206.190.75.8]: rm extra paren

File size: 1.2 KB
Line 
1[[tags:tutorials assert testing]]
2
3== How to use assertions in CHICKEN code
4
5At some point this file will hold some useful
6tutorial on how to include assertions in CHICKEN
7code, making it easy to detect errors in programs.
8
9Please help us improve it!
10
11== Use {{assert}}
12
13You can use {{assert}} with an expression that should
14always evaluate to true.  For example,
15
16<enscript highlight="scheme">
17 (define (string-first str)
18   (assert (string? str))
19   (string-ref str 0))
20</enscript>
21
22If the above function is called with, say, a number, you'll get
23the following error:
24
25 Error: assertion failed: (string? str)
26
27Note that this doesn't always point to the function where
28the assertion failed.  If you have multiple {{(string? str)}}
29assertions, you'll have to hunt it down.
30For this reason, [[alejandro forero cuervo|I]]
31include the function's name in the assertion:
32
33<enscript highlight="scheme">
34 (define (string-first str)
35   (assert (and 'string-first (string? str)))
36   (string-ref str 0))
37</enscript>
38
39In this case the error for the failed assertion will include
40the function's name, making it easier to find where it occurred.
41Of course, you will still need to find the caller...
Note: See TracBrowser for help on using the repository browser.