|
64 | 64 | [ns v m]
|
65 | 65 | (let [c (when (seq test/*testing-contexts*) (test/testing-contexts-str))
|
66 | 66 | i (count (get (@current-report :results) (:name (meta v))))
|
67 |
| - f (or (:test (meta v)) @v)] ; test fn or deref'ed fixture |
68 |
| - (merge {:ns ns, :var (:name (meta v)), :index i, :context c} |
69 |
| - (if (#{:fail :error} (:type m)) |
70 |
| - (-> (if-let [e (when (= :error (:type m)) (:actual m))] |
71 |
| - (assoc m :error e, :line (:line (stack-frame e f))) |
72 |
| - m) |
73 |
| - (update-in [:expected] #(with-out-str (pp/pprint %))) |
74 |
| - (update-in [:actual] #(with-out-str (pp/pprint %)))) |
75 |
| - (dissoc m :expected :actual))))) |
| 67 | + t (:type m)] |
| 68 | + ;; Errors outside assertions (faults) do not return an :expected value. |
| 69 | + ;; Type :fail returns :actual value. Type :error returns :error and :line. |
| 70 | + (merge (dissoc m :expected :actual) |
| 71 | + {:ns ns, :var (:name (meta v)), :index i, :context c} |
| 72 | + (when (and (#{:fail :error} t) (not (:fault m))) |
| 73 | + {:expected (with-out-str (pp/pprint (:expected m)))}) |
| 74 | + (when (#{:fail} t) |
| 75 | + {:actual (with-out-str (pp/pprint (:actual m)))}) |
| 76 | + (when (#{:error} t) |
| 77 | + (let [e (:actual m) |
| 78 | + f (or (:test (meta v)) @v)] ; test fn or deref'ed fixture |
| 79 | + {:error e |
| 80 | + :line (:line (stack-frame e f))}))))) |
76 | 81 |
|
77 | 82 | (defn report
|
78 | 83 | "Handle reporting for test events. This takes a test event map as an argument
|
|
104 | 109 | fixture (resolve (symbol (:var frame)))]
|
105 | 110 | (swap! current-report update-in [:summary :test] dec)
|
106 | 111 | (binding [test/*testing-vars* (list fixture)]
|
107 |
| - (report {:type :error, :expected nil, :actual e |
108 |
| - :message "Unhandled exception in test fixture"})))) |
| 112 | + (report {:type :error, :fault true, :expected nil, :actual e |
| 113 | + :message "Uncaught exception in test fixture"})))) |
109 | 114 |
|
110 | 115 | ;;; ## Test Execution
|
111 | 116 | ;; These functions are based on the ones in `clojure.test`, updated to accept
|
112 |
| -;; a list of vars to test, and use the report implementation above. |
| 117 | +;; a list of vars to test, use the report implementation above, and distinguish |
| 118 | +;; between test errors and faults outside of assertions. |
| 119 | + |
| 120 | +(defn test-var |
| 121 | + "If var `v` has a function in its `:test` metadata, call that function, |
| 122 | + with `clojure.test/*testing-vars*` bound to append `v`." |
| 123 | + [v] |
| 124 | + (when-let [t (:test (meta v))] |
| 125 | + (binding [test/*testing-vars* (conj test/*testing-vars* v)] |
| 126 | + (test/do-report {:type :begin-test-var :var v}) |
| 127 | + (test/inc-report-counter :test) |
| 128 | + (try (t) |
| 129 | + (catch Throwable e |
| 130 | + (test/do-report {:type :error, :fault true, :expected nil, :actual e |
| 131 | + :message "Uncaught exception, not in assertion"}))) |
| 132 | + (test/do-report {:type :end-test-var :var v})))) |
113 | 133 |
|
114 | 134 | (defn test-vars
|
115 |
| - "Call `clojure.test/test-var` on each var, with the fixtures defined for |
116 |
| - namespace object `ns`." |
| 135 | + "Call `test-var` on each var, with the fixtures defined for namespace |
| 136 | + object `ns`." |
117 | 137 | [ns vars]
|
118 | 138 | (let [once-fixture-fn (test/join-fixtures (::test/once-fixtures (meta ns)))
|
119 | 139 | each-fixture-fn (test/join-fixtures (::test/each-fixtures (meta ns)))]
|
120 | 140 | (try (once-fixture-fn
|
121 | 141 | (fn []
|
122 | 142 | (doseq [v vars]
|
123 | 143 | (when (:test (meta v))
|
124 |
| - (each-fixture-fn (fn [] (test/test-var v))))))) |
| 144 | + (each-fixture-fn (fn [] (test-var v))))))) |
125 | 145 | (catch Throwable e
|
126 | 146 | (report-fixture-error ns e)))))
|
127 | 147 |
|
|
0 commit comments