~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test__static_tuple.py

  • Committer: Martin Pool
  • Date: 2009-10-29 06:11:23 UTC
  • mto: This revision was merged to the branch mainline in revision 4776.
  • Revision ID: mbp@sourcefrog.net-20091029061123-vyjm0si2r97rgsp2
one more the the

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009, 2010, 2011 Canonical Ltd
 
1
# Copyright (C) 2009 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
17
17
"""Tests for the StaticTuple type."""
18
18
 
19
19
import cPickle
 
20
import gc
20
21
import sys
21
22
 
22
23
from bzrlib import (
23
24
    _static_tuple_py,
24
 
    debug,
 
25
    errors,
25
26
    osutils,
26
27
    static_tuple,
27
28
    tests,
28
29
    )
29
 
from bzrlib.tests import (
30
 
    features,
31
 
    )
32
30
 
33
31
 
34
32
def load_tests(standard_tests, module, loader):
35
33
    """Parameterize tests for all versions of groupcompress."""
36
 
    global compiled_static_tuple_feature
37
 
    suite, compiled_static_tuple_feature = tests.permute_tests_for_extension(
38
 
        standard_tests, loader, 'bzrlib._static_tuple_py',
39
 
        'bzrlib._static_tuple_c')
40
 
    return suite
 
34
    scenarios = [
 
35
        ('python', {'module': _static_tuple_py}),
 
36
    ]
 
37
    suite = loader.suiteClass()
 
38
    if CompiledStaticTuple.available():
 
39
        from bzrlib import _static_tuple_c
 
40
        scenarios.append(('C', {'module': _static_tuple_c}))
 
41
    else:
 
42
        # the compiled module isn't available, so we add a failing test
 
43
        class FailWithoutFeature(tests.TestCase):
 
44
            def test_fail(self):
 
45
                self.requireFeature(CompiledStaticTuple)
 
46
        suite.addTest(loader.loadTestsFromTestCase(FailWithoutFeature))
 
47
    result = tests.multiply_tests(standard_tests, scenarios, suite)
 
48
    return result
 
49
 
 
50
 
 
51
class _CompiledStaticTuple(tests.Feature):
 
52
 
 
53
    def _probe(self):
 
54
        try:
 
55
            import bzrlib._static_tuple_c
 
56
        except ImportError:
 
57
            return False
 
58
        return True
 
59
 
 
60
    def feature_name(self):
 
61
        return 'bzrlib._static_tuple_c'
 
62
 
 
63
CompiledStaticTuple = _CompiledStaticTuple()
 
64
 
 
65
 
 
66
class _Meliae(tests.Feature):
 
67
 
 
68
    def _probe(self):
 
69
        try:
 
70
            from meliae import scanner
 
71
        except ImportError:
 
72
            return False
 
73
        return True
 
74
 
 
75
    def feature_name(self):
 
76
        return "Meliae - python memory debugger"
 
77
 
 
78
Meliae = _Meliae()
41
79
 
42
80
 
43
81
class TestStaticTuple(tests.TestCase):
63
101
    def test_create_bad_args(self):
64
102
        args_256 = ['a']*256
65
103
        # too many args
66
 
        self.assertRaises(TypeError, self.module.StaticTuple, *args_256)
 
104
        self.assertRaises(ValueError, self.module.StaticTuple, *args_256)
67
105
        args_300 = ['a']*300
68
 
        self.assertRaises(TypeError, self.module.StaticTuple, *args_300)
 
106
        self.assertRaises(ValueError, self.module.StaticTuple, *args_300)
69
107
        # not a string
70
108
        self.assertRaises(TypeError, self.module.StaticTuple, object())
71
109
 
109
147
        k = self.module.StaticTuple('foo')
110
148
        t = k.as_tuple()
111
149
        self.assertEqual(('foo',), t)
112
 
        self.assertIsInstance(t, tuple)
113
 
        self.assertFalse(isinstance(t, self.module.StaticTuple))
114
150
        k = self.module.StaticTuple('foo', 'bar')
115
151
        t = k.as_tuple()
116
152
        self.assertEqual(('foo', 'bar'), t)
117
 
        k2 = self.module.StaticTuple(1, k)
118
 
        t = k2.as_tuple()
119
 
        self.assertIsInstance(t, tuple)
120
 
        # For pickling to work, we need to keep the sub-items as StaticTuple so
121
 
        # that it knows that they also need to be converted.
122
 
        self.assertIsInstance(t[1], self.module.StaticTuple)
123
 
        self.assertEqual((1, ('foo', 'bar')), t)
124
 
 
125
 
    def test_as_tuples(self):
126
 
        k1 = self.module.StaticTuple('foo', 'bar')
127
 
        t = static_tuple.as_tuples(k1)
128
 
        self.assertIsInstance(t, tuple)
129
 
        self.assertEqual(('foo', 'bar'), t)
130
 
        k2 = self.module.StaticTuple(1, k1)
131
 
        t = static_tuple.as_tuples(k2)
132
 
        self.assertIsInstance(t, tuple)
133
 
        self.assertIsInstance(t[1], tuple)
134
 
        self.assertEqual((1, ('foo', 'bar')), t)
135
 
        mixed = (1, k1)
136
 
        t = static_tuple.as_tuples(mixed)
137
 
        self.assertIsInstance(t, tuple)
138
 
        self.assertIsInstance(t[1], tuple)
139
 
        self.assertEqual((1, ('foo', 'bar')), t)
140
153
 
141
154
    def test_len(self):
142
155
        k = self.module.StaticTuple()
432
445
        # amount of referenced memory. Unfortunately gc.get_referents() first
433
446
        # checks the IS_GC flag before it traverses anything. We could write a
434
447
        # helper func, but that won't work for the generic implementation...
435
 
        self.requireFeature(features.meliae_feature)
 
448
        self.requireFeature(Meliae)
436
449
        from meliae import scanner
437
450
        strs = ['foo', 'bar', 'baz', 'bing']
438
451
        k = self.module.StaticTuple(*strs)
443
456
        self.assertEqual(sorted(refs), sorted(scanner.get_referents(k)))
444
457
 
445
458
    def test_nested_referents(self):
446
 
        self.requireFeature(features.meliae_feature)
 
459
        self.requireFeature(Meliae)
447
460
        from meliae import scanner
448
461
        strs = ['foo', 'bar', 'baz', 'bing']
449
462
        k1 = self.module.StaticTuple(*strs[:2])
602
615
        # Make sure the right implementation is available from
603
616
        # bzrlib.static_tuple.StaticTuple.
604
617
        if self.module is _static_tuple_py:
605
 
            if compiled_static_tuple_feature.available():
 
618
            if CompiledStaticTuple.available():
606
619
                # We will be using the C version
607
620
                return
608
621
        self.assertIs(static_tuple.StaticTuple,
609
622
                      self.module.StaticTuple)
610
 
 
611
 
 
612
 
class TestEnsureStaticTuple(tests.TestCase):
613
 
 
614
 
    def test_is_static_tuple(self):
615
 
        st = static_tuple.StaticTuple('foo')
616
 
        st2 = static_tuple.expect_static_tuple(st)
617
 
        self.assertIs(st, st2)
618
 
 
619
 
    def test_is_tuple(self):
620
 
        t = ('foo',)
621
 
        st = static_tuple.expect_static_tuple(t)
622
 
        self.assertIsInstance(st, static_tuple.StaticTuple)
623
 
        self.assertEqual(t, st)
624
 
 
625
 
    def test_flagged_is_static_tuple(self):
626
 
        debug.debug_flags.add('static_tuple')
627
 
        st = static_tuple.StaticTuple('foo')
628
 
        st2 = static_tuple.expect_static_tuple(st)
629
 
        self.assertIs(st, st2)
630
 
 
631
 
    def test_flagged_is_tuple(self):
632
 
        debug.debug_flags.add('static_tuple')
633
 
        t = ('foo',)
634
 
        self.assertRaises(TypeError, static_tuple.expect_static_tuple, t)