[plt-scheme] module access from test cases file

From: Eli Barzilay (eli at barzilay.org)
Date: Thu Jan 15 17:27:55 EST 2009

On Jan 15, Noel Welsh wrote:
> On Thu, Jan 15, 2009 at 8:56 AM, Sigrid Keydana <keydana at gmx.de> wrote:
> > I would like to keep my test cases in a file separate from the
> > main application, but this seems to mean I have to export all
> > tested functions from the module (with provide), which I don't
> > need/want to otherwise.
> See require/expose in SchemeUnit.  Or just provide all the
> definitions you want.  People will only program to the documented
> API so really it isn't worth worrying about.

Even better for test cases is to use the sandbox library.  For

  (require scheme/sandbox)
  (define e (make-module-evaluator (string->path "...your file...")))
  (e '...some-expression...)

Some notes:

* `make-module-evaluator' is suited for cases where you have some code
  that is a module, and you want to get an evaluator in that context.
  The result is an evaluator function that is very similar to a
  drscheme repl in the `Module' language.

* The reason for the `string->path' is that giving it a string will
  try to evaluate the string as containing the source for a module.

* The sandbox is very secure by default -- it is intended to be
  convenient for running random code.  So in the above, the module
  code will not be allowed to access the filesystem, the network, and
  a number of other restriction.  It is also common to want a sandbox
  for trusted code -- especially in testing code you usually will want
  the convenience of the sandboxed evaluator, but without the

  In the current svn version (which will turn to a new release
  shortly) there is a convenient utility for this:

  (define e
      (lambda ()
        (make-module-evaluator (string->path "...your file...")))))

* Finally, you can go to an even less sandboxed environment using
  `module->namespace'.  For example:

    (require (file "...your file..."))
    (define e
      (let ([ns (module->namespace '(file "...your file..."))])
        (lambda (expr) (eval expr ns))))

    (e '...some-expression...)

  This is, very roughly, the core of the sandbox library -- which
  takes care of a bunch of other considerations.  (It is also how the
  `require/expose' form that Noel mentioned works.)

          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!

Posted on the users mailing list.