Permalink
Browse files

[Issue #82] a lot more syntax validations for formula

  • Loading branch information...
1 parent f8abb7c commit 57ed06c2d036ba709588b76b460968a5e45d8124 @AlexBaranosky AlexBaranosky committed Mar 14, 2012
Showing with 50 additions and 19 deletions.
  1. +3 −2 FORMULAS-BACKLOG-AND-FEATURE-IDEAS.md
  2. +12 −6 src/midje/ideas/formulas.clj
  3. +35 −11 test/midje/ideas/t_formulas.clj
@@ -12,9 +12,10 @@
* [x] don't run more tests than need be if there is already failure in this formula's batch.
* [x] syntax validate thata formula only has one check in it
- a. [ ] need to make this more thorough... right now the only test of this feature checks a
+ a. [x] need to make this more thorough... right now the only test of this feature checks a
simple provided case, but needs to work with against-background, background and
- other more interesting cases
+ other more interesting cases
+ b. [ ] it is more thorough now, but let's make it *seriously* thoough :)
* [x] formula macro calls a (constantly []) version of shrink on failures
@@ -1,11 +1,11 @@
(ns ^{:doc "Midje's special blend of generative-style testing."}
midje.ideas.formulas
- (:use [midje.util.form-utils :only [named? pop-docstring]]
+ (:use [midje.util.form-utils :only [first-named? named? pop-docstring]]
[midje.error-handling.validation-errors :only [simple-report-validation-error
validate when-valid]]
[midje.ideas.prerequisites :only [is-head-of-form-providing-prerequisites?]]
- [midje.ideas.arrows :only [leaves-contain-arrow?
- leaf-expect-arrows]]))
+ [midje.ideas.arrows :only [leaf-expect-arrows leaves-contain-arrow?]]
+ [clojure.walk :only [prewalk]]))
(def ^{:doc "The number of facts generated per formula."
:dynamic true}
@@ -66,9 +66,12 @@
(finally
~conclusion-signal)))))
-(defn- check-part-of [form]
- (take-while #(not (and (named? %) (#{"provided" "against-background"} (name %))))
- (flatten form)))
+(defn- check-part-of [form]
+ (prewalk (fn [form]
+ (if (some (partial first-named? form) ["against-background" "background" "provided"])
+ '()
+ form))
+ form))
(defmethod validate "formula" [[_formula_ & args :as form]]
(cond (not (leaves-contain-arrow? (check-part-of args)))
@@ -82,6 +85,9 @@
(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")
:else
args))
@@ -13,31 +13,55 @@
(formula [a 1])
(formula [a 1] 1)
(formula "vector fact" [a 1] (contains 3))
- (formula "vector fact" [a 1] (contains 3))
- (formula "vector fact" [a 1]
+ (formula "ignores arrows in provideds" [a 1]
(contains 3)
(provided (h anything) => 5))
- (formula "vector fact" [a 1]
+ (formula "ignores arrows in against-background" [a 1]
(contains 3)
(against-background (h anything) => 5))
-; (formula "vector fact" [a 1]
-; (against-background (h anything) => 5)
-; (contains 3))
- )
+ (formula "ignores arrows in against-background - even when it comes first"
+ [a 1]
+ (against-background (h anything) => 5)
+ (contains 3))
-(causes-validation-error #"There are too many expections in your formula form"
- (formula "vector fact" [a 1] a => 1 a => 1))
+ (formula "ignores arrows in background" [a 1]
+ (contains 3)
+ (background (h anything) => 5))
+
+ (formula "ignores arrows in background - even when it comes first"
+ [a 1]
+ (background (h anything) => 5)
+ (contains 3)))
(each-causes-validation-error #"Formula requires bindings to be an even numbered vector of 2 or more"
(formula "vector fact" :not-vector 1 => 1)
(formula "vector fact" [a 1 1] 1 => 1)
(formula "vector fact" [] 1 => 1))
+(causes-validation-error #"There are too many expections in your formula form"
+ (formula "vector fact" [a 1] a => 1 a => 1))
+
+(defn z [x] )
+(causes-validation-error #"background cannot be used inside of formula"
+ (formula [a 1]
+ (background (h 1) => 5)
+ (z a) => 10))
+
+;; Things that should be valid
+
+(defn k [x] (* 2 (h x)))
+(formula "against-backgrounds at the front of the body are fine" [a 1]
+ (against-background (h 1) => 5)
+ (k a) => 10)
+
+
+;; *num-generations-per-formula* binding validation
+
(defn- gen-int [pred]
- (rand-nth (filter pred [-5 -4 -3 -2 -1 0 1 2 3 4 5])))
+ (rand-nth (filter pred [-999 -100 -20 -5 -1 0 1 5 20 100 999])))
(formula
"allows users to dynamically rebind to 1+"
@@ -46,7 +70,7 @@
=> (throws #"must be an integer 1 or greater"))
(formula
- "binding too smalla value - gives nice error msg"
+ "binding too small a value - gives nice error msg"
[n (gen-int #(>= % 1))]
(binding [midje.ideas.formulas/*num-generations-per-formula* n] nil)
=not=> (throws Exception))

0 comments on commit 57ed06c

Please sign in to comment.