Skip to content
Browse files

[Issue #82] added opt-map to formulas with :num-trials key to specify…

… number of trials on a per formula basis
  • Loading branch information...
1 parent 968f636 commit d6c488aed5297d174d68bddb5f451739f2561453 @AlexBaranosky AlexBaranosky committed
Showing with 32 additions and 4 deletions.
  1. +10 −0 FORMULAS-BACKLOG-AND-FEATURE-IDEAS.md
  2. +10 −3 src/midje/ideas/formulas.clj
  3. +12 −1 test/midje/ideas/t_formulas.clj
View
10 FORMULAS-BACKLOG-AND-FEATURE-IDEAS.md
@@ -20,6 +20,16 @@
* [x] formula macro calls a (constantly []) version of shrink on failures
* [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
+ for groups of formulas.
+
+* [ ] validate that opt-map is only used with valid keys.
+
+* [ ] validate that :num-trials is 1+
+
+* [ ] more formula syntax valiidation cases inspired by the new opt map
* [ ] Work with Meikel Brandmeyer to combine ClojureCheck's Generators with Shrink.
implement shrinking. Report only the first fully shrunken failure
View
13 src/midje/ideas/formulas.clj
@@ -33,6 +33,13 @@
~(formula-fact docstring body)))
(recur (map rest shrunk-vectors#))))))
+(defn- deconstruct-formula-args [args]
+ (let [[docstring? more-args] (pop-docstring args)
+ [opts bindings body] (if (map? (first more-args))
+ [(first more-args) (second more-args) (rest (rest more-args))]
+ [{} (first more-args) (rest more-args)])]
+ [docstring? opts bindings body]))
+
(defmacro formula
"ALPHA/EXPERIMENTAL (subject to change) - Generative-style fact macro.
@@ -47,14 +54,14 @@
{:arglists '([docstring? bindings & body])}
[& args]
(when-valid &form
- (let [[docstring? [bindings & body]] (pop-docstring args)
+ (let [[docstring? opts bindings body] (deconstruct-formula-args args)
fact (formula-fact docstring? body)
conclusion-signal `(midje.sweet/fact
:always-pass midje.sweet/=> :always-pass
:formula :formula-conclude )]
`(try
- (loop [cnt-down# midje.ideas.formulas/*num-generations-per-formula*]
+ (loop [cnt-down# (if (contains? ~opts :num-trials) (:num-trials ~opts) midje.ideas.formulas/*num-generations-per-formula*)]
(when (pos? cnt-down#)
(let [snd-bindings# ~(vec (take-nth 2 (rest bindings)))
~(vec (take-nth 2 bindings)) snd-bindings#]
@@ -83,7 +90,7 @@
(> (count (leaf-expect-arrows (check-part-of args))) 1)
(simple-report-validation-error form "There are too many expections in your formula form:")
- (let [[_ [bindings & _]] (pop-docstring args)]
+ (let [[docstring? opts bindings body] (deconstruct-formula-args args)]
(or (not (vector? bindings))
(odd? (count bindings))
(< (count bindings) 2)))
View
13 test/midje/ideas/t_formulas.clj
@@ -81,7 +81,7 @@
;; the first formula use ever!
(defn make-string []
(rand-nth ["a" "b" "c" "d" "e" "f" "g" "i"]))
-(formula "can now use simple generative-style formulas - with multipel bindings"
+(formula "can now use simple generative-style formulas - with multiple bindings"
[a (make-string) b (make-string) c (make-string)]
(str a b c) => (has-prefix (str a b)))
@@ -114,6 +114,17 @@
(fact @my-str-count => 77)
+;; can specify number of trials to run in options map - overrides *num-generations-per-formula* 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
+ (formula "asdf" {:num-trials 88} [foo (foo-maker)]
+ (my-double-str foo) => "doublefoo"))
+(fact @foo-maker-count => 88)
+(fact @my-double-str-count => 88)
+
+
;; runs only as few times as needed to see a failure
(defn-call-countable z-maker [] "z")
(defn-call-countable my-identity [x] (identity x))

0 comments on commit d6c488a

Please sign in to comment.
Something went wrong with that request. Please try again.