File tree Expand file tree Collapse file tree 3 files changed +65
-34
lines changed Expand file tree Collapse file tree 3 files changed +65
-34
lines changed Original file line number Diff line number Diff line change @@ -240,9 +240,7 @@ to mock multiple protocols.
240
240
```
241
241
242
242
You will also find a ` spy ` macro within the protocol namespace, this
243
- currently supports spying on an implementation for a single protocol.
244
- Caution: the protocol ` spy ` macro will change in the future when I add support for spying on multiple protocols, you can see examples in the
245
- ` spy.protocol-test ` namespace.
243
+ can also be used to spy on multiple protocols: ` (spy.protocol/spy Proto1...ProtoN impl) `
246
244
247
245
## Contributing
248
246
Original file line number Diff line number Diff line change 45
45
methods))
46
46
47
47
(defmacro spy
48
- " Reify the protocol, spies attached to the reified object via metadata"
48
+ " Reify the protocols, spies attached to the reified object via metadata.
49
+ Can accept multiple protocols at once: (spy.protocol/spy Proto1...ProtoN impl)"
49
50
{:style/indent [:defn [1 ]]}
50
- [protocol instance]
51
- (let [methods (vals (protocol-methods @(resolve protocol)))
51
+ [& args]
52
+ (let [protocols (butlast args)
53
+ instance (last args)
54
+ proto-methods (reduce (fn [acc protocol]
55
+ (assoc acc protocol (vals (protocol-methods @(resolve protocol)))))
56
+ {}
57
+ protocols)
58
+ all-methods (->> proto-methods vals (apply concat))
52
59
spy-fns-sym (gensym " spy-fns-" )]
53
- `(let [~spy-fns-sym ~(->spy-fns methods instance)]
60
+
61
+ `(let [~spy-fns-sym ~(->spy-fns all-methods instance)]
54
62
(with-meta
55
- (reify ~protocol
56
- ~@(mapcat (fn [{:keys [name arglists]}]
57
- (map (fn [arglist]
58
- (let [args (->args arglist)]
59
- (list name args (concat (list (list (keyword name) spy-fns-sym)) args))))
60
- arglists))
61
- methods))
63
+ ~@(let [protos+impls
64
+ (for [[protocol methods] proto-methods]
65
+ (concat
66
+ (list (symbol protocol))
67
+ (mapcat (fn [{:keys [name arglists]}]
68
+ (map (fn [arglist]
69
+ (let [args (->args arglist)]
70
+ (list name args (concat (list (list (keyword name) spy-fns-sym)) args))))
71
+ arglists))
72
+ methods)))]
73
+ (list (concat '(reify)
74
+ (apply concat protos+impls))))
62
75
~spy-fns-sym))))
63
76
64
77
(defn spies [instance]
Original file line number Diff line number Diff line change 100
100
methods (protocol-methods p)]
101
101
102
102
(is (= {:hello
103
- {:name 'hello
104
- :arglists [['this 'a] ['this 'a 'b 'c]]
105
- :var #'spy.protocol-test/hello}}
103
+ {:name 'hello
104
+ :arglists [['this 'a] ['this 'a 'b 'c]]
105
+ :var #'spy.protocol-test/hello}}
106
106
methods)))
107
107
108
108
(let [p @(resolve 'spy.protocol-test/AllSorts)
157
157
(method-b [this x]))
158
158
159
159
(deftest multi-protocols-test
160
- (let [mock (protocol/mock
161
- ProtocolA
162
- (method-a [this x]
163
- :a )
164
-
165
- ProtocolB
166
- (method-b [this x]
167
- :b ))]
168
- (is (= :a (method-a mock :testing )))
169
- (is (= :b (method-b mock :test )))
170
- (is (spy/called-once-with? (:method-a (protocol/spies mock))
171
- mock
172
- :testing ))
173
-
174
- (is (spy/called-once-with? (:method-b (protocol/spies mock))
175
- mock
176
- :test ))))
160
+ (testing " mock"
161
+ (let [mock (protocol/mock
162
+ ProtocolA
163
+ (method-a [this x]
164
+ :a )
165
+
166
+ ProtocolB
167
+ (method-b [this x]
168
+ :b ))]
169
+ (is (= :a (method-a mock :testing )))
170
+ (is (= :b (method-b mock :test )))
171
+ (is (spy/called-once-with? (:method-a (protocol/spies mock))
172
+ mock
173
+ :testing ))
174
+
175
+ (is (spy/called-once-with? (:method-b (protocol/spies mock))
176
+ mock
177
+ :test ))))
178
+ (testing " spy"
179
+ (let [impl (reify
180
+ ProtocolA
181
+ (method-a [this x ] :a )
182
+ ProtocolB
183
+ (method-b [this x] :b ))
184
+ spied (protocol/spy ProtocolA ProtocolB impl)]
185
+
186
+ (is (= :a (method-a spied :testing )))
187
+ (is (= :b (method-b spied :test )))
188
+
189
+ (is (spy/called-once-with? (:method-a (protocol/spies spied))
190
+ spied
191
+ :testing ))
192
+
193
+ (is (spy/called-once-with? (:method-b (protocol/spies spied))
194
+ spied
195
+ :test )))))
196
+
You can’t perform that action at this time.
0 commit comments