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