rlm@10
|
1 ; Copyright (c) Rich Hickey. All rights reserved.
|
rlm@10
|
2 ; The use and distribution terms for this software are covered by the
|
rlm@10
|
3 ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
|
rlm@10
|
4 ; which can be found in the file epl-v10.html at the root of this distribution.
|
rlm@10
|
5 ; By using this software in any fashion, you are agreeing to be bound by
|
rlm@10
|
6 ; the terms of this license.
|
rlm@10
|
7 ; You must not remove this notice, or any other, from this software.
|
rlm@10
|
8
|
rlm@10
|
9 ;;; test_clojure/test.clj: unit tests for test.clj
|
rlm@10
|
10
|
rlm@10
|
11 ;; by Stuart Sierra
|
rlm@10
|
12 ;; January 16, 2009
|
rlm@10
|
13
|
rlm@10
|
14 ;; Thanks to Chas Emerick, Allen Rohner, and Stuart Halloway for
|
rlm@10
|
15 ;; contributions and suggestions.
|
rlm@10
|
16
|
rlm@10
|
17
|
rlm@10
|
18 (ns clojure.test-clojure.test
|
rlm@10
|
19 (:use clojure.test))
|
rlm@10
|
20
|
rlm@10
|
21 (deftest can-test-symbol
|
rlm@10
|
22 (let [x true]
|
rlm@10
|
23 (is x "Should pass"))
|
rlm@10
|
24 (let [x false]
|
rlm@10
|
25 (is x "Should fail")))
|
rlm@10
|
26
|
rlm@10
|
27 (deftest can-test-boolean
|
rlm@10
|
28 (is true "Should pass")
|
rlm@10
|
29 (is false "Should fail"))
|
rlm@10
|
30
|
rlm@10
|
31 (deftest can-test-nil
|
rlm@10
|
32 (is nil "Should fail"))
|
rlm@10
|
33
|
rlm@10
|
34 (deftest can-test-=
|
rlm@10
|
35 (is (= 2 (+ 1 1)) "Should pass")
|
rlm@10
|
36 (is (= 3 (+ 2 2)) "Should fail"))
|
rlm@10
|
37
|
rlm@10
|
38 (deftest can-test-instance
|
rlm@10
|
39 (is (instance? Integer (+ 2 2)) "Should pass")
|
rlm@10
|
40 (is (instance? Float (+ 1 1)) "Should fail"))
|
rlm@10
|
41
|
rlm@10
|
42 (deftest can-test-thrown
|
rlm@10
|
43 (is (thrown? ArithmeticException (/ 1 0)) "Should pass")
|
rlm@10
|
44 ;; No exception is thrown:
|
rlm@10
|
45 (is (thrown? Exception (+ 1 1)) "Should fail")
|
rlm@10
|
46 ;; Wrong class of exception is thrown:
|
rlm@10
|
47 (is (thrown? ArithmeticException (throw (RuntimeException.))) "Should error"))
|
rlm@10
|
48
|
rlm@10
|
49 (deftest can-test-thrown-with-msg
|
rlm@10
|
50 (is (thrown-with-msg? ArithmeticException #"Divide by zero" (/ 1 0)) "Should pass")
|
rlm@10
|
51 ;; Wrong message string:
|
rlm@10
|
52 (is (thrown-with-msg? ArithmeticException #"Something else" (/ 1 0)) "Should fail")
|
rlm@10
|
53 ;; No exception is thrown:
|
rlm@10
|
54 (is (thrown? Exception (+ 1 1)) "Should fail")
|
rlm@10
|
55 ;; Wrong class of exception is thrown:
|
rlm@10
|
56 (is (thrown-with-msg? IllegalArgumentException #"Divide by zero" (/ 1 0)) "Should error"))
|
rlm@10
|
57
|
rlm@10
|
58 (deftest can-catch-unexpected-exceptions
|
rlm@10
|
59 (is (= 1 (throw (Exception.))) "Should error"))
|
rlm@10
|
60
|
rlm@10
|
61 (deftest can-test-method-call
|
rlm@10
|
62 (is (.startsWith "abc" "a") "Should pass")
|
rlm@10
|
63 (is (.startsWith "abc" "d") "Should fail"))
|
rlm@10
|
64
|
rlm@10
|
65 (deftest can-test-anonymous-fn
|
rlm@10
|
66 (is (#(.startsWith % "a") "abc") "Should pass")
|
rlm@10
|
67 (is (#(.startsWith % "d") "abc") "Should fail"))
|
rlm@10
|
68
|
rlm@10
|
69 (deftest can-test-regexps
|
rlm@10
|
70 (is (re-matches #"^ab.*$" "abbabba") "Should pass")
|
rlm@10
|
71 (is (re-matches #"^cd.*$" "abbabba") "Should fail")
|
rlm@10
|
72 (is (re-find #"ab" "abbabba") "Should pass")
|
rlm@10
|
73 (is (re-find #"cd" "abbabba") "Should fail"))
|
rlm@10
|
74
|
rlm@10
|
75 (deftest #^{:has-meta true} can-add-metadata-to-tests
|
rlm@10
|
76 (is (:has-meta (meta #'can-add-metadata-to-tests)) "Should pass"))
|
rlm@10
|
77
|
rlm@10
|
78 ;; still have to declare the symbol before testing unbound symbols
|
rlm@10
|
79 (declare does-not-exist)
|
rlm@10
|
80
|
rlm@10
|
81 (deftest can-test-unbound-symbol
|
rlm@10
|
82 (is (= nil does-not-exist) "Should error"))
|
rlm@10
|
83
|
rlm@10
|
84 (deftest can-test-unbound-function
|
rlm@10
|
85 (is (does-not-exist) "Should error"))
|
rlm@10
|
86
|
rlm@10
|
87
|
rlm@10
|
88 ;; Here, we create an alternate version of test/report, that
|
rlm@10
|
89 ;; compares the event with the message, then calls the original
|
rlm@10
|
90 ;; 'report' with modified arguments.
|
rlm@10
|
91
|
rlm@10
|
92 (declare original-report)
|
rlm@10
|
93
|
rlm@10
|
94 (defn custom-report [data]
|
rlm@10
|
95 (let [event (:type data)
|
rlm@10
|
96 msg (:message data)
|
rlm@10
|
97 expected (:expected data)
|
rlm@10
|
98 actual (:actual data)
|
rlm@10
|
99 passed (cond
|
rlm@10
|
100 (= event :fail) (= msg "Should fail")
|
rlm@10
|
101 (= event :pass) (= msg "Should pass")
|
rlm@10
|
102 (= event :error) (= msg "Should error")
|
rlm@10
|
103 :else true)]
|
rlm@10
|
104 (if passed
|
rlm@10
|
105 (original-report {:type :pass, :message msg,
|
rlm@10
|
106 :expected expected, :actual actual})
|
rlm@10
|
107 (original-report {:type :fail, :message (str msg " but got " event)
|
rlm@10
|
108 :expected expected, :actual actual}))))
|
rlm@10
|
109
|
rlm@10
|
110 ;; test-ns-hook will be used by test/test-ns to run tests in this
|
rlm@10
|
111 ;; namespace.
|
rlm@10
|
112 (defn test-ns-hook []
|
rlm@10
|
113 (binding [original-report report
|
rlm@10
|
114 report custom-report]
|
rlm@10
|
115 (test-all-vars (find-ns 'clojure.test-clojure.test))))
|