aboutsummaryrefslogtreecommitdiff
path: root/test-is.clj
blob: c6d71225a2f8720522ae50db13a44a6a1a70dfca (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
;;; test-is.clj: test framework for Clojure

;; by Stuart Sierra, http://stuartsierra.com/
;; June 5, 2008

;; Copyright (c) 2008 Stuart Sierra. All rights reserved.  The use and
;; distribution terms for this software are covered by the Common
;; Public License 1.0 (http://www.opensource.org/licenses/cpl1.0.php)
;; which can be found in the file CPL.TXT at the root of this
;; distribution.  By using this software in any fashion, you are
;; agreeing to be bound by the terms of this license.  You must not
;; remove this notice, or any other, from this software.



;; Inspired by many Common Lisp test frameworks and clojure/test, this
;; file is a Clojure test framework.
;;
;; Define tests as :test metadata on your fns.  Use the "is" macro
;; for assertions.  Examples:
;;
;;     (defn add2
;;       ([x] (+ x 2))
;;       {:test (fn [] (is (= (add2 3) 5))
;;                     (is (= (add2 -4) -2)
;;                     (is (> (add2 50) 50)))})
;;
;; You can also define tests in isolation with the "deftest" macro:
;;
;;     (deftest test-new-fn
;;       (is (= (new-fn) "Awesome")))
;;
;; Run tests with (test-ns) or (test-all). As in any language with
;; macros, you may need to recompile functions after changing a macro
;; definition.



(clojure/in-ns 'test-is)
(clojure/refer 'clojure)

;;; PRIVATE

(defmacro #^{:private true} defcounter [ref-name fn-name]
  `(do (def ~(with-meta ref-name {:private true}) nil)
       (defn ~fn-name []
         (when ~ref-name (sync nil (commute ~ref-name inc))))))

(defcounter *tests* count-test)
(defcounter *assertions* count-assertion)
(defcounter *failures* count-failure)
(defcounter *exceptions* count-exception)

(defmacro failure [reason message]
  `(throw (new java.lang.AssertionError
               (str ~reason (when ~message (str "; " ~message))))))

(defn- assert-expr [form message]
  `(do (count-assertion)
       (let [value# ~form]
         (when-not value#
           (failure (str ~(pr-str form) " was false/nil")
                    ~message)))))

(defn- assert-equal [form message]
  (let [expr1 (second form)
        expr2 (nth form 2)]
    `(do (count-assertion)
         (let [value1# ~expr1
               value2# ~expr2]
           (when-not (= value1# value2#)
             (failure (str ~(pr-str expr1) " is " (pr-str value1#)
                           " but should be " (pr-str value2#))
                      ~message))))))

(defn- always-fail-assert [message]
  `(do (count-assertion)
       (failure ~message nil)))

(defmacro #^{:private true} with-test-counters [& body]
  `(binding [*tests* (ref 0)
             *assertions* (ref 0)
             *failures* (ref 0)
             *exceptions* (ref 0)]
     ~@body
     (.. System err (println (str "\nRan " @*tests* " tests with "
                                  @*assertions* " assertions.\n"
                                  @*failures* " failures, "
                                  @*exceptions* " exceptions.")))))

(defn- run-test-fn
  "Calls the function; reports errors/exceptions."
  [f name]
  (try
   (count-test)
   (f)
   (catch java.lang.AssertionError e
     (count-failure)
     (.. System err (println (str "FAIL in " name ": "
                                  (.getMessage e)))))
   (catch java.lang.Exception e
     (count-exception)
     (.. System err (println (str "EXCEPTION in " name ": " e))))))

(defn- test-var
  "Finds and calls the fn in a var's :test metadata."
  [v]
  (when-let f (:test (meta v))
    (run-test-fn f (str v))))

(defn- test-interns
  "Tests all interned symbols in the namespace."
  [ns]
  (let [ns (if (symbol? ns) (find-ns ns) ns)]
    (.. System err (println (str "Testing " ns)))
    (dorun (map test-var (vals (ns-interns ns))))))


;;; PUBLIC

(defmacro is
  "Generic assertion macro.  Throws AssertionError if form evaluates
  logical false.  Optional message will be added to the error.

  form may be one of:
    * an equality test like (= expression expected-value)
    * nil, which always fails
    * an arbitrary expression, fails if it returns false/nil"
  ([form] `(is ~form nil))
  ([form message]
     (cond
      (nil? form) (always-fail-assert message)
      (and (seq? form) (= '= (first form))) (assert-equal form message)
      :else (assert-expr form message))))

(defn test-ns
  "Runs tests on all interned symbols in the namespaces 
  (symbols or namespace objects) and reports results."
  ([] (test-ns *ns*))
  ([& namespaces]
     (with-test-counters (dorun (map test-interns namespaces)))))

(defn test-all
  "Runs all tests in all namespaces."
  []
  (apply test-ns (all-ns)))

(defmacro deftest
  "Defs an unbound Var with body in its :test fn."
  [name & body]
  `(def ~(with-meta name {:test `(fn [] ~@body)})))