/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test__static_tuple.py

  • Committer: Martin
  • Date: 2017-06-27 00:10:52 UTC
  • mto: This revision was merged to the branch mainline in revision 6721.
  • Revision ID: gzlist@googlemail.com-20170627001052-o70zln144nmwhamo
Switch c_api helpers for _static_tuple_c to capsules

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2009, 2010, 2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
16
16
 
17
17
"""Tests for the StaticTuple type."""
18
18
 
19
 
import cPickle
20
 
import gc
 
19
try:
 
20
    import cPickle as pickle
 
21
except ImportError:
 
22
    import pickle
21
23
import sys
22
24
 
23
 
from bzrlib import (
 
25
from breezy import (
24
26
    _static_tuple_py,
25
27
    debug,
26
 
    errors,
27
28
    osutils,
28
29
    static_tuple,
29
30
    tests,
30
31
    )
31
 
 
32
 
 
33
 
def load_tests(standard_tests, module, loader):
 
32
from breezy.sixish import (
 
33
    PY3,
 
34
    text_type,
 
35
    )
 
36
from breezy.tests import (
 
37
    features,
 
38
    )
 
39
 
 
40
 
 
41
def load_tests(loader, standard_tests, pattern):
34
42
    """Parameterize tests for all versions of groupcompress."""
35
43
    global compiled_static_tuple_feature
36
44
    suite, compiled_static_tuple_feature = tests.permute_tests_for_extension(
37
 
        standard_tests, loader, 'bzrlib._static_tuple_py',
38
 
        'bzrlib._static_tuple_c')
 
45
        standard_tests, loader, 'breezy._static_tuple_py',
 
46
        'breezy._static_tuple_c')
39
47
    return suite
40
48
 
41
49
 
42
 
class _Meliae(tests.Feature):
43
 
 
44
 
    def _probe(self):
45
 
        try:
46
 
            from meliae import scanner
47
 
        except ImportError:
48
 
            return False
49
 
        return True
50
 
 
51
 
    def feature_name(self):
52
 
        return "Meliae - python memory debugger"
53
 
 
54
 
Meliae = _Meliae()
55
 
 
56
 
 
57
50
class TestStaticTuple(tests.TestCase):
58
51
 
59
52
    def assertRefcount(self, count, obj):
227
220
        self.assertRaises(TypeError, self.module.StaticTuple, subint(2))
228
221
 
229
222
    def test_holds_long(self):
230
 
        k1 = self.module.StaticTuple(2L**65)
 
223
        if PY3:
 
224
            self.skipTest("No long type on Python 3")
 
225
        k1 = self.module.StaticTuple(2**65)
231
226
        class sublong(long):
232
227
            pass
233
228
        # But not a subclass
239
234
            pass
240
235
        self.assertRaises(TypeError, self.module.StaticTuple, subfloat(1.5))
241
236
 
242
 
    def test_holds_str(self):
243
 
        k1 = self.module.StaticTuple('astring')
244
 
        class substr(str):
 
237
    def test_holds_bytes(self):
 
238
        k1 = self.module.StaticTuple(b'astring')
 
239
        class substr(bytes):
245
240
            pass
246
 
        self.assertRaises(TypeError, self.module.StaticTuple, substr('a'))
 
241
        self.assertRaises(TypeError, self.module.StaticTuple, substr(b'a'))
247
242
 
248
243
    def test_holds_unicode(self):
249
244
        k1 = self.module.StaticTuple(u'\xb5')
250
 
        class subunicode(unicode):
 
245
        class subunicode(text_type):
251
246
            pass
252
247
        self.assertRaises(TypeError, self.module.StaticTuple,
253
248
                          subunicode(u'\xb5'))
430
425
        k = self.module.StaticTuple('foo', 'bar', 'baz', 'bing')
431
426
        self.assertEqual(('foo', 'bar'), k[:2])
432
427
        self.assertEqual(('baz',), k[2:-1])
433
 
        try:
434
 
            val = k[::2]
435
 
        except TypeError:
436
 
            # C implementation raises a TypeError, we don't need the
437
 
            # implementation yet, so allow this to pass
438
 
            pass
439
 
        else:
440
 
            # Python implementation uses a regular Tuple, so make sure it gives
441
 
            # the right result
442
 
            self.assertEqual(('foo', 'baz'), val)
 
428
        self.assertEqual(('foo', 'baz',), k[::2])
 
429
        self.assertRaises(TypeError, k.__getitem__, 'not_slice')
443
430
 
444
431
    def test_referents(self):
445
432
        # We implement tp_traverse so that things like 'meliae' can measure the
446
433
        # amount of referenced memory. Unfortunately gc.get_referents() first
447
434
        # checks the IS_GC flag before it traverses anything. We could write a
448
435
        # helper func, but that won't work for the generic implementation...
449
 
        self.requireFeature(Meliae)
 
436
        self.requireFeature(features.meliae)
450
437
        from meliae import scanner
451
438
        strs = ['foo', 'bar', 'baz', 'bing']
452
439
        k = self.module.StaticTuple(*strs)
457
444
        self.assertEqual(sorted(refs), sorted(scanner.get_referents(k)))
458
445
 
459
446
    def test_nested_referents(self):
460
 
        self.requireFeature(Meliae)
 
447
        self.requireFeature(features.meliae)
461
448
        from meliae import scanner
462
449
        strs = ['foo', 'bar', 'baz', 'bing']
463
450
        k1 = self.module.StaticTuple(*strs[:2])
596
583
 
597
584
    def test_pickle(self):
598
585
        st = self.module.StaticTuple('foo', 'bar')
599
 
        pickled = cPickle.dumps(st)
600
 
        unpickled = cPickle.loads(pickled)
 
586
        pickled = pickle.dumps(st)
 
587
        unpickled = pickle.loads(pickled)
601
588
        self.assertEqual(unpickled, st)
602
589
 
603
590
    def test_pickle_empty(self):
604
591
        st = self.module.StaticTuple()
605
 
        pickled = cPickle.dumps(st)
606
 
        unpickled = cPickle.loads(pickled)
 
592
        pickled = pickle.dumps(st)
 
593
        unpickled = pickle.loads(pickled)
607
594
        self.assertIs(st, unpickled)
608
595
 
609
596
    def test_pickle_nested(self):
610
597
        st = self.module.StaticTuple('foo', self.module.StaticTuple('bar'))
611
 
        pickled = cPickle.dumps(st)
612
 
        unpickled = cPickle.loads(pickled)
 
598
        pickled = pickle.dumps(st)
 
599
        unpickled = pickle.loads(pickled)
613
600
        self.assertEqual(unpickled, st)
614
601
 
615
602
    def test_static_tuple_thunk(self):
616
603
        # Make sure the right implementation is available from
617
 
        # bzrlib.static_tuple.StaticTuple.
 
604
        # breezy.static_tuple.StaticTuple.
618
605
        if self.module is _static_tuple_py:
619
606
            if compiled_static_tuple_feature.available():
620
607
                # We will be using the C version