This page includes excerpts from the SRFI document, but is primarily intended to document the forms exported by the egg. For a full description of the SRFI, see the SRFI document.
A simple mechanism is defined for testing Scheme programs.
As a most primitive example, the expression
(check (+ 1 1) => 3)
evaluates the expression (+ 1 1) and compares the result with the expected result 3 provided after the syntactic keyword =>. Then the outcome of this comparison is reported in human-readable form by printing a message of the form
(+ 1 1) => 2 ; *** failed *** ; expected result: 3
Moreover, the outcome of any executed check is recorded in a global state counting the number of correct and failed checks and storing the first failed check. At the end of a file, or at any other point, the user can print a summary.
In addition to the simple test above, it is also possible to execute a parametric sequence of checks. Syntactically, this takes the form of an eager comprehension in the sense of SRFI 42. For example,
(check-ec (:range e 100) (:let x (expt 2.0 e)) (= (+ x 1) x) => #f (e x))
This statement runs the variable e through 0..99 and for each binding defines x as (expt 2.0 e). Then it is checked if (+ x 1) is equal to x, and it is expected that this is not the case (i.e. expected value is #f). The trailing (e x) tells the reporting mechanism to print the values of both e and x in case of a failed check. The output could look like this:
(let ((e 53) (x 9007199254740992.0)) (= (+ x 1) x)) => #t ; *** failed *** ; expected result: #f
The specification of bindings to report, (e x) in the example, is optional but very informative.
Other features of this SRFI are:
- A way to specify a different equality predicate (default is equal?).
- Controlling the amount of reporting being printed.
- Switching off the execution and reporting of checks entirely.
- Retrieving a boolean if all checks have been executed and passed.
- (check expr = expected)syntax
Evaluates expr and compares the value to the value of expected using the predicate equal, which is equal? when omitted. Then a report is printed according to the current mode setting (see below) and the outcome is recorded in a global state to be used in check-report.
The precise order of evaluation is that first equal and expected are evaluated (in unspecified order) and then expr is evaluated. Example: (check (+ 1 1) = 2)
- (check-ec qualifier₁ qualifier₂ … expr = expected)syntax
An eager comprehension for executing a parametric set of checks.
Enumerates the sequence of bindings specified by the qualifiers. For each binding evaluates equal and expected in unspecified order. Then evaluates expr and compares the value obtained to the value of expected using the value of equal as predicate, which is equal? when omitted.
The comprehension stops after the first failed check, if there is any. Then a report is printed according to the current mode setting (see below) and the outcome is recorded in a global state to be used in check-report. The entire check-ec counts as a single check.
In case the check fails, the arguments are used for constructing an informative message with the argument values. Use arguments to list the relevant free variables of expr (see examples) that you want to have printed.
A qualifier is any qualifier of an eager comprehension as specified in SRFI 42.
(check-ec (: e 100) (positive? (expt 2 e)) => #t (e)) ; fails on fixnums (check-ec (: e 100) (:let x (expt 2.0 e)) (= (+ x 1) x) => #f (x)) ; fails (check-ec (: x 10) (: y 10) (: z 10) (* x (+ y z)) => (+ (* x y) (* x z)) (x y z)) ; passes with 10^3 cases checked
Prints a summary and the first failed check, if there is any, depending on the current mode settings.
- check-set-mode! modeprocedure
Sets the current mode to mode, which must be a symbol in '(off summary report-failed report), default is report.
The mode symbols have the following meaning:
- Do not execute any of the checks.
- Print only summary in check-report and nothing else.
- Report failed checks when they happen, and in summary.
- Report every example executed.
Resets the global state (counters of correct/failed examples) to the state immediately after loading the module for the first time, i.e. no checks have been executed.
- check-passed? expected-total-countprocedure
Returns #t if there were no failed checks and expected-total-count correct checks, and #f otherwise.
Rationale: This procedure can be used in automated tests by terminating a test program with the statement (exit (if (check-passed? n) 0 1)).
Ported to Chicken Scheme 5 by Sergey Goldgaber
wcm at sigwinch dot xyzzy minus the zy
Copyright (C) Sebastian Egner (2005-2006). All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.