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