PageRenderTime 27ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/test/fogus/me/invariant_tests.clj

https://github.com/goodmike/trammel
Clojure | 55 lines | 35 code | 9 blank | 11 comment | 18 complexity | eb4e1e7e90b06dc6f120cc32295531f3 MD5 | raw file
Possible License(s): EPL-1.0
  1. ;;; invariant_tests.clj -- Contracts programming library for Clojure
  2. ;; by Michael Fogus - http://fogus.me/fun/
  3. ;; Sept 16, 2010
  4. ;; Copyright (c) Michael Fogus, 2010. All rights reserved. The use
  5. ;; and distribution terms for this software are covered by the Eclipse
  6. ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
  7. ;; which can be found in the file COPYING the root of this
  8. ;; distribution. By using this software in any fashion, you are
  9. ;; agreeing to be bound by the terms of this license. You must not
  10. ;; remove this notice, or any other, from this software.
  11. (ns fogus.me.invariant-tests
  12. (:use [trammel.core :only [defconstrainedrecord]])
  13. (:use [clojure.test :only [deftest is]]))
  14. (defconstrainedrecord Foo [a 1 b 2]
  15. [(every? number? [a b])]
  16. Object
  17. (toString [this] (str "record Foo has " a " and " b)))
  18. (deftest test-constrained-record-with-vector-spec
  19. (is (= (:a (new-Foo)) 1))
  20. (is (= (:b (new-Foo)) 2))
  21. (is (= (:a (new-Foo :a 42)) 42))
  22. (is (= (:b (new-Foo :b 108)) 108))
  23. (is (= (:a (new-Foo :a 42 :b 108)) 42))
  24. (is (= (:b (new-Foo :a 42 :b 108)) 108))
  25. (is (= (:a (new-Foo :a 42 :b 108 :c 36)) 42))
  26. (is (= (:b (new-Foo :a 42 :b 108 :c 36)) 108))
  27. (is (= (:c (new-Foo :a 42 :b 108 :c 36)) 36))
  28. (is (thrown? Error (new-Foo :a :b)))
  29. (is (thrown? Error (new-Foo :a 42 :b nil))))
  30. ;; testing default clojure pre/post maps
  31. (defconstrainedrecord Bar [a 1 b 2]
  32. {:pre [(every? number? [a b])]}
  33. Object
  34. (toString [this] (str "record Bar has " a " and " b)))
  35. (deftest test-constrained-record-with-map-spec
  36. (is (= (:a (new-Bar)) 1))
  37. (is (= (:b (new-Bar)) 2))
  38. (is (= (:a (new-Bar :a 42)) 42))
  39. (is (= (:b (new-Bar :b 108)) 108))
  40. (is (= (:a (new-Bar :a 42 :b 108)) 42))
  41. (is (= (:b (new-Bar :a 42 :b 108)) 108))
  42. (is (= (:a (new-Bar :a 42 :b 108 :c 36)) 42))
  43. (is (= (:b (new-Bar :a 42 :b 108 :c 36)) 108))
  44. (is (= (:c (new-Bar :a 42 :b 108 :c 36)) 36))
  45. (is (thrown? Error (new-Bar :a :b)))
  46. (is (thrown? Error (new-Bar :a 42 :b nil))))