~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test__static_tuple.py

merge shell-like-tests into description resolving conflicts

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 
23
23
from bzrlib import (
24
24
    _static_tuple_py,
 
25
    debug,
25
26
    errors,
26
27
    osutils,
27
28
    static_tuple,
147
148
        k = self.module.StaticTuple('foo')
148
149
        t = k.as_tuple()
149
150
        self.assertEqual(('foo',), t)
 
151
        self.assertIsInstance(t, tuple)
 
152
        self.assertFalse(isinstance(t, self.module.StaticTuple))
150
153
        k = self.module.StaticTuple('foo', 'bar')
151
154
        t = k.as_tuple()
152
155
        self.assertEqual(('foo', 'bar'), t)
 
156
        k2 = self.module.StaticTuple(1, k)
 
157
        t = k2.as_tuple()
 
158
        self.assertIsInstance(t, tuple)
 
159
        # For pickling to work, we need to keep the sub-items as StaticTuple so
 
160
        # that it knows that they also need to be converted.
 
161
        self.assertIsInstance(t[1], self.module.StaticTuple)
 
162
        self.assertEqual((1, ('foo', 'bar')), t)
 
163
 
 
164
    def test_as_tuples(self):
 
165
        k1 = self.module.StaticTuple('foo', 'bar')
 
166
        t = static_tuple.as_tuples(k1)
 
167
        self.assertIsInstance(t, tuple)
 
168
        self.assertEqual(('foo', 'bar'), t)
 
169
        k2 = self.module.StaticTuple(1, k1)
 
170
        t = static_tuple.as_tuples(k2)
 
171
        self.assertIsInstance(t, tuple)
 
172
        self.assertIsInstance(t[1], tuple)
 
173
        self.assertEqual((1, ('foo', 'bar')), t)
 
174
        mixed = (1, k1)
 
175
        t = static_tuple.as_tuples(mixed)
 
176
        self.assertIsInstance(t, tuple)
 
177
        self.assertIsInstance(t[1], tuple)
 
178
        self.assertEqual((1, ('foo', 'bar')), t)
153
179
 
154
180
    def test_len(self):
155
181
        k = self.module.StaticTuple()
571
597
    def test_from_sequence_not_sequence(self):
572
598
        self.assertRaises(TypeError,
573
599
                          self.module.StaticTuple.from_sequence, object())
 
600
        self.assertRaises(TypeError,
 
601
                          self.module.StaticTuple.from_sequence, 10)
574
602
 
575
603
    def test_from_sequence_incorrect_args(self):
576
604
        self.assertRaises(TypeError,
578
606
        self.assertRaises(TypeError,
579
607
                          self.module.StaticTuple.from_sequence, foo='a')
580
608
 
 
609
    def test_from_sequence_iterable(self):
 
610
        st = self.module.StaticTuple.from_sequence(iter(['foo', 'bar']))
 
611
        self.assertIsInstance(st, self.module.StaticTuple)
 
612
        self.assertEqual(('foo', 'bar'), st)
 
613
 
 
614
    def test_from_sequence_generator(self):
 
615
        def generate_tuple():
 
616
            yield 'foo'
 
617
            yield 'bar'
 
618
        st = self.module.StaticTuple.from_sequence(generate_tuple())
 
619
        self.assertIsInstance(st, self.module.StaticTuple)
 
620
        self.assertEqual(('foo', 'bar'), st)
 
621
 
581
622
    def test_pickle(self):
582
623
        st = self.module.StaticTuple('foo', 'bar')
583
624
        pickled = cPickle.dumps(st)
605
646
                return
606
647
        self.assertIs(static_tuple.StaticTuple,
607
648
                      self.module.StaticTuple)
 
649
 
 
650
 
 
651
class TestEnsureStaticTuple(tests.TestCase):
 
652
 
 
653
    def test_is_static_tuple(self):
 
654
        st = static_tuple.StaticTuple('foo')
 
655
        st2 = static_tuple.expect_static_tuple(st)
 
656
        self.assertIs(st, st2)
 
657
 
 
658
    def test_is_tuple(self):
 
659
        t = ('foo',)
 
660
        st = static_tuple.expect_static_tuple(t)
 
661
        self.assertIsInstance(st, static_tuple.StaticTuple)
 
662
        self.assertEqual(t, st)
 
663
 
 
664
    def test_flagged_is_static_tuple(self):
 
665
        debug.debug_flags.add('static_tuple')
 
666
        st = static_tuple.StaticTuple('foo')
 
667
        st2 = static_tuple.expect_static_tuple(st)
 
668
        self.assertIs(st, st2)
 
669
 
 
670
    def test_flagged_is_tuple(self):
 
671
        debug.debug_flags.add('static_tuple')
 
672
        t = ('foo',)
 
673
        self.assertRaises(TypeError, static_tuple.expect_static_tuple, t)