1
# Copyright (C) 2009 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Tests for _rio_*."""
25
def load_tests(standard_tests, module, loader):
26
# parameterize all tests in this module
27
suite = loader.suiteClass()
28
import bzrlib._rio_py as py_module
29
scenarios = [('python', {'module': py_module})]
30
if CompiledRioFeature.available():
31
import bzrlib._rio_pyx as c_module
32
scenarios.append(('C', {'module': c_module}))
34
# the compiled module isn't available, so we add a failing test
35
class FailWithoutFeature(tests.TestCase):
37
self.requireFeature(CompiledRioFeature)
38
suite.addTest(loader.loadTestsFromTestCase(FailWithoutFeature))
39
tests.multiply_tests(standard_tests, scenarios, suite)
43
class _CompiledRioFeature(tests.Feature):
47
import bzrlib._rio_pyx
52
def feature_name(self):
53
return 'bzrlib._rio_pyx'
55
CompiledRioFeature = _CompiledRioFeature()
58
class TestValidTag(tests.TestCase):
60
module = None # Filled in by test parameterization
63
self.assertTrue(self.module._valid_tag("foo"))
65
def test_no_spaces(self):
66
self.assertFalse(self.module._valid_tag("foo bla"))
68
def test_numeric(self):
69
self.assertTrue(self.module._valid_tag("3foo423"))
71
def test_no_colon(self):
72
self.assertFalse(self.module._valid_tag("foo:bla"))
74
def test_type_error(self):
75
self.assertRaises(TypeError, self.module._valid_tag, 423)
78
self.assertFalse(self.module._valid_tag(""))
80
def test_unicode(self):
81
self.assertRaises(TypeError, self.module._valid_tag, u"foo")
83
def test_non_ascii_char(self):
84
self.assertFalse(self.module._valid_tag("\xb5"))
87
class TestReadUTF8Stanza(tests.TestCase):
89
module = None # Filled in by test parameterization
91
def assertReadStanza(self, result, line_iter):
92
s = self.module._read_stanza_utf8(line_iter)
93
self.assertEquals(result, s)
95
for tag, value in s.iter_pairs():
96
self.assertIsInstance(tag, str)
97
self.assertIsInstance(value, unicode)
99
def assertReadStanzaRaises(self, exception, line_iter):
100
self.assertRaises(exception, self.module._read_stanza_utf8, line_iter)
102
def test_no_string(self):
103
self.assertReadStanzaRaises(TypeError, [21323])
105
def test_empty(self):
106
self.assertReadStanza(None, [])
109
self.assertReadStanza(None, [""])
111
def test_simple(self):
112
self.assertReadStanza(rio.Stanza(foo="bar"), ["foo: bar\n", ""])
114
def test_multi_line(self):
115
self.assertReadStanza(rio.Stanza(foo="bar\nbla"),
116
["foo: bar\n", "\tbla\n"])
118
def test_repeated(self):
122
self.assertReadStanza(s, ["foo: bar\n", "foo: foo\n"])
124
def test_invalid_early_colon(self):
125
self.assertReadStanzaRaises(ValueError, ["f:oo: bar\n"])
127
def test_invalid_tag(self):
128
self.assertReadStanzaRaises(ValueError, ["f%oo: bar\n"])
130
def test_continuation_too_early(self):
131
self.assertReadStanzaRaises(ValueError, ["\tbar\n"])
133
def test_large(self):
135
self.assertReadStanza(rio.Stanza(foo=value),
136
["foo: %s\n" % value])
138
def test_non_ascii_char(self):
139
self.assertReadStanza(rio.Stanza(foo=u"n\xe5me"),
140
[u"foo: n\xe5me\n".encode("utf-8")])
143
class TestReadUnicodeStanza(tests.TestCase):
145
module = None # Filled in by test parameterization
147
def assertReadStanza(self, result, line_iter):
148
s = self.module._read_stanza_unicode(line_iter)
149
self.assertEquals(result, s)
151
for tag, value in s.iter_pairs():
152
self.assertIsInstance(tag, str)
153
self.assertIsInstance(value, unicode)
155
def assertReadStanzaRaises(self, exception, line_iter):
156
self.assertRaises(exception, self.module._read_stanza_unicode,
159
def test_no_string(self):
160
self.assertReadStanzaRaises(TypeError, [21323])
162
def test_empty(self):
163
self.assertReadStanza(None, [])
166
self.assertReadStanza(None, [u""])
168
def test_simple(self):
169
self.assertReadStanza(rio.Stanza(foo="bar"), [u"foo: bar\n", u""])
171
def test_multi_line(self):
172
self.assertReadStanza(rio.Stanza(foo="bar\nbla"),
173
[u"foo: bar\n", u"\tbla\n"])
175
def test_repeated(self):
179
self.assertReadStanza(s, [u"foo: bar\n", u"foo: foo\n"])
181
def test_invalid_early_colon(self):
182
self.assertReadStanzaRaises(ValueError, [u"f:oo: bar\n"])
184
def test_invalid_tag(self):
185
self.assertReadStanzaRaises(ValueError, [u"f%oo: bar\n"])
187
def test_continuation_too_early(self):
188
self.assertReadStanzaRaises(ValueError, [u"\tbar\n"])
190
def test_large(self):
191
value = u"bla" * 9000
192
self.assertReadStanza(rio.Stanza(foo=value),
193
[u"foo: %s\n" % value])
195
def test_non_ascii_char(self):
196
self.assertReadStanza(rio.Stanza(foo=u"n\xe5me"), [u"foo: n\xe5me\n"])