-
Notifications
You must be signed in to change notification settings - Fork 2
/
testing.clj
75 lines (68 loc) · 1.98 KB
/
testing.clj
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
(ns rill.wheel.testing
"Tools for unit-testing ring.wheel code."
(:require [rill.event-store.memory :refer [memory-store]]
[rill.wheel.bare-repository :refer [bare-repository]]
[clojure.spec.test.alpha :as stest]))
(defn sub?
"true if `sub` is either `nil`, equal to `x`, or a recursive
subcollection of `x`.
If sub is a sequential collection of size N the first N elements of
`x` are tested. If sub is a map every value in sub is tested with
the corresponding value in `x`. If `sub` is a set every *key* in sub
should exist in `x`.
**Examples:**
(sub? nil
{:anything :at-all})
(sub? [:a :b]
[:a :b :c])
(not (sub? [:a :b :c]
[:a :b]))
(sub? {:a [1 2 3]}
{:a [1 2 3 4] :b 2})
(sub? {:a [1 nil 3]}
{:a [1 2 3 4] :b 2})
(not (sub? {:a [1 2 3 4]}
{:a [1 2 3] :b 2}))
(sub? #{:a}
{:a 1 :b 2})
(sub? #{:a}
#{:a :b})
(not (sub? #{:a :c}
#{:a :b}))
(sub? :something
:something)
(sub? [:1 :2 :3]
(list :1 :2 :3))
(sub? [:1 :2]
(list :1 :2 :3))
(sub? (list :1 :2 :3)
[:1 :2 :3])
(not (sub? (list nil 2)
[:1 :2 :3]))
"
[sub x]
(cond (nil? sub)
true
(sequential? sub)
(and (<= (count sub)
(count x))
(every? (fn [[i el]]
(sub? el (nth x i)))
(map-indexed vector sub)))
(map? sub)
(every? (fn [[k v]] (sub? v (get x k)))
sub)
(set? sub)
(every? #(contains? x %)
sub)
:else
(= sub x)))
(defn ephemeral-repository
"Return an empty repostory backed by an in-memory event-store."
[]
(bare-repository (memory-store)))
(defn with-instrument-all
[t]
(let [instrumented (stest/instrument)]
(t)
(stest/unstrument instrumented)))