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_*."""
19
from __future__ import absolute_import
27
def load_tests(loader, standard_tests, pattern):
28
suite, _ = tests.permute_tests_for_extension(standard_tests, loader,
29
'breezy._rio_py', 'breezy._rio_pyx')
33
class TestValidTag(tests.TestCase):
35
module = None # Filled in by test parameterization
38
self.assertTrue(self.module._valid_tag("foo"))
40
def test_no_spaces(self):
41
self.assertFalse(self.module._valid_tag("foo bla"))
43
def test_numeric(self):
44
self.assertTrue(self.module._valid_tag("3foo423"))
46
def test_no_colon(self):
47
self.assertFalse(self.module._valid_tag("foo:bla"))
49
def test_type_error(self):
50
self.assertRaises(TypeError, self.module._valid_tag, 423)
53
self.assertFalse(self.module._valid_tag(""))
55
def test_unicode(self):
56
# When str is a unicode type, it is valid for a tag
57
self.assertTrue(self.module._valid_tag(u"foo"))
59
def test_non_ascii_char(self):
60
self.assertFalse(self.module._valid_tag("\xb5"))
63
class TestReadUTF8Stanza(tests.TestCase):
65
module = None # Filled in by test parameterization
67
def assertReadStanza(self, result, line_iter):
68
s = self.module._read_stanza_utf8(line_iter)
69
self.assertEqual(result, s)
71
for tag, value in s.iter_pairs():
72
self.assertIsInstance(tag, str)
73
self.assertIsInstance(value, str)
75
def assertReadStanzaRaises(self, exception, line_iter):
76
self.assertRaises(exception, self.module._read_stanza_utf8, line_iter)
78
def test_no_string(self):
79
self.assertReadStanzaRaises(TypeError, [21323])
82
self.assertReadStanza(None, [])
85
self.assertReadStanza(None, [b""])
87
def test_simple(self):
88
self.assertReadStanza(rio.Stanza(foo="bar"), [b"foo: bar\n", b""])
90
def test_multi_line(self):
91
self.assertReadStanza(
92
rio.Stanza(foo="bar\nbla"), [b"foo: bar\n", b"\tbla\n"])
94
def test_repeated(self):
98
self.assertReadStanza(s, [b"foo: bar\n", b"foo: foo\n"])
100
def test_invalid_early_colon(self):
101
self.assertReadStanzaRaises(ValueError, [b"f:oo: bar\n"])
103
def test_invalid_tag(self):
104
self.assertReadStanzaRaises(ValueError, [b"f%oo: bar\n"])
106
def test_continuation_too_early(self):
107
self.assertReadStanzaRaises(ValueError, [b"\tbar\n"])
109
def test_large(self):
110
value = b"bla" * 9000
111
self.assertReadStanza(rio.Stanza(foo=value),
112
[b"foo: %s\n" % value])
114
def test_non_ascii_char(self):
115
self.assertReadStanza(rio.Stanza(foo=u"n\xe5me"),
116
[u"foo: n\xe5me\n".encode("utf-8")])
119
class TestReadUnicodeStanza(tests.TestCase):
121
module = None # Filled in by test parameterization
123
def assertReadStanza(self, result, line_iter):
124
s = self.module._read_stanza_unicode(line_iter)
125
self.assertEqual(result, s)
127
for tag, value in s.iter_pairs():
128
self.assertIsInstance(tag, str)
129
self.assertIsInstance(value, str)
131
def assertReadStanzaRaises(self, exception, line_iter):
132
self.assertRaises(exception, self.module._read_stanza_unicode,
135
def test_no_string(self):
136
self.assertReadStanzaRaises(TypeError, [21323])
138
def test_empty(self):
139
self.assertReadStanza(None, [])
142
self.assertReadStanza(None, [u""])
144
def test_simple(self):
145
self.assertReadStanza(rio.Stanza(foo="bar"), [u"foo: bar\n", u""])
147
def test_multi_line(self):
148
self.assertReadStanza(rio.Stanza(foo="bar\nbla"),
149
[u"foo: bar\n", u"\tbla\n"])
151
def test_repeated(self):
155
self.assertReadStanza(s, [u"foo: bar\n", u"foo: foo\n"])
157
def test_invalid_early_colon(self):
158
self.assertReadStanzaRaises(ValueError, [u"f:oo: bar\n"])
160
def test_invalid_tag(self):
161
self.assertReadStanzaRaises(ValueError, [u"f%oo: bar\n"])
163
def test_continuation_too_early(self):
164
self.assertReadStanzaRaises(ValueError, [u"\tbar\n"])
166
def test_large(self):
167
value = u"bla" * 9000
168
self.assertReadStanza(rio.Stanza(foo=value),
169
[u"foo: %s\n" % value])
171
def test_non_ascii_char(self):
172
self.assertReadStanza(rio.Stanza(foo=u"n\xe5me"), [u"foo: n\xe5me\n"])