/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5557.1.15 by John Arbash Meinel
Merge bzr.dev 5597 to resolve NEWS, aka bzr-2.3.txt
1
# Copyright (C) 2009, 2010, 2011 Canonical Ltd
4679.3.1 by John Arbash Meinel
Start working on a Keys type.
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
17
"""Tests for the StaticTuple type."""
4679.3.1 by John Arbash Meinel
Start working on a Keys type.
18
6715.1.2 by Martin
Update _static_tuple tests so all pass bar mixed type comparisons
19
try:
20
    import cPickle as pickle
21
except ImportError:
22
    import pickle
6715.1.9 by Martin
Adapt StaticTuple tests to change in comparison semantics on Python 3
23
import operator
4679.3.2 by John Arbash Meinel
Add a get_key() function, which then returns tuples for the given items.
24
import sys
25
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
26
from breezy import (
4679.3.40 by John Arbash Meinel
Switch away from the name Key instead start branding as StaticTuple.
27
    _static_tuple_py,
4668.3.1 by John Arbash Meinel
Fix bug #471193, allow tuples into the CHK code.
28
    debug,
4679.3.29 by John Arbash Meinel
Start work on implementing a Key.intern() function.
29
    osutils,
4679.8.3 by John Arbash Meinel
Expose bzrlib.static_tuple.StaticTuple as a thunk
30
    static_tuple,
4679.3.1 by John Arbash Meinel
Start working on a Keys type.
31
    tests,
32
    )
6715.1.2 by Martin
Update _static_tuple tests so all pass bar mixed type comparisons
33
from breezy.sixish import (
34
    PY3,
35
    text_type,
36
    )
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
37
from breezy.tests import (
5967.12.1 by Martin Pool
Move all test features into bzrlib.tests.features
38
    features,
39
    )
4679.3.1 by John Arbash Meinel
Start working on a Keys type.
40
41
6625.1.5 by Martin
Drop custom load_tests implementation and use unittest signature
42
def load_tests(loader, standard_tests, pattern):
4679.3.1 by John Arbash Meinel
Start working on a Keys type.
43
    """Parameterize tests for all versions of groupcompress."""
4913.3.1 by John Arbash Meinel
Implement a permute_for_extension helper.
44
    global compiled_static_tuple_feature
45
    suite, compiled_static_tuple_feature = tests.permute_tests_for_extension(
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
46
        standard_tests, loader, 'breezy._static_tuple_py',
47
        'breezy._static_tuple_c')
4913.3.1 by John Arbash Meinel
Implement a permute_for_extension helper.
48
    return suite
4679.3.1 by John Arbash Meinel
Start working on a Keys type.
49
50
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
51
class TestStaticTuple(tests.TestCase):
4679.3.16 by John Arbash Meinel
Initial work for a Key class.
52
4679.3.81 by John Arbash Meinel
Fix up _simple_set_pyx.pyx to be compatible with pyrex again.
53
    def assertRefcount(self, count, obj):
54
        """Assert that the refcount for obj is what we expect.
55
56
        Note that this automatically adjusts for the fact that calling
57
        assertRefcount actually creates a new pointer, as does calling
58
        sys.getrefcount. So pass the expected value *before* the call.
59
        """
60
        # I don't understand why it is getrefcount()-3 here, but it seems to be
61
        # correct. If I check in the calling function, with:
62
        # self.assertEqual(count, sys.getrefcount(obj)-1)
63
        # Then it works fine. Something about passing it to assertRefcount is
64
        # actually double-incrementing (and decrementing) the refcount
7143.15.2 by Jelmer Vernooij
Run autopep8.
65
        self.assertEqual(count, sys.getrefcount(obj) - 3)
4679.3.81 by John Arbash Meinel
Fix up _simple_set_pyx.pyx to be compatible with pyrex again.
66
4679.3.16 by John Arbash Meinel
Initial work for a Key class.
67
    def test_create(self):
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
68
        k = self.module.StaticTuple('foo')
69
        k = self.module.StaticTuple('foo', 'bar')
4679.3.16 by John Arbash Meinel
Initial work for a Key class.
70
71
    def test_create_bad_args(self):
7143.15.2 by Jelmer Vernooij
Run autopep8.
72
        args_256 = ['a'] * 256
4679.3.16 by John Arbash Meinel
Initial work for a Key class.
73
        # too many args
4763.2.2 by Matt Nordhoff
Oops, really commit the test changes this time
74
        self.assertRaises(TypeError, self.module.StaticTuple, *args_256)
7143.15.2 by Jelmer Vernooij
Run autopep8.
75
        args_300 = ['a'] * 300
4763.2.2 by Matt Nordhoff
Oops, really commit the test changes this time
76
        self.assertRaises(TypeError, self.module.StaticTuple, *args_300)
4679.3.16 by John Arbash Meinel
Initial work for a Key class.
77
        # not a string
4759.2.8 by John Arbash Meinel
Set up a test suite for hash() and richcompare against lots of acceptable types.
78
        self.assertRaises(TypeError, self.module.StaticTuple, object())
4759.2.2 by John Arbash Meinel
Update _static_tuple_py.py with the same concatenation behavior
79
80
    def test_concat(self):
81
        st1 = self.module.StaticTuple('foo')
82
        st2 = self.module.StaticTuple('bar')
83
        st3 = self.module.StaticTuple('foo', 'bar')
84
        st4 = st1 + st2
85
        self.assertEqual(st3, st4)
86
        self.assertIsInstance(st4, self.module.StaticTuple)
87
88
    def test_concat_with_tuple(self):
89
        st1 = self.module.StaticTuple('foo')
90
        t2 = ('bar',)
91
        st3 = self.module.StaticTuple('foo', 'bar')
92
        st4 = self.module.StaticTuple('bar', 'foo')
93
        st5 = st1 + t2
94
        st6 = t2 + st1
95
        self.assertEqual(st3, st5)
96
        self.assertIsInstance(st5, self.module.StaticTuple)
97
        self.assertEqual(st4, st6)
98
        if self.module is _static_tuple_py:
99
            # _static_tuple_py has StaticTuple(tuple), so tuple thinks it
100
            # already knows how to concatenate, as such we can't "inject" our
101
            # own concatenation...
102
            self.assertIsInstance(st6, tuple)
103
        else:
104
            self.assertIsInstance(st6, self.module.StaticTuple)
105
106
    def test_concat_with_bad_tuple(self):
107
        st1 = self.module.StaticTuple('foo')
108
        t2 = (object(),)
4759.2.11 by John Arbash Meinel
Review feedback from Andrew.
109
        # Using st1.__add__ doesn't give the same results as doing the '+' form
110
        self.assertRaises(TypeError, lambda: st1 + t2)
4759.2.5 by John Arbash Meinel
Add a bit more tests.
111
112
    def test_concat_with_non_tuple(self):
113
        st1 = self.module.StaticTuple('foo')
4759.2.11 by John Arbash Meinel
Review feedback from Andrew.
114
        self.assertRaises(TypeError, lambda: st1 + 10)
7143.15.2 by Jelmer Vernooij
Run autopep8.
115
4679.3.16 by John Arbash Meinel
Initial work for a Key class.
116
    def test_as_tuple(self):
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
117
        k = self.module.StaticTuple('foo')
4679.3.29 by John Arbash Meinel
Start work on implementing a Key.intern() function.
118
        t = k.as_tuple()
4679.3.16 by John Arbash Meinel
Initial work for a Key class.
119
        self.assertEqual(('foo',), t)
4789.28.3 by John Arbash Meinel
Add a static_tuple.as_tuples() helper.
120
        self.assertIsInstance(t, tuple)
121
        self.assertFalse(isinstance(t, self.module.StaticTuple))
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
122
        k = self.module.StaticTuple('foo', 'bar')
4679.3.29 by John Arbash Meinel
Start work on implementing a Key.intern() function.
123
        t = k.as_tuple()
4679.3.16 by John Arbash Meinel
Initial work for a Key class.
124
        self.assertEqual(('foo', 'bar'), t)
4789.28.3 by John Arbash Meinel
Add a static_tuple.as_tuples() helper.
125
        k2 = self.module.StaticTuple(1, k)
126
        t = k2.as_tuple()
127
        self.assertIsInstance(t, tuple)
128
        # For pickling to work, we need to keep the sub-items as StaticTuple so
129
        # that it knows that they also need to be converted.
130
        self.assertIsInstance(t[1], self.module.StaticTuple)
131
        self.assertEqual((1, ('foo', 'bar')), t)
132
133
    def test_as_tuples(self):
134
        k1 = self.module.StaticTuple('foo', 'bar')
135
        t = static_tuple.as_tuples(k1)
136
        self.assertIsInstance(t, tuple)
137
        self.assertEqual(('foo', 'bar'), t)
138
        k2 = self.module.StaticTuple(1, k1)
139
        t = static_tuple.as_tuples(k2)
140
        self.assertIsInstance(t, tuple)
141
        self.assertIsInstance(t[1], tuple)
142
        self.assertEqual((1, ('foo', 'bar')), t)
143
        mixed = (1, k1)
144
        t = static_tuple.as_tuples(mixed)
145
        self.assertIsInstance(t, tuple)
146
        self.assertIsInstance(t[1], tuple)
147
        self.assertEqual((1, ('foo', 'bar')), t)
4679.3.16 by John Arbash Meinel
Initial work for a Key class.
148
4679.3.17 by John Arbash Meinel
Implement some of the sequence items.
149
    def test_len(self):
4679.3.42 by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects.
150
        k = self.module.StaticTuple()
151
        self.assertEqual(0, len(k))
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
152
        k = self.module.StaticTuple('foo')
4679.3.17 by John Arbash Meinel
Implement some of the sequence items.
153
        self.assertEqual(1, len(k))
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
154
        k = self.module.StaticTuple('foo', 'bar')
4679.3.17 by John Arbash Meinel
Implement some of the sequence items.
155
        self.assertEqual(2, len(k))
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
156
        k = self.module.StaticTuple('foo', 'bar', 'b', 'b', 'b', 'b', 'b')
4679.3.17 by John Arbash Meinel
Implement some of the sequence items.
157
        self.assertEqual(7, len(k))
7143.15.2 by Jelmer Vernooij
Run autopep8.
158
        args = ['foo'] * 255
4679.3.42 by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects.
159
        k = self.module.StaticTuple(*args)
160
        self.assertEqual(255, len(k))
161
162
    def test_hold_other_static_tuples(self):
163
        k = self.module.StaticTuple('foo', 'bar')
164
        k2 = self.module.StaticTuple(k, k)
165
        self.assertEqual(2, len(k2))
166
        self.assertIs(k, k2[0])
167
        self.assertIs(k, k2[1])
4679.3.17 by John Arbash Meinel
Implement some of the sequence items.
168
169
    def test_getitem(self):
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
170
        k = self.module.StaticTuple('foo', 'bar', 'b', 'b', 'b', 'b', 'z')
4679.3.17 by John Arbash Meinel
Implement some of the sequence items.
171
        self.assertEqual('foo', k[0])
172
        self.assertEqual('foo', k[0])
173
        self.assertEqual('foo', k[0])
174
        self.assertEqual('z', k[6])
175
        self.assertEqual('z', k[-1])
4679.5.5 by John Arbash Meinel
Review feedback from Andrew Bennetts.
176
        self.assertRaises(IndexError, k.__getitem__, 7)
7143.15.2 by Jelmer Vernooij
Run autopep8.
177
        self.assertRaises(IndexError, k.__getitem__, 256 + 7)
4679.5.5 by John Arbash Meinel
Review feedback from Andrew Bennetts.
178
        self.assertRaises(IndexError, k.__getitem__, 12024)
179
        # Python's [] resolver handles the negative arguments, so we can't
180
        # really test StaticTuple_item() with negative values.
181
        self.assertRaises(TypeError, k.__getitem__, 'not-an-int')
182
        self.assertRaises(TypeError, k.__getitem__, '5')
4679.3.17 by John Arbash Meinel
Implement some of the sequence items.
183
184
    def test_refcount(self):
185
        f = 'fo' + 'oo'
7143.15.2 by Jelmer Vernooij
Run autopep8.
186
        num_refs = sys.getrefcount(f) - 1  # sys.getrefcount() adds one
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
187
        k = self.module.StaticTuple(f)
4679.3.81 by John Arbash Meinel
Fix up _simple_set_pyx.pyx to be compatible with pyrex again.
188
        self.assertRefcount(num_refs + 1, f)
189
        b = k[0]
190
        self.assertRefcount(num_refs + 2, f)
191
        b = k[0]
192
        self.assertRefcount(num_refs + 2, f)
4679.3.17 by John Arbash Meinel
Implement some of the sequence items.
193
        c = k[0]
4679.3.81 by John Arbash Meinel
Fix up _simple_set_pyx.pyx to be compatible with pyrex again.
194
        self.assertRefcount(num_refs + 3, f)
4679.3.17 by John Arbash Meinel
Implement some of the sequence items.
195
        del b, c
4679.3.81 by John Arbash Meinel
Fix up _simple_set_pyx.pyx to be compatible with pyrex again.
196
        self.assertRefcount(num_refs + 1, f)
4679.3.17 by John Arbash Meinel
Implement some of the sequence items.
197
        del k
4679.3.81 by John Arbash Meinel
Fix up _simple_set_pyx.pyx to be compatible with pyrex again.
198
        self.assertRefcount(num_refs, f)
4679.3.17 by John Arbash Meinel
Implement some of the sequence items.
199
4679.3.18 by John Arbash Meinel
Copy the hash and richcompare implementations, and add some tests.
200
    def test__repr__(self):
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
201
        k = self.module.StaticTuple('foo', 'bar', 'baz', 'bing')
4679.3.79 by John Arbash Meinel
Change the repr to print out 'StaticTuple'
202
        self.assertEqual("StaticTuple('foo', 'bar', 'baz', 'bing')", repr(k))
4679.3.18 by John Arbash Meinel
Copy the hash and richcompare implementations, and add some tests.
203
4679.3.21 by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples.
204
    def assertCompareEqual(self, k1, k2):
205
        self.assertTrue(k1 == k2)
206
        self.assertTrue(k1 <= k2)
207
        self.assertTrue(k1 >= k2)
208
        self.assertFalse(k1 != k2)
209
        self.assertFalse(k1 < k2)
210
        self.assertFalse(k1 > k2)
211
4759.2.8 by John Arbash Meinel
Set up a test suite for hash() and richcompare against lots of acceptable types.
212
    def test_holds_None(self):
213
        k1 = self.module.StaticTuple(None)
4759.2.11 by John Arbash Meinel
Review feedback from Andrew.
214
        # You cannot subclass None anyway
4759.2.8 by John Arbash Meinel
Set up a test suite for hash() and richcompare against lots of acceptable types.
215
216
    def test_holds_int(self):
217
        k1 = self.module.StaticTuple(1)
7143.15.2 by Jelmer Vernooij
Run autopep8.
218
4759.2.11 by John Arbash Meinel
Review feedback from Andrew.
219
        class subint(int):
220
            pass
221
        # But not a subclass, because subint could introduce refcycles
222
        self.assertRaises(TypeError, self.module.StaticTuple, subint(2))
4759.2.8 by John Arbash Meinel
Set up a test suite for hash() and richcompare against lots of acceptable types.
223
4759.2.9 by John Arbash Meinel
Implement support for lots of types.
224
    def test_holds_long(self):
6715.1.2 by Martin
Update _static_tuple tests so all pass bar mixed type comparisons
225
        if PY3:
226
            self.skipTest("No long type on Python 3")
6619.3.17 by Jelmer Vernooij
Run 2to3 numliterals fixer.
227
        k1 = self.module.StaticTuple(2**65)
7143.15.2 by Jelmer Vernooij
Run autopep8.
228
4759.2.11 by John Arbash Meinel
Review feedback from Andrew.
229
        class sublong(long):
230
            pass
231
        # But not a subclass
232
        self.assertRaises(TypeError, self.module.StaticTuple, sublong(1))
4759.2.9 by John Arbash Meinel
Implement support for lots of types.
233
4759.2.8 by John Arbash Meinel
Set up a test suite for hash() and richcompare against lots of acceptable types.
234
    def test_holds_float(self):
235
        k1 = self.module.StaticTuple(1.2)
7143.15.2 by Jelmer Vernooij
Run autopep8.
236
4759.2.11 by John Arbash Meinel
Review feedback from Andrew.
237
        class subfloat(float):
238
            pass
239
        self.assertRaises(TypeError, self.module.StaticTuple, subfloat(1.5))
240
6715.1.2 by Martin
Update _static_tuple tests so all pass bar mixed type comparisons
241
    def test_holds_bytes(self):
242
        k1 = self.module.StaticTuple(b'astring')
7143.15.2 by Jelmer Vernooij
Run autopep8.
243
6715.1.2 by Martin
Update _static_tuple tests so all pass bar mixed type comparisons
244
        class substr(bytes):
4759.2.11 by John Arbash Meinel
Review feedback from Andrew.
245
            pass
6715.1.2 by Martin
Update _static_tuple tests so all pass bar mixed type comparisons
246
        self.assertRaises(TypeError, self.module.StaticTuple, substr(b'a'))
4759.2.8 by John Arbash Meinel
Set up a test suite for hash() and richcompare against lots of acceptable types.
247
248
    def test_holds_unicode(self):
249
        k1 = self.module.StaticTuple(u'\xb5')
7143.15.2 by Jelmer Vernooij
Run autopep8.
250
6715.1.2 by Martin
Update _static_tuple tests so all pass bar mixed type comparisons
251
        class subunicode(text_type):
4759.2.11 by John Arbash Meinel
Review feedback from Andrew.
252
            pass
253
        self.assertRaises(TypeError, self.module.StaticTuple,
254
                          subunicode(u'\xb5'))
4759.2.8 by John Arbash Meinel
Set up a test suite for hash() and richcompare against lots of acceptable types.
255
256
    def test_hold_bool(self):
257
        k1 = self.module.StaticTuple(True)
258
        k2 = self.module.StaticTuple(False)
4759.2.11 by John Arbash Meinel
Review feedback from Andrew.
259
        # Cannot subclass bool
4759.2.8 by John Arbash Meinel
Set up a test suite for hash() and richcompare against lots of acceptable types.
260
4679.3.21 by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples.
261
    def test_compare_same_obj(self):
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
262
        k1 = self.module.StaticTuple('foo', 'bar')
4679.3.21 by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples.
263
        self.assertCompareEqual(k1, k1)
4679.3.42 by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects.
264
        k2 = self.module.StaticTuple(k1, k1)
265
        self.assertCompareEqual(k2, k2)
4759.2.9 by John Arbash Meinel
Implement support for lots of types.
266
        k3 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True,
267
                                     k1)
4759.2.8 by John Arbash Meinel
Set up a test suite for hash() and richcompare against lots of acceptable types.
268
        self.assertCompareEqual(k3, k3)
4679.3.21 by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples.
269
270
    def test_compare_equivalent_obj(self):
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
271
        k1 = self.module.StaticTuple('foo', 'bar')
272
        k2 = self.module.StaticTuple('foo', 'bar')
4679.3.21 by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples.
273
        self.assertCompareEqual(k1, k2)
4679.3.42 by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects.
274
        k3 = self.module.StaticTuple(k1, k2)
275
        k4 = self.module.StaticTuple(k2, k1)
276
        self.assertCompareEqual(k1, k2)
4759.2.9 by John Arbash Meinel
Implement support for lots of types.
277
        k5 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True,
278
                                     k1)
279
        k6 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True,
280
                                     k1)
4759.2.8 by John Arbash Meinel
Set up a test suite for hash() and richcompare against lots of acceptable types.
281
        self.assertCompareEqual(k5, k6)
282
        k7 = self.module.StaticTuple(None)
283
        k8 = self.module.StaticTuple(None)
284
        self.assertCompareEqual(k7, k8)
4679.3.21 by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples.
285
286
    def test_compare_similar_obj(self):
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
287
        k1 = self.module.StaticTuple('foo' + ' bar', 'bar' + ' baz')
288
        k2 = self.module.StaticTuple('fo' + 'o bar', 'ba' + 'r baz')
4679.3.21 by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples.
289
        self.assertCompareEqual(k1, k2)
4679.3.42 by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects.
290
        k3 = self.module.StaticTuple('foo ' + 'bar', 'bar ' + 'baz')
291
        k4 = self.module.StaticTuple('f' + 'oo bar', 'b' + 'ar baz')
292
        k5 = self.module.StaticTuple(k1, k2)
293
        k6 = self.module.StaticTuple(k3, k4)
294
        self.assertCompareEqual(k5, k6)
4679.3.21 by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples.
295
6715.1.9 by Martin
Adapt StaticTuple tests to change in comparison semantics on Python 3
296
    def check_strict_compare(self, k1, k2, mismatched_types):
297
        """True if on Python 3 and stricter comparison semantics are used."""
298
        if PY3 and mismatched_types:
299
            for op in ("ge", "gt", "le", "lt"):
300
                self.assertRaises(TypeError, getattr(operator, op), k1, k2)
301
            return True
302
        return False
303
304
    def assertCompareDifferent(self, k_small, k_big, mismatched_types=False):
4679.3.21 by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples.
305
        self.assertFalse(k_small == k_big)
306
        self.assertTrue(k_small != k_big)
6715.1.9 by Martin
Adapt StaticTuple tests to change in comparison semantics on Python 3
307
        if not self.check_strict_compare(k_small, k_big, mismatched_types):
308
            self.assertFalse(k_small >= k_big)
309
            self.assertFalse(k_small > k_big)
310
            self.assertTrue(k_small <= k_big)
311
            self.assertTrue(k_small < k_big)
4679.3.21 by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples.
312
6715.1.9 by Martin
Adapt StaticTuple tests to change in comparison semantics on Python 3
313
    def assertCompareNoRelation(self, k1, k2, mismatched_types=False):
4679.5.8 by John Arbash Meinel
Add some tests that we *can* compare to strings, even if we don't care
314
        """Run the comparison operators, make sure they do something.
315
316
        However, we don't actually care what comes first or second. This is
317
        stuff like cross-class comparisons. We don't want to segfault/raise an
318
        exception, but we don't care about the sort order.
319
        """
320
        self.assertFalse(k1 == k2)
321
        self.assertTrue(k1 != k2)
6715.1.9 by Martin
Adapt StaticTuple tests to change in comparison semantics on Python 3
322
        if not self.check_strict_compare(k1, k2, mismatched_types):
323
            # Do the comparison, but we don't care about the result
324
            k1 >= k2
325
            k1 > k2
326
            k1 <= k2
327
            k1 < k2
4679.5.8 by John Arbash Meinel
Add some tests that we *can* compare to strings, even if we don't care
328
4679.3.45 by John Arbash Meinel
Do some work to handle comparison to object that aren't tuples or strings.
329
    def test_compare_vs_none(self):
330
        k1 = self.module.StaticTuple('baz', 'bing')
6715.1.9 by Martin
Adapt StaticTuple tests to change in comparison semantics on Python 3
331
        self.assertCompareDifferent(None, k1, mismatched_types=True)
7143.15.2 by Jelmer Vernooij
Run autopep8.
332
4679.5.8 by John Arbash Meinel
Add some tests that we *can* compare to strings, even if we don't care
333
    def test_compare_cross_class(self):
334
        k1 = self.module.StaticTuple('baz', 'bing')
6715.1.9 by Martin
Adapt StaticTuple tests to change in comparison semantics on Python 3
335
        self.assertCompareNoRelation(10, k1, mismatched_types=True)
336
        self.assertCompareNoRelation('baz', k1, mismatched_types=True)
4679.3.45 by John Arbash Meinel
Do some work to handle comparison to object that aren't tuples or strings.
337
4679.3.21 by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples.
338
    def test_compare_all_different_same_width(self):
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
339
        k1 = self.module.StaticTuple('baz', 'bing')
340
        k2 = self.module.StaticTuple('foo', 'bar')
4679.3.21 by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples.
341
        self.assertCompareDifferent(k1, k2)
4679.3.42 by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects.
342
        k3 = self.module.StaticTuple(k1, k2)
343
        k4 = self.module.StaticTuple(k2, k1)
344
        self.assertCompareDifferent(k3, k4)
4759.2.8 by John Arbash Meinel
Set up a test suite for hash() and richcompare against lots of acceptable types.
345
        k5 = self.module.StaticTuple(1)
346
        k6 = self.module.StaticTuple(2)
347
        self.assertCompareDifferent(k5, k6)
348
        k7 = self.module.StaticTuple(1.2)
349
        k8 = self.module.StaticTuple(2.4)
350
        self.assertCompareDifferent(k7, k8)
351
        k9 = self.module.StaticTuple(u's\xb5')
352
        k10 = self.module.StaticTuple(u's\xe5')
353
        self.assertCompareDifferent(k9, k10)
4679.3.21 by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples.
354
355
    def test_compare_some_different(self):
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
356
        k1 = self.module.StaticTuple('foo', 'bar')
357
        k2 = self.module.StaticTuple('foo', 'zzz')
4679.3.21 by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples.
358
        self.assertCompareDifferent(k1, k2)
4679.3.42 by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects.
359
        k3 = self.module.StaticTuple(k1, k1)
360
        k4 = self.module.StaticTuple(k1, k2)
361
        self.assertCompareDifferent(k3, k4)
4759.2.8 by John Arbash Meinel
Set up a test suite for hash() and richcompare against lots of acceptable types.
362
        k5 = self.module.StaticTuple('foo', None)
6715.1.9 by Martin
Adapt StaticTuple tests to change in comparison semantics on Python 3
363
        self.assertCompareDifferent(k5, k1, mismatched_types=True)
364
        self.assertCompareDifferent(k5, k2, mismatched_types=True)
4679.3.21 by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples.
365
366
    def test_compare_diff_width(self):
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
367
        k1 = self.module.StaticTuple('foo')
368
        k2 = self.module.StaticTuple('foo', 'bar')
4679.3.21 by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples.
369
        self.assertCompareDifferent(k1, k2)
4679.3.42 by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects.
370
        k3 = self.module.StaticTuple(k1)
371
        k4 = self.module.StaticTuple(k1, k2)
372
        self.assertCompareDifferent(k3, k4)
4679.3.21 by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples.
373
4759.2.8 by John Arbash Meinel
Set up a test suite for hash() and richcompare against lots of acceptable types.
374
    def test_compare_different_types(self):
375
        k1 = self.module.StaticTuple('foo', 'bar')
4759.2.9 by John Arbash Meinel
Implement support for lots of types.
376
        k2 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True,
377
                                     k1)
6715.1.9 by Martin
Adapt StaticTuple tests to change in comparison semantics on Python 3
378
        self.assertCompareNoRelation(k1, k2, mismatched_types=True)
4759.2.8 by John Arbash Meinel
Set up a test suite for hash() and richcompare against lots of acceptable types.
379
        k3 = self.module.StaticTuple('foo')
380
        self.assertCompareDifferent(k3, k1)
381
        k4 = self.module.StaticTuple(None)
6715.1.9 by Martin
Adapt StaticTuple tests to change in comparison semantics on Python 3
382
        self.assertCompareDifferent(k4, k1, mismatched_types=True)
4759.2.8 by John Arbash Meinel
Set up a test suite for hash() and richcompare against lots of acceptable types.
383
        k5 = self.module.StaticTuple(1)
6715.1.9 by Martin
Adapt StaticTuple tests to change in comparison semantics on Python 3
384
        self.assertCompareNoRelation(k1, k5, mismatched_types=True)
4759.2.8 by John Arbash Meinel
Set up a test suite for hash() and richcompare against lots of acceptable types.
385
4679.3.21 by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples.
386
    def test_compare_to_tuples(self):
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
387
        k1 = self.module.StaticTuple('foo')
4679.3.21 by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples.
388
        self.assertCompareEqual(k1, ('foo',))
389
        self.assertCompareEqual(('foo',), k1)
390
        self.assertCompareDifferent(k1, ('foo', 'bar'))
391
        self.assertCompareDifferent(k1, ('foo', 10))
392
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
393
        k2 = self.module.StaticTuple('foo', 'bar')
4679.3.21 by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples.
394
        self.assertCompareEqual(k2, ('foo', 'bar'))
395
        self.assertCompareEqual(('foo', 'bar'), k2)
396
        self.assertCompareDifferent(k2, ('foo', 'zzz'))
397
        self.assertCompareDifferent(('foo',), k2)
398
        self.assertCompareDifferent(('foo', 'aaa'), k2)
399
        self.assertCompareDifferent(('baz', 'bing'), k2)
6715.1.9 by Martin
Adapt StaticTuple tests to change in comparison semantics on Python 3
400
        self.assertCompareDifferent(('foo', 10), k2, mismatched_types=True)
4679.3.18 by John Arbash Meinel
Copy the hash and richcompare implementations, and add some tests.
401
4679.3.42 by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects.
402
        k3 = self.module.StaticTuple(k1, k2)
403
        self.assertCompareEqual(k3, (('foo',), ('foo', 'bar')))
404
        self.assertCompareEqual((('foo',), ('foo', 'bar')), k3)
405
        self.assertCompareEqual(k3, (k1, ('foo', 'bar')))
406
        self.assertCompareEqual((k1, ('foo', 'bar')), k3)
407
4679.8.11 by John Arbash Meinel
Handle the case where the recursive call ends up returning NotImplemented.
408
    def test_compare_mixed_depths(self):
409
        stuple = self.module.StaticTuple
4679.8.13 by John Arbash Meinel
clean up the test case a little bit.
410
        k1 = stuple(stuple('a',), stuple('b',))
411
        k2 = stuple(stuple(stuple('c',), stuple('d',)),
412
                    stuple('b',))
4679.8.11 by John Arbash Meinel
Handle the case where the recursive call ends up returning NotImplemented.
413
        # This requires comparing a StaticTuple to a 'string', and then
414
        # interpreting that value in the next higher StaticTuple. This used to
415
        # generate a PyErr_BadIternalCall. We now fall back to *something*.
6715.1.9 by Martin
Adapt StaticTuple tests to change in comparison semantics on Python 3
416
        self.assertCompareNoRelation(k1, k2, mismatched_types=True)
4679.8.11 by John Arbash Meinel
Handle the case where the recursive call ends up returning NotImplemented.
417
4679.3.18 by John Arbash Meinel
Copy the hash and richcompare implementations, and add some tests.
418
    def test_hash(self):
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
419
        k = self.module.StaticTuple('foo')
4679.3.18 by John Arbash Meinel
Copy the hash and richcompare implementations, and add some tests.
420
        self.assertEqual(hash(k), hash(('foo',)))
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
421
        k = self.module.StaticTuple('foo', 'bar', 'baz', 'bing')
4679.3.18 by John Arbash Meinel
Copy the hash and richcompare implementations, and add some tests.
422
        as_tuple = ('foo', 'bar', 'baz', 'bing')
423
        self.assertEqual(hash(k), hash(as_tuple))
424
        x = {k: 'foo'}
425
        # Because k == , it replaces the slot, rather than having both
426
        # present in the dict.
427
        self.assertEqual('foo', x[as_tuple])
428
        x[as_tuple] = 'bar'
429
        self.assertEqual({as_tuple: 'bar'}, x)
430
4679.3.42 by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects.
431
        k2 = self.module.StaticTuple(k)
432
        as_tuple2 = (('foo', 'bar', 'baz', 'bing'),)
433
        self.assertEqual(hash(k2), hash(as_tuple2))
434
4759.2.9 by John Arbash Meinel
Implement support for lots of types.
435
        k3 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True,
436
                                     k)
437
        as_tuple3 = ('foo', 1, None, u'\xb5', 1.2, 2**65, True, k)
4759.2.8 by John Arbash Meinel
Set up a test suite for hash() and richcompare against lots of acceptable types.
438
        self.assertEqual(hash(as_tuple3), hash(k3))
439
4679.3.19 by John Arbash Meinel
implement slicing as a tuple thunk.
440
    def test_slice(self):
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
441
        k = self.module.StaticTuple('foo', 'bar', 'baz', 'bing')
4679.3.19 by John Arbash Meinel
implement slicing as a tuple thunk.
442
        self.assertEqual(('foo', 'bar'), k[:2])
443
        self.assertEqual(('baz',), k[2:-1])
6715.1.2 by Martin
Update _static_tuple tests so all pass bar mixed type comparisons
444
        self.assertEqual(('foo', 'baz',), k[::2])
445
        self.assertRaises(TypeError, k.__getitem__, 'not_slice')
4679.3.19 by John Arbash Meinel
implement slicing as a tuple thunk.
446
4679.3.20 by John Arbash Meinel
Implement tp_traverse, and add a soft dependency on meliae to test it.
447
    def test_referents(self):
448
        # We implement tp_traverse so that things like 'meliae' can measure the
449
        # amount of referenced memory. Unfortunately gc.get_referents() first
4679.5.5 by John Arbash Meinel
Review feedback from Andrew Bennetts.
450
        # checks the IS_GC flag before it traverses anything. We could write a
451
        # helper func, but that won't work for the generic implementation...
6091.2.2 by Max Bowsher
Per jam's review comments, get rid of features.meliae_feature, which is new in
452
        self.requireFeature(features.meliae)
4679.3.29 by John Arbash Meinel
Start work on implementing a Key.intern() function.
453
        from meliae import scanner
4679.3.20 by John Arbash Meinel
Implement tp_traverse, and add a soft dependency on meliae to test it.
454
        strs = ['foo', 'bar', 'baz', 'bing']
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
455
        k = self.module.StaticTuple(*strs)
4679.3.45 by John Arbash Meinel
Do some work to handle comparison to object that aren't tuples or strings.
456
        if self.module is _static_tuple_py:
4679.3.78 by John Arbash Meinel
Change the pure-python version of StaticTuple
457
            refs = strs + [self.module.StaticTuple]
4679.3.29 by John Arbash Meinel
Start work on implementing a Key.intern() function.
458
        else:
4679.3.78 by John Arbash Meinel
Change the pure-python version of StaticTuple
459
            refs = strs
460
        self.assertEqual(sorted(refs), sorted(scanner.get_referents(k)))
4679.3.20 by John Arbash Meinel
Implement tp_traverse, and add a soft dependency on meliae to test it.
461
4679.3.45 by John Arbash Meinel
Do some work to handle comparison to object that aren't tuples or strings.
462
    def test_nested_referents(self):
6091.2.2 by Max Bowsher
Per jam's review comments, get rid of features.meliae_feature, which is new in
463
        self.requireFeature(features.meliae)
4679.3.78 by John Arbash Meinel
Change the pure-python version of StaticTuple
464
        from meliae import scanner
465
        strs = ['foo', 'bar', 'baz', 'bing']
466
        k1 = self.module.StaticTuple(*strs[:2])
467
        k2 = self.module.StaticTuple(*strs[2:])
468
        k3 = self.module.StaticTuple(k1, k2)
469
        refs = [k1, k2]
470
        if self.module is _static_tuple_py:
471
            refs.append(self.module.StaticTuple)
472
        self.assertEqual(sorted(refs),
473
                         sorted(scanner.get_referents(k3)))
4679.3.45 by John Arbash Meinel
Do some work to handle comparison to object that aren't tuples or strings.
474
4679.3.44 by John Arbash Meinel
Special case the empty tuple as a singleton.
475
    def test_empty_is_singleton(self):
476
        key = self.module.StaticTuple()
477
        self.assertIs(key, self.module._empty_tuple)
478
4679.3.29 by John Arbash Meinel
Start work on implementing a Key.intern() function.
479
    def test_intern(self):
480
        unique_str1 = 'unique str ' + osutils.rand_chars(20)
481
        unique_str2 = 'unique str ' + osutils.rand_chars(20)
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
482
        key = self.module.StaticTuple(unique_str1, unique_str2)
4679.3.51 by John Arbash Meinel
Add a _static_tuple_c.pxd file to define the C api to pyrex code.
483
        self.assertFalse(key in self.module._interned_tuples)
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
484
        key2 = self.module.StaticTuple(unique_str1, unique_str2)
4679.3.29 by John Arbash Meinel
Start work on implementing a Key.intern() function.
485
        self.assertEqual(key, key2)
486
        self.assertIsNot(key, key2)
4679.3.33 by John Arbash Meinel
Change Key away from being a PyVarObject.
487
        key3 = key.intern()
488
        self.assertIs(key, key3)
4679.3.51 by John Arbash Meinel
Add a _static_tuple_c.pxd file to define the C api to pyrex code.
489
        self.assertTrue(key in self.module._interned_tuples)
490
        self.assertEqual(key, self.module._interned_tuples[key])
4679.3.33 by John Arbash Meinel
Change Key away from being a PyVarObject.
491
        key2 = key2.intern()
492
        self.assertIs(key, key2)
4679.3.29 by John Arbash Meinel
Start work on implementing a Key.intern() function.
493
4679.3.33 by John Arbash Meinel
Change Key away from being a PyVarObject.
494
    def test__c_intern_handles_refcount(self):
4679.3.40 by John Arbash Meinel
Switch away from the name Key instead start branding as StaticTuple.
495
        if self.module is _static_tuple_py:
7143.15.2 by Jelmer Vernooij
Run autopep8.
496
            return  # Not applicable
4679.3.33 by John Arbash Meinel
Change Key away from being a PyVarObject.
497
        unique_str1 = 'unique str ' + osutils.rand_chars(20)
498
        unique_str2 = 'unique str ' + osutils.rand_chars(20)
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
499
        key = self.module.StaticTuple(unique_str1, unique_str2)
4679.3.81 by John Arbash Meinel
Fix up _simple_set_pyx.pyx to be compatible with pyrex again.
500
        self.assertRefcount(1, key)
4679.3.51 by John Arbash Meinel
Add a _static_tuple_c.pxd file to define the C api to pyrex code.
501
        self.assertFalse(key in self.module._interned_tuples)
4679.3.35 by John Arbash Meinel
Work on making intern() not generate immortal Key objects.
502
        self.assertFalse(key._is_interned())
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
503
        key2 = self.module.StaticTuple(unique_str1, unique_str2)
4679.3.81 by John Arbash Meinel
Fix up _simple_set_pyx.pyx to be compatible with pyrex again.
504
        self.assertRefcount(1, key)
505
        self.assertRefcount(1, key2)
4679.3.33 by John Arbash Meinel
Change Key away from being a PyVarObject.
506
        self.assertEqual(key, key2)
507
        self.assertIsNot(key, key2)
4679.3.30 by John Arbash Meinel
Interning with a regular 'dict' is a tradeoff for bzr.dev of:
508
509
        key3 = key.intern()
510
        self.assertIs(key, key3)
4679.3.51 by John Arbash Meinel
Add a _static_tuple_c.pxd file to define the C api to pyrex code.
511
        self.assertTrue(key in self.module._interned_tuples)
512
        self.assertEqual(key, self.module._interned_tuples[key])
4679.3.81 by John Arbash Meinel
Fix up _simple_set_pyx.pyx to be compatible with pyrex again.
513
        # key and key3, but we 'hide' the one in _interned_tuples
514
        self.assertRefcount(2, key)
4679.3.30 by John Arbash Meinel
Interning with a regular 'dict' is a tradeoff for bzr.dev of:
515
        del key3
4679.3.81 by John Arbash Meinel
Fix up _simple_set_pyx.pyx to be compatible with pyrex again.
516
        self.assertRefcount(1, key)
4679.3.35 by John Arbash Meinel
Work on making intern() not generate immortal Key objects.
517
        self.assertTrue(key._is_interned())
4679.3.81 by John Arbash Meinel
Fix up _simple_set_pyx.pyx to be compatible with pyrex again.
518
        self.assertRefcount(1, key2)
519
        key3 = key2.intern()
520
        # key3 now points to key as well, and *not* to key2
521
        self.assertRefcount(2, key)
522
        self.assertRefcount(1, key2)
523
        self.assertIs(key, key3)
524
        self.assertIsNot(key3, key2)
525
        del key2
526
        del key3
527
        self.assertRefcount(1, key)
4679.3.38 by John Arbash Meinel
add a test that Key.intern() doesn't create an immortal object.
528
529
    def test__c_keys_are_not_immortal(self):
4679.3.40 by John Arbash Meinel
Switch away from the name Key instead start branding as StaticTuple.
530
        if self.module is _static_tuple_py:
7143.15.2 by Jelmer Vernooij
Run autopep8.
531
            return  # Not applicable
4679.3.38 by John Arbash Meinel
add a test that Key.intern() doesn't create an immortal object.
532
        unique_str1 = 'unique str ' + osutils.rand_chars(20)
533
        unique_str2 = 'unique str ' + osutils.rand_chars(20)
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
534
        key = self.module.StaticTuple(unique_str1, unique_str2)
4679.3.51 by John Arbash Meinel
Add a _static_tuple_c.pxd file to define the C api to pyrex code.
535
        self.assertFalse(key in self.module._interned_tuples)
4679.3.81 by John Arbash Meinel
Fix up _simple_set_pyx.pyx to be compatible with pyrex again.
536
        self.assertRefcount(1, key)
4679.3.38 by John Arbash Meinel
add a test that Key.intern() doesn't create an immortal object.
537
        key = key.intern()
4679.3.81 by John Arbash Meinel
Fix up _simple_set_pyx.pyx to be compatible with pyrex again.
538
        self.assertRefcount(1, key)
4679.3.51 by John Arbash Meinel
Add a _static_tuple_c.pxd file to define the C api to pyrex code.
539
        self.assertTrue(key in self.module._interned_tuples)
4679.3.38 by John Arbash Meinel
add a test that Key.intern() doesn't create an immortal object.
540
        self.assertTrue(key._is_interned())
541
        del key
542
        # Create a new entry, which would point to the same location
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
543
        key = self.module.StaticTuple(unique_str1, unique_str2)
4679.3.81 by John Arbash Meinel
Fix up _simple_set_pyx.pyx to be compatible with pyrex again.
544
        self.assertRefcount(1, key)
4679.3.51 by John Arbash Meinel
Add a _static_tuple_c.pxd file to define the C api to pyrex code.
545
        # This old entry in _interned_tuples should be gone
546
        self.assertFalse(key in self.module._interned_tuples)
4679.3.38 by John Arbash Meinel
add a test that Key.intern() doesn't create an immortal object.
547
        self.assertFalse(key._is_interned())
4679.3.47 by John Arbash Meinel
Work out how to expose the C api using the Python PyCObject interface.
548
549
    def test__c_has_C_API(self):
550
        if self.module is _static_tuple_py:
551
            return
552
        self.assertIsNot(None, self.module._C_API)
4679.8.3 by John Arbash Meinel
Expose bzrlib.static_tuple.StaticTuple as a thunk
553
4739.4.1 by John Arbash Meinel
Implement StaticTuple.from_sequence()
554
    def test_from_sequence_tuple(self):
555
        st = self.module.StaticTuple.from_sequence(('foo', 'bar'))
556
        self.assertIsInstance(st, self.module.StaticTuple)
557
        self.assertEqual(('foo', 'bar'), st)
558
559
    def test_from_sequence_str(self):
560
        st = self.module.StaticTuple.from_sequence('foo')
561
        self.assertIsInstance(st, self.module.StaticTuple)
562
        self.assertEqual(('f', 'o', 'o'), st)
563
564
    def test_from_sequence_list(self):
565
        st = self.module.StaticTuple.from_sequence(['foo', 'bar'])
566
        self.assertIsInstance(st, self.module.StaticTuple)
567
        self.assertEqual(('foo', 'bar'), st)
568
569
    def test_from_sequence_static_tuple(self):
570
        st = self.module.StaticTuple('foo', 'bar')
571
        st2 = self.module.StaticTuple.from_sequence(st)
572
        # If the source is a StaticTuple already, we return the exact object
573
        self.assertIs(st, st2)
574
575
    def test_from_sequence_not_sequence(self):
576
        self.assertRaises(TypeError,
577
                          self.module.StaticTuple.from_sequence, object())
4771.2.2 by John Arbash Meinel
Clean up the C code a bit, using a goto.
578
        self.assertRaises(TypeError,
579
                          self.module.StaticTuple.from_sequence, 10)
4739.4.1 by John Arbash Meinel
Implement StaticTuple.from_sequence()
580
581
    def test_from_sequence_incorrect_args(self):
582
        self.assertRaises(TypeError,
583
                          self.module.StaticTuple.from_sequence, object(), 'a')
584
        self.assertRaises(TypeError,
585
                          self.module.StaticTuple.from_sequence, foo='a')
4739.4.2 by John Arbash Meinel
Merge bzr.dev resolve test conflict.
586
4771.2.1 by Matt Nordhoff
_static_tuple_c.StaticTuple.from_sequence() now supports arbitrary iterables (by converting them to tuples first).
587
    def test_from_sequence_iterable(self):
4771.2.2 by John Arbash Meinel
Clean up the C code a bit, using a goto.
588
        st = self.module.StaticTuple.from_sequence(iter(['foo', 'bar']))
589
        self.assertIsInstance(st, self.module.StaticTuple)
590
        self.assertEqual(('foo', 'bar'), st)
591
592
    def test_from_sequence_generator(self):
593
        def generate_tuple():
594
            yield 'foo'
595
            yield 'bar'
596
        st = self.module.StaticTuple.from_sequence(generate_tuple())
4771.2.1 by Matt Nordhoff
_static_tuple_c.StaticTuple.from_sequence() now supports arbitrary iterables (by converting them to tuples first).
597
        self.assertIsInstance(st, self.module.StaticTuple)
598
        self.assertEqual(('foo', 'bar'), st)
599
4759.2.16 by Matt Nordhoff
Add a test
600
    def test_pickle(self):
601
        st = self.module.StaticTuple('foo', 'bar')
6715.1.2 by Martin
Update _static_tuple tests so all pass bar mixed type comparisons
602
        pickled = pickle.dumps(st)
603
        unpickled = pickle.loads(pickled)
4759.2.16 by Matt Nordhoff
Add a test
604
        self.assertEqual(unpickled, st)
4759.2.20 by Matt Nordhoff
Review: Add a Py_INCREF, and test pickling a nested StaticTuple.
605
606
    def test_pickle_empty(self):
4759.2.17 by Matt Nordhoff
Test pickling the empty StaticTuple too
607
        st = self.module.StaticTuple()
6715.1.2 by Martin
Update _static_tuple tests so all pass bar mixed type comparisons
608
        pickled = pickle.dumps(st)
609
        unpickled = pickle.loads(pickled)
4759.2.17 by Matt Nordhoff
Test pickling the empty StaticTuple too
610
        self.assertIs(st, unpickled)
4759.2.16 by Matt Nordhoff
Add a test
611
4759.2.20 by Matt Nordhoff
Review: Add a Py_INCREF, and test pickling a nested StaticTuple.
612
    def test_pickle_nested(self):
613
        st = self.module.StaticTuple('foo', self.module.StaticTuple('bar'))
6715.1.2 by Martin
Update _static_tuple tests so all pass bar mixed type comparisons
614
        pickled = pickle.dumps(st)
615
        unpickled = pickle.loads(pickled)
4759.2.20 by Matt Nordhoff
Review: Add a Py_INCREF, and test pickling a nested StaticTuple.
616
        self.assertEqual(unpickled, st)
617
4679.8.3 by John Arbash Meinel
Expose bzrlib.static_tuple.StaticTuple as a thunk
618
    def test_static_tuple_thunk(self):
619
        # Make sure the right implementation is available from
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
620
        # breezy.static_tuple.StaticTuple.
4679.8.3 by John Arbash Meinel
Expose bzrlib.static_tuple.StaticTuple as a thunk
621
        if self.module is _static_tuple_py:
4913.2.20 by John Arbash Meinel
Change all of the compiled_foo to compiled_foo_feature
622
            if compiled_static_tuple_feature.available():
4679.8.3 by John Arbash Meinel
Expose bzrlib.static_tuple.StaticTuple as a thunk
623
                # We will be using the C version
624
                return
4739.4.2 by John Arbash Meinel
Merge bzr.dev resolve test conflict.
625
        self.assertIs(static_tuple.StaticTuple,
4679.8.3 by John Arbash Meinel
Expose bzrlib.static_tuple.StaticTuple as a thunk
626
                      self.module.StaticTuple)
4668.3.1 by John Arbash Meinel
Fix bug #471193, allow tuples into the CHK code.
627
628
629
class TestEnsureStaticTuple(tests.TestCase):
630
631
    def test_is_static_tuple(self):
632
        st = static_tuple.StaticTuple('foo')
633
        st2 = static_tuple.expect_static_tuple(st)
634
        self.assertIs(st, st2)
635
636
    def test_is_tuple(self):
637
        t = ('foo',)
638
        st = static_tuple.expect_static_tuple(t)
639
        self.assertIsInstance(st, static_tuple.StaticTuple)
640
        self.assertEqual(t, st)
641
642
    def test_flagged_is_static_tuple(self):
643
        debug.debug_flags.add('static_tuple')
644
        st = static_tuple.StaticTuple('foo')
645
        st2 = static_tuple.expect_static_tuple(st)
646
        self.assertIs(st, st2)
647
648
    def test_flagged_is_tuple(self):
649
        debug.debug_flags.add('static_tuple')
650
        t = ('foo',)
651
        self.assertRaises(TypeError, static_tuple.expect_static_tuple, t)