|
1 |
| -(ns basilisp.test) |
| 1 | +(ns basilisp.test |
| 2 | + (:import |
| 3 | + inspect)) |
2 | 4 |
|
3 | 5 | (def ^:private collected-tests
|
4 | 6 | (atom []))
|
|
15 | 17 | (def ^:dynamic *test-section* nil)
|
16 | 18 | (def ^:dynamic *test-failures* nil)
|
17 | 19 |
|
| 20 | +(defn line-no |
| 21 | + "Get the line number from the current interpreter stack. |
| 22 | + |
| 23 | + This is a horrible hack and it requires each particular assertion case |
| 24 | + to define their frame offset from here, but it seems to be the most |
| 25 | + accessible way of determining the line number without capturing it |
| 26 | + for every test (including non-failing tests)." |
| 27 | + [n] |
| 28 | + (.-lineno (nth (inspect/stack) n))) |
| 29 | + |
| 30 | +(defmulti gen-assert |
| 31 | + (fn [expr _] |
| 32 | + (cond |
| 33 | + (list? expr) (first expr) |
| 34 | + :else :default))) |
| 35 | + |
| 36 | +(defmethod gen-assert '= |
| 37 | + [expr msg] |
| 38 | + `(when-not ~expr |
| 39 | + (swap! *test-failures* |
| 40 | + conj |
| 41 | + {:test-name *test-name* |
| 42 | + :test-section *test-section* |
| 43 | + :message ~msg |
| 44 | + :expr (quote ~expr) |
| 45 | + :actual ~(nth expr 2) |
| 46 | + :expected ~(second expr) |
| 47 | + :line (line-no 4) |
| 48 | + :type :failure}))) |
| 49 | + |
| 50 | +(defmethod gen-assert 'thrown? |
| 51 | + [expr msg] |
| 52 | + (let [exc-type (second expr) |
| 53 | + body (nthnext expr 2)] |
| 54 | + `(try |
| 55 | + (let [result# (do ~@body)] |
| 56 | + (swap! *test-failures* |
| 57 | + conj |
| 58 | + {:test-name *test-name* |
| 59 | + :test-section *test-section* |
| 60 | + :message ~msg |
| 61 | + :expr (quote ~expr) |
| 62 | + :actual result# |
| 63 | + :expected (quote ~exc-type) |
| 64 | + :line (line-no 3) |
| 65 | + :type :failure})) |
| 66 | + (catch ~exc-type _ nil) |
| 67 | + (catch builtins/Exception e# |
| 68 | + (swap! *test-failures* |
| 69 | + conj |
| 70 | + {:test-name *test-name* |
| 71 | + :test-section *test-section* |
| 72 | + :message (str "Expected " ~exc-type "; got " (builtins/type e#) " instead") |
| 73 | + :expr (quote ~expr) |
| 74 | + :actual e# |
| 75 | + :expected ~exc-type |
| 76 | + :line (line-no 3) |
| 77 | + :type :failure}))))) |
| 78 | + |
| 79 | +(defmethod gen-assert :default |
| 80 | + [expr msg] |
| 81 | + `(let [computed# ~expr] |
| 82 | + (when-not computed# |
| 83 | + (swap! *test-failures* |
| 84 | + conj |
| 85 | + {:test-name *test-name* |
| 86 | + :test-section *test-section* |
| 87 | + :message ~msg |
| 88 | + :expr (quote ~expr) |
| 89 | + :actual computed# |
| 90 | + :expected computed# |
| 91 | + :line (line-no 6) |
| 92 | + :type :failure})))) |
| 93 | + |
18 | 94 | (defmacro is
|
19 | 95 | "Assert that expr is true. Must appear inside of a deftest form."
|
20 | 96 | ([expr]
|
21 |
| - `(is ~expr (str "Test failure: " ~expr))) |
| 97 | + `(is ~expr (str "Test failure: " (pr-str (quote ~expr))))) |
22 | 98 | ([expr msg]
|
23 |
| - (let [line-no (:basilisp.lang.reader/line (meta &form)) |
24 |
| - |
25 |
| - eq-sym `= |
26 |
| - |
27 |
| - deconstruct? (and (list? expr) (= eq-sym (first expr))) |
28 |
| - |
29 |
| - expected (if deconstruct? |
30 |
| - (second expr) |
31 |
| - expr) |
32 |
| - actual (if deconstruct? |
33 |
| - (nth expr 2) |
34 |
| - expr)] |
35 |
| - `(let [computed# ~expr |
36 |
| - actual# ~actual |
37 |
| - expected# ~expected] |
38 |
| - (when-not computed# |
39 |
| - ;; Collect test failures in the atom bound to |
40 |
| - ;; `*test-failures*` by `deftest`. |
41 |
| - (swap! *test-failures* |
42 |
| - conj |
43 |
| - [~msg |
44 |
| - {:test-name *test-name* |
45 |
| - :test-section *test-section* |
46 |
| - :expr (quote ~expr) |
47 |
| - :line ~line-no |
48 |
| - :actual actual# |
49 |
| - :expected expected#}])))))) |
| 99 | + `(try |
| 100 | + ~(gen-assert expr msg) |
| 101 | + (catch builtins/Exception e# |
| 102 | + (swap! *test-failures* |
| 103 | + conj |
| 104 | + {:test-name *test-name* |
| 105 | + :test-section *test-section* |
| 106 | + :message (str "Unexpected exception thrown during test run: " (builtins/repr e#)) |
| 107 | + :expr (quote ~expr) |
| 108 | + :actual e# |
| 109 | + :expected (quote ~expr) |
| 110 | + :line (line-no 2) |
| 111 | + :type :failure}))))) |
50 | 112 |
|
51 | 113 | (defmacro testing
|
52 | 114 | "Wrapper for test cases to provide additional messaging and context
|
|
0 commit comments