Search code examples
unit-testinglispracketdr.racket

Using unit-testing for a procedure in Racket


If I call in Racket/Dr. Racket the following code:

> add1

I get:

#<procedure:add1>

If I am using a library for unit-test called rackunit and I try this:

(require rackunit)

(check-equal? add1 #<procedure:add1>)

The test fails with a syntax error:

read: bad syntax `#<'

Why does this happen?


Solution

  • For many types of data, what you write can be read back. But in the case of procedures you can't, and thus it gets outputted as an unreadable value.

    Procedure equality is like opaque structure equality: it only checks identity. If this all you need, you will still be able to store the procedure somewhere and use check-equal? (for example, a list with add1 will compare equal to a list with add1). If you need to actually check that the procedure is logically the same, this is not possible in general.