-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Fogus
committed
Jun 4, 2010
1 parent
caabe97
commit be80534
Showing
2 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
;;; defcnstrfn-tests.clj -- Contracts programming library for Clojure | ||
|
||
;; by Michael Fogus - http://fogus.me/fun/ | ||
;; June 04, 2010 | ||
|
||
;; Copyright (c) Michael Fogus, 2010. All rights reserved. The use | ||
;; and distribution terms for this software are covered by the Eclipse | ||
;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) | ||
;; which can be found in the file COPYING the root of this | ||
;; distribution. By using this software in any fashion, you are | ||
;; agreeing to be bound by the terms of this license. You must not | ||
;; remove this notice, or any other, from this software. | ||
|
||
(ns fogus.me.defcnstrfn-tests | ||
(:use [fogus.me.trammel :only [defconstrainedfn]]) | ||
(:use [clojure.test :only [deftest is]])) | ||
|
||
(defconstrainedfn positive-nums | ||
"test" | ||
([x] | ||
:requires | ||
(number? x) | ||
(pos? x) | ||
|
||
:ensures | ||
(float? %) | ||
|
||
:body | ||
(float x)) | ||
([x y] | ||
:requires | ||
(every? number? [x y]) | ||
(every? pos? [x y]) | ||
|
||
:ensures | ||
(every? float? %) | ||
|
||
:body | ||
(str "this is a noop, meant to ensure that multi-expr bodies work") | ||
[(float x) (float y)])) | ||
|
||
(deftest multibody-cnstr-fn-test | ||
(is (= 1.0 (positive-nums 1))) | ||
(is (= [1.0 2.0] (positive-nums 1 2))) | ||
(is (= 42 | ||
(try (positive-nums -1) | ||
(catch Error e 42)))) | ||
(is (= 42 | ||
(try (positive-nums -1 2) | ||
(catch Error e 42)))) | ||
(is (= 42 | ||
(try (positive-nums 2 -1) | ||
(catch Error e 42)))) | ||
(is (= 42 | ||
(try (positive-nums -1 -2) | ||
(catch Error e 42))))) | ||
|
||
|
||
(defconstrainedfn sqr | ||
"Calculates the square of a number." | ||
([n] | ||
:requires | ||
(number? n) | ||
(not (zero? n)) | ||
|
||
:ensures | ||
(pos? %) | ||
|
||
:body | ||
(* n n))) | ||
|
||
(deftest cnstr-sqr-test | ||
(is (= 36 (sqr 6))) | ||
(is (= 36 (sqr -6))) | ||
(is (= 42 | ||
(try (sqr 0) | ||
(catch Error e 42)))) | ||
(is (= 42 | ||
(try (sqr :a) | ||
(catch Error e 42))))) | ||
|
||
(deftest defconstrainedfn-test | ||
(multibody-cnstr-fn-test) | ||
(cnstr-sqr-test)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
;;; defcontract_tests.clj -- Contracts programming library for Clojure | ||
|
||
;; by Michael Fogus - http://fogus.me/fun/ | ||
;; May 26, 2010 | ||
|
||
;; Copyright (c) Michael Fogus, 2010. All rights reserved. The use | ||
;; and distribution terms for this software are covered by the Eclipse | ||
;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) | ||
;; which can be found in the file COPYING the root of this | ||
;; distribution. By using this software in any fashion, you are | ||
;; agreeing to be bound by the terms of this license. You must not | ||
;; remove this notice, or any other, from this software. | ||
|
||
(ns fogus.me.kontract-tests | ||
(:use [fogus.me.trammel :only [contract kontract]]) | ||
(:use [clojure.test :only [deftest is]])) | ||
|
||
(def doubler-kontract | ||
(kontract doubler | ||
[x] | ||
(requires | ||
(pos? x)) | ||
(ensures | ||
(= (* 2 x) %)) | ||
[x y] | ||
(requires | ||
(pos? x) | ||
(pos? y)) | ||
(ensures | ||
(= (* 2 (+ x y)) %)))) | ||
|
||
(deftest doubler-kontract-test | ||
(is (= 10 ((partial doubler-kontract #(* 2 (+ %1 %2))) 2 3))) | ||
(is (= 10 ((partial doubler-kontract #(+ %1 %1 %2 %2)) 2 3))) | ||
(is (= 10 ((partial doubler-kontract #(* 2 %)) 5))) | ||
(is (= 42 | ||
(try ((partial doubler-kontract #(* 3 (+ %1 %2))) 2 3) | ||
(catch Error e 42))))) | ||
|
||
(deftest kontract-test | ||
(doubler-kontract-test)) | ||
|