Frage

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.

War es hilfreich?

Lösung

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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top