13

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)
GoZoner
  • 67,920
  • 20
  • 95
  • 145
user3259073
  • 145
  • 1
  • 1
  • 6

4 Answers4

12

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

Guilherme
  • 608
  • 1
  • 6
  • 13
6

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.

dyoo
  • 11,795
  • 1
  • 34
  • 44
  • No possibility to write the requirements directly in the code so I make the file machine-independent (the solution with the menu works only on the current computer)? Something like `(require lang/htdp-beginner)` or `#lang 2htdp/bsl` ? – Liviu Jul 18 '16 at 21:03
  • @Liviu I added an answer just now that may help you. – Joseph Hansen Aug 20 '16 at 20:31
1

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

knb
  • 9,138
  • 4
  • 58
  • 85
0

I took a class using DrRacket and looked at the first assignment in Terminal (Mac).

The line in this file, automatically added by DrRacket, which made check-expect available is:

#reader(lib "htdp-beginner-reader.ss" "lang")((modname basic) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f)))



As a side note, I wanted to try a Racket program without DrRacket. Just to test, I decided to do (+ 1 2). To get it to work, my file looks like this:

#! /Applications/Racket\ v6.2.1/bin/racket
#lang racket
(+ 1 2)

I run it in Terminal like this:

racket test.rkt

I put this in .bash_profile:

alias racket='/Applications/Racket\ v6.2.1/bin/racket'
Joseph Hansen
  • 12,665
  • 8
  • 50
  • 68