Skip to content

Instantly share code, notes, and snippets.

@jhrr
Last active December 27, 2015 18:19
Show Gist options
  • Save jhrr/7368985 to your computer and use it in GitHub Desktop.
Save jhrr/7368985 to your computer and use it in GitHub Desktop.
#lang racket
(require (for-syntax racket/syntax))
(require rackunit)
;; Macro for auto-writing tests with defines.
;; Syntax:
;; (define+test (function-name parameters ...)
;; ((test-input desired-result)
;; ...)
;; (body
;; ...))
(define-syntax (define+test stx)
(syntax-case stx ()
[(_ (func-name parameters ...)
((test-input desired-result)
...)
body
...)
#'(begin
(define (func-name parameters ...)
body
...)
(module+ test
(check-equal? test-input desired-result)
...))]))
(define+test (cubic x)
(((cubic 2) 8)
((cubic 3) 27)
((cubic 4) 64))
(expt x 3))
(define+test (insert-rambo input-list)
(((insert-rambo '(jungle now contains)) '(jungle now contains rambo))
((insert-rambo '(nothing is impossible for)) '(nothing is impossible for rambo))
((insert-rambo '(john)) '(john rambo)))
(append input-list '(rambo)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment