1
# Copyright (C) 2009, 2010 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
suite, _ = tests.permute_tests_for_extension(standard_tests, loader,
27
'bzrlib._rio_py', 'bzrlib._rio_pyx')
31
class TestValidTag(tests.TestCase):
33
module = None # Filled in by test parameterization
36
self.assertTrue(self.module._valid_tag("foo"))
38
def test_no_spaces(self):
39
self.assertFalse(self.module._valid_tag("foo bla"))
41
def test_numeric(self):
42
self.assertTrue(self.module._valid_tag("3foo423"))
44
def test_no_colon(self):
45
self.assertFalse(self.module._valid_tag("foo:bla"))
47
def test_type_error(self):
48
self.assertRaises(TypeError, self.module._valid_tag, 423)
51
self.assertFalse(self.module._valid_tag(""))
53
def test_unicode(self):
54
self.assertRaises(TypeError, self.module._valid_tag, u"foo")
56
def test_non_ascii_char(self):
57
self.assertFalse(self.module._valid_tag("\xb5"))
60
class TestReadUTF8Stanza(tests.TestCase):
62
module = None # Filled in by test parameterization
64
def assertReadStanza(self, result, line_iter):
65
s = self.module._read_stanza_utf8(line_iter)
66
self.assertEquals(result, s)
68
for tag, value in s.iter_pairs():
69
self.assertIsInstance(tag, str)
70
self.assertIsInstance(value, unicode)
72
def assertReadStanzaRaises(self, exception, line_iter):
73
self.assertRaises(exception, self.module._read_stanza_utf8, line_iter)
75
def test_no_string(self):
76
self.assertReadStanzaRaises(TypeError, [21323])
79
self.assertReadStanza(None, [])
82
self.assertReadStanza(None, [""])
84
def test_simple(self):
85
self.assertReadStanza(rio.Stanza(foo="bar"), ["foo: bar\n", ""])
87
def test_multi_line(self):
88
self.assertReadStanza(rio.Stanza(foo="bar\nbla"),
89
["foo: bar\n", "\tbla\n"])
91
def test_repeated(self):
95
self.assertReadStanza(s, ["foo: bar\n", "foo: foo\n"])
97
def test_invalid_early_colon(self):
98
self.assertReadStanzaRaises(ValueError, ["f:oo: bar\n"])
100
def test_invalid_tag(self):
101
self.assertReadStanzaRaises(ValueError, ["f%oo: bar\n"])
103
def test_continuation_too_early(self):
104
self.assertReadStanzaRaises(ValueError, ["\tbar\n"])
106
def test_large(self):
108
self.assertReadStanza(rio.Stanza(foo=value),
109
["foo: %s\n" % value])
111
def test_non_ascii_char(self):
112
self.assertReadStanza(rio.Stanza(foo=u"n\xe5me"),
113
[u"foo: n\xe5me\n".encode("utf-8")])
116
class TestReadUnicodeStanza(tests.TestCase):
118
module = None # Filled in by test parameterization
120
def assertReadStanza(self, result, line_iter):
121
s = self.module._read_stanza_unicode(line_iter)
122
self.assertEquals(result, s)
124
for tag, value in s.iter_pairs():
125
self.assertIsInstance(tag, str)
126
self.assertIsInstance(value, unicode)
128
def assertReadStanzaRaises(self, exception, line_iter):
129
self.assertRaises(exception, self.module._read_stanza_unicode,
132
def test_no_string(self):
133
self.assertReadStanzaRaises(TypeError, [21323])
135
def test_empty(self):
136
self.assertReadStanza(None, [])
139
self.assertReadStanza(None, [u""])
141
def test_simple(self):
142
self.assertReadStanza(rio.Stanza(foo="bar"), [u"foo: bar\n", u""])
144
def test_multi_line(self):
145
self.assertReadStanza(rio.Stanza(foo="bar\nbla"),
146
[u"foo: bar\n", u"\tbla\n"])
148
def test_repeated(self):
152
self.assertReadStanza(s, [u"foo: bar\n", u"foo: foo\n"])
154
def test_invalid_early_colon(self):
155
self.assertReadStanzaRaises(ValueError, [u"f:oo: bar\n"])
157
def test_invalid_tag(self):
158
self.assertReadStanzaRaises(ValueError, [u"f%oo: bar\n"])
160
def test_continuation_too_early(self):
161
self.assertReadStanzaRaises(ValueError, [u"\tbar\n"])
163
def test_large(self):
164
value = u"bla" * 9000
165
self.assertReadStanza(rio.Stanza(foo=value),
166
[u"foo: %s\n" % value])
168
def test_non_ascii_char(self):
169
self.assertReadStanza(rio.Stanza(foo=u"n\xe5me"), [u"foo: n\xe5me\n"])