rlm@10: ; Copyright (c) Rich Hickey. All rights reserved. rlm@10: ; The use and distribution terms for this software are covered by the rlm@10: ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) rlm@10: ; which can be found in the file epl-v10.html at the root of this distribution. rlm@10: ; By using this software in any fashion, you are agreeing to be bound by rlm@10: ; the terms of this license. rlm@10: ; You must not remove this notice, or any other, from this software. rlm@10: rlm@10: ;;; test_clojure/test.clj: unit tests for test.clj rlm@10: rlm@10: ;; by Stuart Sierra rlm@10: ;; January 16, 2009 rlm@10: rlm@10: ;; Thanks to Chas Emerick, Allen Rohner, and Stuart Halloway for rlm@10: ;; contributions and suggestions. rlm@10: rlm@10: rlm@10: (ns clojure.test-clojure.test rlm@10: (:use clojure.test)) rlm@10: rlm@10: (deftest can-test-symbol rlm@10: (let [x true] rlm@10: (is x "Should pass")) rlm@10: (let [x false] rlm@10: (is x "Should fail"))) rlm@10: rlm@10: (deftest can-test-boolean rlm@10: (is true "Should pass") rlm@10: (is false "Should fail")) rlm@10: rlm@10: (deftest can-test-nil rlm@10: (is nil "Should fail")) rlm@10: rlm@10: (deftest can-test-= rlm@10: (is (= 2 (+ 1 1)) "Should pass") rlm@10: (is (= 3 (+ 2 2)) "Should fail")) rlm@10: rlm@10: (deftest can-test-instance rlm@10: (is (instance? Integer (+ 2 2)) "Should pass") rlm@10: (is (instance? Float (+ 1 1)) "Should fail")) rlm@10: rlm@10: (deftest can-test-thrown rlm@10: (is (thrown? ArithmeticException (/ 1 0)) "Should pass") rlm@10: ;; No exception is thrown: rlm@10: (is (thrown? Exception (+ 1 1)) "Should fail") rlm@10: ;; Wrong class of exception is thrown: rlm@10: (is (thrown? ArithmeticException (throw (RuntimeException.))) "Should error")) rlm@10: rlm@10: (deftest can-test-thrown-with-msg rlm@10: (is (thrown-with-msg? ArithmeticException #"Divide by zero" (/ 1 0)) "Should pass") rlm@10: ;; Wrong message string: rlm@10: (is (thrown-with-msg? ArithmeticException #"Something else" (/ 1 0)) "Should fail") rlm@10: ;; No exception is thrown: rlm@10: (is (thrown? Exception (+ 1 1)) "Should fail") rlm@10: ;; Wrong class of exception is thrown: rlm@10: (is (thrown-with-msg? IllegalArgumentException #"Divide by zero" (/ 1 0)) "Should error")) rlm@10: rlm@10: (deftest can-catch-unexpected-exceptions rlm@10: (is (= 1 (throw (Exception.))) "Should error")) rlm@10: rlm@10: (deftest can-test-method-call rlm@10: (is (.startsWith "abc" "a") "Should pass") rlm@10: (is (.startsWith "abc" "d") "Should fail")) rlm@10: rlm@10: (deftest can-test-anonymous-fn rlm@10: (is (#(.startsWith % "a") "abc") "Should pass") rlm@10: (is (#(.startsWith % "d") "abc") "Should fail")) rlm@10: rlm@10: (deftest can-test-regexps rlm@10: (is (re-matches #"^ab.*$" "abbabba") "Should pass") rlm@10: (is (re-matches #"^cd.*$" "abbabba") "Should fail") rlm@10: (is (re-find #"ab" "abbabba") "Should pass") rlm@10: (is (re-find #"cd" "abbabba") "Should fail")) rlm@10: rlm@10: (deftest #^{:has-meta true} can-add-metadata-to-tests rlm@10: (is (:has-meta (meta #'can-add-metadata-to-tests)) "Should pass")) rlm@10: rlm@10: ;; still have to declare the symbol before testing unbound symbols rlm@10: (declare does-not-exist) rlm@10: rlm@10: (deftest can-test-unbound-symbol rlm@10: (is (= nil does-not-exist) "Should error")) rlm@10: rlm@10: (deftest can-test-unbound-function rlm@10: (is (does-not-exist) "Should error")) rlm@10: rlm@10: rlm@10: ;; Here, we create an alternate version of test/report, that rlm@10: ;; compares the event with the message, then calls the original rlm@10: ;; 'report' with modified arguments. rlm@10: rlm@10: (declare original-report) rlm@10: rlm@10: (defn custom-report [data] rlm@10: (let [event (:type data) rlm@10: msg (:message data) rlm@10: expected (:expected data) rlm@10: actual (:actual data) rlm@10: passed (cond rlm@10: (= event :fail) (= msg "Should fail") rlm@10: (= event :pass) (= msg "Should pass") rlm@10: (= event :error) (= msg "Should error") rlm@10: :else true)] rlm@10: (if passed rlm@10: (original-report {:type :pass, :message msg, rlm@10: :expected expected, :actual actual}) rlm@10: (original-report {:type :fail, :message (str msg " but got " event) rlm@10: :expected expected, :actual actual})))) rlm@10: rlm@10: ;; test-ns-hook will be used by test/test-ns to run tests in this rlm@10: ;; namespace. rlm@10: (defn test-ns-hook [] rlm@10: (binding [original-report report rlm@10: report custom-report] rlm@10: (test-all-vars (find-ns 'clojure.test-clojure.test))))