I'm trying to write a macro to test expected exceptions.  I'd like it
to have the following forms:

(throws exception-generator-function proc msg)
(throws exception-generator-function string msg)

Where 'proc' would be something like exn:fail? and 'string' would be
matched against the message inside the exception.

I got all the pieces of this working but not playing nice together.
There's three elements in the pattern for both versions, so the only
way to distinguish which to use is by the types of the arguments.  I
looked at syntax classes but made no headway on grokking that.  I saw
the #:when keyword for patterns and thought that would do what I
needed.  The following code seems like it should work, but it doesn't.
What am I missing?


(define-syntax (throws stx)
  (syntax-parse stx
                [(_ boom pred msg)
                 #:when (lambda () (procedure? #'pred))
                 #'(with-handlers
                    ([(lambda (x) #t) ;; already checked it's a proc
                      (lambda (xcpt)
                        (println "procedure form"))])
                    boom)]

                [(_ boom pred msg)
                 #:when (lambda () (string? #'pred))
                 #'(with-handlers
                    ([(lambda (x) #t) ;; already checked it's a string
                      (println "string form")])
                    boom)]))

(define (boom) (raise-argument-error 'boom "PEBKAC" 18)) ;; random choices
(throws (boom) exn:fail? "should trigger the proc form")
(throws (boom) "PEBKAC"  "should trigger the string form")

-- 
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