/brz/remove-bazaar

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

« back to all changes in this revision

Viewing changes to bzrlib/tests/test__static_tuple.py

  • Committer: Andrew Bennetts
  • Date: 2009-10-21 11:13:40 UTC
  • mto: This revision was merged to the branch mainline in revision 4762.
  • Revision ID: andrew.bennetts@canonical.com-20091021111340-w7x4d5yf83qwjncc
Add test that WSGI glue allows request handlers to access paths above that request's. backing transport, so long as it is within the WSGI app's backing transport.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2009 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
16
16
 
17
17
"""Tests for the StaticTuple type."""
18
18
 
19
 
import cPickle
20
19
import gc
21
20
import sys
22
21
 
23
22
from bzrlib import (
24
23
    _static_tuple_py,
25
 
    debug,
26
24
    errors,
27
25
    osutils,
28
26
    static_tuple,
32
30
 
33
31
def load_tests(standard_tests, module, loader):
34
32
    """Parameterize tests for all versions of groupcompress."""
35
 
    global compiled_static_tuple_feature
36
 
    suite, compiled_static_tuple_feature = tests.permute_tests_for_extension(
37
 
        standard_tests, loader, 'bzrlib._static_tuple_py',
38
 
        'bzrlib._static_tuple_c')
39
 
    return suite
 
33
    scenarios = [
 
34
        ('python', {'module': _static_tuple_py}),
 
35
    ]
 
36
    suite = loader.suiteClass()
 
37
    if CompiledStaticTuple.available():
 
38
        from bzrlib import _static_tuple_c
 
39
        scenarios.append(('C', {'module': _static_tuple_c}))
 
40
    else:
 
41
        # the compiled module isn't available, so we add a failing test
 
42
        class FailWithoutFeature(tests.TestCase):
 
43
            def test_fail(self):
 
44
                self.requireFeature(CompiledStaticTuple)
 
45
        suite.addTest(loader.loadTestsFromTestCase(FailWithoutFeature))
 
46
    result = tests.multiply_tests(standard_tests, scenarios, suite)
 
47
    return result
 
48
 
 
49
 
 
50
class _CompiledStaticTuple(tests.Feature):
 
51
 
 
52
    def _probe(self):
 
53
        try:
 
54
            import bzrlib._static_tuple_c
 
55
        except ImportError:
 
56
            return False
 
57
        return True
 
58
 
 
59
    def feature_name(self):
 
60
        return 'bzrlib._static_tuple_c'
 
61
 
 
62
CompiledStaticTuple = _CompiledStaticTuple()
40
63
 
41
64
 
42
65
class _Meliae(tests.Feature):
77
100
    def test_create_bad_args(self):
78
101
        args_256 = ['a']*256
79
102
        # too many args
80
 
        self.assertRaises(TypeError, self.module.StaticTuple, *args_256)
 
103
        self.assertRaises(ValueError, self.module.StaticTuple, *args_256)
81
104
        args_300 = ['a']*300
82
 
        self.assertRaises(TypeError, self.module.StaticTuple, *args_300)
 
105
        self.assertRaises(ValueError, self.module.StaticTuple, *args_300)
83
106
        # not a string
84
 
        self.assertRaises(TypeError, self.module.StaticTuple, object())
85
 
 
86
 
    def test_concat(self):
87
 
        st1 = self.module.StaticTuple('foo')
88
 
        st2 = self.module.StaticTuple('bar')
89
 
        st3 = self.module.StaticTuple('foo', 'bar')
90
 
        st4 = st1 + st2
91
 
        self.assertEqual(st3, st4)
92
 
        self.assertIsInstance(st4, self.module.StaticTuple)
93
 
 
94
 
    def test_concat_with_tuple(self):
95
 
        st1 = self.module.StaticTuple('foo')
96
 
        t2 = ('bar',)
97
 
        st3 = self.module.StaticTuple('foo', 'bar')
98
 
        st4 = self.module.StaticTuple('bar', 'foo')
99
 
        st5 = st1 + t2
100
 
        st6 = t2 + st1
101
 
        self.assertEqual(st3, st5)
102
 
        self.assertIsInstance(st5, self.module.StaticTuple)
103
 
        self.assertEqual(st4, st6)
104
 
        if self.module is _static_tuple_py:
105
 
            # _static_tuple_py has StaticTuple(tuple), so tuple thinks it
106
 
            # already knows how to concatenate, as such we can't "inject" our
107
 
            # own concatenation...
108
 
            self.assertIsInstance(st6, tuple)
109
 
        else:
110
 
            self.assertIsInstance(st6, self.module.StaticTuple)
111
 
 
112
 
    def test_concat_with_bad_tuple(self):
113
 
        st1 = self.module.StaticTuple('foo')
114
 
        t2 = (object(),)
115
 
        # Using st1.__add__ doesn't give the same results as doing the '+' form
116
 
        self.assertRaises(TypeError, lambda: st1 + t2)
117
 
 
118
 
    def test_concat_with_non_tuple(self):
119
 
        st1 = self.module.StaticTuple('foo')
120
 
        self.assertRaises(TypeError, lambda: st1 + 10)
 
107
        self.assertRaises(TypeError, self.module.StaticTuple, 10)
121
108
        
122
109
    def test_as_tuple(self):
123
110
        k = self.module.StaticTuple('foo')
124
111
        t = k.as_tuple()
125
112
        self.assertEqual(('foo',), t)
126
 
        self.assertIsInstance(t, tuple)
127
 
        self.assertFalse(isinstance(t, self.module.StaticTuple))
128
113
        k = self.module.StaticTuple('foo', 'bar')
129
114
        t = k.as_tuple()
130
115
        self.assertEqual(('foo', 'bar'), t)
131
 
        k2 = self.module.StaticTuple(1, k)
132
 
        t = k2.as_tuple()
133
 
        self.assertIsInstance(t, tuple)
134
 
        # For pickling to work, we need to keep the sub-items as StaticTuple so
135
 
        # that it knows that they also need to be converted.
136
 
        self.assertIsInstance(t[1], self.module.StaticTuple)
137
 
        self.assertEqual((1, ('foo', 'bar')), t)
138
 
 
139
 
    def test_as_tuples(self):
140
 
        k1 = self.module.StaticTuple('foo', 'bar')
141
 
        t = static_tuple.as_tuples(k1)
142
 
        self.assertIsInstance(t, tuple)
143
 
        self.assertEqual(('foo', 'bar'), t)
144
 
        k2 = self.module.StaticTuple(1, k1)
145
 
        t = static_tuple.as_tuples(k2)
146
 
        self.assertIsInstance(t, tuple)
147
 
        self.assertIsInstance(t[1], tuple)
148
 
        self.assertEqual((1, ('foo', 'bar')), t)
149
 
        mixed = (1, k1)
150
 
        t = static_tuple.as_tuples(mixed)
151
 
        self.assertIsInstance(t, tuple)
152
 
        self.assertIsInstance(t[1], tuple)
153
 
        self.assertEqual((1, ('foo', 'bar')), t)
154
116
 
155
117
    def test_len(self):
156
118
        k = self.module.StaticTuple()
215
177
        self.assertFalse(k1 < k2)
216
178
        self.assertFalse(k1 > k2)
217
179
 
218
 
    def test_holds_None(self):
219
 
        k1 = self.module.StaticTuple(None)
220
 
        # You cannot subclass None anyway
221
 
 
222
 
    def test_holds_int(self):
223
 
        k1 = self.module.StaticTuple(1)
224
 
        class subint(int):
225
 
            pass
226
 
        # But not a subclass, because subint could introduce refcycles
227
 
        self.assertRaises(TypeError, self.module.StaticTuple, subint(2))
228
 
 
229
 
    def test_holds_long(self):
230
 
        k1 = self.module.StaticTuple(2L**65)
231
 
        class sublong(long):
232
 
            pass
233
 
        # But not a subclass
234
 
        self.assertRaises(TypeError, self.module.StaticTuple, sublong(1))
235
 
 
236
 
    def test_holds_float(self):
237
 
        k1 = self.module.StaticTuple(1.2)
238
 
        class subfloat(float):
239
 
            pass
240
 
        self.assertRaises(TypeError, self.module.StaticTuple, subfloat(1.5))
241
 
 
242
 
    def test_holds_str(self):
243
 
        k1 = self.module.StaticTuple('astring')
244
 
        class substr(str):
245
 
            pass
246
 
        self.assertRaises(TypeError, self.module.StaticTuple, substr('a'))
247
 
 
248
 
    def test_holds_unicode(self):
249
 
        k1 = self.module.StaticTuple(u'\xb5')
250
 
        class subunicode(unicode):
251
 
            pass
252
 
        self.assertRaises(TypeError, self.module.StaticTuple,
253
 
                          subunicode(u'\xb5'))
254
 
 
255
 
    def test_hold_bool(self):
256
 
        k1 = self.module.StaticTuple(True)
257
 
        k2 = self.module.StaticTuple(False)
258
 
        # Cannot subclass bool
259
 
 
260
180
    def test_compare_same_obj(self):
261
181
        k1 = self.module.StaticTuple('foo', 'bar')
262
182
        self.assertCompareEqual(k1, k1)
263
183
        k2 = self.module.StaticTuple(k1, k1)
264
184
        self.assertCompareEqual(k2, k2)
265
 
        k3 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True,
266
 
                                     k1)
267
 
        self.assertCompareEqual(k3, k3)
268
185
 
269
186
    def test_compare_equivalent_obj(self):
270
187
        k1 = self.module.StaticTuple('foo', 'bar')
273
190
        k3 = self.module.StaticTuple(k1, k2)
274
191
        k4 = self.module.StaticTuple(k2, k1)
275
192
        self.assertCompareEqual(k1, k2)
276
 
        k5 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True,
277
 
                                     k1)
278
 
        k6 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True,
279
 
                                     k1)
280
 
        self.assertCompareEqual(k5, k6)
281
 
        k7 = self.module.StaticTuple(None)
282
 
        k8 = self.module.StaticTuple(None)
283
 
        self.assertCompareEqual(k7, k8)
284
193
 
285
194
    def test_compare_similar_obj(self):
286
195
        k1 = self.module.StaticTuple('foo' + ' bar', 'bar' + ' baz')
331
240
        k3 = self.module.StaticTuple(k1, k2)
332
241
        k4 = self.module.StaticTuple(k2, k1)
333
242
        self.assertCompareDifferent(k3, k4)
334
 
        k5 = self.module.StaticTuple(1)
335
 
        k6 = self.module.StaticTuple(2)
336
 
        self.assertCompareDifferent(k5, k6)
337
 
        k7 = self.module.StaticTuple(1.2)
338
 
        k8 = self.module.StaticTuple(2.4)
339
 
        self.assertCompareDifferent(k7, k8)
340
 
        k9 = self.module.StaticTuple(u's\xb5')
341
 
        k10 = self.module.StaticTuple(u's\xe5')
342
 
        self.assertCompareDifferent(k9, k10)
343
243
 
344
244
    def test_compare_some_different(self):
345
245
        k1 = self.module.StaticTuple('foo', 'bar')
348
248
        k3 = self.module.StaticTuple(k1, k1)
349
249
        k4 = self.module.StaticTuple(k1, k2)
350
250
        self.assertCompareDifferent(k3, k4)
351
 
        k5 = self.module.StaticTuple('foo', None)
352
 
        self.assertCompareDifferent(k5, k1)
353
 
        self.assertCompareDifferent(k5, k2)
354
251
 
355
252
    def test_compare_diff_width(self):
356
253
        k1 = self.module.StaticTuple('foo')
360
257
        k4 = self.module.StaticTuple(k1, k2)
361
258
        self.assertCompareDifferent(k3, k4)
362
259
 
363
 
    def test_compare_different_types(self):
364
 
        k1 = self.module.StaticTuple('foo', 'bar')
365
 
        k2 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True,
366
 
                                     k1)
367
 
        self.assertCompareNoRelation(k1, k2)
368
 
        k3 = self.module.StaticTuple('foo')
369
 
        self.assertCompareDifferent(k3, k1)
370
 
        k4 = self.module.StaticTuple(None)
371
 
        self.assertCompareDifferent(k4, k1)
372
 
        k5 = self.module.StaticTuple(1)
373
 
        self.assertCompareNoRelation(k1, k5)
374
 
 
375
260
    def test_compare_to_tuples(self):
376
261
        k1 = self.module.StaticTuple('foo')
377
262
        self.assertCompareEqual(k1, ('foo',))
421
306
        as_tuple2 = (('foo', 'bar', 'baz', 'bing'),)
422
307
        self.assertEqual(hash(k2), hash(as_tuple2))
423
308
 
424
 
        k3 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True,
425
 
                                     k)
426
 
        as_tuple3 = ('foo', 1, None, u'\xb5', 1.2, 2**65, True, k)
427
 
        self.assertEqual(hash(as_tuple3), hash(k3))
428
 
 
429
309
    def test_slice(self):
430
310
        k = self.module.StaticTuple('foo', 'bar', 'baz', 'bing')
431
311
        self.assertEqual(('foo', 'bar'), k[:2])
572
452
    def test_from_sequence_not_sequence(self):
573
453
        self.assertRaises(TypeError,
574
454
                          self.module.StaticTuple.from_sequence, object())
575
 
        self.assertRaises(TypeError,
576
 
                          self.module.StaticTuple.from_sequence, 10)
577
455
 
578
456
    def test_from_sequence_incorrect_args(self):
579
457
        self.assertRaises(TypeError,
581
459
        self.assertRaises(TypeError,
582
460
                          self.module.StaticTuple.from_sequence, foo='a')
583
461
 
584
 
    def test_from_sequence_iterable(self):
585
 
        st = self.module.StaticTuple.from_sequence(iter(['foo', 'bar']))
586
 
        self.assertIsInstance(st, self.module.StaticTuple)
587
 
        self.assertEqual(('foo', 'bar'), st)
588
 
 
589
 
    def test_from_sequence_generator(self):
590
 
        def generate_tuple():
591
 
            yield 'foo'
592
 
            yield 'bar'
593
 
        st = self.module.StaticTuple.from_sequence(generate_tuple())
594
 
        self.assertIsInstance(st, self.module.StaticTuple)
595
 
        self.assertEqual(('foo', 'bar'), st)
596
 
 
597
 
    def test_pickle(self):
598
 
        st = self.module.StaticTuple('foo', 'bar')
599
 
        pickled = cPickle.dumps(st)
600
 
        unpickled = cPickle.loads(pickled)
601
 
        self.assertEqual(unpickled, st)
602
 
 
603
 
    def test_pickle_empty(self):
604
 
        st = self.module.StaticTuple()
605
 
        pickled = cPickle.dumps(st)
606
 
        unpickled = cPickle.loads(pickled)
607
 
        self.assertIs(st, unpickled)
608
 
 
609
 
    def test_pickle_nested(self):
610
 
        st = self.module.StaticTuple('foo', self.module.StaticTuple('bar'))
611
 
        pickled = cPickle.dumps(st)
612
 
        unpickled = cPickle.loads(pickled)
613
 
        self.assertEqual(unpickled, st)
614
 
 
615
462
    def test_static_tuple_thunk(self):
616
463
        # Make sure the right implementation is available from
617
464
        # bzrlib.static_tuple.StaticTuple.
618
465
        if self.module is _static_tuple_py:
619
 
            if compiled_static_tuple_feature.available():
 
466
            if CompiledStaticTuple.available():
620
467
                # We will be using the C version
621
468
                return
622
469
        self.assertIs(static_tuple.StaticTuple,
623
470
                      self.module.StaticTuple)
624
 
 
625
 
 
626
 
class TestEnsureStaticTuple(tests.TestCase):
627
 
 
628
 
    def test_is_static_tuple(self):
629
 
        st = static_tuple.StaticTuple('foo')
630
 
        st2 = static_tuple.expect_static_tuple(st)
631
 
        self.assertIs(st, st2)
632
 
 
633
 
    def test_is_tuple(self):
634
 
        t = ('foo',)
635
 
        st = static_tuple.expect_static_tuple(t)
636
 
        self.assertIsInstance(st, static_tuple.StaticTuple)
637
 
        self.assertEqual(t, st)
638
 
 
639
 
    def test_flagged_is_static_tuple(self):
640
 
        debug.debug_flags.add('static_tuple')
641
 
        st = static_tuple.StaticTuple('foo')
642
 
        st2 = static_tuple.expect_static_tuple(st)
643
 
        self.assertIs(st, st2)
644
 
 
645
 
    def test_flagged_is_tuple(self):
646
 
        debug.debug_flags.add('static_tuple')
647
 
        t = ('foo',)
648
 
        self.assertRaises(TypeError, static_tuple.expect_static_tuple, t)