PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/test/clojure/test_clojure/test.clj

https://gitlab.com/hcaty/clojure
Clojure | 123 lines | 74 code | 26 blank | 23 comment | 11 complexity | 401bfd05468c49c380b55bf4d8825f6e MD5 | raw file
  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. ;;; test_clojure/test.clj: unit tests for test.clj
  9. ;; by Stuart Sierra
  10. ;; January 16, 2009
  11. ;; Thanks to Chas Emerick, Allen Rohner, and Stuart Halloway for
  12. ;; contributions and suggestions.
  13. (ns clojure.test-clojure.test
  14. (:use clojure.test)
  15. (:require [clojure.stacktrace :as stack]))
  16. (deftest can-test-symbol
  17. (let [x true]
  18. (is x "Should pass"))
  19. (let [x false]
  20. (is x "Should fail")))
  21. (deftest can-test-boolean
  22. (is true "Should pass")
  23. (is false "Should fail"))
  24. (deftest can-test-nil
  25. (is nil "Should fail"))
  26. (deftest can-test-=
  27. (is (= 2 (+ 1 1)) "Should pass")
  28. (is (= 3 (+ 2 2)) "Should fail"))
  29. (deftest can-test-instance
  30. (is (instance? Long (+ 2 2)) "Should pass")
  31. (is (instance? Float (+ 1 1)) "Should fail"))
  32. (deftest can-test-thrown
  33. (is (thrown? ArithmeticException (/ 1 0)) "Should pass")
  34. ;; No exception is thrown:
  35. (is (thrown? Exception (+ 1 1)) "Should fail")
  36. ;; Wrong class of exception is thrown:
  37. (is (thrown? ArithmeticException (throw (RuntimeException.))) "Should error"))
  38. (deftest can-test-thrown-with-msg
  39. (is (thrown-with-msg? ArithmeticException #"Divide by zero" (/ 1 0)) "Should pass")
  40. ;; Wrong message string:
  41. (is (thrown-with-msg? ArithmeticException #"Something else" (/ 1 0)) "Should fail")
  42. ;; No exception is thrown:
  43. (is (thrown? Exception (+ 1 1)) "Should fail")
  44. ;; Wrong class of exception is thrown:
  45. (is (thrown-with-msg? IllegalArgumentException #"Divide by zero" (/ 1 0)) "Should error"))
  46. (deftest can-catch-unexpected-exceptions
  47. (is (= 1 (throw (Exception.))) "Should error"))
  48. (deftest can-test-method-call
  49. (is (.startsWith "abc" "a") "Should pass")
  50. (is (.startsWith "abc" "d") "Should fail"))
  51. (deftest can-test-anonymous-fn
  52. (is (#(.startsWith % "a") "abc") "Should pass")
  53. (is (#(.startsWith % "d") "abc") "Should fail"))
  54. (deftest can-test-regexps
  55. (is (re-matches #"^ab.*$" "abbabba") "Should pass")
  56. (is (re-matches #"^cd.*$" "abbabba") "Should fail")
  57. (is (re-find #"ab" "abbabba") "Should pass")
  58. (is (re-find #"cd" "abbabba") "Should fail"))
  59. (deftest clj-1102-empty-stack-trace-should-not-throw-exceptions
  60. (let [empty-stack (into-array (Class/forName "java.lang.StackTraceElement")
  61. [])
  62. t (doto (Exception.) (.setStackTrace empty-stack))]
  63. (is (map? (#'clojure.test/file-and-line t 0)) "Should pass")
  64. (is (string? (with-out-str (stack/print-stack-trace t))) "Should pass")))
  65. (deftest #^{:has-meta true} can-add-metadata-to-tests
  66. (is (:has-meta (meta #'can-add-metadata-to-tests)) "Should pass"))
  67. ;; still have to declare the symbol before testing unbound symbols
  68. (declare does-not-exist)
  69. #_(deftest can-test-unbound-symbol
  70. (is (= nil does-not-exist) "Should error"))
  71. #_(deftest can-test-unbound-function
  72. (is (does-not-exist) "Should error"))
  73. ;; Here, we create an alternate version of test/report, that
  74. ;; compares the event with the message, then calls the original
  75. ;; 'report' with modified arguments.
  76. (declare ^:dynamic original-report)
  77. (defn custom-report [data]
  78. (let [event (:type data)
  79. msg (:message data)
  80. expected (:expected data)
  81. actual (:actual data)
  82. passed (cond
  83. (= event :fail) (= msg "Should fail")
  84. (= event :pass) (= msg "Should pass")
  85. (= event :error) (= msg "Should error")
  86. :else true)]
  87. (if passed
  88. (original-report {:type :pass, :message msg,
  89. :expected expected, :actual actual})
  90. (original-report {:type :fail, :message (str msg " but got " event)
  91. :expected expected, :actual actual}))))
  92. ;; test-ns-hook will be used by test/test-ns to run tests in this
  93. ;; namespace.
  94. (defn test-ns-hook []
  95. (binding [original-report report
  96. report custom-report]
  97. (test-all-vars (find-ns 'clojure.test-clojure.test))))