1
# Copyright (C) 2009, 2010, 2016 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(loader, standard_tests, pattern):
26
suite, _ = tests.permute_tests_for_extension(standard_tests, loader,
27
'breezy._rio_py', 'breezy._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
# When str is a unicode type, it is valid for a tag
55
self.assertTrue(self.module._valid_tag(u"foo"))
57
def test_non_ascii_char(self):
58
self.assertFalse(self.module._valid_tag("\xb5"))
61
class TestReadUTF8Stanza(tests.TestCase):
63
module = None # Filled in by test parameterization
65
def assertReadStanza(self, result, line_iter):
66
s = self.module._read_stanza_utf8(line_iter)
67
self.assertEqual(result, s)
69
for tag, value in s.iter_pairs():
70
self.assertIsInstance(tag, str)
71
self.assertIsInstance(value, str)
73
def assertReadStanzaRaises(self, exception, line_iter):
74
self.assertRaises(exception, self.module._read_stanza_utf8, line_iter)
76
def test_no_string(self):
77
self.assertReadStanzaRaises(TypeError, [21323])
80
self.assertReadStanza(None, [])
83
self.assertReadStanza(None, [b""])
85
def test_simple(self):
86
self.assertReadStanza(rio.Stanza(foo="bar"), [b"foo: bar\n", b""])
88
def test_multi_line(self):
89
self.assertReadStanza(
90
rio.Stanza(foo="bar\nbla"), [b"foo: bar\n", b"\tbla\n"])
92
def test_repeated(self):
96
self.assertReadStanza(s, [b"foo: bar\n", b"foo: foo\n"])
98
def test_invalid_early_colon(self):
99
self.assertReadStanzaRaises(ValueError, [b"f:oo: bar\n"])
101
def test_invalid_tag(self):
102
self.assertReadStanzaRaises(ValueError, [b"f%oo: bar\n"])
104
def test_continuation_too_early(self):
105
self.assertReadStanzaRaises(ValueError, [b"\tbar\n"])
107
def test_large(self):
108
value = b"bla" * 9000
109
self.assertReadStanza(rio.Stanza(foo=value),
110
[b"foo: %s\n" % value])
112
def test_non_ascii_char(self):
113
self.assertReadStanza(rio.Stanza(foo=u"n\xe5me"),
114
[u"foo: n\xe5me\n".encode("utf-8")])
117
class TestReadUnicodeStanza(tests.TestCase):
119
module = None # Filled in by test parameterization
121
def assertReadStanza(self, result, line_iter):
122
s = self.module._read_stanza_unicode(line_iter)
123
self.assertEqual(result, s)
125
for tag, value in s.iter_pairs():
126
self.assertIsInstance(tag, str)
127
self.assertIsInstance(value, str)
129
def assertReadStanzaRaises(self, exception, line_iter):
130
self.assertRaises(exception, self.module._read_stanza_unicode,
133
def test_no_string(self):
134
self.assertReadStanzaRaises(TypeError, [21323])
136
def test_empty(self):
137
self.assertReadStanza(None, [])
140
self.assertReadStanza(None, [u""])
142
def test_simple(self):
143
self.assertReadStanza(rio.Stanza(foo="bar"), [u"foo: bar\n", u""])
145
def test_multi_line(self):
146
self.assertReadStanza(rio.Stanza(foo="bar\nbla"),
147
[u"foo: bar\n", u"\tbla\n"])
149
def test_repeated(self):
153
self.assertReadStanza(s, [u"foo: bar\n", u"foo: foo\n"])
155
def test_invalid_early_colon(self):
156
self.assertReadStanzaRaises(ValueError, [u"f:oo: bar\n"])
158
def test_invalid_tag(self):
159
self.assertReadStanzaRaises(ValueError, [u"f%oo: bar\n"])
161
def test_continuation_too_early(self):
162
self.assertReadStanzaRaises(ValueError, [u"\tbar\n"])
164
def test_large(self):
165
value = u"bla" * 9000
166
self.assertReadStanza(rio.Stanza(foo=value),
167
[u"foo: %s\n" % value])
169
def test_non_ascii_char(self):
170
self.assertReadStanza(rio.Stanza(foo=u"n\xe5me"), [u"foo: n\xe5me\n"])