Skip to content

Commit

Permalink
[Issue #101] created expose-testables to make be able to test vars …
Browse files Browse the repository at this point in the history
…with ^:testable metadata - cannot use them in `provided` however
  • Loading branch information
AlexBaranosky committed Feb 5, 2012
1 parent 727e6bd commit 7951db6
Show file tree
Hide file tree
Showing 16 changed files with 60 additions and 61 deletions.
2 changes: 1 addition & 1 deletion src/midje/checkers/collection.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

(def #^:private looseness-modifiers #{:in-any-order :gaps-ok})

(defn- separate-looseness
(defn- #^:testable separate-looseness
"Distinguish expected results from looseness descriptions.
1 :in-any-order => [1 [:in-any-order]]
1 2 :in-any-order => [ [1 2] [:in-any-order] ]
Expand Down
4 changes: 2 additions & 2 deletions src/midje/error_handling/validation_errors.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

;; Making validation errors

(defn- #^:tested-private as-validation-error [form]
(defn- #^:testable as-validation-error [form]
(vary-meta form assoc :midje-validation-error true))

(defn validation-error-form? [form]
Expand All @@ -40,7 +40,7 @@
(defmacro valid-let [let-vector & body]
`(domonad midje-maybe-m ~let-vector ~@body))

(defn- #^:tested-private spread-validation-error [collection]
(defn- #^:testable spread-validation-error [collection]
(or (find-first validation-error-form? collection)
collection))

Expand Down
4 changes: 2 additions & 2 deletions src/midje/ideas/background.clj
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
(and (bindings '?first-form)
(or (not (bindings '?after)) (bindings '?second-form)))))

(defn- #^:tested-private extract-state-descriptions+fakes [forms]
(defn- #^:testable extract-state-descriptions+fakes [forms]
(loop [expanded []
in-progress forms]
(pred-cond in-progress
Expand All @@ -94,7 +94,7 @@
(recur (conj expanded (first in-progress))
(rest in-progress)))))

(defn- #^:tested-private state-wrapper [[_before-after-or-around_ wrapping-target & _ :as state-description]]
(defn- #^:testable state-wrapper [[_before-after-or-around_ wrapping-target & _ :as state-description]]
(with-wrapping-target
(macroexpand-1 (map-first #(symbol "midje.ideas.background" (name %)) state-description))
wrapping-target))
Expand Down
4 changes: 2 additions & 2 deletions src/midje/ideas/tabular.clj
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@
(headings-rows+values [table locals]
(split-with (partial table-variable? locals) (remove-pipes+where table)))]

(defn- #^:tested-private table-binding-maps [table locals]
(defn- #^:testable table-binding-maps [table locals]
(let [[headings-row values] (headings-rows+values table locals)
value-rows (partition (count headings-row) values)]
(map (partial ordered-zipmap headings-row) value-rows)))

(defn- #^:tested-private add-binding-note
(defn- #^:testable add-binding-note
[expect-containing-form ordered-binding-map]
(translate-zipper expect-containing-form
expect?
Expand Down
18 changes: 9 additions & 9 deletions src/midje/internal_ideas/fakes.clj
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@

;;; Binding

(defn #^:tested-private usable-default-function? [fake]
(defn usable-default-function? [fake]
(and (bound? (:lhs fake))
(let [value-in-var (var-get (:lhs fake))
unfinished-fun (:midje/unfinished-fun (meta (:lhs fake)))]
Expand All @@ -137,7 +137,7 @@

(def #^:dynamic #^:private *call-action-count* (atom 0))

(defn- #^:tested-private best-call-action [function-var actual-args fakes]
(defn- #^:testable best-call-action [function-var actual-args fakes]
(when (= 2 @*call-action-count*)
(throw (user-error "You seem to have created a prerequisite for"
(str (pr-str function-var) " that interferes with that function's use in Midje's")
Expand All @@ -159,7 +159,7 @@
:else (:value-at-time-of-faking
(first possible-fakes)))))))

(defn- #^:tested-private call-faker
(defn- #^:testable call-faker
"This is the function that handles all mocked calls."
[function-var actual-args fakes]
(macrolet [(counting-nested-calls [& forms]
Expand All @@ -182,10 +182,10 @@

;; Binding map related

(defn- #^:tested-private unique-vars [fakes]
(defn- #^:testable unique-vars [fakes]
(distinct (map :lhs fakes)))

(defn- #^:tested-private binding-map-with-function-fakes [fakes]
(defn- #^:testable binding-map-with-function-fakes [fakes]
(letfn [(fn-that-implements-a-fake [function]
(vary-meta function assoc :midje/faked-function true))
(make-faker [var]
Expand All @@ -194,12 +194,12 @@
(for [var (unique-vars fakes)]
[var (make-faker var)]))))

(defn- #^:tested-private merge-metaconstant-bindings [bindings]
(defn- #^:testable merge-metaconstant-bindings [bindings]
(apply merge-with (fn [^Metaconstant v1 ^Metaconstant v2]
(Metaconstant. (.name v1) (merge (.storage v1) (.storage v2))))
bindings))

(defn- #^:tested-private data-fakes-to-metaconstant-bindings [fakes]
(defn- #^:testable data-fakes-to-metaconstant-bindings [fakes]
(for [{var :lhs, contents :contained} fakes]
{var (Metaconstant. (object-name var) contents)}))

Expand Down Expand Up @@ -253,7 +253,7 @@
;; mapping. These substitutions are used both to "flatten" a fake form and also
;; to generate new fakes.

(defn- #^:tested-private mockable-funcall? [x]
(defn- #^:testable mockable-funcall? [x]
(let [constructor? (fn [symbol]
(.endsWith (name symbol) "."))
special-forms '[quote fn let new]
Expand Down Expand Up @@ -290,7 +290,7 @@
(let [new-args (for [a args] (get substitutions a a))]
`(~fake (~fun ~@new-args) ~@rest)))

(defn- #^:tested-private unfolding-step
(defn- #^:testable unfolding-step
"This walks through a `pending` list that may contain fakes. Each element is
copied to the `finished` list. If it is a suitable fake, its nested
are flattened (replaced with a metaconstant). If the metaconstant was newly
Expand Down
2 changes: 1 addition & 1 deletion src/midje/open_protocols.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
(:use [midje.production-mode :only [user-desires-checking?]]
[midje.internal-ideas.fakes :only [implements-a-fake?]]))

(defn- #^:tested-private implementation?
(defn- #^:testable implementation?
"Is this thing a protocol or a function definition?"
[name-or-impl]
(not (symbol? name-or-impl)))
Expand Down
4 changes: 2 additions & 2 deletions src/midje/semi_sweet.clj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
;;
;; FURTHERMORE, I wanted to use set operations to check for fake and not-called,
;; but those fail for reasons I don't understand. Bah.
(defn- #^:tested-private check-for-arrow [arrow]
(defn- #^:testable check-for-arrow [arrow]
(get {=> :check-match
=expands-to=> :check-match
=not=> :check-negated-match
Expand Down Expand Up @@ -131,7 +131,7 @@
[forms]
(not-called* forms))

(defn- #^:tested-private a-fake? [x]
(defn- #^:testable a-fake? [x]
(and (seq? x)
(is-semi-sweet-keyword? (first x))))

Expand Down
9 changes: 9 additions & 0 deletions src/midje/sweet.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
future-fact-variant-names]])
(:require [midje.ideas.background :as background]
midje.checkers
[midje.test-util :as test-util]
[midje.internal-ideas.report :as report]))

(immigrate 'midje.unprocessed)
Expand All @@ -35,6 +36,14 @@
(intern+keep-meta *ns* 'after #'background/after)
(intern+keep-meta *ns* 'around #'background/around)

(defmacro expose-testables
"Enables testing of vars in the target ns which have ^:testable metadata"
[target-ns]
(macro-for [testable-sym (for [[sym var] (ns-interns target-ns)
:when (:testable (meta var))]
sym) ]
`(def ~testable-sym (intern '~target-ns '~testable-sym))))

(defmacro background
"Runs facts against setup code which is run before, around, or after
:contents, :facts or :checks. Optionally, contains one or more provided forms
Expand Down
2 changes: 1 addition & 1 deletion test/midje/checkers/t_collection.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[midje.checkers.defining :only [checker?]]
[midje.checkers.chatty :only [chatty-falsehood-to-map
chatty-checker-falsehood?]]))
(testable-privates midje.checkers.collection separate-looseness)
(expose-testables midje.checkers.collection)

(defrecord AB [a b])
(defrecord AB-different-class [a b])
Expand Down
2 changes: 1 addition & 1 deletion test/midje/error_handling/t_validation_errors.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[midje.internal-ideas.file-position :only [form-position]]
[clojure.algo.monads]))

(testable-privates midje.error-handling.validation-errors as-validation-error spread-validation-error)
(expose-testables midje.error-handling.validation-errors)

(fact "any form can be turned into a validation-error form"
(meta (as-validation-error '(form))) => (contains {:midje-validation-error true})
Expand Down
3 changes: 1 addition & 2 deletions test/midje/ideas/t_background.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
[midje.error-handling validation-errors]
[midje.ideas.background :only [separate-background-forms setup-teardown-bindings
seq-headed-by-setup-teardown-form? background-wrappers]]))
(testable-privates midje.ideas.background
extract-state-descriptions+fakes state-wrapper)
(expose-testables midje.ideas.background)

(unfinished unused used)
(defn calls-nothing [] )
Expand Down
2 changes: 1 addition & 1 deletion test/midje/ideas/t_tabular.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[midje sweet test-util]
[ordered.map :only (ordered-map)]))

(testable-privates midje.ideas.tabular add-binding-note table-binding-maps)
(expose-testables midje.ideas.tabular)

;; Core midje.sweet API

Expand Down
53 changes: 25 additions & 28 deletions test/midje/internal_ideas/t_fakes.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,34 @@
[midje.test-util])
(:import midje.ideas.metaconstants.Metaconstant))

(testable-privates midje.internal-ideas.fakes
mockable-funcall? unfolding-step merge-metaconstant-bindings
data-fakes-to-metaconstant-bindings binding-map-with-function-fakes unique-vars
call-faker best-call-action)
(expose-testables midje.internal-ideas.fakes)

(tabular
(facts "the arg matcher maker handles functions specially"
(facts "the arg matcher maker handles functions specially"
((arg-matcher-maker ?expected) ?actual) => ?result)
?expected ?actual ?result
1 1 TRUTHY
1 odd? falsey
?expected ?actual ?result
1 1 TRUTHY
1 odd? falsey

anything 3 TRUTHY
anything odd? TRUTHY
(roughly 3) 3 TRUTHY
(roughly 3) 0 falsey
(contains 1) [3 1] TRUTHY
(contains 1) [3 3] falsey
(contains odd?) [3] TRUTHY
(contains odd?) [2 odd?] falsey

anything 3 TRUTHY
anything odd? TRUTHY
(roughly 3) 3 TRUTHY
(roughly 3) 0 falsey
(contains 1) [3 1] TRUTHY
(contains 1) [3 3] falsey
(contains odd?) [3] TRUTHY
(contains odd?) [2 odd?] falsey
(exactly odd?) odd? TRUTHY
(exactly odd?) 3 falsey

(exactly odd?) odd? TRUTHY
(exactly odd?) 3 falsey
(as-checker odd?) odd? falsey
(as-checker odd?) 3 TRUTHY

(as-checker odd?) odd? falsey
(as-checker odd?) 3 TRUTHY
odd? odd? TRUTHY
odd? 3 falsey)

odd? odd? TRUTHY
odd? 3 falsey)



(declare f g)
(fact "unique variables can be found in fakes"
Expand Down Expand Up @@ -175,7 +172,7 @@
(defn doubler [n] (+ n n))

(after-silently
(fact
(fact
(doubler 3) => 0
(provided
(+ 3 3) => 0))
Expand Down Expand Up @@ -261,7 +258,7 @@
(defmacro some-macro [& rest] )

(tabular "things that are not fake-sexps don't need to be unfolded"
(fact ?thing ?arrow folded-fake?)
(fact ?thing ?arrow folded-fake?)

;; things not a proper fake macro
?thing ?arrow
Expand All @@ -274,11 +271,11 @@
(cons 'midje.semi-sweet/fake '((f (h 3)) =test=> 3)) => )

(tabular
(fact "unfolding depends on the inner structure of a funcall"
(fact "unfolding depends on the inner structure of a funcall"
'(midje.semi-sweet/fake ?call =test=> 3) ?arrow folded-fake?)

?call ?arrow
;; Things that might be misinterpreted as nested funcalls
?call ?arrow
;; Things that might be misinterpreted as nested funcalls
(f) =not=>
(f 1) =not=>
(f 1 '(foo)) =not=>
Expand Down
4 changes: 1 addition & 3 deletions test/midje/internal_ideas/t_report.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
[midje.error-handling.exceptions :only [captured-throwable]]
[midje sweet test-util]))


(testable-privates midje.internal-ideas.report attractively-stringified-form)

(expose-testables midje.internal-ideas.report)

;; This set of tests generate failures. The following code prevents
;; them from being counted as failures when the final summary is
Expand Down
2 changes: 1 addition & 1 deletion test/midje/t_semi_sweet.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[midje.util form-utils]
[midje.test-util])
(:require [clojure.zip :as zip]))
(testable-privates midje.semi-sweet a-fake? check-for-arrow)
(expose-testables midje.semi-sweet)

(unfinished faked-function mocked-function other-function)

Expand Down
6 changes: 1 addition & 5 deletions test/midje/test_util.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
(:use [clojure.test]
midje.checkers
[clojure.set :only [subset?]]
[midje.util.form-utils :only [macro-for]]))

(defmacro testable-privates [namespace & symbols]
(macro-for [sym symbols]
`(def ~sym (intern '~namespace '~sym))))
[midje.util.form-utils :only [macro-for alias-var]]))

(def reported (atom []))

Expand Down

0 comments on commit 7951db6

Please sign in to comment.