Next: , Up: API  


1.1 (veritas assert)

This module is a Scheme testing library that provides a comprehensive set of assertion functions. At its core is the assert procedure, a flexible and powerful tool for comparing an expected value against a result.

Beyond simple value comparison, the module offers a set of tools for testing program behavior related to error handling.

These functions provide a complete assertion toolkit, enabling developers to write robust tests that cover both expected outcomes and predictable failures, thereby improving the reliability and quality of their Scheme code.

1.1.1 Procedures

Procedure: assert KEY: #:expect #:got #:compare #:name

Returns a function taking a VC (veritas-context) that returns an assertion that two values are equal based on a given comparison procedure. This is the core assertion procedure that the other ‘assert-equal‘ variants are built upon. It’s used to compare an expected value with a computed value.

The got parameter can be a value or a special list to compute a value dynamically. If got is a list starting with compute, comp, or computare, the second element of the list is evaluated to get the actual value for comparison.

For example, (assert #:got '(compute (+ 1 1)) ...) would evaluate (+ 1 1) to 2 before the comparison.

expect

The expected value.

got

The value to compare against, or a list to be computed.

compare

The comparison procedure, the predicate, which should be a function of two arguments, e.g., ‘equal?‘, ‘eqv?‘, or ‘eq?‘.

name

A doc string to identify the assertion in the logs.

(assert #:expect 3
               #:got (+ 1 1 1)
               #:compare equal?
               #:name "Ensuring addition works")
⇒ #<procedure 7f58ec5ce390 at veritas/assert.scm:69:2 (vc)>

;; Upon applying vc to the procedure we will compute the assertion:
`((assertion-successful . #t)
  (assertion-name "Ensuring addition works"))
Procedure: assert-approximate KEY: #:expect #:got #:name #:epsilon

Returns a function taking a VC (veritas-context) that returns an assertion checking that expect and got are approximately equal.

Two values are considered approximately equal if their difference is smaller than epsilon.

This is useful for floating-point computations.

(assert-approximate #:expect 3.14
                    #:got 3.141
                    #:epsilon 0.01)
Procedure: assert-eq KEY: #:expect #:got #:name

Returns a function taking a VC (veritas-context) that returns an assertion checking that expect and got are identical using eq?.

This is the strictest equality assertion and is typically used for symbols, booleans, and identity-sensitive comparisons.

It is a convenience wrapper around assert.

expect

The expected value.

got

The value to compare against.

name

A string to identify the assertion in the logs.

(assert-eq #:expect 'foo
           #:got 'foo)
Procedure: assert-equal KEY: #:expect #:got #:name

Returns a function taking a VC (veritas-context) that returns an assertion checking that expect and got are structurally equal using equal? predicate.

This is the standard assertion for comparing lists, strings, vectors, and other compound data structures.

It is a convenience wrapper around assert.

expect

The expected value.

got

The value to compare against.

name

A string to identify the assertion in the logs.

(assert-equal #:expect '(1 2 3)
              #:got '(1 2 3)
              #:name "Lists should match")
Procedure: assert-eqv KEY: #:expect #:got #:name

Returns a function taking a VC (veritas-context) that returns an assertion checking that expect and got are equivalent using eqv? predicate.

This is useful for comparing numbers, characters, and symbols where value equivalence matters.

It is a convenience wrapper around assert.

expect

The expected value.

got

The value to compare against.

name

A string to identify the assertion in the logs.

(assert-eqv #:expect 42
            #:got (+ 40 2))
Procedure: assert-error thunk KEY: #:name

Returns a function taking a VC (veritas-context) that returns an assertion that checks that a thunk (a procedure with no arguments) raises an error when called.

This is a crucial function for testing error-handling logic. It attempts to execute the provided thunk. If an error is caught during its execution, the assertion passes. If the thunk completes without raising an error, the assertion fails.

thunk

A procedure of no arguments to be executed.

name

A string to identify the assertion in the logs.

(assert-error (lambda ()
                      (sum-list-elements '(1 2 "a" 4)))
                    #:name
                    "Should fail when list contains non-numbers")
⇒ #<procedure 8f58ec5ce390 at veritas/assert.scm:69:2 (vc)>

;; Upon applying vc to the procedure we will compute the assertion:
`((assertion-successful . #t)
  (assertion-name "Should fail when list contains non-numbers"))
Procedure: assert-false x KEY: #:name

Returns a function taking a VC (veritas-context) that returns an assertion checking that x is #f.

This is a convenience wrapper for boolean falsehood assertions.

(assert-false (< 5 2)
              #:name "5 is not less than 2")
Procedure: assert-no-error thunk KEY: #:name

Returns a function taking a VC (veritas-context) that returns an assertion that checks that a thunk (a procedure with no arguments) does not raise an error when called.

This is a crucial function for testing error-handling logic. It attempts to execute the provided thunk. If no error is caught during its execution, the assertion passes. If the thunk raises an error, the assertion fails.

thunk

A procedure of no arguments to be executed.

name

A string to identify the assertion in the logs.

(assert-no-error (lambda ()
                      (sum-list-elements '(1 2 "a" 4))))
⇒ #<procedure 8f58ec5ce390 at veritas/assert.scm:69:2 (vc)>

;; Upon applying vc to the procedure we will compute the assertion:
`((assertion-successful . #f))
Procedure: assert-not-approximate KEY: #:expect #:got #:name #:epsilon

Returns a function taking a VC (veritas-context) that returns an assertion checking that expect and got are not approximately equal.

This assertion passes when the difference between the values is greater than or equal to epsilon.

It is the inverse of assert-approximate.

(assert-not-approximate #:expect 3.14
                        #:got 3.5
                        #:epsilon 0.01)
Procedure: assert-not-eq KEY: #:expect #:got #:name

Returns a function taking a VC (veritas-context) that returns an assertion checking that expect and got are not identical using eq?.

This assertion passes when the two values do not share identity.

It is the inverse of assert-eq.

(assert-not-eq #:expect '(1 2)
               #:got '(1 2))
Procedure: assert-not-equal KEY: #:expect #:got #:name

Returns a function taking a VC (veritas-context) that returns an assertion checking that expect and got are not structurally equal using equal?.

This assertion is useful when testing for inequality of compound values.

It is the inverse of assert-equal.

(assert-not-equal #:expect '(1 2 3)
                  #:got '(1 2 4))
Procedure: assert-not-eqv KEY: #:expect #:got #:name

Returns a function taking a VC (veritas-context) that returns an assertion checking that expect and got are not equivalent using eqv?.

This assertion is useful when values should differ in meaning or representation.

It is the inverse of assert-eqv.

(assert-not-eqv #:expect 1
                #:got 2)
Procedure: assert-true x KEY: #:name

Returns a function taking a VC (veritas-context) that returns an assertion checking that x is #t.

This is a convenience wrapper for boolean truth assertions.

(assert-true (> 5 2)
             #:name "5 is greater than 2")
Procedure: is-approximate? a b KEY: #:epsilon

Returns #t if the absolute difference between a and b is smaller than epsilon.

This is useful for comparing floating-point values where exact equality may not be reliable.

a

The first numeric value.

b

The second numeric value.

epsilon

The maximum allowed difference.

(is-approximate? 3.14159 3.14160
                 #:epsilon 0.0001)
⇒ #t

Next: (veritas model), Up: API