rackunit-chk:   a short hand for writing rackunit tests
chk
check-equal?
check-not-equal?
check-not-false
check-false
check-exn
check-not-exn
8.12

rackunit-chk: a short hand for writing rackunit tests🔗ℹ

Jay McCarthy <jay@racket-lang.org>

 (require rackunit/chk) package: rackunit-chk

This module defines a macro, chk, that allows you to write many rackunit tests quickly. In addition, it provides some convenient wrappers around rackunit’s checks that work better with multiple values and exceptions.

syntax

(chk test ...)

Runs each test provided it matches the following grammar:

  test = strict-test
  | (strict-test)
  | (~seq actual expected)
  | (~seq actual)
     
  strict-test = (~seq #:f test)
  | (~seq #:t actual)
  | (~seq #:exn actual exn-expected)
  | (~seq #:= actual expected)

If a test is not strict-test, then if it has two expressions, it is equivalent to a #:= strict-test. If it has one, it is equivalent to a #:t strict-test.

A #:f strict-test is equivalent to the opposite of the test provided as an argument.

A #:t strict-test is equivalent to check-not-false. Its opposite is check-false.

A #:exn strict-test is equivalent to check-exn. Its opposite is check-not-exn.

A #:= strict-test is equivalent to check-equal?. Its opposite is check-not-equal?.

syntax

(check-equal? x y)

Checks if either (a) x and y evaluate to the same values (compared with equal?) or that (b) they both evaluate to exceptions with the same message.

syntax

(check-not-equal? x y)

Negation of (check-equal? x y).

syntax

(check-not-false x)

Checks that x does not evaluate to #f.

syntax

(check-false x)

Checks that x does evaluate to #f.

syntax

(check-exn x e)

Checks that x throws an exception during execution. If e evaluates to a string, then it must occur inside the exception message. If e evaluates to a regexp, then it must match against the exception message. If e evaluates to a procedure, it must not return #f on the exception.

syntax

(check-not-exn x e)

Checks that if x throws an exception during execution, the exception does not match e according to the rules of check-exn.

Consider the following examples:

Examples:
> (require rackunit/chk)
> (chk
   1 1
   #:f 1 0
   #:f #:f #:f 1 0
   #:f #:f 1 1
   #:f (/ 1 0) +inf.0
   (/ 1 0) (/ 1 0)
   #:f (error 'xxx "a") (error 'xxx "b")
  
   #:f #:t (/ 1 0)
   #:t (values 0 1)
   #:t (values #f 1)
   #:f #:t (values #f 1)
  
   1 1
   2 2
   #:exn (/ 1 0) "division"
   #:exn (/ 1 0) #rx"di.ision"
   #:exn (/ 1 0) exn:fail?
   #:f #:exn (/ 1 1) "division"
   #:f #:exn (/ 1 0) "diblision"
   (/ 1 0) (error '/ "division by zero")
  
   #:t (chk 1)
   #:t 1
   #:f #f
   #:f #:t #f
   1 1
   #:t 1
   #:f 2 3
  
   (values 1 2) (values 1 2)
   #:f (values 1 2) (values 2 3)
   #:f (values 1 2) 3
   #:f 3 (values 1 2)
   (quotient/remainder 10 3) (values 3 1)
  
   #:= 1 1
   [#:exn (/ 1 0) "division"]
   [#:f #f]
   [#:t 1]
   [#:= 1 1])