Source

pyobjc / pyobjc-core / PyObjCTest / test_set_property.py

Full commit
  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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
from PyObjCTools.TestSupport import *
import objc
from PyObjCTest.test_object_property import OCObserve
import sys
import pickle

NSObject = objc.lookUpClass('NSObject')

class TestSetPropertyHelper (NSObject):
    aSet = objc.set_property()
    aSet2 = objc.set_property()
    aROSet = objc.set_property(read_only=True)

class TestSetProperty (TestCase):
    def testCopying(self):
        o = TestSetPropertyHelper.alloc().init()
        aSet = set([1,2])
        o.aSet = aSet

        self.assertEqual(len(o.aSet), 2)
        self.assertEqual(len(aSet), 2)
        aSet.add(3)
        self.assertEqual(len(o.aSet), 2)
        self.assertEqual(len(aSet), 3)

    def testRepr(self):
        o = TestSetPropertyHelper.alloc().init()
        aSet = set([1,2])
        o.aSet = aSet

        self.assertEqual(repr(o.aSet), '<set proxy for property aSet %r>'%(aSet,))

    def testDefault(self):
        with OCObserve.alloc().init() as observer:
            o = TestSetPropertyHelper.alloc().init()

            observer.register(o, 'aSet')

            o.aSet = set()
            self.assertEqual(observer.seen, {'aSet': set()})


            self.assertEqual(len(o.aSet), 0)
            o.aSet.add('a')
            o.aSet.add('b')
            self.assertEqual(len(o.aSet), 2)
            self.assertEqual(observer.seen, {'aSet': {'b'}})

            self.assertEqual(observer.values[-2][-1]['kind'], 2)
            self.assertNotIn('old', observer.values[-2][-1])
            self.assertEqual(observer.values[-2][-1]['new'], set('a'))

            self.assertEqual(observer.values[-1][-1]['kind'], 2)
            self.assertNotIn('old', observer.values[-1][-1])
            self.assertEqual(observer.values[-1][-1]['new'], set('b'))

            v = list(o.aSet)
            v.sort()
            self.assertEqual(v, ['a', 'b'])

            self.assertEqual(o.aSet, set(['a', 'b']))

            proxy = o.aSet


            o.aSet.clear()
            self.assertEqual(len(o.aSet), 0)
            self.assertEqual(len(proxy), 0)

            self.assertEqual(observer.values[-1][-1]['old'], set(['a', 'b']))
            self.assertNotIn('new', observer.values[-1][-1])

    def testForwarding(self):
        o = TestSetPropertyHelper.alloc().init()
        o.aSet = set()

        self.assertTrue(o.aSet.issubset({1,2}))

    def testSetting(self):
        o = TestSetPropertyHelper.alloc().init()
        s = {1,2}
        o.aSet = s
        self.assertEqual(o.aSet, s)
        s.add(3)
        self.assertNotEqual(o.aSet, s)
        o.aSet2 = o.aSet
        o.aSet.add(4)
        self.assertNotEqual(o.aSet, o.aSet2)

        v = o.aSet
        o._aSet = None
        self.assertEqual(v, set())




    def testDifferenceUpdate(self):
        with OCObserve.alloc().init() as observer:
            o = TestSetPropertyHelper.alloc().init()

            o.aSet.add(1)
            o.aSet.add(2)
            o.aSet.add(3)

            observer.register(o, 'aSet')

            o.aSet.difference_update(set([1,4]))
            self.assertEqual(o.aSet, set([2,3]))

            self.assertEqual(len(observer.values), 1)
            self.assertEqual(observer.values[-1][-1]['kind'], 3)
            self.assertNotIn('new', observer.values[-1][-1])
            self.assertEqual(observer.values[-1][-1]['old'], set([1]))

    def testSymmetricDifferenceUpdate(self):
        with OCObserve.alloc().init() as observer:
            o = TestSetPropertyHelper.alloc().init()

            o.aSet = set([1,2,3])

            observer.register(o, 'aSet')

            self.assertEqual(len(observer.values), 0)
            o.aSet.symmetric_difference_update(set([1,4]))
            self.assertEqual(o.aSet, set([2, 3, 4]))

            self.assertEqual(len(observer.values), 2)

            haveAdd = False
            haveRemove = False

            for i in 0, 1:
                if observer.values[i][-1]['kind'] == 3:
                    # Remove
                    self.assertNotIn('new', observer.values[i][-1])
                    self.assertEqual(observer.values[i][-1]['old'], set([1]))
                    haveRemove = True

                elif observer.values[i][-1]['kind'] == 2:
                    # Remove
                    self.assertNotIn('old', observer.values[i][-1])
                    self.assertEqual(observer.values[i][-1]['new'], set([4]))
                    haveAdd = True

                else:
                    self.fail("Unexpected update kind")

            if not haveAdd and haveRemove:
                self.fail("Do not have both an add and remove notification")

    def testUpdate(self):
        with OCObserve.alloc().init() as observer:
            o = TestSetPropertyHelper.alloc().init()
            observer.register(o, 'aSet')
            self.assertEqual(o.aSet, set())
            self.assertEqual(len(observer.values), 1)

            o.aSet.update([1,2,3])
            self.assertEqual(len(observer.values), 2)
            self.assertEqual(observer.values[-1][-1]['kind'], 2)
            self.assertNotIn('old', observer.values[-1][-1])
            self.assertEqual(observer.values[-1][-1]['new'], set([1, 2, 3]))

            o.aSet.update(set([3,4,5]))
            self.assertEqual(len(observer.values), 3)
            self.assertEqual(observer.values[-1][-1]['kind'], 2)
            self.assertNotIn('old', observer.values[-1][-1])
            self.assertEqual(observer.values[-1][-1]['new'], set([4, 5]))



    def testAddDiscard(self):
        with OCObserve.alloc().init() as observer:
            o = TestSetPropertyHelper.alloc().init()
            observer.register(o, 'aSet')

            o.aSet.add(1)
            self.assertEqual(observer.seen, {'aSet': {1}})
            self.assertNotIn('old', observer.values[-1][-1])
            self.assertEqual(observer.values[-1][-1]['new'], set([1]))

            o.aSet.discard(1)
            self.assertNotIn('new', observer.values[-1][-1])
            self.assertEqual(observer.values[-1][-1]['old'], set([1]))

            o.aSet.discard(2)
            self.assertNotIn('new', observer.values[-1][-1])
            self.assertEqual(observer.values[-1][-1]['old'], set([]))


    def testAddRemove(self):
        with OCObserve.alloc().init() as observer:
            o = TestSetPropertyHelper.alloc().init()
            observer.register(o, 'aSet')

            o.aSet.add(1)
            self.assertEqual(observer.seen, {'aSet': {1}})
            self.assertNotIn('old', observer.values[-1][-1])
            self.assertEqual(observer.values[-1][-1]['new'], set([1]))

            o.aSet.remove(1)
            self.assertNotIn('new', observer.values[-1][-1])
            self.assertEqual(observer.values[-1][-1]['old'], set([1]))

            self.assertRaises(KeyError, o.aSet.remove, 2)
            self.assertNotIn('new', observer.values[-1][-1])
            self.assertEqual(observer.values[-1][-1]['old'], set([]))

            o.aSet.add(1)
            o.aSet.add(2)
            v = o.aSet.pop()
            self.assertTrue(v in (1, 2))
            self.assertNotIn('new', observer.values[-1][-1])
            self.assertEqual(observer.values[-1][-1]['old'], set([v]))

            o.aSet = set()
            self.assertRaises(KeyError, o.aSet.pop)

    def testOperators(self):
        with OCObserve.alloc().init() as observer:
            o = TestSetPropertyHelper.alloc().init()
            o.aSet = {1,2,3}

            observer.register(o, 'aSet')
            self.assertEquals(observer.seen, {})

            self.assertEquals(o.aSet - {2}, {1,3})
            self.assertEquals(o.aSet, {1,2,3})

            self.assertEquals(o.aSet | {4}, {1,2,3,4})
            self.assertEquals(o.aSet, {1,2,3})

            self.assertEquals(o.aSet & {3,4}, {3})
            self.assertEquals(o.aSet, {1,2,3})

            self.assertEquals(o.aSet ^ {3,4}, {1,2,4})
            self.assertEquals(o.aSet, {1,2,3})

    def testInplace(self):
        # FIXME: the disabled lines in this test indicate a problem in 
        # either the set_proxy implementation, or my understanding of
        # unordered collection properties.
        with OCObserve.alloc().init() as observer:
            o = TestSetPropertyHelper.alloc().init()
            o.aSet.add(1)
            observer.register(o, 'aSet')


            # The inplace operators aren't actually implemented, which means
            # "o.aSet |= value" is actually  "o.aSet = o.aSet | value" and
            # we get the some KVO notificatons as when a new value is set.

            self.assertEqual(o.aSet, {1})
            o.aSet |= set([2,3])
            self.assertEqual(o.aSet, {1,2,3})
            self.assertEqual(o.aSet, set([1,2,3]))
            self.assertEqual(len(observer.values), 2)
            self.assertEqual(observer.values[-1][-1]['kind'], 1)
            #self.assertEqual(observer.values[-1][-1]['old'], set([1]))
            self.assertEqual(observer.values[-1][-1]['new'], set([1,2,3]))

            self.assertEqual(o.aSet, {1,2,3})
            o.aSet &= set([3, 4])
            self.assertEqual(o.aSet, {3})
            self.assertEqual(o.aSet, set([3]))
            self.assertEqual(len(observer.values), 4)
            self.assertEqual(observer.values[-1][-1]['kind'], 1)
            #self.assertEqual(observer.values[-1][-1]['old'], set([1,2,3]))
            #self.assertEqual(observer.values[-1][-1]['new'], set([3]))

            self.assertEqual(o.aSet, {3})
            o.aSet -= {3}
            self.assertEqual(o.aSet, set())
            self.assertEqual(o.aSet, set([]))
            self.assertEqual(len(observer.values), 6)
            self.assertEqual(observer.values[-1][-1]['kind'], 1)
            #self.assertEqual(observer.values[-1][-1]['old'], set([3]))
            #self.assertEqual(observer.values[-1][-1]['new'], set())

            o.aSet = set([1,2,3])
            #self.assertEqual(len(observer.values), 8)

            self.assertEqual(o.aSet, {1,2,3})
            o.aSet ^= set([1, 4])
            self.assertEqual(o.aSet, {2, 3, 4})
            #self.assertEqual(len(observer.values), 9)
            self.assertEqual(observer.values[-1][-1]['kind'], 1)
            #self.assertEqual(observer.values[-1][-1]['old'], set([1,2,3]))
            #self.assertEqual(observer.values[-1][-1]['new'], set([2,3,4]))

    def testObjCAccessors(self):
        # Check that the right ObjC array accessors are defined and work properly
        self.assertTrue(TestSetPropertyHelper.instancesRespondToSelector_(b"setASet:"))
        self.assertTrue(TestSetPropertyHelper.instancesRespondToSelector_(b"aSet"))
        self.assertTrue(TestSetPropertyHelper.instancesRespondToSelector_(b"countOfASet"))
        self.assertTrue(TestSetPropertyHelper.instancesRespondToSelector_(b"enumeratorOfASet"))
        self.assertTrue(TestSetPropertyHelper.instancesRespondToSelector_(b"memberOfASet:"))
        self.assertTrue(TestSetPropertyHelper.instancesRespondToSelector_(b"addASet:"))
        self.assertTrue(TestSetPropertyHelper.instancesRespondToSelector_(b"addASetObject:"))
        self.assertTrue(TestSetPropertyHelper.instancesRespondToSelector_(b"removeASet:"))
        self.assertTrue(TestSetPropertyHelper.instancesRespondToSelector_(b"removeASetObject:"))

        o = TestSetPropertyHelper.alloc().init()
        self.assertEqual(0, o.pyobjc_instanceMethods.countOfASet())
        self.assertRaises(AttributeError, getattr, o, 'countOfASet')
        o.aSet.add(1)
        o.aSet.add(2)

        v = list(sorted(o.pyobjc_instanceMethods.enumeratorOfASet()))
        self.assertEqual(v, [1,2])

        class Testing (object):
            def __hash__(self):
                return 42

            def __eq__(self, other):
                return isinstance(other, Testing)

        p = Testing()
        o.aSet.add(p)

        v = o.pyobjc_instanceMethods.memberOfASet_(Testing())
        self.assertIs(p, v)
        v = o.pyobjc_instanceMethods.memberOfASet_(9)
        self.assertIs(v, None)

        self.assertNotIn(9, o.aSet)
        o.pyobjc_instanceMethods.addASet_(9)
        self.assertIn(9, o.aSet)

        self.assertNotIn(10, o.aSet)
        o.pyobjc_instanceMethods.addASetObject_(10)
        self.assertIn(10, o.aSet)

        self.assertIn(9, o.aSet)
        o.pyobjc_instanceMethods.removeASet_(9)
        self.assertNotIn(9, o.aSet)

        self.assertIn(10, o.aSet)
        o.pyobjc_instanceMethods.removeASetObject_(10)
        self.assertNotIn(10, o.aSet)

    def test_ro_set(self):
        o = TestSetPropertyHelper.alloc().init()
        o._aROSet = { 1, 2, 3, 4 }

        self.assertEqual(o.aROSet, { 1, 2, 3, 4 })
        self.assertIsNot(type(o.aROSet), set)

        self.assertRaises(ValueError, o.aROSet.add, 1)
        self.assertRaises(ValueError, o.aROSet.clear)
        self.assertRaises(ValueError, o.aROSet.pop)
        self.assertRaises(ValueError, o.aROSet.remove, 1)
        self.assertRaises(ValueError, o.aROSet.difference_update, { 1, 2})
        self.assertRaises(ValueError, o.aROSet.intersection_update, { 1, 2})
        self.assertRaises(ValueError, o.aROSet.symmetric_difference_update, { 1, 2})
        self.assertRaises(ValueError, o.aROSet.update, { 1, 2})
        self.assertRaises(ValueError, o.aROSet.discard, 4)

        try:
            o.aROSet |= {1,2}
        except ValueError:
            pass
        else:
            self.fail()

        try:
            o.aROSet -= {1,2}
        except ValueError:
            pass
        else:
            self.fail()

        try:
            o.aROSet ^= {1,2}
        except ValueError:
            pass
        else:
            self.fail()

        try:
            o.aROSet &= {1,2}
        except ValueError:
            pass
        else:
            self.fail()

    def test_compare(self):
        o = TestSetPropertyHelper.alloc().init()
        o.aSet =  {1, 2, 3}
        o.aSet2 = {1, 2, 3}

        self.assertTrue(o.aSet ==  o.aSet2)
        self.assertTrue(o.aSet ==  {1, 2, 3})
        self.assertTrue(o.aSet <=  o.aSet2)
        self.assertTrue(o.aSet <=  {1, 2, 3})
        self.assertTrue(o.aSet >=  o.aSet2)
        self.assertTrue(o.aSet >=  {1, 2, 3})
        self.assertFalse(o.aSet != o.aSet2)
        self.assertFalse(o.aSet != {1, 2, 3})

        o.aSet2 = {2, 3}
        self.assertTrue(o.aSet !=  o.aSet2)
        self.assertTrue(o.aSet !=  {1, 2})
        self.assertFalse(o.aSet == o.aSet2)
        self.assertFalse(o.aSet == {1, 2})

        o.aSet2 = { 1, 2, 3, 4}
        self.assertTrue(o.aSet < o.aSet2)
        self.assertTrue(o.aSet < {1, 2, 3, 4})
        self.assertTrue(o.aSet <= o.aSet2)
        self.assertTrue(o.aSet <= {1, 2, 3, 4})
        self.assertFalse(o.aSet > o.aSet2)
        self.assertFalse(o.aSet > {1, 2, 3, 4})
        self.assertFalse(o.aSet >= o.aSet2)
        self.assertFalse(o.aSet >= {1, 2, 3, 4})

        o.aSet2 = { 1 }
        self.assertTrue(o.aSet > o.aSet2)
        self.assertTrue(o.aSet > {1})
        self.assertTrue(o.aSet >= o.aSet2)
        self.assertTrue(o.aSet >= {1})
        self.assertFalse(o.aSet < o.aSet2)
        self.assertFalse(o.aSet < {1})
        self.assertFalse(o.aSet <= o.aSet2)
        self.assertFalse(o.aSet <= {1})

        if sys.version_info[0] == 2:
            o.aSet = {1, 2, 3}
            o.aSet2 = {1, 2 }

            self.assertRaises(TypeError, cmp, o.aSet, o.aSet2)
            #self.assertRaises(TypeError, cmp, o.aSet, {1})

            self.assertRaises(TypeError, o.aSet.__cmp__, o.aSet2)
            self.assertRaises(TypeError, o.aSet.__cmp__, {1})

    def testPickling(self):
        o = TestSetPropertyHelper.alloc().init()
        o.aSet = {1, 2, 3}

        self.assertFalse(isinstance(o.aSet, set))

        p = pickle.dumps(o.aSet)
        v = pickle.loads(p)
        self.assertEqual(o.aSet, v)
        self.assertTrue(isinstance(v, set))


if __name__ == "__main__":
    main()