forked from carp-lang/Carp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tester.carp
56 lines (49 loc) · 2.24 KB
/
tester.carp
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
(def *active-test-suite* "")
(defn tester/set-suite! [test-suite-name]
(reset! *active-test-suite* test-suite-name))
(def tester/test-suites {})
(defn tester/add-test! [test-name]
(let [existing-tests (get-maybe tester/test-suites *active-test-suite*)]
(reset! tester/test-suites (assoc tester/test-suites *active-test-suite* (cons-last existing-tests test-name)))))
(defn tester/run-suite [test-suite-name]
(let [tests (get tester/test-suites test-suite-name)
failed-count 0
start-time (now)]
(if (= 0 (count tests))
(println (str (get-console-color console-color-yellow)
"No tests to run in test suite '" test-suite-name "'."
(get-normal-console-color)))
(do (map (fn [test-name]
(let [test-func (eval (symbol test-name))
e (catch-error (test-func))]
(if (nil? e)
:success
(do
(println (str (get-console-color console-color-red)
(inc failed-count) "."
" Error in test '" test-name "' \n"
(get-normal-console-color)
e "\n"))
(swap! failed-count inc)))))
tests)
(println (str (if (= 0 failed-count)
(get-console-color console-color-green)
(get-console-color console-color-red))
"Ran " (count tests) " tests in test suite '" test-suite-name
"', " failed-count " of them failed (took " (/ (- (now) start-time) 1000) " secs)."
(get-normal-console-color)))))))
(defmacro deftest [name body]
(list 'do
(list 'defn name [] body)
(list 'tester/add-test! (prn name))))
;; Example usage:
;; (tester/set-suite! "foo")
;; (deftest t1 (assert-eq 2 2))
;; (deftest t2 (assert-eq 3 3))
;; (deftest t3 (assert-eq 12 12))
;; (tester/run-suite "foo")
;; (defmacro in-test-suite [test-suite-name forms]
;; (list 'do
;; (list 'reset! *active-test-suite* (prn test-suite-name))
;; forms
;; (list 'reset! *active-test-suite* (prn ""))))