diff src/clojure/contrib/test_contrib/test_mock.clj @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/clojure/contrib/test_contrib/test_mock.clj	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,131 @@
     1.4 +(ns clojure.contrib.test-mock
     1.5 + (:use clojure.test)
     1.6 + (:require [clojure.contrib.mock :as mock]))
     1.7 +
     1.8 +; Used as dummy dependency functions
     1.9 +(defn fn1 {:dynamic true} [x] :ignore)
    1.10 +(defn fn2 {:dynamic true} [x y] :ignore)
    1.11 +(defn fn3 {:dynamic true} ([x] :ignore)
    1.12 +  ([x y z] :ignore))
    1.13 +(defn fn4 {:dynamic true} [x y & r] :ignore)
    1.14 +
    1.15 +;functions created using fn directly lack the argslist meta data
    1.16 +(def #^{:dynamic true} deffed-differently (fn [x] :ignore))
    1.17 +
    1.18 +(defmacro assert-called [fn-name called? & body]
    1.19 +  `(let [called-status?# (atom false)]
    1.20 +     (binding [~fn-name (fn [& args#] (reset! called-status?# true))] ~@body)
    1.21 +     (is (= ~called? @called-status?#))))
    1.22 +
    1.23 +(deftest test-convenience
    1.24 +  (testing "once"
    1.25 +    (is (false? (mock/once 0)))
    1.26 +    (is (false? (mock/once 123)))
    1.27 +    (is (true? (mock/once 1))))
    1.28 +
    1.29 +  (testing "never"
    1.30 +    (is (false? (mock/never 4)))
    1.31 +    (is (true? (mock/never 0))))
    1.32 +
    1.33 +  (testing "more-than"
    1.34 +    (is (false? ((mock/more-than 5) 3)))
    1.35 +    (is (true? ((mock/more-than 5) 9))))
    1.36 +
    1.37 +  (testing "less-than"
    1.38 +    (is (true? ((mock/less-than 5) 3)))
    1.39 +    (is (false? ((mock/less-than 5) 9))))
    1.40 +
    1.41 +  (testing "between"
    1.42 +    (is (true? ((mock/between 5 8) 6)))
    1.43 +    (is (false? ((mock/between 5 8) 5)))))
    1.44 +
    1.45 +
    1.46 +(deftest test-returns
    1.47 +  (is (= {:returns 5} (mock/returns 5)))
    1.48 +  (is (= {:other-key "test" :returns nil} (mock/returns nil {:other-key "test"}))))
    1.49 +
    1.50 +
    1.51 +(deftest test-has-args
    1.52 +  (let [ex (:has-args (mock/has-args [1]))]
    1.53 +    (is (fn? ex))
    1.54 +    (is (ex 'fn1 1))
    1.55 +    (is (ex 'fn1 1 5 6))
    1.56 +    (assert-called mock/unexpected-args true (ex 'fn1 5)))
    1.57 +  (is (contains? (mock/has-args [] {:pre-existing-key "test"}) :pre-existing-key))
    1.58 +  (is (true? (((mock/has-args [5]) :has-args)'fn1 5))))
    1.59 +
    1.60 +
    1.61 +(deftest test-has-matching-signature
    1.62 +  (assert-called mock/no-matching-function-signature true
    1.63 +    (mock/has-matching-signature? 'clojure.contrib.test-mock/fn2 [1]))
    1.64 +  (assert-called mock/no-matching-function-signature true
    1.65 +    (mock/has-matching-signature? 'clojure.contrib.test-mock/fn3 [1 3]))
    1.66 +  (assert-called mock/no-matching-function-signature false
    1.67 +    (mock/has-matching-signature? 'clojure.contrib.test-mock/fn3 [1 3 5]))
    1.68 +  (assert-called mock/no-matching-function-signature false
    1.69 +    (mock/has-matching-signature? 'clojure.contrib.test-mock/fn4 [1 3 5 7 9]))
    1.70 +  (assert-called mock/no-matching-function-signature false
    1.71 +    (mock/has-matching-signature? 'clojure.contrib.test-mock/fn4 [1 3]))
    1.72 +  (assert-called mock/no-matching-function-signature true
    1.73 +    (mock/has-matching-signature? 'clojure.contrib.test-mock/fn4 [1]))
    1.74 +  (assert-called mock/no-matching-function-signature false
    1.75 +    (mock/has-matching-signature? 'clojure.contrib.test-mock/deffed-differently [1])))
    1.76 +
    1.77 +
    1.78 +(deftest test-times
    1.79 +  (is (fn? ((mock/times #(= 1 %)) :times)))
    1.80 +  (is (contains? (mock/times #(= 1 %) {:existing-key "test"}) :existing-key)))
    1.81 +
    1.82 +(deftest test-make-mock
    1.83 +  (testing "invalid arguments"
    1.84 +    (is (thrown? IllegalArgumentException (mock/make-mock [5]))))
    1.85 +
    1.86 +  (testing "valid counter and unevaluated returns"
    1.87 +    (let [[mock counter count-checker] (mock/make-mock 'fn1 (mock/returns 5 (mock/times 1)))]
    1.88 +      (is (fn? mock))
    1.89 +      (is (= 0 @counter))
    1.90 +      (is (= 5 (mock :ignore-me)))
    1.91 +      (is (= 1 @counter))))
    1.92 +
    1.93 +  (testing "returns as expected"
    1.94 +    (let [[mock] (mock/make-mock 'fn1 (mock/returns 5))]
    1.95 +      (is (= 5 (mock :ignore))))
    1.96 +    (let [[mock] (mock/make-mock 'fn1 (mock/returns #(* 2 %)))]
    1.97 +      (is (= 10 ((mock :ignore) 5)) ":returns a function should not automatically
    1.98 +                                     evaluate it.")))
    1.99 +
   1.100 +  (testing "calls replacement-fn and returns the result"
   1.101 +    (let [[mock] (mock/make-mock 'fn1 (mock/calls #(* 3 %)))]
   1.102 +      (is (= 15 (mock 5))))
   1.103 +    (let [[mock] (mock/make-mock 'fn1 (mock/calls #(* 2 %) (mock/returns 3)))]
   1.104 +      (is (= 10 (mock 5)))))
   1.105 +
   1.106 +  (testing "argument validation"
   1.107 +    (let [[mock] (mock/make-mock 'fn1 (mock/has-args [#(= 5 %)]))]
   1.108 +      (assert-called mock/unexpected-args true (mock "test"))
   1.109 +      (is (nil? (mock 5))))))
   1.110 +
   1.111 +
   1.112 +(deftest test-make-count-checker
   1.113 +  (let [checker (mock/make-count-checker 5 5)]
   1.114 +    (assert-called mock/incorrect-invocation-count false (checker 'fn1 5))
   1.115 +    (assert-called mock/incorrect-invocation-count true (checker 'fn1 3))))
   1.116 +
   1.117 +
   1.118 +(deftest test-validate-counts
   1.119 +  (assert-called mock/incorrect-invocation-count false
   1.120 +    (mock/validate-counts (list [(fn []) (atom 0) (mock/make-count-checker #(< % 6) '#(< % 6)) 'fn1])))
   1.121 +  (assert-called mock/incorrect-invocation-count true
   1.122 +    (mock/validate-counts (list [(fn []) (atom 0) (mock/make-count-checker 4 4) 'fn1]))))
   1.123 +
   1.124 +
   1.125 +(deftest test-expect-macro
   1.126 +  (let [under-test (fn [x] (fn1 x))]
   1.127 +    (is (true? (mock/expect [fn1 (mock/times 1 (mock/has-args [#(= 3 %)]))]
   1.128 +                 (under-test 3))))
   1.129 +    (assert-called mock/unexpected-args true (mock/expect [fn1 (mock/times 1 (mock/has-args [#(= 4 %)]))]
   1.130 +                             (under-test 3))))
   1.131 +  (let [under-test (fn [] (fn2 (fn1 1) 3))]
   1.132 +    (is (true? (mock/expect [fn1 (mock/times 1 (mock/has-args [#(= 1 %)] (mock/returns 2)))
   1.133 +                        fn2 (mock/times 1 (mock/has-args [#(= 2 %) #(= 3 %)] (mock/returns 5)))]
   1.134 +                 (under-test))))))
   1.135 \ No newline at end of file