Browse files

Minimal tests

  • Loading branch information...
1 parent cf5a82c commit d39f3d77a88a7d2d7b1def4cc5bd7a6feeac6e71 @fogus fogus committed Sep 13, 2012
View
11 project.clj
@@ -2,15 +2,14 @@
:description "Contracts programming for Clojure."
:dependencies [[org.clojure/clojure "1.5.0-master-SNAPSHOT"]
[org.clojure/core.unify "0.5.3"]]
- :dev-dependencies [[jline "0.9.94"]
- [swank-clojure "1.4.2"]
- [lein-marginalia "0.7.1"]
- [lein-multi "1.1.0"]]
+ :plugins [[lein-swank "1.4.4"]
+ [lein-marginalia "0.7.1"]
+ [lein-multi "1.1.0"]]
:multi-deps {:all [[org.clojure/core.unify "0.5.3"]]
"1.2" [[org.clojure/clojure "1.2.0"]]
"1.2.1" [[org.clojure/clojure "1.2.1"]]
"1.3" [[org.clojure/clojure "1.3.0"]]
"1.4" [[org.clojure/clojure "1.4.0"]]}
:repositories {"sonatype-oss-public" "https://oss.sonatype.org/content/groups/public/"}
- :source-path "src/main/clojure"
- :test-path "src/test/clojure")
+ :source-paths ["src/main/clojure"]
+ :test-paths ["src/test/clojure"])
View
2 src/main/clojure/clojure/core/contracts.clj
@@ -37,7 +37,7 @@
[& kontracts]
(let [fn-names (map first kontracts)
kontracts (for [[n ds & more] kontracts]
- (if (list? (first more))
+ (if (vector? (first more))
(list* `contract n ds more)
(first more)))]
`(do
View
2 src/main/clojure/data_readers.clj
@@ -2,4 +2,4 @@
;cc/contract clojure.core.contracts.impl.readers/read-constraints
;cc/fun clojure.core/identity
;cc/invariants clojure.core/identity
- }
+}
View
6 src/test/clojure/clojure/core/contracts_tests.clj
@@ -27,11 +27,5 @@
(is (thrown? AssertionError (bad-doubler 2)))
(is (thrown? AssertionError (bad-doubler 2 3)))))
-(deftest test-with-constraints
- (defer "with-constraints"))
-
-(deftest test-provide
- (defer "provide"))
-
(deftest test-regressions
(defer "regression testing"))
View
57 src/test/clojure/clojure/core/provide_tests.clj
@@ -0,0 +1,57 @@
+(ns clojure.core.provide-tests
+ (:require [clojure.core.contracts :as contracts :refer (contract)])
+ (:use [clojure.test :only (deftest is)]))
+
+;; # Inline contract syntax
+
+(defn sqr [n]
+ (* n n))
+
+(contracts/provide
+ (sqr "The constraining of sqr" [n]
+ [number? (not= 0 n)
+ =>
+ pos? number?]))
+
+(deftest test-provide
+ (is (= 25 (sqr 5)))
+ (is (= 25 (sqr -5)))
+ (is (thrown? AssertionError (sqr 0))))
+
+
+;; # Contract name use
+
+(defn sqr2 [n]
+ (* n n))
+
+(def sqr-c
+ (contract sqr-contract
+ "Defines the constraints on squaring."
+ [n] [number? (not= 0 n) => pos? number?]))
+
+(contracts/provide
+ (sqr2 "Apply the contract for squaring" sqr-c))
+
+(deftest apply-existing-contract-test
+ (is (= 25 (sqr2 5)))
+ (is (= 25 (sqr2 -5)))
+ (is (thrown? AssertionError (sqr2 0))))
+
+;; Multi-arity
+
+(defn times2
+ ([x] (* 2 x))
+ ([x y] (* y x 2)))
+
+(contracts/provide
+ (times2 "The constraining of times2"
+ [n] [number? => number? (== % (* 2 n))]
+ [x y] [(every? number? [x y]) => number? (== % (* x y 2))]))
+
+(deftest apply-contract-arity2-test
+ (is (= 10 (times2 5)))
+ (is (= -10 (times2 -5)))
+ (is (thrown? AssertionError (times2 :a)))
+ (is (= 20 (times2 2 5)))
+ (is (= -20 (times2 -5 2)))
+ (is (thrown? AssertionError (times2 5 :a))))
View
22 src/test/clojure/clojure/core/with_constraints_tests.clj
@@ -0,0 +1,22 @@
+(ns clojure.core.with-constraints-tests
+ (:use [clojure.core.contracts :only (contract with-constraints provide)]
+ [clojure.test :only [deftest is]]
+ clojure.core.contracts.impl.transformers))
+
+(defn sqr [n]
+ (* n n))
+
+(def sqr-contract
+ (contract sqr-constraints
+ "Defines the constraints for squaring"
+ [n] [number? (not= 0 n) => pos? number?]))
+
+(def constrained-sqr
+ (with-constraints
+ sqr
+ sqr-contract))
+
+(deftest test-with-constraints
+ (is (= 4 (constrained-sqr 2)))
+ (is (thrown? AssertionError (constrained-sqr 0))))
+

0 comments on commit d39f3d7

Please sign in to comment.