Skip to content

Instantly share code, notes, and snippets.

@samdphillips
Last active September 14, 2021 14:08
Show Gist options
  • Save samdphillips/913a65998067967fb17c7fd567f01df8 to your computer and use it in GitHub Desktop.
Save samdphillips/913a65998067967fb17c7fd567f01df8 to your computer and use it in GitHub Desktop.
#lang racket/base
(require (for-syntax racket/base
"wraps.rkt")
(prefix-in ru: rackunit))
(provide check-equal?
check-true)
(define-syntax (check-equal? stx)
(wrap-check2 #'ru:check-equal? stx))
(define-syntax (check-true stx)
(wrap-check1 #'ru:check-true stx))
#lang racket/base
(require racket/sequence
syntax/parse
syntax/stx
(for-template racket/base
rackunit))
(provide wrap-check1
wrap-check2)
(define (extract-arguments stx)
(syntax-case stx ()
[(_ t* ...)
(for/list ([t (in-syntax #'(t* ...))] #:when (identifier? t)) t)]
[_ null]))
(module+ test
(require rackunit)
(check-true (null? (extract-arguments #'(+ 3 4))))
(check-true (null? (extract-arguments #'+)))
(check-equal? (map syntax->datum
(extract-arguments #'(+ x y)))
'(x y)))
(define (wrap-check1 check-stx stx)
(syntax-parse stx
[(_ expr {~optional msg #:defaults ([msg #''#f])})
#:with (id ...) (extract-arguments #'expr)
#:with check check-stx
(if (stx-null? #'(id ...))
#'(check expr msg)
#'(with-check-info (['id id] ...) (check expr msg)))]))
(define (wrap-check2 check-stx stx)
(syntax-parse stx
[(_ actual expected {~optional msg #:defaults ([msg #''#f])})
#:with (actual-id ...) (extract-arguments #'actual)
#:with (expected-id ...) (extract-arguments #'expected)
#:with check check-stx
(if (and (stx-null? #'(actual-id ...))
(stx-null? #'(expected-id ...)))
#'(check actual expected msg)
#'(with-check-info (['actual-id actual-id] ...
['expected-id expected-id] ...)
(check actual expected msg)))]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment