4 Common First Use Cases
4.1 Testing Printed Output
Use expect when the behavior you care about is what the code prints:
#:lang at-exp racket (require recspecs) (define (greet name) (printf "Hello, ~a!\n" name)) (expect (greet "Ada") "Hello, Ada!")
4.2 Testing Printed Values
Use expect/print when you want to test the value an expression returns. Recspecs prints the value with print before comparing it:
#:lang at-exp racket (require recspecs) (expect/print (map string-upcase '("red" "blue")) "'(\"RED\" \"BLUE\")")
Use expect/pretty for data that is easier to inspect with pretty-print:
#:lang at-exp racket (require recspecs) (expect/pretty '(shopping-list (item "apples" #:qty 2 #:aisle "produce") (item "bread" #:qty 1 #:aisle "bakery") (item "coffee" #:qty 1 #:aisle "dry goods")) "'(shopping-list""\n"" " "(item \"apples\" #:qty 2 #:aisle \"produce\")""\n"" " "(item \"bread\" #:qty 1 #:aisle \"bakery\")""\n"" " "(item \"coffee\" #:qty 1 #:aisle \"dry goods\"))")
4.3 Testing Error Messages
Use expect-exn when the expected behavior is an exception:
#:lang at-exp racket (require recspecs) (define (parse-port n) (unless (and (integer? n) (<= 0 n 65535)) (raise-user-error 'parse-port "expected an integer from 0 to 65535")) n) (expect-exn (parse-port 70000) "parse-port: expected an integer from 0 to 65535")
4.4 Capturing Output Without an Expectation
Use capture-output when you need the printed text as a string for some other assertion or helper:
(require recspecs rackunit) (define out (capture-output (lambda () (display "ready")))) (check-equal? out "ready")