rlm@10
|
1 (ns clojure.contrib.test-mock
|
rlm@10
|
2 (:use clojure.test)
|
rlm@10
|
3 (:require [clojure.contrib.mock :as mock]))
|
rlm@10
|
4
|
rlm@10
|
5 ; Used as dummy dependency functions
|
rlm@10
|
6 (defn fn1 {:dynamic true} [x] :ignore)
|
rlm@10
|
7 (defn fn2 {:dynamic true} [x y] :ignore)
|
rlm@10
|
8 (defn fn3 {:dynamic true} ([x] :ignore)
|
rlm@10
|
9 ([x y z] :ignore))
|
rlm@10
|
10 (defn fn4 {:dynamic true} [x y & r] :ignore)
|
rlm@10
|
11
|
rlm@10
|
12 ;functions created using fn directly lack the argslist meta data
|
rlm@10
|
13 (def #^{:dynamic true} deffed-differently (fn [x] :ignore))
|
rlm@10
|
14
|
rlm@10
|
15 (defmacro assert-called [fn-name called? & body]
|
rlm@10
|
16 `(let [called-status?# (atom false)]
|
rlm@10
|
17 (binding [~fn-name (fn [& args#] (reset! called-status?# true))] ~@body)
|
rlm@10
|
18 (is (= ~called? @called-status?#))))
|
rlm@10
|
19
|
rlm@10
|
20 (deftest test-convenience
|
rlm@10
|
21 (testing "once"
|
rlm@10
|
22 (is (false? (mock/once 0)))
|
rlm@10
|
23 (is (false? (mock/once 123)))
|
rlm@10
|
24 (is (true? (mock/once 1))))
|
rlm@10
|
25
|
rlm@10
|
26 (testing "never"
|
rlm@10
|
27 (is (false? (mock/never 4)))
|
rlm@10
|
28 (is (true? (mock/never 0))))
|
rlm@10
|
29
|
rlm@10
|
30 (testing "more-than"
|
rlm@10
|
31 (is (false? ((mock/more-than 5) 3)))
|
rlm@10
|
32 (is (true? ((mock/more-than 5) 9))))
|
rlm@10
|
33
|
rlm@10
|
34 (testing "less-than"
|
rlm@10
|
35 (is (true? ((mock/less-than 5) 3)))
|
rlm@10
|
36 (is (false? ((mock/less-than 5) 9))))
|
rlm@10
|
37
|
rlm@10
|
38 (testing "between"
|
rlm@10
|
39 (is (true? ((mock/between 5 8) 6)))
|
rlm@10
|
40 (is (false? ((mock/between 5 8) 5)))))
|
rlm@10
|
41
|
rlm@10
|
42
|
rlm@10
|
43 (deftest test-returns
|
rlm@10
|
44 (is (= {:returns 5} (mock/returns 5)))
|
rlm@10
|
45 (is (= {:other-key "test" :returns nil} (mock/returns nil {:other-key "test"}))))
|
rlm@10
|
46
|
rlm@10
|
47
|
rlm@10
|
48 (deftest test-has-args
|
rlm@10
|
49 (let [ex (:has-args (mock/has-args [1]))]
|
rlm@10
|
50 (is (fn? ex))
|
rlm@10
|
51 (is (ex 'fn1 1))
|
rlm@10
|
52 (is (ex 'fn1 1 5 6))
|
rlm@10
|
53 (assert-called mock/unexpected-args true (ex 'fn1 5)))
|
rlm@10
|
54 (is (contains? (mock/has-args [] {:pre-existing-key "test"}) :pre-existing-key))
|
rlm@10
|
55 (is (true? (((mock/has-args [5]) :has-args)'fn1 5))))
|
rlm@10
|
56
|
rlm@10
|
57
|
rlm@10
|
58 (deftest test-has-matching-signature
|
rlm@10
|
59 (assert-called mock/no-matching-function-signature true
|
rlm@10
|
60 (mock/has-matching-signature? 'clojure.contrib.test-mock/fn2 [1]))
|
rlm@10
|
61 (assert-called mock/no-matching-function-signature true
|
rlm@10
|
62 (mock/has-matching-signature? 'clojure.contrib.test-mock/fn3 [1 3]))
|
rlm@10
|
63 (assert-called mock/no-matching-function-signature false
|
rlm@10
|
64 (mock/has-matching-signature? 'clojure.contrib.test-mock/fn3 [1 3 5]))
|
rlm@10
|
65 (assert-called mock/no-matching-function-signature false
|
rlm@10
|
66 (mock/has-matching-signature? 'clojure.contrib.test-mock/fn4 [1 3 5 7 9]))
|
rlm@10
|
67 (assert-called mock/no-matching-function-signature false
|
rlm@10
|
68 (mock/has-matching-signature? 'clojure.contrib.test-mock/fn4 [1 3]))
|
rlm@10
|
69 (assert-called mock/no-matching-function-signature true
|
rlm@10
|
70 (mock/has-matching-signature? 'clojure.contrib.test-mock/fn4 [1]))
|
rlm@10
|
71 (assert-called mock/no-matching-function-signature false
|
rlm@10
|
72 (mock/has-matching-signature? 'clojure.contrib.test-mock/deffed-differently [1])))
|
rlm@10
|
73
|
rlm@10
|
74
|
rlm@10
|
75 (deftest test-times
|
rlm@10
|
76 (is (fn? ((mock/times #(= 1 %)) :times)))
|
rlm@10
|
77 (is (contains? (mock/times #(= 1 %) {:existing-key "test"}) :existing-key)))
|
rlm@10
|
78
|
rlm@10
|
79 (deftest test-make-mock
|
rlm@10
|
80 (testing "invalid arguments"
|
rlm@10
|
81 (is (thrown? IllegalArgumentException (mock/make-mock [5]))))
|
rlm@10
|
82
|
rlm@10
|
83 (testing "valid counter and unevaluated returns"
|
rlm@10
|
84 (let [[mock counter count-checker] (mock/make-mock 'fn1 (mock/returns 5 (mock/times 1)))]
|
rlm@10
|
85 (is (fn? mock))
|
rlm@10
|
86 (is (= 0 @counter))
|
rlm@10
|
87 (is (= 5 (mock :ignore-me)))
|
rlm@10
|
88 (is (= 1 @counter))))
|
rlm@10
|
89
|
rlm@10
|
90 (testing "returns as expected"
|
rlm@10
|
91 (let [[mock] (mock/make-mock 'fn1 (mock/returns 5))]
|
rlm@10
|
92 (is (= 5 (mock :ignore))))
|
rlm@10
|
93 (let [[mock] (mock/make-mock 'fn1 (mock/returns #(* 2 %)))]
|
rlm@10
|
94 (is (= 10 ((mock :ignore) 5)) ":returns a function should not automatically
|
rlm@10
|
95 evaluate it.")))
|
rlm@10
|
96
|
rlm@10
|
97 (testing "calls replacement-fn and returns the result"
|
rlm@10
|
98 (let [[mock] (mock/make-mock 'fn1 (mock/calls #(* 3 %)))]
|
rlm@10
|
99 (is (= 15 (mock 5))))
|
rlm@10
|
100 (let [[mock] (mock/make-mock 'fn1 (mock/calls #(* 2 %) (mock/returns 3)))]
|
rlm@10
|
101 (is (= 10 (mock 5)))))
|
rlm@10
|
102
|
rlm@10
|
103 (testing "argument validation"
|
rlm@10
|
104 (let [[mock] (mock/make-mock 'fn1 (mock/has-args [#(= 5 %)]))]
|
rlm@10
|
105 (assert-called mock/unexpected-args true (mock "test"))
|
rlm@10
|
106 (is (nil? (mock 5))))))
|
rlm@10
|
107
|
rlm@10
|
108
|
rlm@10
|
109 (deftest test-make-count-checker
|
rlm@10
|
110 (let [checker (mock/make-count-checker 5 5)]
|
rlm@10
|
111 (assert-called mock/incorrect-invocation-count false (checker 'fn1 5))
|
rlm@10
|
112 (assert-called mock/incorrect-invocation-count true (checker 'fn1 3))))
|
rlm@10
|
113
|
rlm@10
|
114
|
rlm@10
|
115 (deftest test-validate-counts
|
rlm@10
|
116 (assert-called mock/incorrect-invocation-count false
|
rlm@10
|
117 (mock/validate-counts (list [(fn []) (atom 0) (mock/make-count-checker #(< % 6) '#(< % 6)) 'fn1])))
|
rlm@10
|
118 (assert-called mock/incorrect-invocation-count true
|
rlm@10
|
119 (mock/validate-counts (list [(fn []) (atom 0) (mock/make-count-checker 4 4) 'fn1]))))
|
rlm@10
|
120
|
rlm@10
|
121
|
rlm@10
|
122 (deftest test-expect-macro
|
rlm@10
|
123 (let [under-test (fn [x] (fn1 x))]
|
rlm@10
|
124 (is (true? (mock/expect [fn1 (mock/times 1 (mock/has-args [#(= 3 %)]))]
|
rlm@10
|
125 (under-test 3))))
|
rlm@10
|
126 (assert-called mock/unexpected-args true (mock/expect [fn1 (mock/times 1 (mock/has-args [#(= 4 %)]))]
|
rlm@10
|
127 (under-test 3))))
|
rlm@10
|
128 (let [under-test (fn [] (fn2 (fn1 1) 3))]
|
rlm@10
|
129 (is (true? (mock/expect [fn1 (mock/times 1 (mock/has-args [#(= 1 %)] (mock/returns 2)))
|
rlm@10
|
130 fn2 (mock/times 1 (mock/has-args [#(= 2 %) #(= 3 %)] (mock/returns 5)))]
|
rlm@10
|
131 (under-test)))))) |