5

I want to extend srfi-78 by a macro that tests for syntax exception. I want something like this:

#! /usr/bin/env scheme-script
#!r6rs

(import (rnrs) (srfi :78 lightweight-testing))

; the macros I want to test
(define-syntax some-macros
  (syntax-rules ()
    [(_) 'ok]))

; the extension to srfi-78
(define-syntax check-exception
  (syntax-rules ()
        ; ... some code ...
        ))

; tests

; prints "correct" or someting like that
(check (some-macros) => 'ok)

; should print "correct" (i. e. the test passed)
(check-exception (some-macros 'arg)) 

; should print "error"
; (i. e. the exception was not thrown as expected)
(check-exception (some-macros)) 

Is it possible at all? If not, how would you write tests for macroses?

I know about test-read-eval-string from srfi-64. It accepts a string, translates it into a form and evaluates this form in initial environment. I want a macro that evaluates given form in current environment and catches exceptions.

amakarov
  • 524
  • 3
  • 16

1 Answers1

3

The only portable way to do this is, is to call the code via eval, and wrap that in a guard.

Eg:

(define (safe-eval code env)
  (guard [e [(syntax-violation? e) (display e)]]
    (eval code env)))

Usage:

> (safe-eval '(let a v) (environment '(rnrs)))
&who: let
&message: "invalid syntax"
&syntax:
  form: (let a v)
  subform: #f
leppie
  • 115,091
  • 17
  • 196
  • 297
  • That should not work for macros as the macros expansion goes before the evaluation of `safe-eval` – amakarov Dec 05 '12 at 10:05
  • 1
    Note that code is quoted, so the expansion will only happen in eval. – leppie Dec 05 '12 at 10:34
  • Ok, thank you. Unfortunatelly, that is not what I want because to use your `safe-eval` I have to pass the definition of the macros to it: `(safe-eval '(begin (define-syntax some-macros ...) (some macros)))`. It almost the same as `test-read-eval-string` above accepting forms instead of string. I would like to test already defined macros. It seems it is impossible. – amakarov Dec 05 '12 at 15:57
  • One way this feature could be implemented is to copy the current environment and use the copy as the last arg to your `safe-eval`. I could not find such a function in the interpreter I use. Btw, as far as I can see `interaction-environment` is not portable and the portable alternatives all yield immutable environments requiring to use `let-syntax` in the first argument to introduce the macros I want to test. – amakarov Dec 05 '12 at 16:02
  • 2
    If your macros are implemented in a library, I dont see the problem. – leppie Dec 05 '12 at 18:05
  • 1
    Great. The library could be imported to the environment that passed to `safe-eval`. Thank you – amakarov Dec 06 '12 at 04:48