Skip to content

Commit

Permalink
[Issue #82] with-num-trials provides a nice syntax for multiple for…
Browse files Browse the repository at this point in the history
…mulas to generate n trials each
  • Loading branch information
AlexBaranosky committed Mar 21, 2012
1 parent 8091c69 commit d4d01bb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/midje/ideas/formulas.clj
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@
(finally
~conclusion-signal))))

(defmacro with-num-trials [num-trials & formulas]
`(binding [midje.ideas.formulas/*num-trials* ~num-trials]
~@formulas))

(def future-formula-variant-names (map #(str % "formula") future-prefixes))

(defn- check-part-of [form]
Expand Down
10 changes: 9 additions & 1 deletion test/midje/ideas/t_formulas.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
(:use midje.test-util
midje.sweet
midje.util.ecosystem
[midje.ideas.formulas :only [*num-trials*]] ))
[midje.ideas.formulas :only [*num-trials* with-num-trials]] ))


;;;; Formulas
Expand Down Expand Up @@ -52,6 +52,14 @@
(fact @my-str-count => 77)


;; There is syntactic sugar for binding *num-trials*
(defn-call-countable k-maker [] "k")
(with-num-trials 1000
(formula [a 1] (k-maker) => "k")
(formula [a 1] (k-maker) => "k")
(formula [a 1] (k-maker) => "k"))
(fact @k-maker-count => 3000)

;; Can specify number of trials to run in options map - overrides *num-trials* var value
(defn-call-countable foo-maker [] "foo")
(defn-call-countable my-double-str [s] (str "double" s))
Expand Down

0 comments on commit d4d01bb

Please sign in to comment.