Search code examples
unit-testinglispracket

Unit-testing in Racket with multiple outputs


I am trying to use unit-tests with Racket.

Usually, I am successful and I really like rackunit. However, I am having trouble with this specific case.

The function to be tested outputs two values. How can I test this using rackunit?

When I call:

(game-iter 10)
>>  5 10

I tried using this test:

(check-equal? (game-iter 10) 5 10)

However, it fails:

. . result arity mismatch;
 expected number of values not received
  expected: 1
  received: 2
  values...:

Solution

  • I couldn't find anything that already exists, so I came up with the long way to do it. If you don't have many functions that return multiple values, you could do something like

    (define-values (a b) (game-iter 10))
    (check-equal? a 5)
    (check-equal? b 10)
    

    You can pick better names for a and b.

    You can abstract this somewhat with something like:

    ;; check if (game-iter n) produces (values a-expect b-expect)
    (define-simple-check (check-game-iter n a-expect b-expect)
      (define-values (a b) (game-iter n))
      (and (equal? a a-expect)
           (equal? b b-expect)))
    (check-game-iter 10 5 10)
    

    (Again, pick better names than a b.)

    If you want to make this even more general, take a look at call-with-values.