I'm trying to unit test the code i've written in racket and i don't want to have to provide each and every function singularly, so i've written it all in a module:
(module avl_tree racket
(provide all-defined-out)
(define (is_tree? tree)
; does whatever)
; so on with other code
)
This is in the file avl_tree.rkt
.
I'm trying to unit test this in another file in the same directory, avl_tree_testing, which looks like this:
#lang racket
(require rackunit)
(require "avl_tree.rkt")
(is_tree? '(5 (1 null null) (9 (6 null null) (10 null null))))
However, no matter what I do, I get this error:
<filepath>\avl_tree\avl_tree_testing.rkt:5:1: is_tree?: unbound identifier
in: is_tree?Racket
I've tried prefixing is_tree? with the name of the module avl_tree:is_tree?
but that's gotten me nowhere.
Each source file in Racket is implicitly a module; the module
form creates submodules which can be required with submod
:
(require (submod "avl_tree.rkt" avl_tree))
You also need to use (all-defined-out)
in the provide
form; note the parens.
In your example then, you want to drop the module
and just keep its body:
#lang racket/base
;;; avl_tree.rkt
(provide (all-defined-out))
(define (is-tree? tree)
; does whatever)
; so on with other code
)
With this, (require "avl_tree.rkt")
will work as you expected.
(Also note using idiomatic kebab-case; is-tree?
not is_tree?
. Stick with the conventions and other people will be much happier reading your code. Just plain tree?
might be even better; the question mark implies the is-
part. Compare to standard functions like string?
or number?
)
Additionally, you can add tests to the same file as the code you're testing with no loss in efficiency by using the test
submodule, which is automatically run by DrRacket's Run command (ctrl+r) or by the raco test
command line tool. The key here is to use module+
to create the submodule:
#lang racket/base
;;; avl_tree.rkt
(provide (all-defined-out))
(module+ test (require rackunit))
(define (is-tree? tree)
; does whatever)
; so on with other code
)
(module+ test
(define some-tree '(5 (1 null null) (9 (6 null null) (10 null null))))
(check-pred is-tree? some-tree))