Skip to content

Commit

Permalink
Minimal tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fogus committed Sep 13, 2012
1 parent cf5a82c commit d39f3d7
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 14 deletions.
11 changes: 5 additions & 6 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
2 changes: 1 addition & 1 deletion src/main/clojure/clojure/core/contracts.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/main/clojure/data_readers.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
;cc/contract clojure.core.contracts.impl.readers/read-constraints
;cc/fun clojure.core/identity
;cc/invariants clojure.core/identity
}
}
6 changes: 0 additions & 6 deletions src/test/clojure/clojure/core/contracts_tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
57 changes: 57 additions & 0 deletions src/test/clojure/clojure/core/provide_tests.clj
Original file line number Diff line number Diff line change
@@ -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))))
22 changes: 22 additions & 0 deletions src/test/clojure/clojure/core/with_constraints_tests.clj
Original file line number Diff line number Diff line change
@@ -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.