Skip to content

Commit

Permalink
Allow key/value pairs to override (fact ...) transformations.
Browse files Browse the repository at this point in the history
  • Loading branch information
marick committed Sep 13, 2010
1 parent c6e88b4 commit 7718b30
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 20 deletions.
23 changes: 13 additions & 10 deletions src/midje/fact_body_transformation.clj
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@
(recur (zip/down end-form))
end-form)))

(defn n-times [n zip-fn loc]
(if (zero? n)
loc
(recur (dec n) zip-fn (zip-fn loc))))

;; Editing

(defn remove-n [n loc]
(if (= n 0)
loc
(recur (- n 1) (zip/remove loc))))

(defn remove-n [loc n]
(if (= n 0)
loc
(recur (zip/remove loc) (- n 1))))
(defn remove-moving-right [loc]
(-> loc zip/remove zip/next)
)

(defn delete-enclosing-provided-form__at-previous-full-expect-form [loc]
(assert (head-of-provided-form? loc))
Expand All @@ -75,8 +75,11 @@
(let [right-hand (-> loc zip/right zip/right)
additions (overrides right-hand)
edited-loc (zip/edit loc
(fn [loc] `(expect ~loc => ~(zip/node right-hand))))]
(-> edited-loc zip/right zip/right (remove-n 2))))
(fn [loc] `(expect ~loc => ~(zip/node right-hand) ~@additions)))]
(->> edited-loc
zip/right
(n-times (+ 1 (count additions)) remove-moving-right)
zip/remove)))

;; The meat of it.

Expand Down
34 changes: 25 additions & 9 deletions test/midje/fact_body_transformation_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@

;; Editing

(deftest should-be-able-to-delete-and-end-up-at-next-form-of-same-level
(let [z (zip/seq-zip '( (f n) => (+ 3 4)))
loc (-> z zip/down zip/right)]
(expect (zip/node (remove-moving-right loc)) => '(+ 3 4))
(expect (zip/root (remove-moving-right loc)) => '( (f n) (+ 3 4)))))



(deftest should-be-able-to-delete-provided-form-and-position-for-appending
Expand Down Expand Up @@ -124,15 +130,25 @@
(expect (zip/next resulting-loc) => (node "next"))
(expect (zip/root resulting-loc) => '( (midje.semi-sweet/expect (f 1) midje.semi-sweet/=> (+ 2 3)) "next")))))

;; (deftest should-be-able-to-include-annotations-in-expect-form-wrapping
;; (let [z (zip/seq-zip '( (f 1) => (+ 2 3) :key "value" "next"))
;; loc (-> z zip/down)]
;; (expect (zip/node loc) => '(f 1))
;; (expect (start-of-arrow-sequence? loc) => truthy)
;; (let [resulting-loc (wrap-with-expect__at-rightmost-wrapped-location loc)]
;; (expect (zip/next resulting-loc) => (node "next"))
;; (expect (zip/root resulting-loc) =>
;; '( (midje.semi-sweet/expect (f 1) midje.semi-sweet/=> (+ 2 3) :key "value") "next")))))
(deftest should-be-able-to-include-annotations-in-expect-form-wrapping
(let [z (zip/seq-zip '( (f 1) => (+ 2 3) :key "value" "next"))
loc (-> z zip/down)]
(expect (zip/node loc) => '(f 1))
(expect (start-of-arrow-sequence? loc) => truthy)
(let [resulting-loc (wrap-with-expect__at-rightmost-wrapped-location loc)]
(expect (zip/next resulting-loc) => (node "next"))
(expect (zip/root resulting-loc) =>
'( (midje.semi-sweet/expect (f 1) midje.semi-sweet/=> (+ 2 3) :key "value") "next")))))

(deftest expectations-at-end-of-form-are-successfully-wrapped
(let [z (zip/seq-zip '( (f 1) => (+ 2 3) :key "value"))
loc (-> z zip/down)]
(expect (zip/node loc) => '(f 1))
(expect (start-of-arrow-sequence? loc) => truthy)
(let [resulting-loc (wrap-with-expect__at-rightmost-wrapped-location loc)]
(expect (zip/next resulting-loc) => zip/end?)
(expect (zip/root resulting-loc) =>
'( (midje.semi-sweet/expect (f 1) midje.semi-sweet/=> (+ 2 3) :key "value"))))))

;; ;; top-level

Expand Down
5 changes: 4 additions & 1 deletion test/midje/sweet_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,7 @@
(fact (always-one ...anything...) => 1)
(fact (g-caller ...something...) => ...g-value...
(provided (g-caller ...something...) => ...g-value...))
)
)

(deftest overriding-defaults
(fact (always-one 3) => 3 :expected-result 1))

0 comments on commit 7718b30

Please sign in to comment.