make (many of) the rackunit examples executable

and fix a buggy example
  closes PR 11894
This commit is contained in:
Robby Findler 2011-05-04 14:01:30 -05:00
parent 34b8dc249e
commit 0415bf6b7f

View File

@ -1,6 +1,9 @@
#lang scribble/doc #lang scribble/doc
@(require "base.rkt") @(require "base.rkt")
@(define rackunit-eval (make-base-eval))
@(interaction-eval #:eval rackunit-eval (require rackunit))
@title{Checks} @title{Checks}
Checks are the basic building block of RackUnit. A check Checks are the basic building block of RackUnit. A check
@ -33,7 +36,7 @@ fails.
For example, the following checks all fail: For example, the following checks all fail:
@racketblock[ @interaction[#:eval rackunit-eval
(check-eq? (list 1) (list 1) "allocated data not eq?") (check-eq? (list 1) (list 1) "allocated data not eq?")
(check-not-eq? 1 1 "fixnums are eq?") (check-not-eq? 1 1 "fixnums are eq?")
(check-eqv? 1 1.0 "not eqv?") (check-eqv? 1 1.0 "not eqv?")
@ -52,11 +55,11 @@ the output if the check fails. The value returned by a successful
check is the value returned by @racket[pred]. check is the value returned by @racket[pred].
For example, the following check passes: For example, the following check passes:
@racketblock[ @interaction[#:eval rackunit-eval
(check-pred string? "I work") (check-pred string? "I work")
] ]
The following check fails: The following check fails:
@racketblock[ @interaction[#:eval rackunit-eval
(check-pred number? "I fail") (check-pred number? "I fail")
] ]
} }
@ -71,11 +74,11 @@ fails.
For example, the following check passes: For example, the following check passes:
@racketblock[ @interaction[#:eval rackunit-eval
(check-= 1.0 1.01 0.01 "I work") (check-= 1.0 1.01 0.02 "I work")
] ]
The following check fails: The following check fails:
@racketblock[ @interaction[#:eval rackunit-eval
(check-= 1.0 1.01 0.005 "I fail") (check-= 1.0 1.01 0.005 "I fail")
] ]
} }
@ -90,7 +93,7 @@ in the output if the check fails.
For example, the following checks all fail: For example, the following checks all fail:
@racketblock[ @interaction[#:eval rackunit-eval
(check-true 1) (check-true 1)
(check-false 1) (check-false 1)
(check-not-false #f) (check-not-false #f)
@ -110,18 +113,23 @@ output if the check fails. A common error is to use an expression
instead of a function of no arguments for @racket[thunk]. Remember instead of a function of no arguments for @racket[thunk]. Remember
that checks are conceptually functions. that checks are conceptually functions.
For example, the following check succeeds: For example, the following checks succeed:
@racketblock[ @interaction[#:eval rackunit-eval
(check-exn exn:fail? (check-exn
(lambda () exn:fail?
(raise (make-exn "Hi there" (lambda ()
(current-continuation-marks))))) (raise (make-exn:fail "Hi there"
(current-continuation-marks)))))
(check-exn
exn:fail?
(lambda ()
(error 'hi "there")))
] ]
The following check fails: The following check fails:
@racketblock[ @interaction[#:eval rackunit-eval
(check-exn exn:fail? (check-exn exn:fail?
(lambda () (lambda ()
(break-thread (current-thread)))) (break-thread (current-thread))))
@ -131,7 +139,7 @@ The following example is a common mistake. The call to @racket[error]
is not within a @racket[lambda], so it bypasses @racket[check-exn] is not within a @racket[lambda], so it bypasses @racket[check-exn]
entirely. entirely.
@racketblock[ @interaction[#:eval rackunit-eval
(code:comment "Forgot to wrap the expression in a thunk. Don't do this!") (code:comment "Forgot to wrap the expression in a thunk. Don't do this!")
(check-exn exn:fail? (check-exn exn:fail?
(error 'hi "there")) (error 'hi "there"))
@ -143,6 +151,12 @@ entirely.
Checks that @racket[thunk] does not raise any exceptions. Checks that @racket[thunk] does not raise any exceptions.
The optional @racket[message] is included in the output if The optional @racket[message] is included in the output if
the check fails. the check fails.
@interaction[#:eval rackunit-eval
(check-not-exn (λ () 1))
(check-not-exn (λ () (car '())))
(check-not-exn (λ () (/ 1 0)) "don't divide by 0")]
} }
@defproc[(check-regexp-match (regexp regexp?) @defproc[(check-regexp-match (regexp regexp?)
@ -154,13 +168,13 @@ Checks that @racket[regexp] matches the @racket[string].
For example, the following check succeeds: For example, the following check succeeds:
@racketblock[ @interaction[#:eval rackunit-eval
(check-regexp-match "a+bba" "aaaaaabba") (check-regexp-match "a+bba" "aaaaaabba")
] ]
The following check fails: The following check fails:
@racketblock[ @interaction[#:eval rackunit-eval
(check-regexp-match "a+bba" "aaaabbba") (check-regexp-match "a+bba" "aaaabbba")
] ]
} }
@ -179,13 +193,13 @@ exception of type @racket[exn:test:check]. The optional
For example, the following check succeeds: For example, the following check succeeds:
@racketblock[ @interaction[#:eval rackunit-eval
(check < 2 3) (check < 2 3)
] ]
The following check fails: The following check fails:
@racketblock[ @interaction[#:eval rackunit-eval
(check memq 'pine '(apple orange pear)) (check memq 'pine '(apple orange pear))
] ]
} }
@ -232,9 +246,7 @@ Stores the given @racket[info] on the check-info stack for
the duration (the dynamic extent) of the execution of the duration (the dynamic extent) of the execution of
@racket[thunk]} @racket[thunk]}
Example: @interaction[#:eval rackunit-eval
@racketblock[
(with-check-info* (with-check-info*
(list (make-check-info 'time (current-seconds))) (list (make-check-info 'time (current-seconds)))
(lambda () (check = 1 2))) (lambda () (check = 1 2)))
@ -253,9 +265,7 @@ information in the check information stack for the duration
of the execution of the body expressions. @racket[Name] is of the execution of the body expressions. @racket[Name] is
a quoted symbol and @racket[val] is any value.} a quoted symbol and @racket[val] is any value.}
Example: @interaction[#:eval rackunit-eval
@racketblock[
(for-each (for-each
(lambda (elt) (lambda (elt)
(with-check-info (with-check-info
@ -311,16 +321,16 @@ simple checks cannot report extra information using
For example, the following code defines a check @racket[check-odd?] For example, the following code defines a check @racket[check-odd?]
@racketblock[ @interaction[#:eval rackunit-eval
(define-simple-check (check-odd? number) (define-simple-check (check-odd? number)
(odd? number)) (odd? number))
] ]
We can use these checks in the usual way: We can use these checks in the usual way:
@racketblock[ @interaction[#:eval rackunit-eval
(check-odd? 3) (code:comment "Success") (check-odd? 3)
(check-odd? 2) (code:comment "Failure") (check-odd? 2)
] ]
@defform*[[(define-binary-check (name pred actual expected)) @defform*[[(define-binary-check (name pred actual expected))
@ -334,26 +344,24 @@ predicate and tests if the predicate holds for the given
values. The second form tests if @racket[expr] non-false. values. The second form tests if @racket[expr] non-false.
} }
Examples:
Here's the first form, where we use a predefined predicate Here's the first form, where we use a predefined predicate
to construct a binary check: to construct a binary check:
@racketblock[ @interaction[#:eval rackunit-eval
(define-binary-check (check-char=? char=? actual expected)) (define-binary-check (check-char=? char=? actual expected))
] ]
In use: In use:
@racketblock[ @interaction[#:eval rackunit-eval
(check-char=? (read-char a-port) #\a) (check-char=? (read-char (open-input-string "a")) #\a)
] ]
If the expression is more complicated the second form should If the expression is more complicated the second form should
be used. For example, below we define a binary check that be used. For example, below we define a binary check that
tests a number if within 0.01 of the expected value: tests a number if within 0.01 of the expected value:
@racketblock[ @interaction[#:eval rackunit-eval
(define-binary-check (check-in-tolerance actual expected) (define-binary-check (check-in-tolerance actual expected)
(< (abs (- actual expected)) 0.01)) (< (abs (- actual expected)) 0.01))
] ]
@ -372,3 +380,6 @@ The @racket[fail-check] macro raises an @racket[exn:test:check] with
the contents of the check information stack. the contents of the check information stack.
} }
@close-eval[rackunit-eval]