aboutsummaryrefslogtreecommitdiffstats

Cuprate

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.

Cuprate supports TR7, CHICKEN-5, Foment, Chibi, SKINT, Gauche, and Sagittarius. STKLOS, Mosh, Chez, Guile, Racket, and Loko support soon.

TODO: Test rewriters.

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 <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.

Continuations

Cuprate will raise an exception if

  1. A test/group is exited more than once, or
  2. A test/group has a test/group run in it after it has run.

This means that continuations captured in one invocation of a test/group and used outside of it cannot have internal assertions inside of it, and cannot return through the test/group again.

Exiting a test/group more than once is certainly an error and I cannot imagine a scenario where that is desired.

The second scenario deserves more justification. One could create some coroutines, run asserts on them, and return them through a group to test more, with internal tests in them. However, because the test object is a parameter, the continuations will mutate the test-info object at the point of continuation capture. If the entire stack of test-info objects before the current one is stored in the object, then it would be possible to keep the test numbering and other information up to date. But the parameterized procedures are then unchangable.

The other issue is that requires exposing a lot of mutation in the test-info objects. This basically re-creates the design of SRFI-64. Another issue is that there is no way to know how many tests run in a group until the very end of the testing.

If one wants to write coroutines with internal assertions, then there needs to be a way to splice the current test-info as a parameterized value into the continuation of the coroutine. But since coroutines are like threads, it would be better to just not have internal assertions.

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.)

This library requires SRFI-225. In the compat directory there is a partial implementation of SRFI-225 that only works with alists, called micro-srfi-225, that only requires base R6RS/R7RS. You can use this if all you need is to run the tests.

Instructions Per Implementation

CHICKEN-5

There is an SRFI-225 port for CHICKEN-5, so just run chicken-install cuprate. Because of a bug with compiled syntax-rules patterns, define-test-application has limited support.

Foment

Tested with the latest checkout as of 2025-11-03.

The reference implementation SRFI-225 will work out of the box. Test bodies cannot return multiple values. Because of a bug with compiled syntax-rules patterns, define-test-application has limited support.

Chibi

Tested with 0.10.

The reference implementation of SRFI-225 will work out of the box.

TR7

Tested to work with 2.0.7.

To use SRFI-225, you will also need SRFI-128. Mini-SRFI-225 will also work here.

There is an issue with call/cc escaping out of parameterized blocks. This doesn't affect any of the test cases, but this may break some code.

Gauche

Tested with 0.9.15.

I had to explicitly remove some of the conditional exports from the reference implementation of SRFI 225 in order to get it to work.

SKINT

Tested to work on SKINT 0.6.7, with SRFIs. SKINT bundles SRFI-225, so all you need to do is point SKINT to lib to use cuprate.

STKlos

STKlos currently does not include files from the directory of the library. This will probably be fixed in 2.11, but that has not been released yet.