 ;;; Binding
 
 
-(defn- var-handled-by-fake? [function-var fake]
-  (= function-var (:lhs fake)))
-
-(defmulti ^:private call-handled-by-fake? (fn [function-var actual-args fake] (:type fake)))
-
-(defmethod call-handled-by-fake? :not-called [function-var actual-args fake]
-  (var-handled-by-fake? function-var fake))
-
-(defmethod call-handled-by-fake? :default [function-var actual-args fake]
-  (and (var-handled-by-fake? function-var fake)
-       (= (count actual-args) (count (:arg-matchers fake)))
-       (extended-list-= actual-args (:arg-matchers fake))))
-
-(defn usable-default-function? [fake]
-  (and (bound? (:lhs fake))
-       (let [stashed-value (var-get (:lhs fake))
-             unfinished-fun (:midje/unfinished-fun (meta (:lhs fake)))]
-         (and (extended-fn? stashed-value)
-              (or (nil? unfinished-fun)
-                  (not= unfinished-fun stashed-value))))))
-
-
-(def ^:dynamic ^:private *call-action-count* (atom 0))
+(letfn [(var-handled-by-fake? [function-var fake]
+          (= function-var (:lhs fake)))]
+  
+  (defmulti ^:private call-handled-by-fake? (fn [function-var actual-args fake] (:type fake)))
+  
+  (defmethod call-handled-by-fake? :not-called [function-var actual-args fake]
+    (var-handled-by-fake? function-var fake))
+  
+  (defmethod call-handled-by-fake? :default [function-var actual-args fake]
+    (and (var-handled-by-fake? function-var fake)
+         (= (count actual-args) (count (:arg-matchers fake)))
+         (extended-list-= actual-args (:arg-matchers fake))))
+  
+  (defn #^:tested-private usable-default-function? [fake]
+    (and (bound? (:lhs fake))
+         (let [stashed-value (var-get (:lhs fake))
+               unfinished-fun (:midje/unfinished-fun (meta (:lhs fake)))]
+           (and (extended-fn? stashed-value)
+                (or (nil? unfinished-fun)
+                    (not= unfinished-fun stashed-value))))))
+  
+  
+  (def ^:dynamic ^:private *call-action-count* (atom 0))
+  
+  (defn- 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")
+               (str "own code. To fix, define a function of your own that uses "
+                 (or (:name (meta function-var)) function-var) ", then")
+               "describe that function in a provided clause. For example, instead of this:"
+               "  (provided (every? even? ..xs..) => true)"
+               "do this:"
+               "  (def all-even? (partial every? even?))"
+               "  ;; ..."
+               "  (provided (all-even? ..xs..) => true)")))
+    (if-let [found (find-first (partial call-handled-by-fake? function-var actual-args)
+                               fakes)]
+      found
+      (let [possible-fakes (filter (partial var-handled-by-fake? function-var) fakes)]
+        (cond (empty? possible-fakes)
+          nil
+    
+          ;; For finding a default, any possible fake is as good as any other
+          (not (usable-default-function? (first possible-fakes)))
+          nil
+    
+          :else (:value-at-time-of-faking (first possible-fakes)))))))
+
 (defmacro ^:private counting-nested-calls-calls [& forms]
   `(try
      (swap! *call-action-count* inc)
      ~@forms
      (finally (swap! *call-action-count* dec))))
-
-(defn- 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")
-             (str "own code. To fix, define a function of your own that uses "
-               (or (:name (meta function-var)) function-var) ", then")
-             "describe that function in a provided clause. For example, instead of this:"
-             "  (provided (every? even? ..xs..) => true)"
-             "do this:"
-             "  (def all-even? (partial every? even?))"
-             "  ;; ..."
-             "  (provided (all-even? ..xs..) => true)")))
-  (if-let [found (find-first (partial call-handled-by-fake? function-var actual-args)
-                             fakes)]
-    found
-    (let [possible-fakes (filter (partial var-handled-by-fake? function-var) fakes)]
-      (cond (empty? possible-fakes)
-        nil
-  
-        ;; For finding a default, any possible fake is as good as any other
-        (not (usable-default-function? (first possible-fakes)))
-        nil
-  
-        :else (:value-at-time-of-faking (first possible-fakes))))))
 
 (defn- call-faker
   "This is the function that handles all mocked calls." 

  [finished pending substitutions]
  (let [target (first pending)]
    (if-not (folded-fake? target)
      [(conj finished target), (rest pending), substitutions]
    
      (let [overrides (drop 4 target)
            augmented-substitutions (augment-substitutions substitutions target)
            flattened-target (flatten-fake target augmented-substitutions)
            generated-fakes (generate-fakes (map-difference augmented-substitutions substitutions) overrides)]
        [(conj finished flattened-target), (concat generated-fakes (rest pending)), augmented-substitutions]))))

(defn- unfold-expect-form__then__stay_put [loc]
  (loop [[finished pending substitutions] [[] (zip/node loc) {}]]
    (if (empty? pending)
      (zip/replace loc (apply list finished))
      (recur (unfolding-step finished pending substitutions)))))

(defn unfold-fakes [form]
  (with-fresh-generated-metaconstant-names
    (translate-zipper form
      expect?
      unfold-expect-form__then__stay_put)))
;; mapping. These substitutions are used both to "flatten" a fake form and also
;; to generate new fakes.

(defn- #^:tested-private mockable-funcall? [thing]
  (let [constructor? (fn [symbol]
                       (.endsWith (name symbol) "."))
        special-forms '[quote fn let new]
