[racket] macro and set! problem
Hi,
I was trying to come up with a naive implementation of check-expect/test and I found out that if a macro expands to a set! on a module-level variable directly, an error will be given when using the macro from another module. A simple fix seems to instead have the macro to expands to a function call that performs the set!.
I'm not sure what to think about it and I would like to hear why it works that way or what I have missed.
Here's what I came up with in a few minutes :
(provide check-expect test)
(define tests '())
(define-syntax check-expect (λ (stx) (syntax-case stx () [(_ exp res) #'(add-test (list (delay exp) res #'stx))])))
(define add-test (λ (test) (set! tests (cons test tests)))) ; add-test is there because having check-expect expands to set! doesn't work when called from another module.
(define test (λ () (printf "Ran ~a checks.\n" (length tests)) (let* ([failed (filter (λ (test) (not (equal? (force (car test)) (cadr test)))) (reverse tests))] [nbfailed (length failed)]) (if (> nbfailed 0) (begin (printf "~a tests failed:\n" nbfailed) (for-each (λ (ft) (let ([ft-stx (caddr ft)]) (printf "Actual value ~a differs from ~a, the expected value.\n ~a\n" (force (car ft)) (cadr ft) (format "In ~a at line ~a column ~a" (syntax-source ft-stx) (syntax-line ft-stx) (syntax-column ft-stx))))) failed)) (printf "All test passed!\n")) )))
_________________________________________________________________
Découvrez comment échanger avec vos vrais amis.
http://go.microsoft.com/?linkid=9734398