Skip to content

Commit

Permalink
Put building.clj functions in better places.
Browse files Browse the repository at this point in the history
  • Loading branch information
marick committed Jul 25, 2011
1 parent 6f75ce2 commit 04dce25
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 86 deletions.
17 changes: 16 additions & 1 deletion src/midje/background.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
(ns midje.background
(:use
[clojure.contrib.seq :only [separate]]
[midje.util.form-utils :only [form-first?]])
[midje.util.form-utils :only [form-first?]]
[midje.util.wrapping :only [ensure-correct-form-variable]])
(:require [midje.util.unify :as unify :only [bindings-map-or-nil]]))

(defn background-form? [form] (form-first? form "against-background"))
Expand All @@ -21,3 +22,17 @@
(unify/bindings-map-or-nil form
'(?key ?when ?first-form ?after ?second-form)))


(defmacro before [wrapping-target before-form & [_ after-form & _ ] ]
(ensure-correct-form-variable `(try
~before-form
?form
(finally ~after-form))))

(defmacro after [wrapping-target after-form]
(ensure-correct-form-variable `(try ?form (finally ~after-form))))

(defmacro around [wrapping-target around-form]
(ensure-correct-form-variable around-form))


13 changes: 13 additions & 0 deletions src/midje/fakes.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
[midje.util.form-utils :only [hash-map-duplicates-ok]]
[midje.util.thread-safe-var-nesting :only [namespace-values-inside-out
with-pushed-namespace-values]]
[midje.util.file-position :only [arrow-line-number-from-form]]
[midje.util.wrapping :only [?form with-wrapping-target]]))

(defn tag-function-as-fake [function]
Expand Down Expand Up @@ -130,3 +131,15 @@
(list
(with-wrapping-target around-facts-and-checks :facts))))


;;

(defn make-fake [fake-body]
(let [line-number (arrow-line-number-from-form fake-body)]
(vary-meta
`(midje.semi-sweet/fake ~@fake-body)
assoc :line line-number)))

(defn tag-as-background-fake [fake]
(concat fake '(:type :background)))

16 changes: 16 additions & 0 deletions src/midje/metaconstants.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,19 @@
(intern *ns* metaconstant (symbol metaconstant)))
metaconstants))

(def *metaconstant-counts*)

(defmacro with-fresh-generated-metaconstant-names [& forms]
`(binding [*metaconstant-counts* (atom {})]
~@forms))

(defn metaconstant-for-form [[function-symbol & _ :as inner-form]]
(let [swap-fn (fn [current-value function-symbol]
(if (current-value function-symbol)
(assoc current-value function-symbol
(inc (current-value function-symbol)))
(assoc current-value function-symbol 1)))
number ((swap! *metaconstant-counts* swap-fn function-symbol)
function-symbol)]
(symbol (format "...%s-value-%s..." (name function-symbol) number))))

46 changes: 0 additions & 46 deletions src/midje/midje_forms/building.clj

This file was deleted.

14 changes: 7 additions & 7 deletions src/midje/midje_forms/translating.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
at_arrow__add-line-number-to-end__no-movement]]
[midje.fakes :only [background-fake-wrappers]]
[midje.metaconstants :only [define-metaconstants]]
[midje.midje-forms.building :only [make-background
make-fake
metaconstant-for-form
with-fresh-generated-metadata-names]]
[midje.fakes :only [tag-as-background-fake
make-fake]]
[midje.metaconstants :only [metaconstant-for-form
with-fresh-generated-metaconstant-names]]
[midje.background :only [
raw-wrappers]]
[midje.midje-forms.editing :only [
Expand Down Expand Up @@ -104,7 +104,7 @@

(is-start-of-arrow-sequence? in-progress)
(let [content (take-arrow-sequence in-progress)]
(recur (conj expanded (-> content make-fake make-background))
(recur (conj expanded (-> content make-fake tag-as-background-fake))
(nthnext in-progress (count content))))

(seq-headed-by-setup-teardown-form? in-progress)
Expand All @@ -118,7 +118,7 @@
(defn- final-state-wrapper [canonicalized-non-fake]
(if (some #{(name (first canonicalized-non-fake))} '("before" "after" "around"))
(with-wrapping-target
(macroexpand-1 (cons (symbol "midje.midje-forms.building"
(macroexpand-1 (cons (symbol "midje.background"
(name (first canonicalized-non-fake)))
(rest canonicalized-non-fake)))
(second canonicalized-non-fake))
Expand Down Expand Up @@ -241,7 +241,7 @@
(recur (unfolding-step finished pending substitutions)))))

(defn unfold-prerequisites [form]
(with-fresh-generated-metadata-names
(with-fresh-generated-metaconstant-names
(translate form
loc-is-at-full-expect-form?
unfold-expect-form__then__stay_put)))
Expand Down
8 changes: 4 additions & 4 deletions src/midje/sweet.clj
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
[midje.util.wrapping :only [multiwrap]]
[midje.util.form-utils :only [reader-line-number]]
[midje.util.file-position :only [user-file-position set-fallback-line-number-from]])
(:require [midje.midje-forms.building :as building])
(:require [midje.background :as background])
(:require midje.checkers)
(:require [midje.util.report :as report])
)
(immigrate 'midje.unprocessed)
(immigrate 'midje.semi-sweet)
(intern *ns* 'before #'building/before)
(intern *ns* 'after #'building/after)
(intern *ns* 'around #'building/around)
(intern *ns* 'before #'background/before)
(intern *ns* 'after #'background/after)
(intern *ns* 'around #'background/around)

(defmacro background [& raw-wrappers]
(when (user-desires-checking?)
Expand Down
23 changes: 0 additions & 23 deletions test/midje/midje_forms/t_building.clj

This file was deleted.

4 changes: 2 additions & 2 deletions test/midje/midje_forms/t_translating.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
(:use [midje.sweet])
(:use [midje.util.wrapping :only [for-wrapping-target?]])
(:use midje.test-util)
(:use [midje.midje-forms.building
:only [metaconstant-for-form with-fresh-generated-metadata-names]])
(:use [midje.metaconstants
:only [metaconstant-for-form with-fresh-generated-metaconstant-names]])
(:use [midje.util thread-safe-var-nesting unify])
(:require [clojure.zip :as zip])
(:use clojure.contrib.pprint)
Expand Down
21 changes: 19 additions & 2 deletions test/midje/t_background.clj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
;; -*- indent-tabs-mode: nil -*-

(ns midje.midje-forms.t-dissecting
(ns midje.background
(:require [clojure.zip :as zip])
(:use [midje.midje-forms dissecting recognizing]
(:use [midje.midje-forms recognizing]
[midje.error-handling monadic]
[midje sweet test-util]))

Expand Down Expand Up @@ -36,3 +36,20 @@
[(against-background)
(f 1) => 3 ] [] [(f 1) => 3]
)


(facts "dissecting setup/teardown forms"
(setup-teardown-bindings '(before :checks (+ 1 1))) =>
(contains '{?key before, ?when :checks, ?first-form (+ 1 1), ?after nil})

(setup-teardown-bindings '(before :checks (+ 1 1) :after (- 2 2))) =>
(contains '{?key before, ?when :checks, ?first-form (+ 1 1),
?after :after, ?second-form (- 2 2)})

(setup-teardown-bindings '(after :checks (+ 1 1))) =>
(contains '{?key after, ?when :checks, ?first-form (+ 1 1)})

(setup-teardown-bindings '(around :checks (let [x 1] ?form))) =>
(contains '{?key around, ?when :checks,
?first-form (let [x 1] ?form) }))

5 changes: 5 additions & 0 deletions test/midje/t_fakes.clj
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,8 @@
(provided
(called-because-mock-checking-requires-it) => 33))

(fact "make-fake's result has the line number of the arrow form"
(let [args `( ~(at-line 789 '(f 1)) => 3)]
(:line (meta (make-fake args))) => 789))


14 changes: 13 additions & 1 deletion test/midje/t_metaconstants.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
;; -*- indent-tabs-mode: nil -*-

(ns midje.t-metaconstants
(:use [midje.metaconstants :only [metaconstant? define-metaconstants]])
(:use [midje.metaconstants :only [metaconstant? define-metaconstants
with-fresh-generated-metaconstant-names
metaconstant-for-form]])
(:use midje.sweet)
(:use midje.test-util)
)
Expand Down Expand Up @@ -35,3 +37,13 @@
(against-background (f ...three...) => 3)))
(claim-symbols '(...one... ...two... ...three...))

(fact "metaconstants can be created to stand in for an expression"
(with-fresh-generated-metaconstant-names
(metaconstant-for-form '(g)) => '...g-value-1...
(metaconstant-for-form '(g)) => '...g-value-2...
(metaconstant-for-form '(h)) => '...h-value-1...

"Not fooled by namespaces"
(metaconstant-for-form '(metaconstant-for-form))
=> '...metaconstant-for-form-value-1...))

0 comments on commit 04dce25

Please sign in to comment.