diff options
-rw-r--r-- | build.xml | 4 | ||||
-rw-r--r-- | src/clojure/contrib/mock.clj (renamed from src/clojure/contrib/expect.clj) | 10 | ||||
-rw-r--r-- | src/clojure/contrib/mock/test_adapter.clj (renamed from src/clojure/contrib/expect/test_adapter.clj) | 18 | ||||
-rw-r--r-- | src/clojure/contrib/test_contrib.clj | 2 | ||||
-rw-r--r-- | src/clojure/contrib/test_contrib/expect_test.clj | 131 | ||||
-rw-r--r-- | src/clojure/contrib/test_contrib/mock_test.clj | 131 | ||||
-rw-r--r-- | src/clojure/contrib/test_contrib/mock_test/test_adapter_test.clj (renamed from src/clojure/contrib/test_contrib/expect_test/test_adapter_test.clj) | 10 |
7 files changed, 154 insertions, 152 deletions
@@ -122,8 +122,8 @@ <arg value="clojure.contrib.duck-streams"/> <arg value="clojure.contrib.error-kit"/> <arg value="clojure.contrib.except"/> - <arg value="clojure.contrib.expect"/> - <arg value="clojure.contrib.expect.test-adapter"/> + <arg value="clojure.contrib.mock"/> + <arg value="clojure.contrib.mock.test-adapter"/> <arg value="clojure.contrib.fcase"/> <arg value="clojure.contrib.find-namespaces"/> <arg value="clojure.contrib.fnmap"/> diff --git a/src/clojure/contrib/expect.clj b/src/clojure/contrib/mock.clj index 1d78e939..e079719d 100644 --- a/src/clojure/contrib/expect.clj +++ b/src/clojure/contrib/mock.clj @@ -1,4 +1,4 @@ -;;; clojure.contrib.expect.clj: mocking/expectation framework for Clojure
+;;; clojure.contrib.mock.clj: mocking/expectation framework for Clojure
;; by Matt Clark
@@ -50,7 +50,9 @@ ) ;; end comment
-(ns clojure.contrib.expect
+(ns clojure.contrib.mock
+ #^{:author "Matt Clark",
+ :doc "function mocking/expectations for Clojure" }
(:use [clojure.contrib.seq-utils :only (positions)]
[clojure.contrib.def :only (defmacro-)]))
@@ -269,8 +271,8 @@ Usage: "an even number of forms in expectation bindings")
(let [mock-data (gensym "mock-data_")]
`(let [~mock-data (map (fn [args#]
- (apply clojure.contrib.expect/make-mock args#))
+ (apply clojure.contrib.mock/make-mock args#))
~(cons 'list (map (fn [[n m]] (vector (list 'quote n) m))
(partition 2 expect-bindings))))]
(binding ~(make-bindings expect-bindings mock-data) ~@body)
- (clojure.contrib.expect/validate-counts ~mock-data) true)))
+ (clojure.contrib.mock/validate-counts ~mock-data) true)))
diff --git a/src/clojure/contrib/expect/test_adapter.clj b/src/clojure/contrib/mock/test_adapter.clj index 50e50125..b5aa4340 100644 --- a/src/clojure/contrib/expect/test_adapter.clj +++ b/src/clojure/contrib/mock/test_adapter.clj @@ -9,17 +9,17 @@ ;; agreeing to be bound by the terms of this license. You must not ;; remove this notice, or any other, from this software. -(ns clojure.contrib.expect.test-adapter - (:require [clojure.contrib.expect :as expect]) +(ns clojure.contrib.mock.test-adapter + (:require [clojure.contrib.mock :as mock]) (:use clojure.test clojure.contrib.ns-utils)) -(immigrate 'clojure.contrib.expect) +(immigrate 'clojure.contrib.mock) (defn report-problem "This function is designed to be used in a binding macro to override -the report-problem function in the test-expect namespace. Instead of printing -the error to the console, the error is logged via test-is." +the report-problem function in clojure.contrib.mock. Instead of printing +the error to the console, the error is logged via clojure.test." [fn-name expected actual msg] (report {:type :fail, :message (str msg " Function name: " fn-name), @@ -28,10 +28,10 @@ the error to the console, the error is logged via test-is." (defmacro expect [& body] - "Use this macro instead of the standard test-expect expect macro to have -failures reported through test-is." - `(binding [expect/report-problem report-problem] - (expect/expect ~@body))) + "Use this macro instead of the standard c.c.mock expect macro to have +failures reported through clojure.test." + `(binding [mock/report-problem report-problem] + (mock/expect ~@body))) diff --git a/src/clojure/contrib/test_contrib.clj b/src/clojure/contrib/test_contrib.clj index 8402b7ba..e9f58db7 100644 --- a/src/clojure/contrib/test_contrib.clj +++ b/src/clojure/contrib/test_contrib.clj @@ -20,7 +20,7 @@ [:complex-numbers :fnmap :macro-utils :monads :pprint.pretty :pprint.cl-format :str-utils :shell-out :test-graph :test-dataflow :test-java-utils :test-lazy-seqs - :test-trace :test-jmx :java-utils :expect-test :expect-test.test-adapter-test + :test-trace :test-jmx :java-utils :mock-test :mock-test.test-adapter-test :seq-utils-test]) (def test-namespaces diff --git a/src/clojure/contrib/test_contrib/expect_test.clj b/src/clojure/contrib/test_contrib/expect_test.clj deleted file mode 100644 index 52f37741..00000000 --- a/src/clojure/contrib/test_contrib/expect_test.clj +++ /dev/null @@ -1,131 +0,0 @@ -(ns clojure.contrib.test-contrib.expect-test - (:use clojure.test) - (:require [clojure.contrib.expect :as expect])) - -; Used as dummy dependency functions -(defn fn1 [x] :ignore) -(defn fn2 [x y] :ignore) -(defn fn3 ([x] :ignore) - ([x y z] :ignore)) -(defn fn4 [x y & r] :ignore) - -;functions created using fn directly lack the argslist meta data -(def deffed-differently (fn [x] :ignore)) - -(defmacro assert-called [fn-name called? & body] - `(let [called-status?# (atom false)] - (binding [~fn-name (fn [& args#] (reset! called-status?# true))] ~@body) - (is (= ~called? @called-status?#)))) - -(deftest test-convenience - (testing "once" - (is (false? (expect/once 0))) - (is (false? (expect/once 123))) - (is (true? (expect/once 1)))) - - (testing "never" - (is (false? (expect/never 4))) - (is (true? (expect/never 0)))) - - (testing "more-than" - (is (false? ((expect/more-than 5) 3))) - (is (true? ((expect/more-than 5) 9)))) - - (testing "less-than" - (is (true? ((expect/less-than 5) 3))) - (is (false? ((expect/less-than 5) 9)))) - - (testing "between" - (is (true? ((expect/between 5 8) 6))) - (is (false? ((expect/between 5 8) 5))))) - - -(deftest test-returns - (is (= {:returns 5} (expect/returns 5))) - (is (= {:other-key "test" :returns nil} (expect/returns nil {:other-key "test"})))) - - -(deftest test-has-args - (let [ex (:has-args (expect/has-args [1]))] - (is (fn? ex)) - (is (ex 'fn1 1)) - (is (ex 'fn1 1 5 6)) - (assert-called expect/unexpected-args true (ex 'fn1 5))) - (is (contains? (expect/has-args [] {:pre-existing-key "test"}) :pre-existing-key)) - (is (true? (((expect/has-args [5]) :has-args)'fn1 5)))) - - -(deftest test-has-matching-signature - (assert-called expect/no-matching-function-signature true - (expect/has-matching-signature? 'clojure.contrib.test-contrib.expect-test/fn2 [1])) - (assert-called expect/no-matching-function-signature true - (expect/has-matching-signature? 'clojure.contrib.test-contrib.expect-test/fn3 [1 3])) - (assert-called expect/no-matching-function-signature false - (expect/has-matching-signature? 'clojure.contrib.test-contrib.expect-test/fn3 [1 3 5])) - (assert-called expect/no-matching-function-signature false - (expect/has-matching-signature? 'clojure.contrib.test-contrib.expect-test/fn4 [1 3 5 7 9])) - (assert-called expect/no-matching-function-signature false - (expect/has-matching-signature? 'clojure.contrib.test-contrib.expect-test/fn4 [1 3])) - (assert-called expect/no-matching-function-signature true - (expect/has-matching-signature? 'clojure.contrib.test-contrib.expect-test/fn4 [1])) - (assert-called expect/no-matching-function-signature false - (expect/has-matching-signature? 'clojure.contrib.test-contrib.expect-test/deffed-differently [1]))) - - -(deftest test-times - (is (fn? ((expect/times #(= 1 %)) :times))) - (is (contains? (expect/times #(= 1 %) {:existing-key "test"}) :existing-key))) - -(deftest test-make-mock - (testing "invalid arguments" - (is (thrown? IllegalArgumentException (expect/make-mock [5])))) - - (testing "valid counter and unevaluated returns" - (let [[mock counter count-checker] (expect/make-mock 'fn1 (expect/returns 5 (expect/times 1)))] - (is (fn? mock)) - (is (= 0 @counter)) - (is (= 5 (mock :ignore-me))) - (is (= 1 @counter)))) - - (testing "returns as expected" - (let [[mock] (expect/make-mock 'fn1 (expect/returns 5))] - (is (= 5 (mock :ignore)))) - (let [[mock] (expect/make-mock 'fn1 (expect/returns #(* 2 %)))] - (is (= 10 ((mock :ignore) 5)) ":returns a function should not automatically - evaluate it."))) - - (testing "calls replacement-fn and returns the result" - (let [[mock] (expect/make-mock 'fn1 (expect/calls #(* 3 %)))] - (is (= 15 (mock 5)))) - (let [[mock] (expect/make-mock 'fn1 (expect/calls #(* 2 %) (expect/returns 3)))] - (is (= 10 (mock 5))))) - - (testing "argument validation" - (let [[mock] (expect/make-mock 'fn1 (expect/has-args [#(= 5 %)]))] - (assert-called expect/unexpected-args true (mock "test")) - (is (nil? (mock 5)))))) - - -(deftest test-make-count-checker - (let [checker (expect/make-count-checker 5 5)] - (assert-called expect/incorrect-invocation-count false (checker 'fn1 5)) - (assert-called expect/incorrect-invocation-count true (checker 'fn1 3)))) - - -(deftest test-validate-counts - (assert-called expect/incorrect-invocation-count false - (expect/validate-counts (list [(fn []) (atom 0) (expect/make-count-checker #(< % 6) '#(< % 6)) 'fn1]))) - (assert-called expect/incorrect-invocation-count true - (expect/validate-counts (list [(fn []) (atom 0) (expect/make-count-checker 4 4) 'fn1])))) - - -(deftest test-expect-macro - (let [under-test (fn [x] (fn1 x))] - (is (true? (expect/expect [fn1 (expect/times 1 (expect/has-args [#(= 3 %)]))] - (under-test 3)))) - (assert-called expect/unexpected-args true (expect/expect [fn1 (expect/times 1 (expect/has-args [#(= 4 %)]))] - (under-test 3)))) - (let [under-test (fn [] (fn2 (fn1 1) 3))] - (is (true? (expect/expect [fn1 (expect/times 1 (expect/has-args [#(= 1 %)] (expect/returns 2))) - fn2 (expect/times 1 (expect/has-args [#(= 2 %) #(= 3 %)] (expect/returns 5)))] - (under-test))))))
\ No newline at end of file diff --git a/src/clojure/contrib/test_contrib/mock_test.clj b/src/clojure/contrib/test_contrib/mock_test.clj new file mode 100644 index 00000000..d7fc9854 --- /dev/null +++ b/src/clojure/contrib/test_contrib/mock_test.clj @@ -0,0 +1,131 @@ +(ns clojure.contrib.test-contrib.mock-test + (:use clojure.test) + (:require [clojure.contrib.mock :as mock])) + +; Used as dummy dependency functions +(defn fn1 [x] :ignore) +(defn fn2 [x y] :ignore) +(defn fn3 ([x] :ignore) + ([x y z] :ignore)) +(defn fn4 [x y & r] :ignore) + +;functions created using fn directly lack the argslist meta data +(def deffed-differently (fn [x] :ignore)) + +(defmacro assert-called [fn-name called? & body] + `(let [called-status?# (atom false)] + (binding [~fn-name (fn [& args#] (reset! called-status?# true))] ~@body) + (is (= ~called? @called-status?#)))) + +(deftest test-convenience + (testing "once" + (is (false? (mock/once 0))) + (is (false? (mock/once 123))) + (is (true? (mock/once 1)))) + + (testing "never" + (is (false? (mock/never 4))) + (is (true? (mock/never 0)))) + + (testing "more-than" + (is (false? ((mock/more-than 5) 3))) + (is (true? ((mock/more-than 5) 9)))) + + (testing "less-than" + (is (true? ((mock/less-than 5) 3))) + (is (false? ((mock/less-than 5) 9)))) + + (testing "between" + (is (true? ((mock/between 5 8) 6))) + (is (false? ((mock/between 5 8) 5))))) + + +(deftest test-returns + (is (= {:returns 5} (mock/returns 5))) + (is (= {:other-key "test" :returns nil} (mock/returns nil {:other-key "test"})))) + + +(deftest test-has-args + (let [ex (:has-args (mock/has-args [1]))] + (is (fn? ex)) + (is (ex 'fn1 1)) + (is (ex 'fn1 1 5 6)) + (assert-called mock/unexpected-args true (ex 'fn1 5))) + (is (contains? (mock/has-args [] {:pre-existing-key "test"}) :pre-existing-key)) + (is (true? (((mock/has-args [5]) :has-args)'fn1 5)))) + + +(deftest test-has-matching-signature + (assert-called mock/no-matching-function-signature true + (mock/has-matching-signature? 'clojure.contrib.test-contrib.mock-test/fn2 [1])) + (assert-called mock/no-matching-function-signature true + (mock/has-matching-signature? 'clojure.contrib.test-contrib.mock-test/fn3 [1 3])) + (assert-called mock/no-matching-function-signature false + (mock/has-matching-signature? 'clojure.contrib.test-contrib.mock-test/fn3 [1 3 5])) + (assert-called mock/no-matching-function-signature false + (mock/has-matching-signature? 'clojure.contrib.test-contrib.mock-test/fn4 [1 3 5 7 9])) + (assert-called mock/no-matching-function-signature false + (mock/has-matching-signature? 'clojure.contrib.test-contrib.mock-test/fn4 [1 3])) + (assert-called mock/no-matching-function-signature true + (mock/has-matching-signature? 'clojure.contrib.test-contrib.mock-test/fn4 [1])) + (assert-called mock/no-matching-function-signature false + (mock/has-matching-signature? 'clojure.contrib.test-contrib.mock-test/deffed-differently [1]))) + + +(deftest test-times + (is (fn? ((mock/times #(= 1 %)) :times))) + (is (contains? (mock/times #(= 1 %) {:existing-key "test"}) :existing-key))) + +(deftest test-make-mock + (testing "invalid arguments" + (is (thrown? IllegalArgumentException (mock/make-mock [5])))) + + (testing "valid counter and unevaluated returns" + (let [[mock counter count-checker] (mock/make-mock 'fn1 (mock/returns 5 (mock/times 1)))] + (is (fn? mock)) + (is (= 0 @counter)) + (is (= 5 (mock :ignore-me))) + (is (= 1 @counter)))) + + (testing "returns as expected" + (let [[mock] (mock/make-mock 'fn1 (mock/returns 5))] + (is (= 5 (mock :ignore)))) + (let [[mock] (mock/make-mock 'fn1 (mock/returns #(* 2 %)))] + (is (= 10 ((mock :ignore) 5)) ":returns a function should not automatically + evaluate it."))) + + (testing "calls replacement-fn and returns the result" + (let [[mock] (mock/make-mock 'fn1 (mock/calls #(* 3 %)))] + (is (= 15 (mock 5)))) + (let [[mock] (mock/make-mock 'fn1 (mock/calls #(* 2 %) (mock/returns 3)))] + (is (= 10 (mock 5))))) + + (testing "argument validation" + (let [[mock] (mock/make-mock 'fn1 (mock/has-args [#(= 5 %)]))] + (assert-called mock/unexpected-args true (mock "test")) + (is (nil? (mock 5)))))) + + +(deftest test-make-count-checker + (let [checker (mock/make-count-checker 5 5)] + (assert-called mock/incorrect-invocation-count false (checker 'fn1 5)) + (assert-called mock/incorrect-invocation-count true (checker 'fn1 3)))) + + +(deftest test-validate-counts + (assert-called mock/incorrect-invocation-count false + (mock/validate-counts (list [(fn []) (atom 0) (mock/make-count-checker #(< % 6) '#(< % 6)) 'fn1]))) + (assert-called mock/incorrect-invocation-count true + (mock/validate-counts (list [(fn []) (atom 0) (mock/make-count-checker 4 4) 'fn1])))) + + +(deftest test-expect-macro + (let [under-test (fn [x] (fn1 x))] + (is (true? (mock/expect [fn1 (mock/times 1 (mock/has-args [#(= 3 %)]))] + (under-test 3)))) + (assert-called mock/unexpected-args true (mock/expect [fn1 (mock/times 1 (mock/has-args [#(= 4 %)]))] + (under-test 3)))) + (let [under-test (fn [] (fn2 (fn1 1) 3))] + (is (true? (mock/expect [fn1 (mock/times 1 (mock/has-args [#(= 1 %)] (mock/returns 2))) + fn2 (mock/times 1 (mock/has-args [#(= 2 %) #(= 3 %)] (mock/returns 5)))] + (under-test))))))
\ No newline at end of file diff --git a/src/clojure/contrib/test_contrib/expect_test/test_adapter_test.clj b/src/clojure/contrib/test_contrib/mock_test/test_adapter_test.clj index df74404e..5dc3a43d 100644 --- a/src/clojure/contrib/test_contrib/expect_test/test_adapter_test.clj +++ b/src/clojure/contrib/test_contrib/mock_test/test_adapter_test.clj @@ -1,18 +1,18 @@ -(ns clojure.contrib.test-contrib.expect-test.test-adapter-test - (:use clojure.contrib.expect.test-adapter - [clojure.contrib.test-contrib.expect-test :only (assert-called)] +(ns clojure.contrib.test-contrib.mock-test.test-adapter-test + (:use clojure.contrib.mock.test-adapter + [clojure.contrib.test-contrib.mock-test :only (assert-called)] clojure.test)) (deftest test-report-problem-called (def #^{:private true} fn1 (fn [x] "dummy code")) (def #^{:private true} fn2 (fn [x y] "dummy code2")) (let [under-test (fn [x] (fn1 x))] - (assert-called clojure.contrib.expect.test-adapter/report-problem + (assert-called clojure.contrib.mock.test-adapter/report-problem true (expect [fn1 (times 5)] (under-test "hi"))))) (deftest test-is-report-called (assert-called clojure.test/report true - (clojure.contrib.expect.test-adapter/report-problem + (clojure.contrib.mock.test-adapter/report-problem 'fn-name 5 6 "fake problem"))) |