source: project/wiki/eggref/5/srfi-78 @ 39315

Last change on this file since 39315 was 39315, checked in by gnosis, 2 months ago

SRFI-78: Documented version bump to 0.2

File size: 10.6 KB
Line 
1== SRFI-78: Lightweight testing
2=== Abstract
3A simple mechanism is defined for testing Scheme programs.
4
5As a most primitive example, the expression
6
7<enscript highlight="scheme">
8(check (+ 1 1) => 3)
9</enscript>
10
11evaluates 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
12
13<enscript highlight="scheme">
14(+ 1 1) => 2 ; *** failed ***
15; expected result: 3
16</enscript>
17
18Moreover, 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.
19
20In 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 [[https://srfi.schemers.org/srfi-42|SRFI 42]] [5]. For example,
21
22<enscript highlight="scheme">
23(check-ec (:range e 100)
24          (:let x (expt 2.0 e))
25          (= (+ x 1) x) => #f (e x))
26</enscript>
27
28This 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:
29
30<enscript highlight="scheme">
31(let ((e 53) (x 9007199254740992.0)) (= (+ x 1) x)) => #t ; *** failed ***
32 ; expected result: #f
33</enscript>
34
35The specification of bindings to report, {{(e x)}} in the
36example, is optional but very informative.
37==== Other features of this SRFI are:
38* A way to specify a different equality predicate (default is {{equal?}}).
39* Controlling the amount of reporting being printed.
40* Switching off the execution and reporting of checks entirely.
41* Retrieving a boolean if all checks have been executed and passed.
42==== For more information see:
43[[https://srfi.schemers.org/srfi-78/|SRFI 78: Lightweight testing]]
44=== Rationale
45The mechanism defined in this SRFI should be available in every Scheme system because it has already proven useful for interactive development---of SRFIs.
46
47Although it is extremely straight-forward, the origin of the particular mechanism described here is the 'examples.scm' file accompanying the reference implementation of SRFI 42. The same mechanism has been reimplemented for the reference implementation of SRFI 67, and a simplified version is yet again found in the reference implementation of SRFI 77.
48
49The mechanism in this SRFI does not replace more sophisticated approaches to unit testing, like SRFI 64 [1] or SchemeUnit [2]. These systems provide more control of the testing, separate the definition of a test, its execution, and its reports, and provide several other features.
50
51Neil Van Dyke's Testeez library [3] is very close in spirit to this SRFI. In Testeez, tests are disabled by (re-)defining a macro. The advantage of this method is that the code for the test cases can be removed entirely, and hence even the dependency on the Testeez library. This SRFI on the other hand, uses a Scheme conditional ({{COND}}, {{IF}}) to prevent execution of the testing code. This method is more dynamic but retains dead testing code, unless a compiler and a module system are used to apply constant folding and dead code elimination. The only major addition in SRFI over Testeez is the comprehension for formulating parametric tests.
52==== Design considerations for this SRFI include the following:
53* Reporting is human-readable and as specific as possible, i.e. not just "assertion failed" but the expression with actual and expected value, and if possibly the relevant part of the bindings environment.
54* An effort is made to print closed Scheme expressions, i.e. expressions that can directly be copy/pasted into a REPL for further analysis (e.g. the let expression in the abstract).
55* By default the checks report both correct and failed checks. However, it is possible to reduce the output---or even to switch off the execution of checks. It has turned out useful to be able to run only some subset checks for the features currently under development. This can be done by changing the reporting mode between differnt sections.
56* The global state (correct/failed count) is not made available to the user program. This reduces the dependencies between different checks because it is not possible to use the state.
57* Occasionally, it is useful to check that a certain expression does *not* yield an ordinary result but raises an error. However, R5RS [4] does not specify the mechanism by which this occurs (e.g. raising exception, breaking into a REPL, aborting the program, etc.). For this reason, this SRFI is restricted to the case that the checked expressions evaluate normally.
58* Though usually I am very much in favor of strictly prefix syntax, for this SRFI I make an exception because the infix "=>" syntax is widely used and intuitive.
59=== Specification
60==== check
61
62<macro>(check <expr> (=> <equal>) <expected>)</macro>
63
64<macro>(check <expr>  =>          <expected>)</macro>
65
66evaluates {{<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}}.
67
68The 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)}}
69==== check-ec
70
71<macro>(check-ec <qualifier>^* <expr> (=> <equal>) <expected> (<argument>^*))</macro>
72
73<macro>(check-ec <qualifier>^* <expr>  =>          <expected> (<argument>^*))</macro>
74
75<macro>(check-ec <qualifier>^* <expr> (=> <equal>) <expected>)</macro>
76
77<macro>(check-ec <qualifier>^* <expr>  =>          <expected>)</macro>
78
79an eager comprehension for executing a parametric set of checks.
80
81Enumerates the sequence of bindings specified by {{<qualifier>^*}}. 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.
82
83The 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.
84
85In case the {{check}} fails {{<argument>^*}} is used for constructing an informative message with the argument values. Use {{<argument>^*}} to list the relevant free variables of {{<expr>}} (see examples) that you want to have printed.
86
87A {{<qualifier>}} is any qualifier of an eager comprehension as specified in [[https://srfi.schemers.org/srfi-42|SRFI 42]] [1].
88===== Examples:
89<enscript highlight="scheme">
90(check-ec (: e 100) (positive? (expt 2 e)) => #t (e)) ; fails on fixnums
91(check-ec (: e 100) (:let x (expt 2.0 e)) (= (+ x 1) x) => #f (x)) ; fails
92(check-ec (: x 10) (: y 10) (: z 10)
93          (* x (+ y z)) => (+ (* x y) (* x z))
94          (x y z)) ; passes with 10^3 cases checked
95</enscript>
96==== check-report
97
98<procedure>(check-report)</procedure>
99
100prints a summary and the first failed {{check}}, if there is any, depending on the current mode settings.
101==== check-set-mode!
102
103<procedure>(check-set-mode! mode)</procedure>
104
105sets the current mode to {{mode}}, which must be a symbol in {{'(off summary report-failed report)}}, default is {{'report}}.
106===== The mode symbols have the following meaning:
107* off:           do not execute any of the checks
108* summary:       print only summary in {{(check-report)}} and nothing else
109* report-failed: report failed checks when they happen, and in summary
110* report:        report every example executed
111===== Note that you can change the mode at any time, and that {{check}}, {{check-ec}} and {{check-report}} use the current value.
112==== check-reset!
113
114<procedure>(check-reset!)</procedure>
115
116resets 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.
117==== check-passed?
118
119<procedure>(check-passed? expected-total-count)</procedure>
120
121{{#t}} if there were no failed checks and expected-total-count correct checks, {{#f}} otherwise.
122
123Rationale: This procedure can be used in automatized tests by terminating a test program with the statement {{(exit (if (check-passed? <n>) 0 1))}}.
124=== References
125* [1] [[http://srfi.schemers.org/srfi-64|SRFI 64]] by Per Bothner: A Scheme API for test suites. January 2005.
126* [2] Noel Welsh: [[http://schematics.sourceforge.net/schemeunit.html|SchemeUnit]]. February 2003.
127* [3] Neil Van Dyke: [[http://www.neilvandyke.org/testeez|Testeez]], Lightweight Unit Test Mechanism for Scheme. May 2005.
128* [4] [[http://www.schemers.org/Documents/Standards/R5RS/|Revised^5 Report on the Algorithmic Language Scheme]] (R5RS).
129* [5] [[http://srfi.schemers.org/srfi-42|SRFI 42]] by Sebastian Egner: Eager Comprehensions.
130=== Author
131* Sebastian.Egner@philips.com
132* Ported to Chicken Scheme 5 by Sergey Goldgaber
133=== Copyright
134Copyright (C) Sebastian Egner (2005-2006). All Rights Reserved.
135
136Permission is hereby granted, free of charge, to any person obtaining
137a copy of this software and associated documentation files (the "Software"),
138to deal in the Software without restriction, including without limitation
139the rights to use, copy, modify, merge, publish, distribute, sublicense,
140and/or sell copies of the Software, and to permit persons to whom the
141Software is furnished to do so, subject to the following conditions:
142
143The above copyright notice and this permission notice shall be included
144in all copies or substantial portions of the Software.
145
146THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
147OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
148FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
149THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
150LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
151FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
152DEALINGS IN THE SOFTWARE.
153=== Version history
154* [[https://github.com/diamond-lizard/srfi-78/releases/tag/0.2|0.2]] - Fix for [[https://github.com/diamond-lizard/srfi-78/issues/7|issue 7]]
155* [[https://github.com/diamond-lizard/srfi-78/releases/tag/0.1|0.1]] - Ported to Chicken Scheme 5
156
Note: See TracBrowser for help on using the repository browser.