~ubuntu-branches/debian/jessie/sqlalchemy/jessie

« back to all changes in this revision

Viewing changes to test/base/test_utils.py

  • Committer: Package Import Robot
  • Author(s): Piotr Ożarowski, Jakub Wilk, Piotr Ożarowski
  • Date: 2013-07-06 20:53:52 UTC
  • mfrom: (1.4.23) (16.1.17 experimental)
  • Revision ID: package-import@ubuntu.com-20130706205352-ryppl1eto3illd79
Tags: 0.8.2-1
[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

[ Piotr Ożarowski ]
* New upstream release
* Upload to unstable
* Build depend on python3-all instead of -dev, extensions are not built for
  Python 3.X 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from test.lib.testing import assert_raises, assert_raises_message
2
 
import copy, threading
 
1
import copy
 
2
 
3
3
from sqlalchemy import util, sql, exc
4
 
from test.lib.testing import eq_, is_, ne_, fails_if
5
 
from test.lib.util import gc_collect, picklers
 
4
from sqlalchemy.testing import assert_raises, assert_raises_message, fixtures
 
5
from sqlalchemy.testing import eq_, is_, ne_, fails_if
 
6
from sqlalchemy.testing.util import picklers
6
7
from sqlalchemy.util import classproperty
7
 
from test.lib import fixtures
 
8
 
 
9
 
 
10
class KeyedTupleTest():
 
11
 
 
12
    def test_empty(self):
 
13
        keyed_tuple = util.KeyedTuple([])
 
14
        eq_(type(keyed_tuple), util.KeyedTuple)
 
15
        eq_(str(keyed_tuple), '()')
 
16
        eq_(len(keyed_tuple), 0)
 
17
 
 
18
        eq_(keyed_tuple.__dict__, {'_labels': []})
 
19
        eq_(keyed_tuple.keys(), [])
 
20
        eq_(keyed_tuple._fields, ())
 
21
        eq_(keyed_tuple._asdict(), {})
 
22
 
 
23
    def test_values_but_no_labels(self):
 
24
        keyed_tuple = util.KeyedTuple([1, 2])
 
25
        eq_(type(keyed_tuple), util.KeyedTuple)
 
26
        eq_(str(keyed_tuple), '(1, 2)')
 
27
        eq_(len(keyed_tuple), 2)
 
28
 
 
29
        eq_(keyed_tuple.__dict__, {'_labels': []})
 
30
        eq_(keyed_tuple.keys(), [])
 
31
        eq_(keyed_tuple._fields, ())
 
32
        eq_(keyed_tuple._asdict(), {})
 
33
 
 
34
        eq_(keyed_tuple[0], 1)
 
35
        eq_(keyed_tuple[1], 2)
 
36
 
 
37
    def test_basic_creation(self):
 
38
        keyed_tuple = util.KeyedTuple([1, 2], ['a', 'b'])
 
39
        eq_(str(keyed_tuple), '(1, 2)')
 
40
        eq_(keyed_tuple.keys(), ['a', 'b'])
 
41
        eq_(keyed_tuple._fields, ('a', 'b'))
 
42
        eq_(keyed_tuple._asdict(), {'a': 1, 'b': 2})
 
43
 
 
44
    def test_basic_index_access(self):
 
45
        keyed_tuple = util.KeyedTuple([1, 2], ['a', 'b'])
 
46
        eq_(keyed_tuple[0], 1)
 
47
        eq_(keyed_tuple[1], 2)
 
48
 
 
49
        def should_raise():
 
50
            keyed_tuple[2]
 
51
        assert_raises(IndexError, should_raise)
 
52
 
 
53
    def test_basic_attribute_access(self):
 
54
        keyed_tuple = util.KeyedTuple([1, 2], ['a', 'b'])
 
55
        eq_(keyed_tuple.a, 1)
 
56
        eq_(keyed_tuple.b, 2)
 
57
 
 
58
        def should_raise():
 
59
            keyed_tuple.c
 
60
        assert_raises(AttributeError, should_raise)
 
61
 
 
62
    def test_none_label(self):
 
63
        keyed_tuple = util.KeyedTuple([1, 2, 3], ['a', None, 'b'])
 
64
        eq_(str(keyed_tuple), '(1, 2, 3)')
 
65
 
 
66
        # TODO: consider not allowing None labels
 
67
        expected = {'a': 1, None: 2, 'b': 3, '_labels': ['a', None, 'b']}
 
68
        eq_(keyed_tuple.__dict__, expected)
 
69
        eq_(keyed_tuple.keys(), ['a', 'b'])
 
70
        eq_(keyed_tuple._fields, ('a', 'b'))
 
71
        eq_(keyed_tuple._asdict(), {'a': 1, 'b': 3})
 
72
 
 
73
        # attribute access: can't get at value 2
 
74
        eq_(keyed_tuple.a, 1)
 
75
        eq_(keyed_tuple.b, 3)
 
76
 
 
77
        # index access: can get at value 2
 
78
        eq_(keyed_tuple[0], 1)
 
79
        eq_(keyed_tuple[1], 2)
 
80
        eq_(keyed_tuple[2], 3)
 
81
 
 
82
    def test_duplicate_labels(self):
 
83
        keyed_tuple = util.KeyedTuple([1, 2, 3], ['a', 'b', 'b'])
 
84
        eq_(str(keyed_tuple), '(1, 2, 3)')
 
85
 
 
86
        # TODO: consider not allowing duplicate labels
 
87
        expected = {'a': 1, 'b': 3, '_labels': ['a', 'b', 'b']}
 
88
        eq_(keyed_tuple.__dict__, expected)
 
89
        eq_(keyed_tuple.keys(), ['a', 'b', 'b'])
 
90
        eq_(keyed_tuple._fields, ('a', 'b', 'b'))
 
91
        eq_(keyed_tuple._asdict(), {'a': 1, 'b': 3})
 
92
 
 
93
        # attribute access: can't get at value 2
 
94
        eq_(keyed_tuple.a, 1)
 
95
        eq_(keyed_tuple.b, 3)
 
96
 
 
97
        # index access: can get at value 2
 
98
        eq_(keyed_tuple[0], 1)
 
99
        eq_(keyed_tuple[1], 2)
 
100
        eq_(keyed_tuple[2], 3)
 
101
 
 
102
    def test_immutable(self):
 
103
        keyed_tuple = util.KeyedTuple([1, 2], ['a', 'b'])
 
104
        eq_(str(keyed_tuple), '(1, 2)')
 
105
 
 
106
        # attribute access: mutable
 
107
        eq_(keyed_tuple.a, 1)
 
108
        keyed_tuple.a = 100
 
109
        eq_(keyed_tuple.a, 100)
 
110
        keyed_tuple.c = 300
 
111
        eq_(keyed_tuple.c, 300)
 
112
 
 
113
        # index access: immutable
 
114
        def should_raise():
 
115
            keyed_tuple[0] = 100
 
116
        assert_raises(TypeError, should_raise)
 
117
 
8
118
 
9
119
class OrderedDictTest(fixtures.TestBase):
 
120
 
10
121
    def test_odict(self):
11
122
        o = util.OrderedDict()
12
123
        o['a'] = 1
70
181
        o3 = copy.copy(o)
71
182
        eq_(o3.keys(), o.keys())
72
183
 
 
184
 
73
185
class OrderedSetTest(fixtures.TestBase):
 
186
 
74
187
    def test_mutators_against_iter(self):
75
188
        # testing a set modified against an iterator
76
 
        o = util.OrderedSet([3,2, 4, 5])
77
 
 
78
 
        eq_(o.difference(iter([3,4])), util.OrderedSet([2,5]))
79
 
        eq_(o.intersection(iter([3,4, 6])), util.OrderedSet([3, 4]))
80
 
        eq_(o.union(iter([3,4, 6])), util.OrderedSet([2, 3, 4, 5, 6]))
 
189
        o = util.OrderedSet([3, 2, 4, 5])
 
190
 
 
191
        eq_(o.difference(iter([3, 4])), util.OrderedSet([2, 5]))
 
192
        eq_(o.intersection(iter([3, 4, 6])), util.OrderedSet([3, 4]))
 
193
        eq_(o.union(iter([3, 4, 6])), util.OrderedSet([2, 3, 4, 5, 6]))
 
194
 
81
195
 
82
196
class FrozenDictTest(fixtures.TestBase):
 
197
 
83
198
    def test_serialize(self):
84
 
        d = util.immutabledict({1:2, 3:4})
 
199
        d = util.immutabledict({1: 2, 3: 4})
85
200
        for loads, dumps in picklers():
86
201
            print loads(dumps(d))
87
202
 
88
203
 
89
204
class MemoizedAttrTest(fixtures.TestBase):
 
205
 
90
206
    def test_memoized_property(self):
91
207
        val = [20]
 
208
 
92
209
        class Foo(object):
93
210
            @util.memoized_property
94
211
            def bar(self):
102
219
        eq_(f1.bar, 20)
103
220
        eq_(f1.bar, 20)
104
221
        eq_(val[0], 21)
105
 
        eq_(f1.__dict__['bar'] , 20)
 
222
        eq_(f1.__dict__['bar'], 20)
106
223
 
107
224
    def test_memoized_instancemethod(self):
108
225
        val = [20]
 
226
 
109
227
        class Foo(object):
110
228
            @util.memoized_instancemethod
111
229
            def bar(self):
120
238
        eq_(f1.bar(), 20)
121
239
        eq_(val[0], 21)
122
240
 
 
241
 
123
242
class ColumnCollectionTest(fixtures.TestBase):
 
243
 
124
244
    def test_in(self):
125
245
        cc = sql.ColumnCollection()
126
246
        cc.add(sql.column('col1'))
145
265
        cc1.add(c1)
146
266
        cc2.add(c2)
147
267
        cc3.add(c3)
148
 
        assert (cc1==cc2).compare(c1 == c2)
149
 
        assert not (cc1==cc3).compare(c2 == c3)
 
268
        assert (cc1 == cc2).compare(c1 == c2)
 
269
        assert not (cc1 == cc3).compare(c2 == c3)
 
270
 
150
271
 
151
272
class LRUTest(fixtures.TestBase):
152
273
 
160
281
 
161
282
        l = util.LRUCache(10, threshold=.2)
162
283
 
163
 
        for id in range(1,20):
 
284
        for id in range(1, 20):
164
285
            l[id] = item(id)
165
286
 
166
287
        # first couple of items should be gone
168
289
        assert 2 not in l
169
290
 
170
291
        # next batch over the threshold of 10 should be present
171
 
        for id_ in range(11,20):
 
292
        for id_ in range(11, 20):
172
293
            assert id_ in l
173
294
 
174
295
        l[12]
195
316
class ImmutableSubclass(str):
196
317
    pass
197
318
 
 
319
 
198
320
class FlattenIteratorTest(fixtures.TestBase):
199
321
 
200
322
    def test_flatten(self):
215
337
                    [IterString('x'), IterString('y')]])) == ['asdf',
216
338
                'x', 'y']
217
339
 
 
340
 
218
341
class HashOverride(object):
 
342
 
219
343
    def __init__(self, value=None):
220
344
        self.value = value
 
345
 
221
346
    def __hash__(self):
222
347
        return hash(self.value)
223
348
 
 
349
 
224
350
class EqOverride(object):
 
351
 
225
352
    def __init__(self, value=None):
226
353
        self.value = value
227
354
    __hash__ = object.__hash__
 
355
 
228
356
    def __eq__(self, other):
229
357
        if isinstance(other, EqOverride):
230
358
            return self.value == other.value
231
359
        else:
232
360
            return False
 
361
 
233
362
    def __ne__(self, other):
234
363
        if isinstance(other, EqOverride):
235
364
            return self.value != other.value
236
365
        else:
237
366
            return True
238
367
 
 
368
 
239
369
class HashEqOverride(object):
 
370
 
240
371
    def __init__(self, value=None):
241
372
        self.value = value
 
373
 
242
374
    def __hash__(self):
243
375
        return hash(self.value)
 
376
 
244
377
    def __eq__(self, other):
245
378
        if isinstance(other, EqOverride):
246
379
            return self.value == other.value
247
380
        else:
248
381
            return False
 
382
 
249
383
    def __ne__(self, other):
250
384
        if isinstance(other, EqOverride):
251
385
            return self.value != other.value
254
388
 
255
389
 
256
390
class IdentitySetTest(fixtures.TestBase):
 
391
 
257
392
    def assert_eq(self, identityset, expected_iterable):
258
393
        expected = sorted([id(o) for o in expected_iterable])
259
394
        found = sorted([id(o) for o in identityset])
260
395
        eq_(found, expected)
261
396
 
262
397
    def test_init(self):
263
 
        ids = util.IdentitySet([1,2,3,2,1])
264
 
        self.assert_eq(ids, [1,2,3])
 
398
        ids = util.IdentitySet([1, 2, 3, 2, 1])
 
399
        self.assert_eq(ids, [1, 2, 3])
265
400
 
266
401
        ids = util.IdentitySet(ids)
267
 
        self.assert_eq(ids, [1,2,3])
 
402
        self.assert_eq(ids, [1, 2, 3])
268
403
 
269
404
        ids = util.IdentitySet()
270
405
        self.assert_eq(ids, [])
290
425
                ids.add(data[i])
291
426
            self.assert_eq(ids, data)
292
427
 
293
 
    def test_dunder_sub(self):
 
428
    def test_dunder_sub2(self):
294
429
        IdentitySet = util.IdentitySet
295
430
        o1, o2, o3 = object(), object(), object()
296
431
        ids1 = IdentitySet([o1])
303
438
        ids2 -= ids1
304
439
        eq_(ids2, IdentitySet([o2, o3]))
305
440
 
 
441
    def test_dunder_eq(self):
 
442
        _, _, twin1, twin2, unique1, unique2 = self._create_sets()
 
443
 
 
444
        # basic set math
 
445
        eq_(twin1 == twin2, True)
 
446
        eq_(unique1 == unique2, False)
 
447
 
 
448
        # not an IdentitySet
 
449
        not_an_identity_set = object()
 
450
        eq_(unique1 == not_an_identity_set, False)
 
451
 
 
452
    def test_dunder_ne(self):
 
453
        _, _, twin1, twin2, unique1, unique2 = self._create_sets()
 
454
 
 
455
        # basic set math
 
456
        eq_(twin1 != twin2, False)
 
457
        eq_(unique1 != unique2, True)
 
458
 
 
459
        # not an IdentitySet
 
460
        not_an_identity_set = object()
 
461
        eq_(unique1 != not_an_identity_set, True)
 
462
 
 
463
    def test_dunder_le(self):
 
464
        super_, sub_, twin1, twin2, unique1, unique2 = self._create_sets()
 
465
 
 
466
        # basic set math
 
467
        eq_(sub_ <= super_, True)
 
468
        eq_(super_ <= sub_, False)
 
469
 
 
470
        # the same sets
 
471
        eq_(twin1 <= twin2, True)
 
472
        eq_(twin2 <= twin1, True)
 
473
 
 
474
        # totally different sets
 
475
        eq_(unique1 <= unique2, False)
 
476
        eq_(unique2 <= unique1, False)
 
477
 
 
478
        # not an IdentitySet
 
479
        def should_raise():
 
480
            not_an_identity_set = object()
 
481
            return unique1 <= not_an_identity_set
 
482
        self._assert_unorderable_types(should_raise)
 
483
 
 
484
    def test_dunder_lt(self):
 
485
        super_, sub_, twin1, twin2, unique1, unique2 = self._create_sets()
 
486
 
 
487
        # basic set math
 
488
        eq_(sub_ < super_, True)
 
489
        eq_(super_ < sub_, False)
 
490
 
 
491
        # the same sets
 
492
        eq_(twin1 < twin2, False)
 
493
        eq_(twin2 < twin1, False)
 
494
 
 
495
        # totally different sets
 
496
        eq_(unique1 < unique2, False)
 
497
        eq_(unique2 < unique1, False)
 
498
 
 
499
        # not an IdentitySet
 
500
        def should_raise():
 
501
            not_an_identity_set = object()
 
502
            return unique1 < not_an_identity_set
 
503
        self._assert_unorderable_types(should_raise)
 
504
 
 
505
    def test_dunder_ge(self):
 
506
        super_, sub_, twin1, twin2, unique1, unique2 = self._create_sets()
 
507
 
 
508
        # basic set math
 
509
        eq_(sub_ >= super_, False)
 
510
        eq_(super_ >= sub_, True)
 
511
 
 
512
        # the same sets
 
513
        eq_(twin1 >= twin2, True)
 
514
        eq_(twin2 >= twin1, True)
 
515
 
 
516
        # totally different sets
 
517
        eq_(unique1 >= unique2, False)
 
518
        eq_(unique2 >= unique1, False)
 
519
 
 
520
        # not an IdentitySet
 
521
        def should_raise():
 
522
            not_an_identity_set = object()
 
523
            return unique1 >= not_an_identity_set
 
524
        self._assert_unorderable_types(should_raise)
 
525
 
 
526
    def test_dunder_gt(self):
 
527
        super_, sub_, twin1, twin2, unique1, unique2 = self._create_sets()
 
528
 
 
529
        # basic set math
 
530
        eq_(sub_ > super_, False)
 
531
        eq_(super_ > sub_, True)
 
532
 
 
533
        # the same sets
 
534
        eq_(twin1 > twin2, False)
 
535
        eq_(twin2 > twin1, False)
 
536
 
 
537
        # totally different sets
 
538
        eq_(unique1 > unique2, False)
 
539
        eq_(unique2 > unique1, False)
 
540
 
 
541
        # not an IdentitySet
 
542
        def should_raise():
 
543
            not_an_identity_set = object()
 
544
            return unique1 > not_an_identity_set
 
545
        self._assert_unorderable_types(should_raise)
 
546
 
 
547
    def test_issubset(self):
 
548
        super_, sub_, twin1, twin2, unique1, unique2 = self._create_sets()
 
549
 
 
550
        # basic set math
 
551
        eq_(sub_.issubset(super_), True)
 
552
        eq_(super_.issubset(sub_), False)
 
553
 
 
554
        # the same sets
 
555
        eq_(twin1.issubset(twin2), True)
 
556
        eq_(twin2.issubset(twin1), True)
 
557
 
 
558
        # totally different sets
 
559
        eq_(unique1.issubset(unique2), False)
 
560
        eq_(unique2.issubset(unique1), False)
 
561
 
 
562
        # not an IdentitySet
 
563
        not_an_identity_set = object()
 
564
        assert_raises(TypeError, unique1.issubset, not_an_identity_set)
 
565
 
 
566
    def test_issuperset(self):
 
567
        super_, sub_, twin1, twin2, unique1, unique2 = self._create_sets()
 
568
 
 
569
        # basic set math
 
570
        eq_(sub_.issuperset(super_), False)
 
571
        eq_(super_.issuperset(sub_), True)
 
572
 
 
573
        # the same sets
 
574
        eq_(twin1.issuperset(twin2), True)
 
575
        eq_(twin2.issuperset(twin1), True)
 
576
 
 
577
        # totally different sets
 
578
        eq_(unique1.issuperset(unique2), False)
 
579
        eq_(unique2.issuperset(unique1), False)
 
580
 
 
581
        # not an IdentitySet
 
582
        not_an_identity_set = object()
 
583
        assert_raises(TypeError, unique1.issuperset, not_an_identity_set)
 
584
 
 
585
    def test_union(self):
 
586
        super_, sub_, twin1, twin2, _, _ = self._create_sets()
 
587
 
 
588
        # basic set math
 
589
        eq_(sub_.union(super_), super_)
 
590
        eq_(super_.union(sub_), super_)
 
591
 
 
592
        # the same sets
 
593
        eq_(twin1.union(twin2), twin1)
 
594
        eq_(twin2.union(twin1), twin1)
 
595
 
 
596
        # empty sets
 
597
        empty = util.IdentitySet([])
 
598
        eq_(empty.union(empty), empty)
 
599
 
 
600
        # totally different sets
 
601
        unique1 = util.IdentitySet([1])
 
602
        unique2 = util.IdentitySet([2])
 
603
        eq_(unique1.union(unique2), util.IdentitySet([1, 2]))
 
604
 
 
605
        # not an IdentitySet
 
606
        not_an_identity_set = object()
 
607
        assert_raises(TypeError, unique1.union, not_an_identity_set)
 
608
 
 
609
    def test_dunder_or(self):
 
610
        super_, sub_, twin1, twin2, _, _ = self._create_sets()
 
611
 
 
612
        # basic set math
 
613
        eq_(sub_ | super_, super_)
 
614
        eq_(super_ | sub_, super_)
 
615
 
 
616
        # the same sets
 
617
        eq_(twin1 | twin2, twin1)
 
618
        eq_(twin2 | twin1, twin1)
 
619
 
 
620
        # empty sets
 
621
        empty = util.IdentitySet([])
 
622
        eq_(empty | empty, empty)
 
623
 
 
624
        # totally different sets
 
625
        unique1 = util.IdentitySet([1])
 
626
        unique2 = util.IdentitySet([2])
 
627
        eq_(unique1 | unique2, util.IdentitySet([1, 2]))
 
628
 
 
629
        # not an IdentitySet
 
630
        def should_raise():
 
631
            not_an_identity_set = object()
 
632
            return unique1 | not_an_identity_set
 
633
        assert_raises(TypeError, should_raise)
 
634
 
 
635
    def test_update(self):
 
636
        pass  # TODO
 
637
 
 
638
    def test_dunder_ior(self):
 
639
        super_, sub_, _, _, _, _ = self._create_sets()
 
640
 
 
641
        # basic set math
 
642
        sub_ |= super_
 
643
        eq_(sub_, super_)
 
644
        super_ |= sub_
 
645
        eq_(super_, super_)
 
646
 
 
647
        # totally different sets
 
648
        unique1 = util.IdentitySet([1])
 
649
        unique2 = util.IdentitySet([2])
 
650
        unique1 |= unique2
 
651
        eq_(unique1, util.IdentitySet([1, 2]))
 
652
        eq_(unique2, util.IdentitySet([2]))
 
653
 
 
654
        # not an IdentitySet
 
655
        def should_raise():
 
656
            unique = util.IdentitySet([1])
 
657
            not_an_identity_set = object()
 
658
            unique |= not_an_identity_set
 
659
        assert_raises(TypeError, should_raise)
 
660
 
 
661
    def test_difference(self):
 
662
        _, _, twin1, twin2, _, _ = self._create_sets()
 
663
 
 
664
        # basic set math
 
665
        set1 = util.IdentitySet([1, 2, 3])
 
666
        set2 = util.IdentitySet([2, 3, 4])
 
667
        eq_(set1.difference(set2), util.IdentitySet([1]))
 
668
        eq_(set2.difference(set1), util.IdentitySet([4]))
 
669
 
 
670
        # empty sets
 
671
        empty = util.IdentitySet([])
 
672
        eq_(empty.difference(empty), empty)
 
673
 
 
674
        # the same sets
 
675
        eq_(twin1.difference(twin2), empty)
 
676
        eq_(twin2.difference(twin1), empty)
 
677
 
 
678
        # totally different sets
 
679
        unique1 = util.IdentitySet([1])
 
680
        unique2 = util.IdentitySet([2])
 
681
        eq_(unique1.difference(unique2), util.IdentitySet([1]))
 
682
        eq_(unique2.difference(unique1), util.IdentitySet([2]))
 
683
 
 
684
        # not an IdentitySet
 
685
        not_an_identity_set = object()
 
686
        assert_raises(TypeError, unique1.difference, not_an_identity_set)
 
687
 
 
688
    def test_dunder_sub(self):
 
689
        _, _, twin1, twin2, _, _ = self._create_sets()
 
690
 
 
691
        # basic set math
 
692
        set1 = util.IdentitySet([1, 2, 3])
 
693
        set2 = util.IdentitySet([2, 3, 4])
 
694
        eq_(set1 - set2, util.IdentitySet([1]))
 
695
        eq_(set2 - set1, util.IdentitySet([4]))
 
696
 
 
697
        # empty sets
 
698
        empty = util.IdentitySet([])
 
699
        eq_(empty - empty, empty)
 
700
 
 
701
        # the same sets
 
702
        eq_(twin1 - twin2, empty)
 
703
        eq_(twin2 - twin1, empty)
 
704
 
 
705
        # totally different sets
 
706
        unique1 = util.IdentitySet([1])
 
707
        unique2 = util.IdentitySet([2])
 
708
        eq_(unique1 - unique2, util.IdentitySet([1]))
 
709
        eq_(unique2 - unique1, util.IdentitySet([2]))
 
710
 
 
711
        # not an IdentitySet
 
712
        def should_raise():
 
713
            not_an_identity_set = object()
 
714
            unique1 - not_an_identity_set
 
715
        assert_raises(TypeError, should_raise)
 
716
 
 
717
    def test_difference_update(self):
 
718
        pass  # TODO
 
719
 
 
720
    def test_dunder_isub(self):
 
721
        pass  # TODO
 
722
 
 
723
    def test_intersection(self):
 
724
        super_, sub_, twin1, twin2, unique1, unique2 = self._create_sets()
 
725
 
 
726
        # basic set math
 
727
        eq_(sub_.intersection(super_), sub_)
 
728
        eq_(super_.intersection(sub_), sub_)
 
729
 
 
730
        # the same sets
 
731
        eq_(twin1.intersection(twin2), twin1)
 
732
        eq_(twin2.intersection(twin1), twin1)
 
733
 
 
734
        # empty sets
 
735
        empty = util.IdentitySet([])
 
736
        eq_(empty.intersection(empty), empty)
 
737
 
 
738
        # totally different sets
 
739
        eq_(unique1.intersection(unique2), empty)
 
740
 
 
741
        # not an IdentitySet
 
742
        not_an_identity_set = object()
 
743
        assert_raises(TypeError, unique1.intersection, not_an_identity_set)
 
744
 
 
745
    def test_dunder_and(self):
 
746
        super_, sub_, twin1, twin2, unique1, unique2 = self._create_sets()
 
747
 
 
748
        # basic set math
 
749
        eq_(sub_ & super_, sub_)
 
750
        eq_(super_ & sub_, sub_)
 
751
 
 
752
        # the same sets
 
753
        eq_(twin1 & twin2, twin1)
 
754
        eq_(twin2 & twin1, twin1)
 
755
 
 
756
        # empty sets
 
757
        empty = util.IdentitySet([])
 
758
        eq_(empty & empty, empty)
 
759
 
 
760
        # totally different sets
 
761
        eq_(unique1 & unique2, empty)
 
762
 
 
763
        # not an IdentitySet
 
764
        def should_raise():
 
765
            not_an_identity_set = object()
 
766
            return unique1 & not_an_identity_set
 
767
        assert_raises(TypeError, should_raise)
 
768
 
 
769
    def test_intersection_update(self):
 
770
        pass  # TODO
 
771
 
 
772
    def test_dunder_iand(self):
 
773
        pass  # TODO
 
774
 
 
775
    def test_symmetric_difference(self):
 
776
        _, _, twin1, twin2, _, _ = self._create_sets()
 
777
 
 
778
        # basic set math
 
779
        set1 = util.IdentitySet([1, 2, 3])
 
780
        set2 = util.IdentitySet([2, 3, 4])
 
781
        eq_(set1.symmetric_difference(set2), util.IdentitySet([1, 4]))
 
782
        eq_(set2.symmetric_difference(set1), util.IdentitySet([1, 4]))
 
783
 
 
784
        # empty sets
 
785
        empty = util.IdentitySet([])
 
786
        eq_(empty.symmetric_difference(empty), empty)
 
787
 
 
788
        # the same sets
 
789
        eq_(twin1.symmetric_difference(twin2), empty)
 
790
        eq_(twin2.symmetric_difference(twin1), empty)
 
791
 
 
792
        # totally different sets
 
793
        unique1 = util.IdentitySet([1])
 
794
        unique2 = util.IdentitySet([2])
 
795
        eq_(unique1.symmetric_difference(unique2), util.IdentitySet([1, 2]))
 
796
        eq_(unique2.symmetric_difference(unique1), util.IdentitySet([1, 2]))
 
797
 
 
798
        # not an IdentitySet
 
799
        not_an_identity_set = object()
 
800
        assert_raises(
 
801
            TypeError, unique1.symmetric_difference, not_an_identity_set)
 
802
 
 
803
    def test_dunder_xor(self):
 
804
        _, _, twin1, twin2, _, _ = self._create_sets()
 
805
 
 
806
        # basic set math
 
807
        set1 = util.IdentitySet([1, 2, 3])
 
808
        set2 = util.IdentitySet([2, 3, 4])
 
809
        eq_(set1 ^ set2, util.IdentitySet([1, 4]))
 
810
        eq_(set2 ^ set1, util.IdentitySet([1, 4]))
 
811
 
 
812
        # empty sets
 
813
        empty = util.IdentitySet([])
 
814
        eq_(empty ^ empty, empty)
 
815
 
 
816
        # the same sets
 
817
        eq_(twin1 ^ twin2, empty)
 
818
        eq_(twin2 ^ twin1, empty)
 
819
 
 
820
        # totally different sets
 
821
        unique1 = util.IdentitySet([1])
 
822
        unique2 = util.IdentitySet([2])
 
823
        eq_(unique1 ^ unique2, util.IdentitySet([1, 2]))
 
824
        eq_(unique2 ^ unique1, util.IdentitySet([1, 2]))
 
825
 
 
826
        # not an IdentitySet
 
827
        def should_raise():
 
828
            not_an_identity_set = object()
 
829
            return unique1 ^ not_an_identity_set
 
830
        assert_raises(TypeError, should_raise)
 
831
 
 
832
    def test_symmetric_difference_update(self):
 
833
        pass  # TODO
 
834
 
 
835
    def _create_sets(self):
 
836
        o1, o2, o3, o4, o5 = object(), object(), object(), object(), object()
 
837
        super_ = util.IdentitySet([o1, o2, o3])
 
838
        sub_ = util.IdentitySet([o2])
 
839
        twin1 = util.IdentitySet([o3])
 
840
        twin2 = util.IdentitySet([o3])
 
841
        unique1 = util.IdentitySet([o4])
 
842
        unique2 = util.IdentitySet([o5])
 
843
        return super_, sub_, twin1, twin2, unique1, unique2
 
844
 
 
845
    def _assert_unorderable_types(self, callable_):
 
846
        # Py3K
 
847
        #assert_raises_message(
 
848
        #    TypeError, 'unorderable types', callable_)
 
849
        # Py2K
 
850
        assert_raises_message(
 
851
            TypeError, 'cannot compare sets using cmp()', callable_)
 
852
        # end Py2K
 
853
 
306
854
    def test_basic_sanity(self):
307
855
        IdentitySet = util.IdentitySet
308
856
 
320
868
        assert ids != None
321
869
        assert not(ids == None)
322
870
 
323
 
        ne_(ids, IdentitySet([o1,o2,o3]))
 
871
        ne_(ids, IdentitySet([o1, o2, o3]))
324
872
        ids.clear()
325
873
        assert o1 not in ids
326
874
        ids.add(o2)
329
877
        ids.add(o1)
330
878
        eq_(len(ids), 1)
331
879
 
332
 
        isuper = IdentitySet([o1,o2])
 
880
        isuper = IdentitySet([o1, o2])
333
881
        assert ids < isuper
334
882
        assert ids.issubset(isuper)
335
883
        assert isuper.issuperset(ids)
361
909
            assert True
362
910
 
363
911
        try:
364
 
            s = set([o1,o2])
 
912
            s = set([o1, o2])
365
913
            s |= ids
366
914
            assert False
367
915
        except TypeError:
370
918
        assert_raises(TypeError, util.cmp, ids)
371
919
        assert_raises(TypeError, hash, ids)
372
920
 
373
 
    def test_difference(self):
374
 
        os1 = util.IdentitySet([1,2,3])
375
 
        os2 = util.IdentitySet([3,4,5])
376
 
        s1 = set([1,2,3])
377
 
        s2 = set([3,4,5])
378
 
 
379
 
        eq_(os1 - os2, util.IdentitySet([1, 2]))
380
 
        eq_(os2 - os1, util.IdentitySet([4, 5]))
381
 
        assert_raises(TypeError, lambda: os1 - s2)
382
 
        assert_raises(TypeError, lambda: os1 - [3, 4, 5])
383
 
        assert_raises(TypeError, lambda: s1 - os2)
384
 
        assert_raises(TypeError, lambda: s1 - [3, 4, 5])
385
921
 
386
922
class OrderedIdentitySetTest(fixtures.TestBase):
387
923
 
424
960
                          instance)
425
961
 
426
962
    def test_dict(self):
427
 
        d = dict(a=1,b=2,c=3)
 
963
        d = dict(a=1, b=2, c=3)
428
964
        self._ok(d)
429
965
 
430
966
    def test_subdict(self):
431
967
        class subdict(dict):
432
968
            pass
433
 
        d = subdict(a=1,b=2,c=3)
 
969
        d = subdict(a=1, b=2, c=3)
434
970
        self._ok(d)
435
971
 
436
972
    # Py2K
437
973
    def test_UserDict(self):
438
974
        import UserDict
439
 
        d = UserDict.UserDict(a=1,b=2,c=3)
 
975
        d = UserDict.UserDict(a=1, b=2, c=3)
440
976
        self._ok(d)
441
977
    # end Py2K
442
978
 
462
998
        class duck3(object):
463
999
            def iterkeys(duck):
464
1000
                return iter(['a', 'b', 'c'])
 
1001
 
465
1002
            def __getitem__(duck, key):
466
 
                return dict(a=1,b=2,c=3).get(key)
 
1003
                return dict(a=1, b=2, c=3).get(key)
467
1004
        self._ok(duck3())
468
1005
    # end Py2K
469
1006
 
477
1014
        class duck5(object):
478
1015
            def keys(duck):
479
1016
                return ['a', 'b', 'c']
 
1017
 
480
1018
            def get(duck, key):
481
 
                return dict(a=1,b=2,c=3).get(key)
 
1019
                return dict(a=1, b=2, c=3).get(key)
482
1020
        self._ok(duck5())
483
1021
 
484
1022
    def test_duck_6(self):
489
1027
 
490
1028
 
491
1029
class DuckTypeCollectionTest(fixtures.TestBase):
 
1030
 
492
1031
    def test_sets(self):
493
1032
        # Py2K
494
1033
        import sets
495
1034
        # end Py2K
 
1035
 
496
1036
        class SetLike(object):
497
1037
            def add(self):
498
1038
                pass
519
1059
            instance = type_()
520
1060
            is_(util.duck_type_collection(instance), None)
521
1061
 
 
1062
 
522
1063
class ArgInspectionTest(fixtures.TestBase):
 
1064
 
523
1065
    def test_get_cls_kwargs(self):
 
1066
 
524
1067
        class A(object):
525
1068
            def __init__(self, a):
526
1069
                pass
 
1070
 
527
1071
        class A1(A):
528
1072
            def __init__(self, a1):
529
1073
                pass
 
1074
 
530
1075
        class A11(A1):
531
1076
            def __init__(self, a11, **kw):
532
1077
                pass
 
1078
 
533
1079
        class B(object):
534
1080
            def __init__(self, b, **kw):
535
1081
                pass
 
1082
 
536
1083
        class B1(B):
537
1084
            def __init__(self, b1, **kw):
538
1085
                pass
 
1086
 
 
1087
        class B2(B):
 
1088
            def __init__(self, b2):
 
1089
                pass
 
1090
 
539
1091
        class AB(A, B):
540
1092
            def __init__(self, ab):
541
1093
                pass
 
1094
 
542
1095
        class BA(B, A):
543
1096
            def __init__(self, ba, **kwargs):
544
1097
                pass
 
1098
 
545
1099
        class BA1(BA):
546
1100
            pass
 
1101
 
547
1102
        class CAB(A, B):
548
1103
            pass
 
1104
 
549
1105
        class CBA(B, A):
550
1106
            pass
 
1107
 
 
1108
        class CB1A1(B1, A1):
 
1109
            pass
 
1110
 
551
1111
        class CAB1(A, B1):
552
1112
            pass
 
1113
 
553
1114
        class CB1A(B1, A):
554
1115
            pass
 
1116
 
 
1117
        class CB2A(B2, A):
 
1118
            pass
 
1119
 
555
1120
        class D(object):
556
1121
            pass
557
1122
 
 
1123
        class BA2(B, A):
 
1124
            pass
 
1125
 
 
1126
        class A11B1(A11, B1):
 
1127
            pass
 
1128
 
558
1129
        def test(cls, *expected):
559
1130
            eq_(set(util.get_cls_kwargs(cls)), set(expected))
560
1131
 
567
1138
        test(BA, 'ba', 'b', 'a')
568
1139
        test(BA1, 'ba', 'b', 'a')
569
1140
        test(CAB, 'a')
570
 
        test(CBA, 'b')
 
1141
        test(CBA, 'b', 'a')
571
1142
        test(CAB1, 'a')
572
 
        test(CB1A, 'b1', 'b')
 
1143
        test(CB1A, 'b1', 'b', 'a')
 
1144
        test(CB2A, 'b2')
 
1145
        test(CB1A1, "a1", "b1", "b")
573
1146
        test(D)
 
1147
        test(BA2, "a", "b")
 
1148
        test(A11B1, "a1", "a11", "b", "b1")
574
1149
 
575
1150
    def test_get_func_kwargs(self):
576
 
        def f1(): pass
577
 
        def f2(foo): pass
578
 
        def f3(*foo): pass
579
 
        def f4(**foo): pass
 
1151
 
 
1152
        def f1():
 
1153
            pass
 
1154
 
 
1155
        def f2(foo):
 
1156
            pass
 
1157
 
 
1158
        def f3(*foo):
 
1159
            pass
 
1160
 
 
1161
        def f4(**foo):
 
1162
            pass
580
1163
 
581
1164
        def test(fn, *expected):
582
1165
            eq_(set(util.get_func_kwargs(fn)), set(expected))
586
1169
        test(f3)
587
1170
        test(f4)
588
1171
 
 
1172
 
589
1173
class SymbolTest(fixtures.TestBase):
 
1174
 
590
1175
    def test_basic(self):
591
1176
        sym1 = util.symbol('foo')
592
1177
        assert sym1.name == 'foo'
616
1201
            assert rt is sym1
617
1202
            assert rt is sym2
618
1203
 
619
 
class WeakIdentityMappingTest(fixtures.TestBase):
620
 
    class Data(object):
621
 
        pass
622
 
 
623
 
    def _some_data(self, some=20):
624
 
        return [self.Data() for _ in xrange(some)]
625
 
 
626
 
    def _fixture(self, some=20):
627
 
        data = self._some_data()
628
 
        wim = util.WeakIdentityMapping()
629
 
        for idx, obj in enumerate(data):
630
 
            wim[obj] = idx
631
 
        return data, wim
632
 
 
633
 
    def test_delitem(self):
634
 
        data, wim = self._fixture()
635
 
        needle = data[-1]
636
 
 
637
 
        assert needle in wim
638
 
        assert id(needle) in wim.by_id
639
 
        eq_(wim[needle], wim.by_id[id(needle)])
640
 
 
641
 
        del wim[needle]
642
 
 
643
 
        assert needle not in wim
644
 
        assert id(needle) not in wim.by_id
645
 
        eq_(len(wim), (len(data) - 1))
646
 
 
647
 
        data.remove(needle)
648
 
 
649
 
        assert needle not in wim
650
 
        assert id(needle) not in wim.by_id
651
 
        eq_(len(wim), len(data))
652
 
 
653
 
    def test_setitem(self):
654
 
        data, wim = self._fixture()
655
 
 
656
 
        o1, oid1 = data[-1], id(data[-1])
657
 
 
658
 
        assert o1 in wim
659
 
        assert oid1 in wim.by_id
660
 
        eq_(wim[o1], wim.by_id[oid1])
661
 
        id_keys = set(wim.by_id.keys())
662
 
 
663
 
        wim[o1] = 1234
664
 
        assert o1 in wim
665
 
        assert oid1 in wim.by_id
666
 
        eq_(wim[o1], wim.by_id[oid1])
667
 
        eq_(set(wim.by_id.keys()), id_keys)
668
 
 
669
 
        o2 = self.Data()
670
 
        oid2 = id(o2)
671
 
 
672
 
        wim[o2] = 5678
673
 
        assert o2 in wim
674
 
        assert oid2 in wim.by_id
675
 
        eq_(wim[o2], wim.by_id[oid2])
676
 
 
677
 
    def test_pop(self):
678
 
        data, wim = self._fixture()
679
 
        needle = data[-1]
680
 
 
681
 
        needle = data.pop()
682
 
        assert needle in wim
683
 
        assert id(needle) in wim.by_id
684
 
        eq_(wim[needle], wim.by_id[id(needle)])
685
 
        eq_(len(wim), (len(data) + 1))
686
 
 
687
 
        wim.pop(needle)
688
 
        assert needle not in wim
689
 
        assert id(needle) not in wim.by_id
690
 
        eq_(len(wim), len(data))
691
 
 
692
 
    def test_pop_default(self):
693
 
        data, wim = self._fixture()
694
 
        needle = data[-1]
695
 
 
696
 
        value = wim[needle]
697
 
        x = wim.pop(needle, 123)
698
 
        ne_(x, 123)
699
 
        eq_(x, value)
700
 
        assert needle not in wim
701
 
        assert id(needle) not in wim.by_id
702
 
        eq_(len(data), (len(wim) + 1))
703
 
 
704
 
        n2 = self.Data()
705
 
        y = wim.pop(n2, 456)
706
 
        eq_(y, 456)
707
 
        assert n2 not in wim
708
 
        assert id(n2) not in wim.by_id
709
 
        eq_(len(data), (len(wim) + 1))
710
 
 
711
 
    def test_popitem(self):
712
 
        data, wim = self._fixture()
713
 
        (needle, idx) = wim.popitem()
714
 
 
715
 
        assert needle in data
716
 
        eq_(len(data), (len(wim) + 1))
717
 
        assert id(needle) not in wim.by_id
718
 
 
719
 
    def test_setdefault(self):
720
 
        data, wim = self._fixture()
721
 
 
722
 
        o1 = self.Data()
723
 
        oid1 = id(o1)
724
 
 
725
 
        assert o1 not in wim
726
 
 
727
 
        res1 = wim.setdefault(o1, 123)
728
 
        assert o1 in wim
729
 
        assert oid1 in wim.by_id
730
 
        eq_(res1, 123)
731
 
        id_keys = set(wim.by_id.keys())
732
 
 
733
 
        res2 = wim.setdefault(o1, 456)
734
 
        assert o1 in wim
735
 
        assert oid1 in wim.by_id
736
 
        eq_(res2, 123)
737
 
        assert set(wim.by_id.keys()) == id_keys
738
 
 
739
 
        del wim[o1]
740
 
        assert o1 not in wim
741
 
        assert oid1 not in wim.by_id
742
 
        ne_(set(wim.by_id.keys()), id_keys)
743
 
 
744
 
        res3 = wim.setdefault(o1, 789)
745
 
        assert o1 in wim
746
 
        assert oid1 in wim.by_id
747
 
        eq_(res3, 789)
748
 
        eq_(set(wim.by_id.keys()), id_keys)
749
 
 
750
 
    def test_clear(self):
751
 
        data, wim = self._fixture()
752
 
 
753
 
        assert len(data) == len(wim) == len(wim.by_id)
754
 
        wim.clear()
755
 
 
756
 
        eq_(wim, {})
757
 
        eq_(wim.by_id, {})
758
 
 
759
 
    def test_update(self):
760
 
        data, wim = self._fixture()
761
 
        assert_raises(NotImplementedError, wim.update)
762
 
 
763
 
    def test_weak_clear(self):
764
 
        data, wim = self._fixture()
765
 
 
766
 
        assert len(data) == len(wim) == len(wim.by_id)
767
 
 
768
 
        del data[:]
769
 
        gc_collect()
770
 
 
771
 
        eq_(wim, {})
772
 
        eq_(wim.by_id, {})
773
 
        eq_(wim._weakrefs, {})
774
 
 
775
 
    def test_weak_single(self):
776
 
        data, wim = self._fixture()
777
 
 
778
 
        assert len(data) == len(wim) == len(wim.by_id)
779
 
 
780
 
        oid = id(data[0])
781
 
        del data[0]
782
 
        gc_collect()
783
 
 
784
 
        assert len(data) == len(wim) == len(wim.by_id)
785
 
        assert oid not in wim.by_id
786
 
 
787
 
    def test_weak_threadhop(self):
788
 
        data, wim = self._fixture()
789
 
        data = set(data)
790
 
 
791
 
        cv = threading.Condition()
792
 
 
793
 
        def empty(obj):
794
 
            cv.acquire()
795
 
            obj.clear()
796
 
            cv.notify()
797
 
            cv.release()
798
 
 
799
 
        th = threading.Thread(target=empty, args=(data,))
800
 
 
801
 
        cv.acquire()
802
 
        th.start()
803
 
        cv.wait()
804
 
        cv.release()
805
 
        gc_collect()
806
 
 
807
 
        eq_(wim, {})
808
 
        eq_(wim.by_id, {})
809
 
        eq_(wim._weakrefs, {})
 
1204
    def test_bitflags(self):
 
1205
        sym1 = util.symbol('sym1', canonical=1)
 
1206
        sym2 = util.symbol('sym2', canonical=2)
 
1207
 
 
1208
        assert sym1 & sym1
 
1209
        assert not sym1 & sym2
 
1210
        assert not sym1 & sym1 & sym2
 
1211
 
 
1212
    def test_composites(self):
 
1213
        sym1 = util.symbol('sym1', canonical=1)
 
1214
        sym2 = util.symbol('sym2', canonical=2)
 
1215
        sym3 = util.symbol('sym3', canonical=4)
 
1216
        sym4 = util.symbol('sym4', canonical=8)
 
1217
 
 
1218
        assert sym1 & (sym2 | sym1 | sym4)
 
1219
        assert not sym1 & (sym2 | sym3)
 
1220
 
 
1221
        assert not (sym1 | sym2) & (sym3 | sym4)
 
1222
        assert (sym1 | sym2) & (sym2 | sym4)
810
1223
 
811
1224
 
812
1225
class TestFormatArgspec(fixtures.TestBase):
 
1226
 
813
1227
    def test_specs(self):
814
1228
        def test(fn, wanted, grouped=None):
815
1229
            if grouped is None:
820
1234
 
821
1235
        test(lambda: None,
822
1236
           {'args': '()', 'self_arg': None,
823
 
            'apply_kw': '()', 'apply_pos': '()' })
 
1237
            'apply_kw': '()', 'apply_pos': '()'})
824
1238
 
825
1239
        test(lambda: None,
826
1240
           {'args': '', 'self_arg': None,
827
 
            'apply_kw': '', 'apply_pos': '' },
 
1241
            'apply_kw': '', 'apply_pos': ''},
828
1242
           grouped=False)
829
1243
 
830
1244
        test(lambda self: None,
831
1245
           {'args': '(self)', 'self_arg': 'self',
832
 
            'apply_kw': '(self)', 'apply_pos': '(self)' })
 
1246
            'apply_kw': '(self)', 'apply_pos': '(self)'})
833
1247
 
834
1248
        test(lambda self: None,
835
1249
           {'args': 'self', 'self_arg': 'self',
836
 
            'apply_kw': 'self', 'apply_pos': 'self' },
 
1250
            'apply_kw': 'self', 'apply_pos': 'self'},
837
1251
           grouped=False)
838
1252
 
839
1253
        test(lambda *a: None,
840
1254
           {'args': '(*a)', 'self_arg': 'a[0]',
841
 
            'apply_kw': '(*a)', 'apply_pos': '(*a)' })
 
1255
            'apply_kw': '(*a)', 'apply_pos': '(*a)'})
842
1256
 
843
1257
        test(lambda **kw: None,
844
1258
           {'args': '(**kw)', 'self_arg': None,
845
 
            'apply_kw': '(**kw)', 'apply_pos': '(**kw)' })
 
1259
            'apply_kw': '(**kw)', 'apply_pos': '(**kw)'})
846
1260
 
847
1261
        test(lambda *a, **kw: None,
848
1262
           {'args': '(*a, **kw)', 'self_arg': 'a[0]',
849
 
            'apply_kw': '(*a, **kw)', 'apply_pos': '(*a, **kw)' })
 
1263
            'apply_kw': '(*a, **kw)', 'apply_pos': '(*a, **kw)'})
850
1264
 
851
1265
        test(lambda a, *b: None,
852
1266
           {'args': '(a, *b)', 'self_arg': 'a',
853
 
            'apply_kw': '(a, *b)', 'apply_pos': '(a, *b)' })
 
1267
            'apply_kw': '(a, *b)', 'apply_pos': '(a, *b)'})
854
1268
 
855
1269
        test(lambda a, **b: None,
856
1270
           {'args': '(a, **b)', 'self_arg': 'a',
857
 
            'apply_kw': '(a, **b)', 'apply_pos': '(a, **b)' })
 
1271
            'apply_kw': '(a, **b)', 'apply_pos': '(a, **b)'})
858
1272
 
859
1273
        test(lambda a, *b, **c: None,
860
1274
           {'args': '(a, *b, **c)', 'self_arg': 'a',
861
 
            'apply_kw': '(a, *b, **c)', 'apply_pos': '(a, *b, **c)' })
 
1275
            'apply_kw': '(a, *b, **c)', 'apply_pos': '(a, *b, **c)'})
862
1276
 
863
1277
        test(lambda a, b=1, **c: None,
864
1278
           {'args': '(a, b=1, **c)', 'self_arg': 'a',
865
 
            'apply_kw': '(a, b=b, **c)', 'apply_pos': '(a, b, **c)' })
 
1279
            'apply_kw': '(a, b=b, **c)', 'apply_pos': '(a, b, **c)'})
866
1280
 
867
1281
        test(lambda a=1, b=2: None,
868
1282
           {'args': '(a=1, b=2)', 'self_arg': 'a',
869
 
            'apply_kw': '(a=a, b=b)', 'apply_pos': '(a, b)' })
 
1283
            'apply_kw': '(a=a, b=b)', 'apply_pos': '(a, b)'})
870
1284
 
871
1285
        test(lambda a=1, b=2: None,
872
1286
           {'args': 'a=1, b=2', 'self_arg': 'a',
873
 
            'apply_kw': 'a=a, b=b', 'apply_pos': 'a, b' },
 
1287
            'apply_kw': 'a=a, b=b', 'apply_pos': 'a, b'},
874
1288
           grouped=False)
875
1289
 
876
1290
    @fails_if(lambda: util.pypy, "object.__init__ is introspectable")
883
1297
            'apply_pos': '(self, *args, **kwargs)',
884
1298
            'apply_kw': '(self, *args, **kwargs)'}
885
1299
        custom_spec = {
886
 
            'args': '(slef, a=123)', 'self_arg': 'slef', # yes, slef
 
1300
            'args': '(slef, a=123)', 'self_arg': 'slef',  # yes, slef
887
1301
            'apply_pos': '(slef, a)', 'apply_kw': '(slef, a=a)'}
888
1302
 
889
1303
        self._test_init(None, object_spec, wrapper_spec, custom_spec)
899
1313
            'apply_pos': 'self, *args, **kwargs',
900
1314
            'apply_kw': 'self, *args, **kwargs'}
901
1315
        custom_spec = {
902
 
            'args': 'slef, a=123', 'self_arg': 'slef', # yes, slef
 
1316
            'args': 'slef, a=123', 'self_arg': 'slef',  # yes, slef
903
1317
            'apply_pos': 'slef, a', 'apply_kw': 'slef, a=a'}
904
1318
 
905
1319
        self._test_init(False, object_spec, wrapper_spec, custom_spec)
912
1326
                parsed = util.format_argspec_init(fn, grouped=grouped)
913
1327
            eq_(parsed, wanted)
914
1328
 
915
 
        class O(object): pass
 
1329
        class O(object):
 
1330
            pass
916
1331
 
917
1332
        test(O.__init__, object_spec)
918
1333
 
928
1343
 
929
1344
        test(O.__init__, custom_spec)
930
1345
 
931
 
        class O(list): pass
 
1346
        class O(list):
 
1347
            pass
932
1348
 
933
1349
        test(O.__init__, wrapper_spec)
934
1350
 
952
1368
 
953
1369
 
954
1370
class GenericReprTest(fixtures.TestBase):
 
1371
 
955
1372
    def test_all_positional(self):
956
1373
        class Foo(object):
957
1374
            def __init__(self, a, b, c):
1037
1454
            "Foo()"
1038
1455
        )
1039
1456
 
 
1457
 
1040
1458
class AsInterfaceTest(fixtures.TestBase):
1041
1459
 
1042
1460
    class Something(object):
1043
 
        def _ignoreme(self): pass
1044
 
        def foo(self): pass
1045
 
        def bar(self): pass
 
1461
 
 
1462
        def _ignoreme(self):
 
1463
            pass
 
1464
 
 
1465
        def foo(self):
 
1466
            pass
 
1467
 
 
1468
        def bar(self):
 
1469
            pass
1046
1470
 
1047
1471
    class Partial(object):
1048
 
        def bar(self): pass
1049
 
 
1050
 
    class Object(object): pass
 
1472
 
 
1473
        def bar(self):
 
1474
            pass
 
1475
 
 
1476
    class Object(object):
 
1477
        pass
1051
1478
 
1052
1479
    def test_instance(self):
1053
1480
        obj = object()
1107
1534
        res = util.as_interface(obj, cls=self.Something,
1108
1535
                                required=self.Something)
1109
1536
        assertAdapted(res, 'foo', 'bar')
1110
 
        res = util.as_interface(obj, cls=self.Something, required=('foo'
1111
 
                                , ))
 
1537
        res = util.as_interface(obj, cls=self.Something, required=('foo',))
1112
1538
        assertAdapted(res, 'foo', 'bar')
1113
1539
        res = util.as_interface(obj, methods=('foo', 'bar'))
1114
1540
        assertAdapted(res, 'foo', 'bar')
1115
1541
        res = util.as_interface(obj, methods=('foo', 'bar', 'baz'))
1116
1542
        assertAdapted(res, 'foo', 'bar')
1117
 
        res = util.as_interface(obj, methods=('foo', 'bar'),
1118
 
                                required=('foo', ))
 
1543
        res = util.as_interface(obj, methods=('foo', 'bar'), required=('foo',))
1119
1544
        assertAdapted(res, 'foo', 'bar')
1120
 
        assert_raises(TypeError, util.as_interface, obj, methods=('foo'
1121
 
                      , ))
1122
 
        assert_raises(TypeError, util.as_interface, obj, methods=('foo'
1123
 
                      , 'bar', 'baz'), required=('baz', ))
 
1545
        assert_raises(TypeError, util.as_interface, obj, methods=('foo',))
 
1546
        assert_raises(TypeError, util.as_interface, obj,
 
1547
                      methods=('foo', 'bar', 'baz'), required=('baz', ))
1124
1548
        obj = {'foo': 123}
1125
 
        assert_raises(TypeError, util.as_interface, obj,
1126
 
                      cls=self.Something)
 
1549
        assert_raises(TypeError, util.as_interface, obj, cls=self.Something)
1127
1550
 
1128
1551
 
1129
1552
class TestClassHierarchy(fixtures.TestBase):
 
1553
 
1130
1554
    def test_object(self):
1131
1555
        eq_(set(util.class_hierarchy(object)), set((object,)))
1132
1556
 
1167
1591
 
1168
1592
    def test_simple(self):
1169
1593
        class A(object):
1170
 
            something = {'foo':1}
 
1594
            something = {'foo': 1}
1171
1595
 
1172
1596
        class B(A):
1173
1597
 
1174
1598
            @classproperty
1175
1599
            def something(cls):
1176
 
                d = dict(super(B,cls).something)
1177
 
                d.update({'bazz':2})
 
1600
                d = dict(super(B, cls).something)
 
1601
                d.update({'bazz': 2})
1178
1602
                return d
1179
1603
 
1180
 
        eq_(B.something,{
1181
 
                'foo':1,
1182
 
                'bazz':2,
1183
 
                })
 
1604
        eq_(B.something, {'foo': 1, 'bazz': 2})