Permalink
Browse files

[Issue #82] validate that only valid opt-map keys is :num-trials

  • Loading branch information...
1 parent 0b44a64 commit e81ea7368883d8b7fb5c7ee46b1e4116bbd4c5d3 @AlexBaranosky AlexBaranosky committed Mar 15, 2012
Showing with 23 additions and 15 deletions.
  1. +2 −2 FORMULAS-BACKLOG-AND-FEATURE-IDEAS.md
  2. +18 −13 src/midje/ideas/formulas.clj
  3. +3 −0 test/midje/ideas/t_formulas.clj
View
4 FORMULAS-BACKLOG-AND-FEATURE-IDEAS.md
@@ -25,7 +25,7 @@
*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.
+* [x] validate that opt-map is only used with valid keys.
* [ ] validate that :num-trials is 1+
@@ -52,4 +52,4 @@
* [ ] consider implementing with @marick's metaconstant syntax
a. [ ] if we do metaconstant style, implement generator overriding
-* [ ] double check that users won't get more than one reported failure per formula
+* [ ] figure out what part of t-formulas is registering as a lot more than 1 report per formula. (100???)
View
31 src/midje/ideas/formulas.clj
@@ -6,6 +6,7 @@
[midje.ideas.prerequisites :only [is-head-of-form-providing-prerequisites?]]
[midje.ideas.arrows :only [leaf-expect-arrows leaves-contain-arrow?]]
[midje.ideas.facts :only [future-prefixes]]
+ [clojure.string :only [join]]
[clojure.walk :only [prewalk]]))
(def ^{:doc "The number of trials generated per formula."
@@ -84,20 +85,24 @@
form))
(defmethod validate "formula" [[_formula_ & args :as form]]
- (cond (not (leaves-contain-arrow? (check-part-of args)))
- (simple-report-validation-error form "There is no expection in your formula form:")
+ (let [[_docstring? opt-map bindings _body] (deconstruct-formula-args args)
+ invalid-keys (remove (partial = :num-trials) (keys opt-map))]
+ (cond (not (leaves-contain-arrow? (check-part-of args)))
+ (simple-report-validation-error form "There is no expection in your formula form:")
+
+ (> (count (leaf-expect-arrows (check-part-of args))) 1)
+ (simple-report-validation-error form "There are too many expections in your formula form:")
- (> (count (leaf-expect-arrows (check-part-of args))) 1)
- (simple-report-validation-error form "There are too many expections in your formula form:")
-
- (let [[docstring? opts bindings body] (deconstruct-formula-args args)]
(or (not (vector? bindings))
(odd? (count bindings))
- (< (count bindings) 2)))
- (simple-report-validation-error form "Formula requires bindings to be an even numbered vector of 2 or more:")
-
- (some #(and (named? %) (= "background" (name %))) (flatten args))
- (simple-report-validation-error form "background cannot be used inside of formula")
+ (< (count bindings) 2))
+ (simple-report-validation-error form "Formula requires bindings to be an even numbered vector of 2 or more:")
+
+ (some #(and (named? %) (= "background" (name %))) (flatten args))
+ (simple-report-validation-error form "background cannot be used inside of formula")
- :else
- args))
+ (not (empty? invalid-keys))
+ (simple-report-validation-error form (format "Invalid keys (%s) in formula's options map. Valid keys are: :num-trials" (join ", " invalid-keys)))
+
+ :else
+ args)))
View
3 test/midje/ideas/t_formulas.clj
@@ -45,6 +45,9 @@
(causes-validation-error #"There are too many expections in your formula form"
(formula "vector fact" [a 1] a => 1 a => 1))
+(causes-validation-error #"Invalid keys \(:foo, :bar\) in formula's options map. Valid keys are: :num-trials"
+ (formula {:foo 5 :bar 6} [a 1] a => 1))
+
(defn z [x] )
(causes-validation-error #"background cannot be used inside of formula"
(formula [a 1]

0 comments on commit e81ea73

Please sign in to comment.