 (ns bake.test
-  (:use [cake.contrib.find-namespaces :only [find-namespaces-in-dir]])
-  (:require clojure.test
+  (:use [cake.contrib.find-namespaces :only [find-namespaces-in-dir]]
+        useful)
+  (:require [clojure.test :as test]
             [clojure.stacktrace :as stack]))
-
-(defn map-tags [nses]
-  (reduce (partial merge-with concat)
-          (for [ns nses
-                [name f] (ns-publics ns)
-                tag (:tags (meta f))]
-            {tag [f]})))
 
 (defn all-test-namespaces []
   (find-namespaces-in-dir (java.io.File. "test")))

  (println (:fail m) "failures," (:error m) "errors.")
  (when (:start-time m) (timer (:start-time m))))

(defmethod clojure.test/report :begin-test-ns [m]
  (clojure.test/with-test-out
   (println "\nTesting" (ns-name (:ns m)))))

(defmethod clojure.test/report :summary [m]
  (clojure.test/with-test-out
    (print-results m)))

(defmethod clojure.test/report :fn-summary [m]
  (clojure.test/with-test-out
    (print-results m)))

(defmethod clojure.test/report :begin-auto [m])

(defmethod clojure.test/report :summary-auto [m]
  (clojure.test/with-test-out
    (if (and (= 0 (:fail m))
             (= 0 (:error m))
             (not (:full-report? m)))

        (keyword?  test) :tag
        :else            :ns))

(defn get-grouped-tests [namespaces opts]
  (let [tests (:test opts)]
    (group-by
     test-type
     (if (nil? tests)
       namespaces
       (map prep-opt tests)))))
