# Cuprate TODO: pretty printing Cuprate is an experiment in providing a portable R6RS/R7RS testing library. It uses purely functional data structures in a mutable parameter object, allowing for procedural programming inside of a dynamic extent to not affect the rest of the test system. ## API ### `test-info` procedures test-info test-info? test-dto What SRFI-64 would call the "test runner" is in Cuprate the `test-info`, which contains test-info record containing a pure SRFI-225 dictionary. The dictionary must map at least symbols to values (including procedures). Whenever a test group or a test is entered, a new dynamic extent is entered with a new test-info object. Destructive updates to the new test info are not reflected in the test info of the call. When `test-info` is parameterized with a value satisfying `test-info?`, then that test-info is used in the dynamic extent of the invocation of `parameterize`. Otherwise, a new record is allocated, and the value passed to `parameterize` is inserted into that record. This new record does not share state with any other test-info. The dictionary inside the `test-info` parameter must be a dictionary according to the DTO in `test-dto`. The value in `test-dto` must be a DTO. test-set! test-update! test-update/default! test-delete-all! test-contains? test-ref test-ref/default test-update/default These procedures are the same as their SRFI-225 equivalents, except that the DTO and dictionary arguments are not needed, and that the mutating procedures return unspecified values. For instance, (test-set! key value) is equivalent to `(dict-set! dto dict key value)`. (modify-test-info! proc) Evaluates `(proc dict)`, where `dict` is the current `test-info` dict. The procedure must return a dict satisfying the same DTO. This dictionary is set as the current `test-info` dictionary within the dynamic extent. (inspect-test-info proc) Evaluates `(proc dict)`, where `dict` is the current `test-info` dict, and returns the result. ### `test-info` Standard Procedure Keys before-test (default-before-test name) A procedure with one argument (the name of the test). Called in the dynamic extent of the caller of the test. If it returns false, then the test is skipped. test-setup (test-setup) A procedure of zero arguments. Called in the dynamic extent of a test. Used to set up parts of a test. after-test (default-after-test dict) A procedure of one argument (the dictionary of the test). Called in the dynamic extent of the caller. Used to report information about the test, and to merge desired information into the calling test info. report-test (default-report-test dict) A procedure of one argument (the dictionary of the test). Used by `default-after-test` to report the result of the test to the user. group-begin (default-group-begin name) A procedure of one argument (the name of the group). Called in the dynamic extent of the group. Used to set up common information for a whole group. group-end (default-group-end dict) A procedure of one (the dictionary of the test). Called in the dynamic extent containing the group. Used to report information about the group and merge it with the containing test info. ### `test-info` Standard Keys * `success?`: Truthy if the test passed. * `exception`: The caught exception, if any. * `tests`: Number of run tests. * `passed`: Number of passed tests. * `failed`: Number of failed tests. * `skipped`: Number of skipped tests. * `verbose?`: Used by the default test setups. If false, only failures are printed. Otherwise all test case information is printed. ### Test Procedures and Forms If `test-name` is `#f`, then the test is not given a name. Every procedure here is implemented using `call-as-test`. (call-as-test test-name thunk) Call `thunk` with the test name `test-name`. First executes the procedure stored in `before-test` in the `test-info` of the caller. If that returns non-false, then it creates a new `test-info` inheriting from the `test-info` of the caller, and runs the procedure stored in `test-setup` (in the new `test-info`). Then `thunk` will be executed in this dynamic extent. If `thunk` throws an exception, it will be caught. Afterwards, the procedure stored in `after-test` will be run with the `test-info` of the caller. The test will set * `success?`: Will be set to `#f` if an exception was caught. * `exception`: Only set if an exception was caught. The value is the caught exception. (test-application test-name (name expr) ...) The `name` must be symbols that are mutually not `bound-identifier=?`. Runs a test with `test-name` that evaluates `(expr ...)`. The test will set (in addition to `call-as-test`): * `form`: To be `(expr ...)`, quoted. * `name ...`: To be the passed `expr ...` evaluated. * `success?`: If `(expr ...)` evaluates to not false. (with-test-assert test-name body ...) Execute `body` in a test with `test-name`. The test will set (in addition to `call-as-test`): * `success?`: The returned value of `body` (if an exception is not caught). (test-eq name %expected %actual) (test-eqv name %expected %actual) (test-equal name %expected %actual) Convienence wrappers for (test-application test-name ((procedure ) (expected %expected) (actual %actual))) (test-approximate X Y eps) Tests that |X - Y| <= eps (with-test-error name error-predicate body ...) Evaluates `body ...` in a test. This test will set * `success?`: False if evaluation does not throw an exception. Otherwise the return value of `error-predicate` on the thrown exception. (expect-to-fail body ...) ; syntax Execute all tests in `body ...`, reporting a success if they fail and reporting a failure if the succeed. ## Porting Guide This library requires `make-parameter` and `parameterize` to work like in R7RS. Most R6RS implementations should support dynamic parameters out of the box. The macro `define-test-application` uses the `letrec` trick to create temporary variables in pure `syntax-rules` for use in the pattern side of a generated `syntax-rules` macro. Some implementations, like CHICKEN 5.4.0 (kinda) and Foment, do not work correctly with this type of macro. If the macro doesn't work in your implementation, you will probably need to rewrite it in a low-level macro system. (If your implementation does not offer a low-level macro system, then bug the maintainer of your implementation to fix hygiene in their macro expander.) ## Instructions Per Implementation ### CHICKEN Just run `chicken-install cuprate`. Because of a bug with compiled syntax-rules patterns, `define-test-application` has limited support. ### Foment You will need `srfi-225`. The [reference implementation][SRFI-225] will work out of the box. Test bodies cannot return multiple values. [SRFI-225]: https://github.com/scheme-requests-for-implementation/srfi-225 ### Chibi You will need `srfi-225`. ### TR7 I tried but there were some issues with loading sublibraries. ### Gauche You will need `srfi-225`. I had to explicitly remove some of the conditional exports from the reference implementation of SRFI 225 in order to get it to work.