public
Created

Midje `formula`

  • Download Gist
gistfile1.clj
Clojure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
;; This first swipe at generative-style testing in Midje was super easy:
 
;; first a use of it
 
(defn make-string []
(rand-nth ["a" "b" "c" "d" "e" "f" "g" "i"]))
 
(formula [a (make-string) b (make-string)]
(str a b) => #(.startsWith % a))
 
;; How its defined
 
(def ^:dynamic *num-generations* 100)
 
(defmacro formula [bindings & body]
(macro-for [_ (range *num-generations*)]
`(let ~bindings
(midje.sweet/fact
~@body))))
 
;; NOTES:
;; * just borrow some generator functions from test.generative, and voila
;; a stripped down generative test framework.
;; * this version makes a fact for every generation which would quickly
;; make the fact count in the report meaningless.
;; * doc-strings can be added later.
 
;; QUESTIONS:
;; * how can we make the reporting of these really nice?
;; * is this it? There has to be more to this kind of thing than simply
;; generating 100 of each formula. What do you think?

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.