Skip to content

Commit 6ee62ec

Browse files
gtic: brought clojure.test's self-tests over from contrib
1 parent a12092c commit 6ee62ec

File tree

3 files changed

+157
-0
lines changed

3 files changed

+157
-0
lines changed

test/clojure/test_clojure.clj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
:atoms
4242
:parallel
4343
:java-interop
44+
:test
45+
:test-fixtures
4446
;; libraries
4547
:clojure-set
4648
:clojure-xml

test/clojure/test_clojure/test.clj

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

0 commit comments

Comments
 (0)