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 compiled_rio_feature.available():
31
scenarios.append(('C', {'module': compiled_rio_feature.module}))
33
# the compiled module isn't available, so we add a failing test
34
class FailWithoutFeature(tests.TestCase):
36
self.requireFeature(compiled_rio_feature)
37
suite.addTest(loader.loadTestsFromTestCase(FailWithoutFeature))
38
tests.multiply_tests(standard_tests, scenarios, suite)
42
compiled_rio_feature = tests.ModuleAvailableFeature('bzrlib._rio_pyx')
45
class TestValidTag(tests.TestCase):
47
module = None # Filled in by test parameterization
50
self.assertTrue(self.module._valid_tag("foo"))
52
def test_no_spaces(self):
53
self.assertFalse(self.module._valid_tag("foo bla"))
55
def test_numeric(self):
56
self.assertTrue(self.module._valid_tag("3foo423"))
58
def test_no_colon(self):
59
self.assertFalse(self.module._valid_tag("foo:bla"))
61
def test_type_error(self):
62
self.assertRaises(TypeError, self.module._valid_tag, 423)
65
self.assertFalse(self.module._valid_tag(""))
67
def test_unicode(self):
68
self.assertRaises(TypeError, self.module._valid_tag, u"foo")
70
def test_non_ascii_char(self):
71
self.assertFalse(self.module._valid_tag("\xb5"))
74
class TestReadUTF8Stanza(tests.TestCase):
76
module = None # Filled in by test parameterization
78
def assertReadStanza(self, result, line_iter):
79
s = self.module._read_stanza_utf8(line_iter)
80
self.assertEquals(result, s)
82
for tag, value in s.iter_pairs():
83
self.assertIsInstance(tag, str)
84
self.assertIsInstance(value, unicode)
86
def assertReadStanzaRaises(self, exception, line_iter):
87
self.assertRaises(exception, self.module._read_stanza_utf8, line_iter)
89
def test_no_string(self):
90
self.assertReadStanzaRaises(TypeError, [21323])
93
self.assertReadStanza(None, [])
96
self.assertReadStanza(None, [""])
98
def test_simple(self):
99
self.assertReadStanza(rio.Stanza(foo="bar"), ["foo: bar\n", ""])
101
def test_multi_line(self):
102
self.assertReadStanza(rio.Stanza(foo="bar\nbla"),
103
["foo: bar\n", "\tbla\n"])
105
def test_repeated(self):
109
self.assertReadStanza(s, ["foo: bar\n", "foo: foo\n"])
111
def test_invalid_early_colon(self):
112
self.assertReadStanzaRaises(ValueError, ["f:oo: bar\n"])
114
def test_invalid_tag(self):
115
self.assertReadStanzaRaises(ValueError, ["f%oo: bar\n"])
117
def test_continuation_too_early(self):
118
self.assertReadStanzaRaises(ValueError, ["\tbar\n"])
120
def test_large(self):
122
self.assertReadStanza(rio.Stanza(foo=value),
123
["foo: %s\n" % value])
125
def test_non_ascii_char(self):
126
self.assertReadStanza(rio.Stanza(foo=u"n\xe5me"),
127
[u"foo: n\xe5me\n".encode("utf-8")])
130
class TestReadUnicodeStanza(tests.TestCase):
132
module = None # Filled in by test parameterization
134
def assertReadStanza(self, result, line_iter):
135
s = self.module._read_stanza_unicode(line_iter)
136
self.assertEquals(result, s)
138
for tag, value in s.iter_pairs():
139
self.assertIsInstance(tag, str)
140
self.assertIsInstance(value, unicode)
142
def assertReadStanzaRaises(self, exception, line_iter):
143
self.assertRaises(exception, self.module._read_stanza_unicode,
146
def test_no_string(self):
147
self.assertReadStanzaRaises(TypeError, [21323])
149
def test_empty(self):
150
self.assertReadStanza(None, [])
153
self.assertReadStanza(None, [u""])
155
def test_simple(self):
156
self.assertReadStanza(rio.Stanza(foo="bar"), [u"foo: bar\n", u""])
158
def test_multi_line(self):
159
self.assertReadStanza(rio.Stanza(foo="bar\nbla"),
160
[u"foo: bar\n", u"\tbla\n"])
162
def test_repeated(self):
166
self.assertReadStanza(s, [u"foo: bar\n", u"foo: foo\n"])
168
def test_invalid_early_colon(self):
169
self.assertReadStanzaRaises(ValueError, [u"f:oo: bar\n"])
171
def test_invalid_tag(self):
172
self.assertReadStanzaRaises(ValueError, [u"f%oo: bar\n"])
174
def test_continuation_too_early(self):
175
self.assertReadStanzaRaises(ValueError, [u"\tbar\n"])
177
def test_large(self):
178
value = u"bla" * 9000
179
self.assertReadStanza(rio.Stanza(foo=value),
180
[u"foo: %s\n" % value])
182
def test_non_ascii_char(self):
183
self.assertReadStanza(rio.Stanza(foo=u"n\xe5me"), [u"foo: n\xe5me\n"])