Skip to content

Commit

Permalink
[Issue #82] renamed *num-generations-per-formula* -> *num-trials*
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexBaranosky committed Mar 15, 2012
1 parent d6c488a commit 0b44a64
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion FORMULAS-BACKLOG-AND-FEATURE-IDEAS.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* [x] add future-formula (and variant names)

* [x] cleaner syntax for overriding number of trials per formula. Use the
*num-generations-per-formula* var just for global changes or changes to be visible
*num-trials* var just for global changes or changes to be visible
for groups of formulas.

* [ ] validate that opt-map is only used with valid keys.
Expand Down
12 changes: 6 additions & 6 deletions src/midje/ideas/formulas.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
[midje.ideas.facts :only [future-prefixes]]
[clojure.walk :only [prewalk]]))

(def ^{:doc "The number of facts generated per formula."
(def ^{:doc "The number of trials generated per formula."
:dynamic true}
*num-generations-per-formula* 100)
*num-trials* 100)

(set-validator! #'*num-generations-per-formula*
(set-validator! #'*num-trials*
(fn [new-val]
(if (pos? new-val)
true
(throw (RuntimeException. (str "*num-generations-per-formula* must be an integer 1 or greater. You tried to set it to: " new-val))))))
(throw (RuntimeException. (str "*num-trials* must be an integer 1 or greater. You tried to set it to: " new-val))))))


(defn shrink [& _args] [])
Expand Down Expand Up @@ -49,7 +49,7 @@
Currently, we recommend you use generators from test.generative.generators
The midje.ideas.formulas/*num-generations-per-formula* dynamic var determines
The midje.ideas.formulas/*num-trials* dynamic var determines
how many facts are generated per formula."
{:arglists '([docstring? bindings & body])}
[& args]
Expand All @@ -61,7 +61,7 @@
:formula :formula-conclude )]

`(try
(loop [cnt-down# (if (contains? ~opts :num-trials) (:num-trials ~opts) midje.ideas.formulas/*num-generations-per-formula*)]
(loop [cnt-down# (if (contains? ~opts :num-trials) (:num-trials ~opts) midje.ideas.formulas/*num-trials*)]
(when (pos? cnt-down#)
(let [snd-bindings# ~(vec (take-nth 2 (rest bindings)))
~(vec (take-nth 2 bindings)) snd-bindings#]
Expand Down
15 changes: 8 additions & 7 deletions test/midje/ideas/t_formulas.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
(ns midje.ideas.t-formulas
(:use midje.test-util
midje.sweet
midje.util.ecosystem))
midje.util.ecosystem
[midje.ideas.formulas :only [*num-trials*]] ))

;;;; Validation

Expand Down Expand Up @@ -58,21 +59,21 @@
(k a) => 10)


;; *num-generations-per-formula* binding validation
;; *num-trials* binding validation

(defn- gen-int [pred]
(rand-nth (filter pred [-999 -100 -20 -5 -4 -3 -2 -1 0 1 2 3 4 5 20 100 999])))

(formula
"binding too small a value - gives nice error msg"
[n (gen-int #(< % 1))]
(binding [midje.ideas.formulas/*num-generations-per-formula* n] nil)
(binding [*num-trials* n] nil)
=> (throws #"must be an integer 1 or greater"))

(formula
"allows users to dynamically rebind to 1+"
[n (gen-int #(>= % 1))]
(binding [midje.ideas.formulas/*num-generations-per-formula* n] nil)
(binding [*num-trials* n] nil)
=not=> (throws Exception))


Expand Down Expand Up @@ -107,18 +108,18 @@
(defn-call-countable y-maker [] "y")
(defn-call-countable my-str [s] (str s))

(binding [midje.ideas.formulas/*num-generations-per-formula* 77]
(binding [*num-trials* 77]
(formula [y (y-maker)]
(my-str y) => "y"))
(fact @y-maker-count => 77)
(fact @my-str-count => 77)


;; can specify number of trials to run in options map - overrides *num-generations-per-formula* var value
;; 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))

(binding [midje.ideas.formulas/*num-generations-per-formula* 111] ;; this will be overridden by opt map
(binding [*num-trials* 111] ;; this will be overridden by opt map
(formula "asdf" {:num-trials 88} [foo (foo-maker)]
(my-double-str foo) => "doublefoo"))
(fact @foo-maker-count => 88)
Expand Down

0 comments on commit 0b44a64

Please sign in to comment.