Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use check-expect in Racket

Tags:

scheme

racket

I am trying to use the check-expect function in scheme but I keep being told its an unbound identifier for check-expect. Isn't check-expect a function I can use already? Below is my code:

#lang racket

(define contains (lambda (item list*) 
                   (if (equal? list* '()) 
                        #f
                        (if (equal? item (car list*)) 
                            #t
                            (contains item (cdr list*))))))

(define z (list 1 2 3))
(define q (list 4 5 6))
(define p (list "apple" "orange" "carrot"))
(check-expect (contains 1 z) #t)
like image 318
user3259073 Avatar asked Feb 01 '14 00:02

user3259073


3 Answers

Old question, but answer for the googlers:

You can (require test-engine/racket-tests), which defines check-expect.

Note that, unlike BSL, you'll have to run the tests with (test).

like image 139
Guilherme Avatar answered Nov 15 '22 21:11

Guilherme


check-expect is not technically built into scheme or Racket automatically.

Note that you are using #lang racket. That is the professional Racket language, and that language expects you to know and state explicitly what libraries to import. It will not auto-import them for you.

(Now, you could require a unit testing library; there is one that comes with the Racket standard library.)

But if you are just starting to learn programming, it makes much more sense to use one of the teaching languages within Racket.

For the code you're using above, I suspect you'll probably want this instead. Start DrRacket and choose "Beginner Student Language" from the "How to Design Programs" submenu in the "Language" menu.

See http://www.ccs.neu.edu/home/matthias/HtDP2e/prologue.html for more details.

like image 37
dyoo Avatar answered Nov 15 '22 19:11

dyoo


I've managed to come up with this workaround:

At the top of the file (but after #lang racket) added a line

(require rackunit)

Instead of (check-expect) I've used

(check-equal? (member? "a" (list "b" "a")) #f )

Unlike in check-expect, tests must be added after the function definitions. If the checks are successful, there is no output. Only when a tests fails, the output looks like this:

--------------------
FAILURE
name:       check-equal?
actual:     #f
expected:   #t
expression: (check-equal? #f (member? "a" (list "b" "a")))
message:    "test"

More Info: RackUnit documentation

like image 1
knb Avatar answered Nov 15 '22 20:11

knb