1
# Copyright (C) 2009, 2010 Canonical Ltd
1
# Copyright (C) 2009 Canonical Ltd
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
33
32
def load_tests(standard_tests, module, loader):
34
33
"""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')
35
('python', {'module': _static_tuple_py}),
37
suite = loader.suiteClass()
38
if CompiledStaticTuple.available():
39
from bzrlib import _static_tuple_c
40
scenarios.append(('C', {'module': _static_tuple_c}))
42
# the compiled module isn't available, so we add a failing test
43
class FailWithoutFeature(tests.TestCase):
45
self.requireFeature(CompiledStaticTuple)
46
suite.addTest(loader.loadTestsFromTestCase(FailWithoutFeature))
47
result = tests.multiply_tests(standard_tests, scenarios, suite)
51
class _CompiledStaticTuple(tests.Feature):
55
import bzrlib._static_tuple_c
60
def feature_name(self):
61
return 'bzrlib._static_tuple_c'
63
CompiledStaticTuple = _CompiledStaticTuple()
42
66
class _Meliae(tests.Feature):
77
101
def test_create_bad_args(self):
78
102
args_256 = ['a']*256
80
self.assertRaises(TypeError, self.module.StaticTuple, *args_256)
104
self.assertRaises(ValueError, self.module.StaticTuple, *args_256)
81
105
args_300 = ['a']*300
82
self.assertRaises(TypeError, self.module.StaticTuple, *args_300)
106
self.assertRaises(ValueError, self.module.StaticTuple, *args_300)
84
108
self.assertRaises(TypeError, self.module.StaticTuple, object())
123
147
k = self.module.StaticTuple('foo')
125
149
self.assertEqual(('foo',), t)
126
self.assertIsInstance(t, tuple)
127
self.assertFalse(isinstance(t, self.module.StaticTuple))
128
150
k = self.module.StaticTuple('foo', 'bar')
130
152
self.assertEqual(('foo', 'bar'), t)
131
k2 = self.module.StaticTuple(1, k)
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)
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)
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)
155
154
def test_len(self):
156
155
k = self.module.StaticTuple()
616
615
# Make sure the right implementation is available from
617
616
# bzrlib.static_tuple.StaticTuple.
618
617
if self.module is _static_tuple_py:
619
if compiled_static_tuple_feature.available():
618
if CompiledStaticTuple.available():
620
619
# We will be using the C version
622
621
self.assertIs(static_tuple.StaticTuple,
623
622
self.module.StaticTuple)
626
class TestEnsureStaticTuple(tests.TestCase):
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)
633
def test_is_tuple(self):
635
st = static_tuple.expect_static_tuple(t)
636
self.assertIsInstance(st, static_tuple.StaticTuple)
637
self.assertEqual(t, st)
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)
645
def test_flagged_is_tuple(self):
646
debug.debug_flags.add('static_tuple')
648
self.assertRaises(TypeError, static_tuple.expect_static_tuple, t)