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
25
from ..sixish import (
30
def load_tests(loader, standard_tests, pattern):
31
suite, _ = tests.permute_tests_for_extension(standard_tests, loader,
32
'breezy._rio_py', 'breezy._rio_pyx')
36
class TestValidTag(tests.TestCase):
38
module = None # Filled in by test parameterization
41
self.assertTrue(self.module._valid_tag("foo"))
43
def test_no_spaces(self):
44
self.assertFalse(self.module._valid_tag("foo bla"))
46
def test_numeric(self):
47
self.assertTrue(self.module._valid_tag("3foo423"))
49
def test_no_colon(self):
50
self.assertFalse(self.module._valid_tag("foo:bla"))
52
def test_type_error(self):
53
self.assertRaises(TypeError, self.module._valid_tag, 423)
56
self.assertFalse(self.module._valid_tag(""))
58
def test_unicode(self):
60
# When str is a unicode type, it is valid for a tag
61
self.assertTrue(self.module._valid_tag(u"foo"))
63
self.assertRaises(TypeError, self.module._valid_tag, u"foo")
65
def test_non_ascii_char(self):
66
self.assertFalse(self.module._valid_tag("\xb5"))
69
class TestReadUTF8Stanza(tests.TestCase):
71
module = None # Filled in by test parameterization
73
def assertReadStanza(self, result, line_iter):
74
s = self.module._read_stanza_utf8(line_iter)
75
self.assertEqual(result, s)
77
for tag, value in s.iter_pairs():
78
self.assertIsInstance(tag, str)
79
self.assertIsInstance(value, text_type)
81
def assertReadStanzaRaises(self, exception, line_iter):
82
self.assertRaises(exception, self.module._read_stanza_utf8, line_iter)
84
def test_no_string(self):
85
self.assertReadStanzaRaises(TypeError, [21323])
88
self.assertReadStanza(None, [])
91
self.assertReadStanza(None, [b""])
93
def test_simple(self):
94
self.assertReadStanza(rio.Stanza(foo="bar"), [b"foo: bar\n", b""])
96
def test_multi_line(self):
97
self.assertReadStanza(
98
rio.Stanza(foo="bar\nbla"), [b"foo: bar\n", b"\tbla\n"])
100
def test_repeated(self):
104
self.assertReadStanza(s, [b"foo: bar\n", b"foo: foo\n"])
106
def test_invalid_early_colon(self):
107
self.assertReadStanzaRaises(ValueError, [b"f:oo: bar\n"])
109
def test_invalid_tag(self):
110
self.assertReadStanzaRaises(ValueError, [b"f%oo: bar\n"])
112
def test_continuation_too_early(self):
113
self.assertReadStanzaRaises(ValueError, [b"\tbar\n"])
115
def test_large(self):
116
value = b"bla" * 9000
117
self.assertReadStanza(rio.Stanza(foo=value),
118
[b"foo: %s\n" % value])
120
def test_non_ascii_char(self):
121
self.assertReadStanza(rio.Stanza(foo=u"n\xe5me"),
122
[u"foo: n\xe5me\n".encode("utf-8")])
125
class TestReadUnicodeStanza(tests.TestCase):
127
module = None # Filled in by test parameterization
129
def assertReadStanza(self, result, line_iter):
130
s = self.module._read_stanza_unicode(line_iter)
131
self.assertEqual(result, s)
133
for tag, value in s.iter_pairs():
134
self.assertIsInstance(tag, str)
135
self.assertIsInstance(value, text_type)
137
def assertReadStanzaRaises(self, exception, line_iter):
138
self.assertRaises(exception, self.module._read_stanza_unicode,
141
def test_no_string(self):
142
self.assertReadStanzaRaises(TypeError, [21323])
144
def test_empty(self):
145
self.assertReadStanza(None, [])
148
self.assertReadStanza(None, [u""])
150
def test_simple(self):
151
self.assertReadStanza(rio.Stanza(foo="bar"), [u"foo: bar\n", u""])
153
def test_multi_line(self):
154
self.assertReadStanza(rio.Stanza(foo="bar\nbla"),
155
[u"foo: bar\n", u"\tbla\n"])
157
def test_repeated(self):
161
self.assertReadStanza(s, [u"foo: bar\n", u"foo: foo\n"])
163
def test_invalid_early_colon(self):
164
self.assertReadStanzaRaises(ValueError, [u"f:oo: bar\n"])
166
def test_invalid_tag(self):
167
self.assertReadStanzaRaises(ValueError, [u"f%oo: bar\n"])
169
def test_continuation_too_early(self):
170
self.assertReadStanzaRaises(ValueError, [u"\tbar\n"])
172
def test_large(self):
173
value = u"bla" * 9000
174
self.assertReadStanza(rio.Stanza(foo=value),
175
[u"foo: %s\n" % value])
177
def test_non_ascii_char(self):
178
self.assertReadStanza(rio.Stanza(foo=u"n\xe5me"), [u"foo: n\xe5me\n"])