|
| 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)))) |
0 commit comments