2 Writing Readable Expectations with #lang at-exp racket🔗ℹ

Most expect tests are easiest to read with Racket’s at-exp reader. Start the file with #lang at-exp racket instead of plain #lang racket, then put @ before expect. The expression to run goes in square brackets, and the expected output goes in braces:

#:lang at-exp racket
(require recspecs)
 
(expect (displayln "hello")
"hello")

The @ form is just reader syntax for an ordinary function or macro call. The example above is equivalent to writing an expect form with a string argument, but the expected output can be written as the text you want to see instead of as a string with "\\n" escapes.

Here is a slightly more realistic example that checks a report:

#:lang at-exp racket
(require recspecs)
 
(define (print-shopping-list items)
(for ([item items]
      [n (in-naturals 1)])
(printf "~a. ~a\n" n item)))
 
(expect (print-shopping-list '("apples" "bread" "coffee"))
"1. apples""\n"
"2. bread""\n"
"3. coffee")

Use this style when the expected output has more than one line or when the literal output is clearer than a Racket string. For one-line output, a plain string is still fine.