-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathtest-deftype.pxi
More file actions
80 lines (67 loc) · 2.17 KB
/
test-deftype.pxi
File metadata and controls
80 lines (67 loc) · 2.17 KB
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
(ns pixie.test.test-deftype
(require pixie.test :as t))
(deftype Simple [:val])
(deftype Simple2 [val])
(t/deftest test-simple
(let [o1 (->Simple 1)
o2 (->Simple 2)]
(foreach [obj-and-val [[o1 1] [o2 2]]]
(let [o (first obj-and-val)
v (second obj-and-val)]
(t/assert= (get-field o :val) v)))))
(deftype MagicalVectorMap [] IMap IVector)
(t/deftest test-satisfies
(let [mvm (->MagicalVectorMap)]
(t/assert (satisfies? IVector mvm))
(t/assert (satisfies? IMap mvm))))
(deftype Count [:val]
ICounted
(-count [self] val))
(deftype Count2 [val]
ICounted
(-count [self] val))
(t/deftest test-extend
(let [o1 (->Count 1)
o2 (->Count 2)]
(foreach [obj-and-val [[o1 1] [o2 2]]]
(let [o (first obj-and-val)
v (second obj-and-val)]
(t/assert= (get-field o :val) v)
(t/assert (satisfies? ICounted o))
(t/assert= (-count o) v)
(t/assert= (count o) v)))))
(defprotocol TestObject
(add [self x & args])
(one-plus [self x & xs]))
(deftype Three [:one :two :three]
TestObject
(add [self x & args]
(apply + x args))
(one-plus [self x & xs]
(apply + one x xs))
ICounted
(-count [self] (+ one two three)))
(deftype Three2 [one two three]
TestObject
(add [self x & args]
(apply + x args))
(one-plus [self x & xs]
(apply + one x xs))
ICounted
(-count [self] (+ one two three)))
(t/deftest test-complex
(let [o1 (->Three 1 2 3)
o2 (->Three2 3 4 5)]
(foreach [obj-and-vals [[o1 1 2 3] [o2 3 4 5]]]
(let [o (first obj-and-vals)
one (second obj-and-vals)
two (third obj-and-vals)
three (fourth obj-and-vals)]
(t/assert= (get-field o :one) one)
(t/assert= (get-field o :two) two)
(t/assert= (get-field o :three) three)
(t/assert (satisfies? ICounted o))
(t/assert= (-count o) (+ one two three))
(t/assert= (count o) (+ one two three))
(t/assert= (add o 21 21) 42)
(t/assert= (one-plus o 9) (+ one 9))))))