-
Notifications
You must be signed in to change notification settings - Fork 0
/
impl.cljc
52 lines (48 loc) · 1.98 KB
/
impl.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
(ns dgknght.app-lib.web-mocks.impl
(:require [dgknght.app-lib.core :as lib]
[dgknght.app-lib.web-mocks.matching :refer [match?
meets-spec?
readable]]))
(defn called?
[msg form]
(let [spec (lib/safe-nth form 1)
calls (lib/safe-nth form 2)
matcher (lib/safe-nth form 3)]
`(let [calls# (deref ~calls)
matches# (filter #(match? % ~matcher) calls#)
pass?# (meets-spec? (count matches#) ~spec)]
; TODO: this renders a readable error in clojurescript test results, but not in clojure test results
{:expected (lib/format "%s called %s" (readable ~matcher) ~spec)
:actual (lib/format "Found %s match(es) among %s"
(count matches#)
(mapv #(dissoc % :channel) calls#))
:message ~msg
:type (if pass?# :pass :fail)})))
(defn not-called?
[msg form]
(let [calls (lib/safe-nth form 1)
matcher (lib/safe-nth form 2)]
`(let [matches# (filter #(match? % ~matcher) (deref ~calls))
pass?# (empty? matches#)]
{:expected "no matching calls"
:actual matches#
:message ~msg
:type (if pass?# :pass :fail)})))
(defn matches-headers?
[{:keys [headers]} expected]
(every? (fn [[k v]]
(= v (get-in headers [k])))
expected))
(defn called-with-headers?
[msg form]
(let [expected (lib/safe-nth form 1)
spec (lib/safe-nth form 2)
calls (lib/safe-nth form 3)]
`(let [matches# (filter #(matches-headers? % ~expected) (deref ~calls))
pass?# (meets-spec? (count matches#) ~spec)]
(test/report {:expected {:headers ~expected
:spec ~spec}
:actual {:matches (count matches#)
:calls (deref ~calls)}
:message ~msg
:type (if pass?# :pass :fail)}))))