~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test__static_tuple.py

terminal_width can now returns None.

* bzrlib/win32utils.py:
(get_console_size): Fix typo in comment.

* bzrlib/ui/text.py:
(TextProgressView._show_line): Handle the no terminal present case.

* bzrlib/tests/test_osutils.py:
(TestTerminalWidth): Update tests.

* bzrlib/tests/blackbox/test_too_much.py:
Fix some imports.
(OldTests.test_bzr): Handle the no terminal present case.

* bzrlib/tests/__init__.py:
(VerboseTestResult.report_test_start): Handle the no terminal
present case.

* bzrlib/status.py:
(show_pending_merges): Handle the no terminal present case.
(show_pending_merges.show_log_message): Factor out some
code. Handle the no terminal present case.

* bzrlib/osutils.py:
(terminal_width): Return None if no precise value can be found.

* bzrlib/log.py:
(LineLogFormatter.__init__): Handle the no terminal present case.
(LineLogFormatter.truncate): Accept None as max_len meaning no
truncation.
(LineLogFormatter.log_string): 

* bzrlib/help.py:
(_help_commands_to_text): Handle the no terminal present case.

Show diffs side-by-side

added added

removed removed

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