diff 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
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/clojure/test_clojure/test.clj	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,115 @@
     1.4 +;   Copyright (c) Rich Hickey. All rights reserved.
     1.5 +;   The use and distribution terms for this software are covered by the
     1.6 +;   Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
     1.7 +;   which can be found in the file epl-v10.html at the root of this distribution.
     1.8 +;   By using this software in any fashion, you are agreeing to be bound by
     1.9 +;   the terms of this license.
    1.10 +;   You must not remove this notice, or any other, from this software.
    1.11 +
    1.12 +;;; test_clojure/test.clj: unit tests for test.clj
    1.13 +
    1.14 +;; by Stuart Sierra
    1.15 +;; January 16, 2009
    1.16 +
    1.17 +;; Thanks to Chas Emerick, Allen Rohner, and Stuart Halloway for
    1.18 +;; contributions and suggestions.
    1.19 +
    1.20 +
    1.21 +(ns clojure.test-clojure.test
    1.22 +  (:use clojure.test))
    1.23 +
    1.24 +(deftest can-test-symbol
    1.25 +  (let [x true]
    1.26 +    (is x "Should pass"))
    1.27 +  (let [x false]
    1.28 +    (is x "Should fail")))
    1.29 +
    1.30 +(deftest can-test-boolean
    1.31 +  (is true "Should pass")
    1.32 +  (is false "Should fail"))
    1.33 +
    1.34 +(deftest can-test-nil
    1.35 +  (is nil "Should fail"))
    1.36 +
    1.37 +(deftest can-test-=
    1.38 +  (is (= 2 (+ 1 1)) "Should pass")
    1.39 +  (is (= 3 (+ 2 2)) "Should fail"))
    1.40 +
    1.41 +(deftest can-test-instance
    1.42 +  (is (instance? Integer (+ 2 2)) "Should pass")
    1.43 +  (is (instance? Float (+ 1 1)) "Should fail"))
    1.44 +
    1.45 +(deftest can-test-thrown
    1.46 +  (is (thrown? ArithmeticException (/ 1 0)) "Should pass")
    1.47 +  ;; No exception is thrown:
    1.48 +  (is (thrown? Exception (+ 1 1)) "Should fail")
    1.49 +  ;; Wrong class of exception is thrown:
    1.50 +  (is (thrown? ArithmeticException (throw (RuntimeException.))) "Should error"))
    1.51 +
    1.52 +(deftest can-test-thrown-with-msg
    1.53 +  (is (thrown-with-msg? ArithmeticException #"Divide by zero" (/ 1 0)) "Should pass")
    1.54 +  ;; Wrong message string:
    1.55 +  (is (thrown-with-msg? ArithmeticException #"Something else" (/ 1 0)) "Should fail")
    1.56 +  ;; No exception is thrown:
    1.57 +  (is (thrown? Exception (+ 1 1)) "Should fail")
    1.58 +  ;; Wrong class of exception is thrown:
    1.59 +  (is (thrown-with-msg? IllegalArgumentException #"Divide by zero" (/ 1 0)) "Should error"))
    1.60 +
    1.61 +(deftest can-catch-unexpected-exceptions
    1.62 +  (is (= 1 (throw (Exception.))) "Should error"))
    1.63 +
    1.64 +(deftest can-test-method-call
    1.65 +  (is (.startsWith "abc" "a") "Should pass")
    1.66 +  (is (.startsWith "abc" "d") "Should fail"))
    1.67 +
    1.68 +(deftest can-test-anonymous-fn
    1.69 +  (is (#(.startsWith % "a") "abc") "Should pass")
    1.70 +  (is (#(.startsWith % "d") "abc") "Should fail"))
    1.71 +
    1.72 +(deftest can-test-regexps
    1.73 +  (is (re-matches #"^ab.*$" "abbabba") "Should pass")
    1.74 +  (is (re-matches #"^cd.*$" "abbabba") "Should fail")
    1.75 +  (is (re-find #"ab" "abbabba") "Should pass")
    1.76 +  (is (re-find #"cd" "abbabba") "Should fail"))
    1.77 +
    1.78 +(deftest #^{:has-meta true} can-add-metadata-to-tests
    1.79 +  (is (:has-meta (meta #'can-add-metadata-to-tests)) "Should pass"))
    1.80 +
    1.81 +;; still have to declare the symbol before testing unbound symbols
    1.82 +(declare does-not-exist)
    1.83 +
    1.84 +(deftest can-test-unbound-symbol
    1.85 +  (is (= nil does-not-exist) "Should error"))
    1.86 +
    1.87 +(deftest can-test-unbound-function
    1.88 +  (is (does-not-exist) "Should error"))
    1.89 +
    1.90 +
    1.91 +;; Here, we create an alternate version of test/report, that
    1.92 +;; compares the event with the message, then calls the original
    1.93 +;; 'report' with modified arguments.
    1.94 +
    1.95 +(declare original-report)
    1.96 +
    1.97 +(defn custom-report [data]
    1.98 +  (let [event (:type data)
    1.99 +        msg (:message data)
   1.100 +        expected (:expected data)
   1.101 +        actual (:actual data)
   1.102 +        passed (cond
   1.103 +                 (= event :fail) (= msg "Should fail")
   1.104 +                 (= event :pass) (= msg "Should pass")
   1.105 +                 (= event :error) (= msg "Should error")
   1.106 +                 :else true)]
   1.107 +    (if passed
   1.108 +      (original-report {:type :pass, :message msg,
   1.109 +                        :expected expected, :actual actual})
   1.110 +      (original-report {:type :fail, :message (str msg " but got " event)
   1.111 +                        :expected expected, :actual actual}))))
   1.112 +
   1.113 +;; test-ns-hook will be used by test/test-ns to run tests in this
   1.114 +;; namespace.
   1.115 +(defn test-ns-hook []
   1.116 +  (binding [original-report report
   1.117 +            report custom-report]
   1.118 +    (test-all-vars (find-ns 'clojure.test-clojure.test))))