~nch/glue

ref: 50333171e5b5973cdef9c6e8d045750a3a0936e6 glue/arrowlets_test.py -rw-r--r-- 6.9 KiB
50333171 — nc WIP this seems(?) to be working 1 year, 6 months ago
                                                                                
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import unittest
from arrowlets import *

class Dummy:
    def __init__(self):
        self.event_bindings = {}

    def bind(self, event, f):
        if event in self.event_bindings:
            self.event_bindings[event].add(f)
        else:
            self.event_bindings[event] = {f}

    def unbind(self, event, f):
        if event not in self.event_bindings or f not in self.event_bindings[event]:
            return
        self.event_bindings[event].remove(f)

    def trigger(self, event):
        assert event in self.event_bindings, f'"{event}" event not found'
        # we construct a list to force a copy of this to be created
        # so each function gets triggered, but we may remove it in the
        # middle of iteration
        for f in list(self.event_bindings[event]):
            f(event)

class TestDummy(unittest.TestCase):
    def test_dummy(self):
        d = Dummy()
        flag = [False]
        def f(_):
            flag[0] = True
        d.bind('event', f)
        d.trigger('event')
        self.assertTrue(flag[0])
        flag[0] = False
        d.unbind('event', f)
        d.trigger('event')
        self.assertFalse(flag[0])

class ArrowletsTest(unittest.TestCase):
    def test_CpsA(self):
        add1 = lambda x: x + 1
        add2 = CpsA(add1).next(CpsA(add1))
        self.assertEqual(add2.run(1), 3)

    def test_SimpleEventA(self):
        nclicks = [0]
        def clickTargetA(x):
            target, event = x
            nclicks[0] += 1
            return target

        d = Dummy()
        SimpleEventA('click').next(clickTargetA).run(d)
        d.trigger('click')
        self.assertEqual(nclicks[0], 1)

        nclicks = [0]
        SimpleEventA('click').next(clickTargetA)\
                .next(SimpleEventA('click'))\
                .next(clickTargetA).run(d)
        d.trigger('click')
        d.trigger('click') # unbinds after two clicks
        d.trigger('click')
        self.assertEqual(nclicks[0], 2)

    def test_EventA(self):
        def clickTargetA(x):
            target, event = x
            nclicks[0] += 1
            return target

        target = Dummy()

        ###
        nclicks = [0]

        p = EventA('click')\
                .next(clickTargetA)\
                .next(EventA('click'))\
                .next(clickTargetA)\
                .run(target)

        target.trigger('click')
        target.trigger('click')
        self.assertEqual(nclicks[0], 2)

        ###
        nclicks = [0]

        p = EventA('click')\
                .next(clickTargetA)\
                .next(EventA('click'))\
                .next(clickTargetA)\
                .run(target)

        target.trigger('click') # nclicks++
        p.cancel() # cancel the chain
        self.assertEqual(nclicks[0], 1)
        target.trigger('click') # nclicks shouldn't increment here
        self.assertEqual(nclicks[0], 1)

        ###
        nclicks = [0]

        t = Dummy()
        t.text = 'nothing here'

        p = EventA('click')\
                .next(clickTargetA)\
                .next(EventA('click'))\
                .next(clickTargetA)\
                .run(t)

        def f(_):
            t.text = 'clicked!'
        p.next(f).run(None)

        self.assertEqual(t.text, 'nothing here')
        self.assertEqual(nclicks[0], 0)
        t.trigger('click')
        self.assertEqual(nclicks[0], 1)
        self.assertEqual(t.text, 'clicked!')
        t.trigger('click')
        self.assertEqual(nclicks[0], 2)
        self.assertEqual(t.text, 'clicked!')

    def test_ConstA(self):
        ConstA(1).next(lambda x: self.assertEqual(x, 1)).run(2)

    def test_repeat(self):
        def bubblesort(x):
            lst, i, j = x
            if j + 1 < i:
                if lst[j] > lst[j + 1]:
                    lst[j], lst[j+1] = lst[j+1], lst[j]
                return Repeat((lst, i, j+1))
            elif i > 0:
                return Repeat((lst, i-1, 0))
            else:
                return Done(lst)

        bubblesortA = AsyncA(bubblesort).repeat()
        lst = [1,3,2,0,4]
        bubblesortA.run((lst, 4, 0))
        self.assertEqual(lst, [0,1,2,3,4])

    def test_product(self):
        target1 = Dummy()
        target2 = Dummy()
        def clickTargetsA(a):
            t1, t2 = a[0][0], a[1][0]
            t1.text = 'clicked a'
            t2.text = 'clicked b'

        EventA('click').product(EventA('click'))\
                .next(clickTargetsA)\
                .run((target1, target2))

        target1.trigger('click')
        target2.trigger('click')
        self.assertEqual(target1.text, 'clicked a')
        self.assertEqual(target2.text, 'clicked b')

    def test_or(self):
        def WriteA(s):
            def f(x):
                target, event = x
                target.text = s
                return target
            return f

        heads_elem = Dummy()
        tails_elem = Dummy()
        heads_elem.text = ''
        tails_elem.text = ''
        heads = ConstA(heads_elem)
        tails = ConstA(tails_elem)
        heads.next(EventA('click')).next(WriteA('I win!'))\
            .or_(tails.next(EventA('click')).next(WriteA('You lose!'))).run(None)

        heads_elem.trigger('click')
        self.assertEqual(heads_elem.text, 'I win!')
        self.assertEqual(tails_elem.text, '')

        ###

        heads_elem.text = ''
        tails_elem.text = ''
        heads.next(EventA('click')).next(WriteA('I win!'))\
            .or_(tails.next(EventA('click')).next(WriteA('You lose!'))).run(None)

        tails_elem.trigger('click')
        self.assertEqual(heads_elem.text, '')
        self.assertEqual(tails_elem.text, 'You lose!')

    def test_dragndrop(self):
        target = Dummy()
        proxy = Dummy()
        proxy.target = target
        proxy.status = 'dropped'

        def DragElementA(target):
            # hack because I don't care anymore...
            return ConstA(target).next(lambda _: proxy)

        def setupA(x):
            proxy, event = x
            proxy.status = 'setup'
            return proxy

        def dragA(x):
            proxy, event = x
            proxy.status = 'dragging'
            return proxy

        def dropA(x):
            proxy, event = x
            proxy.status = 'dropped'
            return proxy

        def cancelA(x):
            proxy, event = x
            proxy.status = 'cancelled'
            return proxy

        dragOrDropA = (
             (EventA('mousemove').next(dragA)).next(Repeat)\
        .or_((EventA('mouseup').next(dropA).next(Done)))).repeat()

        dragDropOrCancelA = (
             (EventA('mousemove').next(dragA).next(dragOrDropA))\
        .or_((EventA('mouseup').next(cancelA))))

        dragAndDropA = (
             (EventA('mousedown').next(setupA).next(dragDropOrCancelA)))

        DragElementA('dragtarget').next(dragAndDropA).run(None)
        proxy.trigger('mousedown')
        proxy.trigger('mousemove')
        self.assertEqual(proxy.status, 'dragging')

if __name__ == '__main__':
    unittest.main()