-
Notifications
You must be signed in to change notification settings - Fork 0
/
matchers.cljc
84 lines (72 loc) · 2.86 KB
/
matchers.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
(ns nedap.utils.test.matchers
(:refer-clojure :exclude [gensym])
(:require
[clojure.string :as string]
[matcher-combinators.core :as matcher-combinators]
[matcher-combinators.matchers :as matchers]
[matcher-combinators.model :as model]
[matcher-combinators.result :as result]
[nedap.utils.test.impl :as impl]))
(defn matches?
"returns true if `actual` matches `this`. false otherwise"
[this actual]
(matcher-combinators/indicates-match?
(matcher-combinators/match this actual)))
(defmethod impl/expect-matcher 'match? [_]
{:pred-sym-failure "`to-change` does not match? `to`: (not (match? %s %s))"
:pred-failure "`from` is not allowed to equal `to`: %s"
:assert-expr-sym 'match?
:pred-sym `matches?
:pred =})
(defrecord Gensym [expected]
matcher-combinators/Matcher
(-matcher-for [this] this)
(-matcher-for [this _] this)
(-match [_this actual]
(if (and (symbol? actual)
(= (str expected) (string/replace (str actual) #"(.?)(\d)+" "$1")))
{::result/type :match
::result/value actual
::result/weight 0}
{::result/type :mismatch
::result/value (model/->Mismatch expected actual)
::result/weight 1})))
(defn gensym
"Matcher that will match when given symbol matches the `expected` after the tailing numbers are stripped.
If prefix is not supplied, the prefix is 'G__'"
([] (gensym "G__"))
([prefix-string]
(map->Gensym {:expected prefix-string})))
#?(:clj
(defrecord InAnyOrder [expected timeout]
matcher-combinators/Matcher
(-matcher-for [this] this)
(-matcher-for [this _] this)
(-match [_this actual]
(let [result (deref
(future (matcher-combinators/match (matchers/in-any-order expected) actual))
timeout
::timeout)]
(if (#{::timeout} result)
(throw (ex-info "in-any-order timed out", {:expected expected
:actual actual}))
result)))))
#?(:clj
(defn in-any-order
"Matcher that will match when the given a list that is the same as the
`expected` list but with elements in a different order.
cancels evaluation when `:timeout` is reached (default 5000ms).
drop-in replacement for #'matcher-combinators.matchers/in-any-order"
[expected & {:keys [timeout]
:or {timeout 5000}}]
#?(:clj (map->InAnyOrder {:expected expected
:timeout timeout})
:cljs (matchers/in-any-order expected)))
:cljs
(defn in-any-order
"Matcher that will match when the given a list that is the same as the
`expected` list but with elements in a different order.
Similar to Midje's `(just expected :in-any-order)`.
equal to #'matcher-combinators.matchers/in-any-order"
[expected & {}]
(matchers/in-any-order expected)))