/
ns.clj
71 lines (65 loc) · 3.11 KB
/
ns.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
(ns kaocha.type.ns
(:refer-clojure :exclude [symbol])
(:require [clojure.spec.alpha :as s]
[clojure.test :as t]
[kaocha.core-ext :refer :all]
[kaocha.hierarchy :as hierarchy]
[kaocha.ns :as ns]
[kaocha.testable :as testable]
[kaocha.type :as type]))
(defn ->testable [ns-name]
{:kaocha.testable/type :kaocha.type/ns
:kaocha.testable/id (keyword (str ns-name))
:kaocha.testable/desc (str ns-name)
:kaocha.ns/name ns-name})
(defn run-tests [testable test-plan fixture-fn]
;; It's not guaranteed the the fixture-fn returns the result of calling the
;; tests function, so we need to put it in a box for reference.
(let [result (atom (:kaocha.test-plan/tests testable))]
(fixture-fn #(swap! result testable/run-testables test-plan))
@result))
(defmethod testable/-load :kaocha.type/ns [testable]
;; TODO If the namespace has a test-ns-hook function, call that:
;; if-let [v (find-var (symbol (:kaocha.ns/name testable) "test-ns-hook"))]
(let [ns-name (:kaocha.ns/name testable)
ns-obj (ns/required-ns ns-name)
ns-meta (meta ns-obj)
each-fixture-fn (t/join-fixtures (::t/each-fixtures ns-meta))]
(->> ns-obj
ns-interns
(filter (comp :test meta val))
(sort-by key)
(map (fn [[sym var]]
(let [nsname (:kaocha.ns/name testable)
test-name (symbol (str nsname) (str sym))]
{:kaocha.testable/type :kaocha.type/var
:kaocha.testable/id (keyword test-name)
:kaocha.testable/meta (meta var)
:kaocha.testable/desc (str sym)
:kaocha.var/name test-name
:kaocha.var/var var
:kaocha.var/test (:test (meta var))
:kaocha.testable/wrap (if (::t/each-fixtures ns-meta)
[(fn [t] #(each-fixture-fn t))]
[])})))
(assoc testable
:kaocha.testable/meta (meta ns-obj)
:kaocha.ns/ns ns-obj
:kaocha.test-plan/tests))))
(defmethod testable/-run :kaocha.type/ns [testable test-plan]
(let [do-report #(t/do-report (merge {:ns (:kaocha.ns/ns testable)} %))]
(type/with-report-counters
(do-report {:type :begin-test-ns})
(let [ns-meta (:kaocha.testable/meta testable)
once-fixture-fn (t/join-fixtures (::t/once-fixtures ns-meta))
tests (run-tests testable test-plan once-fixture-fn)
result (assoc (dissoc testable :kaocha.test-plan/tests)
:kaocha.result/tests
tests)]
(do-report {:type :end-test-ns})
result))))
(s/def :kaocha.type/ns (s/keys :req [:kaocha.testable/type
:kaocha.testable/id
:kaocha.ns/name]
:opt [:kaocha.ns/ns
:kaocha.test-plan/tests]))