I'd say that using `eval` or `eval-syntax` is the right idea, but:

 * It's better to use `make-base-namespace` instead of
   `module->namespace`, since `(module->namespace 'racket/base)` gives
   you a namespace for the inside of `racket/base` instead of a
   top-level namespace that has imported `racket/base`.

 * Use `check-module-form` from `syntax/modread` to make the `module`
   identifier in an S-expression module form (from `read-syntax`) is
   bound to the core `module` form.

Putting those together, and using `current-namespace` so that both
`eval-syntax` and check-module-form` use the same namespace:

   (require syntax/modread)

   (parameterize ([current-namespace (make-base-namespace)])
     (eval-syntax
      (check-module-form
       (parameterize ([read-accept-lang #t]
                      [read-accept-reader #t])
          (read-syntax
           "test-module"
           (open-input-string
            "#lang scribble/base\n@section{Introduction}")))
       'ignored #f)))


At Fri, 25 Aug 2017 14:59:14 +0200, Konrad Hinsen wrote:
> Hi all,
> 
> I have been trying for a while to write proper unit tests for a language 
> implementation I am working on. By "proper" tests I mean tests that are
> run using raco test just like any other tests. Until now, I have a 
> separate shell-script based testing framework that runs scripts written
> in my own #lang, but all too often I forgot to run those tests.
> 
> For a standard s-exp based language, this is not difficult:
> 
> (module+ test
> 
>    (module demo lazy
>      (provide foo)
>      (define (foo x) (* 2 x)))
> 
>    (require (only-in 'demo foo))
> 
>    (check-equal? (force (foo 2)) 4))
> 
> But for a #lang that uses a different reader, I haven't found a working 
> solution yet. Even if I am willing to re-write my examples in s-exp 
> syntax, I run into problems:
> 
> (module+ test
> 
>    (module example scribble/base
>      (section "Introduction"))
> 
>    (require (only-in 'example doc))
> 
>    (check-equal? (force (foo 2)) 4))
> 
> This yields the error message
> 
>    module: no #%module-begin binding in the module's language
> 
> Anyway, what I really want is use the #lang's standard syntax. Reading 
> it isn't much of a problem:
> 
> (parameterize ([read-accept-lang #t]
>                  [read-accept-reader #t])
>     (read-syntax "test-module"
>                  (open-input-string "#lang 
> scribble/base\n@section[Introduction]")))
> 
> But then I get a syntax-object for my module, which I need to evaluate 
> somehow. I'd expect eval-syntax to do the job, but...
> 
> (eval-syntax
>   (parameterize ([read-accept-lang #t]
>                  [read-accept-reader #t])
>     (read-syntax "test-module"
>                  (open-input-string "#lang 
> scribble/base\n@section[Introduction]")))
>   (module->namespace 'racket/base))
> 
> only says
> 
> ; test-module::1: module: unbound identifier;
> ;  also, no #%app syntax transformer is bound
> ;   at: module
> 
> although both module and %app are defined in racket/base.
> 
> Any ideas (or pointers to examples) for doing this correctly?
> 
> Thanks in advance,
>    Konrad.
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to