Skip to content

Commit

Permalink
[Issue #82] a lot more syntax validations for formula
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexBaranosky committed Mar 14, 2012
1 parent f8abb7c commit 57ed06c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 19 deletions.
5 changes: 3 additions & 2 deletions FORMULAS-BACKLOG-AND-FEATURE-IDEAS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 12 additions & 6 deletions src/midje/ideas/formulas.clj
Original file line number Diff line number Diff line change
@@ -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}
Expand Down Expand Up @@ -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)))
Expand All @@ -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))
46 changes: 35 additions & 11 deletions test/midje/ideas/t_formulas.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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+"
Expand All @@ -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))
Expand Down

0 comments on commit 57ed06c

Please sign in to comment.