diff --git a/src/midje/ideas/formulas.clj b/src/midje/ideas/formulas.clj index 05fc6aa14..6e4ab9967 100644 --- a/src/midje/ideas/formulas.clj +++ b/src/midje/ideas/formulas.clj @@ -3,13 +3,16 @@ (def #^:dynamic *num-generations* 100) -(defmacro formula [bindings & body] - `(do - (macro-for [_# (range (dec *num-generations*))] +(defmacro formula [docstring? & bindings+body] + (let [[docstring? bindings body] (if (string? docstring?) + [docstring? (first bindings+body) (rest bindings+body)] + [nil docstring? bindings+body])] + `(do + (macro-for [_# (range (dec *num-generations*))] + (let ~bindings + (midje.sweet/fact ~docstring? + ~@body :formula :formula-in-progress ))) + (let ~bindings - (midje.sweet/fact - ~@body :formula :formula-in-progress ))) - - (let ~bindings - (midje.sweet/fact - ~@body :formula :formula-conclude )))) + (midje.sweet/fact ~docstring? + ~@body :formula :formula-conclude ))))) diff --git a/test/midje/t_sweet.clj b/test/midje/t_sweet.clj index 8870664b9..5ae460a9f 100644 --- a/test/midje/t_sweet.clj +++ b/test/midje/t_sweet.clj @@ -325,14 +325,19 @@ (provided (a) =throws=> (IllegalArgumentException. "blammo"))) + +;; failed formulas report once per formula regardless how many generations were run (after-silently - (formula [a "y"] + (formula "some description" [a "y"] a => :foo)) (fact @reported => (one-of (contains {:type :formula-fail - :first-failure (contains {:type :mock-expected-result-failure})}))) + :first-failure (contains {:type :mock-expected-result-failure + :description "some description"})}))) (defn make-string [] (rand-nth ["a" "b" "c" "d" "e" "f" "g" "i"])) -(formula [a (make-string) b (make-string)] +(formula + "can now use simple generative-style formulas" + [a (make-string) b (make-string)] (str a b) => (has-prefix a)) \ No newline at end of file