How to use check-expect in Racket
Asked Answered
S

4

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)
Starkey answered 1/2, 2014 at 0:25 Comment(0)
M
13

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

Marguerita answered 15/2, 2018 at 16:3 Comment(0)
I
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.

Imply answered 1/2, 2014 at 0:36 Comment(2)
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 ?Tasimeter
@Tasimeter I added an answer just now that may help you.Wrecker
L
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

Lisbethlisbon answered 17/6, 2017 at 20:10 Comment(0)
W
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'
Wrecker answered 20/8, 2016 at 20:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.