Single Racket Testing with Multiple Outputs

I am trying to use unit-tests with Racket.

Usually, I succeed, and I really like the satchel. However, I am having problems with this particular case.

The function being tested displays two values. How can I check this with rackunit?

When i call:

(game-iter 10)
>>  5 10

I tried using this test:

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

However, this fails:

. . result arity mismatch;
 expected number of values not received
  expected: 1
  received: 2
  values...:
+4
source share
2 answers

I could not find anything that already exists, so I came up with a long way to do it. If you don't have many functions returning multiple values, you can do something like

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

You can choose the best names for aand b.

- :

;; 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)

( , , a b.)

, call-with-values.

+2

@ Gibstick , , - #racket irc :

(define-syntax check-values-equal?
  (syntax-rules ()
    [(_ a b) (check-equal? (call-with-values (thunk a) list)
                           b)]))

:

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

(, check-equal?, , , .

+1

Source: https://habr.com/ru/post/1663426/


All Articles