1
# Copyright (C) 2009, 2010, 2011 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
17
17
"""Tests for the StaticTuple type."""
22
23
from bzrlib import (
29
from bzrlib.tests import (
34
33
def load_tests(standard_tests, module, loader):
35
34
"""Parameterize tests for all versions of groupcompress."""
36
global compiled_static_tuple_feature
37
suite, compiled_static_tuple_feature = tests.permute_tests_for_extension(
38
standard_tests, loader, 'bzrlib._static_tuple_py',
39
'bzrlib._static_tuple_c')
36
('python', {'module': _static_tuple_py}),
38
suite = loader.suiteClass()
39
if CompiledStaticTuple.available():
40
from bzrlib import _static_tuple_c
41
scenarios.append(('C', {'module': _static_tuple_c}))
43
# the compiled module isn't available, so we add a failing test
44
class FailWithoutFeature(tests.TestCase):
46
self.requireFeature(CompiledStaticTuple)
47
suite.addTest(loader.loadTestsFromTestCase(FailWithoutFeature))
48
result = tests.multiply_tests(standard_tests, scenarios, suite)
52
class _CompiledStaticTuple(tests.Feature):
56
import bzrlib._static_tuple_c
61
def feature_name(self):
62
return 'bzrlib._static_tuple_c'
64
CompiledStaticTuple = _CompiledStaticTuple()
67
class _Meliae(tests.Feature):
71
from meliae import scanner
76
def feature_name(self):
77
return "Meliae - python memory debugger"
43
82
class TestStaticTuple(tests.TestCase):
63
102
def test_create_bad_args(self):
64
103
args_256 = ['a']*256
66
self.assertRaises(TypeError, self.module.StaticTuple, *args_256)
105
self.assertRaises(ValueError, self.module.StaticTuple, *args_256)
67
106
args_300 = ['a']*300
68
self.assertRaises(TypeError, self.module.StaticTuple, *args_300)
107
self.assertRaises(ValueError, self.module.StaticTuple, *args_300)
70
109
self.assertRaises(TypeError, self.module.StaticTuple, object())
109
148
k = self.module.StaticTuple('foo')
111
150
self.assertEqual(('foo',), t)
112
self.assertIsInstance(t, tuple)
113
self.assertFalse(isinstance(t, self.module.StaticTuple))
114
151
k = self.module.StaticTuple('foo', 'bar')
116
153
self.assertEqual(('foo', 'bar'), t)
117
k2 = self.module.StaticTuple(1, k)
119
self.assertIsInstance(t, tuple)
120
# For pickling to work, we need to keep the sub-items as StaticTuple so
121
# that it knows that they also need to be converted.
122
self.assertIsInstance(t[1], self.module.StaticTuple)
123
self.assertEqual((1, ('foo', 'bar')), t)
125
def test_as_tuples(self):
126
k1 = self.module.StaticTuple('foo', 'bar')
127
t = static_tuple.as_tuples(k1)
128
self.assertIsInstance(t, tuple)
129
self.assertEqual(('foo', 'bar'), t)
130
k2 = self.module.StaticTuple(1, k1)
131
t = static_tuple.as_tuples(k2)
132
self.assertIsInstance(t, tuple)
133
self.assertIsInstance(t[1], tuple)
134
self.assertEqual((1, ('foo', 'bar')), t)
136
t = static_tuple.as_tuples(mixed)
137
self.assertIsInstance(t, tuple)
138
self.assertIsInstance(t[1], tuple)
139
self.assertEqual((1, ('foo', 'bar')), t)
141
155
def test_len(self):
142
156
k = self.module.StaticTuple()
432
446
# amount of referenced memory. Unfortunately gc.get_referents() first
433
447
# checks the IS_GC flag before it traverses anything. We could write a
434
448
# helper func, but that won't work for the generic implementation...
435
self.requireFeature(features.meliae_feature)
449
self.requireFeature(Meliae)
436
450
from meliae import scanner
437
451
strs = ['foo', 'bar', 'baz', 'bing']
438
452
k = self.module.StaticTuple(*strs)
443
457
self.assertEqual(sorted(refs), sorted(scanner.get_referents(k)))
445
459
def test_nested_referents(self):
446
self.requireFeature(features.meliae_feature)
460
self.requireFeature(Meliae)
447
461
from meliae import scanner
448
462
strs = ['foo', 'bar', 'baz', 'bing']
449
463
k1 = self.module.StaticTuple(*strs[:2])
602
616
# Make sure the right implementation is available from
603
617
# bzrlib.static_tuple.StaticTuple.
604
618
if self.module is _static_tuple_py:
605
if compiled_static_tuple_feature.available():
619
if CompiledStaticTuple.available():
606
620
# We will be using the C version
608
622
self.assertIs(static_tuple.StaticTuple,