dimanche 6 novembre 2016

Unit-testing in Racket with objects

I am trying to build the classic Snake game with Racket and Dr. Racket.

I like using unit tests to see if I am understanding the concepts behind my code.

I started with a data structure for a snake and I defined a structure for positions in a 2 dimensions (2D) plane:

(struct snake (dir segs)) 

(struct posn (x y))

After that, I created a snake example:

(define snake-example
  (snake "up" (list (posn 1 1) (posn 1 2) (posn 1 3))))

If I try to access the segs on my snake object, I call:

(snake-segs snake-example)

>> '(#<posn> #<posn> #<posn>)

Which is the same as calling:

(list (posn 1 1) (posn 1 2) (posn 1 3))

>> '(#<posn> #<posn> #<posn>)

However, If I try using a test and rackunit to check this, it does not work:

(require rackunit)

(check-equal? (snake-segs snake-example) (list (posn 1 1) (posn 1 2) (posn 1 3)))

I get this answer:

FAILURE
actual:     (#<posn> #<posn> #<posn>)
expected:   (#<posn> #<posn> #<posn>)
name:       check-equal?
location:   (#<path:/home/pedro/Desktop/realm-of-racket/cap-6.rkt> 276 0 8425 81)
expression: (check-equal? (snake-segs snake-example) (list (posn 1 1) (posn 1 2) (posn 1 3)))

Why does this happen?

Aucun commentaire:

Enregistrer un commentaire