Mercurial > lasercutter
comparison src/clojure/test_clojure/test.clj @ 10:ef7dbbd6452c
added clojure source goodness
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 21 Aug 2010 06:25:44 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
9:35cf337adfcf | 10:ef7dbbd6452c |
---|---|
1 ; Copyright (c) Rich Hickey. All rights reserved. | |
2 ; The use and distribution terms for this software are covered by the | |
3 ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) | |
4 ; which can be found in the file epl-v10.html at the root of this distribution. | |
5 ; By using this software in any fashion, you are agreeing to be bound by | |
6 ; the terms of this license. | |
7 ; You must not remove this notice, or any other, from this software. | |
8 | |
9 ;;; test_clojure/test.clj: unit tests for test.clj | |
10 | |
11 ;; by Stuart Sierra | |
12 ;; January 16, 2009 | |
13 | |
14 ;; Thanks to Chas Emerick, Allen Rohner, and Stuart Halloway for | |
15 ;; contributions and suggestions. | |
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 (deftest #^{:has-meta true} can-add-metadata-to-tests | |
76 (is (:has-meta (meta #'can-add-metadata-to-tests)) "Should pass")) | |
77 | |
78 ;; still have to declare the symbol before testing unbound symbols | |
79 (declare does-not-exist) | |
80 | |
81 (deftest can-test-unbound-symbol | |
82 (is (= nil does-not-exist) "Should error")) | |
83 | |
84 (deftest can-test-unbound-function | |
85 (is (does-not-exist) "Should error")) | |
86 | |
87 | |
88 ;; Here, we create an alternate version of test/report, that | |
89 ;; compares the event with the message, then calls the original | |
90 ;; 'report' with modified arguments. | |
91 | |
92 (declare original-report) | |
93 | |
94 (defn custom-report [data] | |
95 (let [event (:type data) | |
96 msg (:message data) | |
97 expected (:expected data) | |
98 actual (:actual data) | |
99 passed (cond | |
100 (= event :fail) (= msg "Should fail") | |
101 (= event :pass) (= msg "Should pass") | |
102 (= event :error) (= msg "Should error") | |
103 :else true)] | |
104 (if passed | |
105 (original-report {:type :pass, :message msg, | |
106 :expected expected, :actual actual}) | |
107 (original-report {:type :fail, :message (str msg " but got " event) | |
108 :expected expected, :actual actual})))) | |
109 | |
110 ;; test-ns-hook will be used by test/test-ns to run tests in this | |
111 ;; namespace. | |
112 (defn test-ns-hook [] | |
113 (binding [original-report report | |
114 report custom-report] | |
115 (test-all-vars (find-ns 'clojure.test-clojure.test)))) |