/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test__rio.py

  • Committer: Jelmer Vernooij
  • Date: 2020-02-07 02:14:30 UTC
  • mto: This revision was merged to the branch mainline in revision 7492.
  • Revision ID: jelmer@jelmer.uk-20200207021430-m49iq3x4x8xlib6x
Drop python2 support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2009, 2010, 2016 Canonical Ltd
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
16
 
 
17
"""Tests for _rio_*."""
 
18
 
 
19
from __future__ import absolute_import
 
20
 
 
21
from .. import (
 
22
    rio,
 
23
    tests,
 
24
    )
 
25
 
 
26
 
 
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')
 
30
    return suite
 
31
 
 
32
 
 
33
class TestValidTag(tests.TestCase):
 
34
 
 
35
    module = None  # Filled in by test parameterization
 
36
 
 
37
    def test_ok(self):
 
38
        self.assertTrue(self.module._valid_tag("foo"))
 
39
 
 
40
    def test_no_spaces(self):
 
41
        self.assertFalse(self.module._valid_tag("foo bla"))
 
42
 
 
43
    def test_numeric(self):
 
44
        self.assertTrue(self.module._valid_tag("3foo423"))
 
45
 
 
46
    def test_no_colon(self):
 
47
        self.assertFalse(self.module._valid_tag("foo:bla"))
 
48
 
 
49
    def test_type_error(self):
 
50
        self.assertRaises(TypeError, self.module._valid_tag, 423)
 
51
 
 
52
    def test_empty(self):
 
53
        self.assertFalse(self.module._valid_tag(""))
 
54
 
 
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"))
 
58
 
 
59
    def test_non_ascii_char(self):
 
60
        self.assertFalse(self.module._valid_tag("\xb5"))
 
61
 
 
62
 
 
63
class TestReadUTF8Stanza(tests.TestCase):
 
64
 
 
65
    module = None  # Filled in by test parameterization
 
66
 
 
67
    def assertReadStanza(self, result, line_iter):
 
68
        s = self.module._read_stanza_utf8(line_iter)
 
69
        self.assertEqual(result, s)
 
70
        if s is not None:
 
71
            for tag, value in s.iter_pairs():
 
72
                self.assertIsInstance(tag, str)
 
73
                self.assertIsInstance(value, str)
 
74
 
 
75
    def assertReadStanzaRaises(self, exception, line_iter):
 
76
        self.assertRaises(exception, self.module._read_stanza_utf8, line_iter)
 
77
 
 
78
    def test_no_string(self):
 
79
        self.assertReadStanzaRaises(TypeError, [21323])
 
80
 
 
81
    def test_empty(self):
 
82
        self.assertReadStanza(None, [])
 
83
 
 
84
    def test_none(self):
 
85
        self.assertReadStanza(None, [b""])
 
86
 
 
87
    def test_simple(self):
 
88
        self.assertReadStanza(rio.Stanza(foo="bar"), [b"foo: bar\n", b""])
 
89
 
 
90
    def test_multi_line(self):
 
91
        self.assertReadStanza(
 
92
            rio.Stanza(foo="bar\nbla"), [b"foo: bar\n", b"\tbla\n"])
 
93
 
 
94
    def test_repeated(self):
 
95
        s = rio.Stanza()
 
96
        s.add("foo", "bar")
 
97
        s.add("foo", "foo")
 
98
        self.assertReadStanza(s, [b"foo: bar\n", b"foo: foo\n"])
 
99
 
 
100
    def test_invalid_early_colon(self):
 
101
        self.assertReadStanzaRaises(ValueError, [b"f:oo: bar\n"])
 
102
 
 
103
    def test_invalid_tag(self):
 
104
        self.assertReadStanzaRaises(ValueError, [b"f%oo: bar\n"])
 
105
 
 
106
    def test_continuation_too_early(self):
 
107
        self.assertReadStanzaRaises(ValueError, [b"\tbar\n"])
 
108
 
 
109
    def test_large(self):
 
110
        value = b"bla" * 9000
 
111
        self.assertReadStanza(rio.Stanza(foo=value),
 
112
                              [b"foo: %s\n" % value])
 
113
 
 
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")])
 
117
 
 
118
 
 
119
class TestReadUnicodeStanza(tests.TestCase):
 
120
 
 
121
    module = None  # Filled in by test parameterization
 
122
 
 
123
    def assertReadStanza(self, result, line_iter):
 
124
        s = self.module._read_stanza_unicode(line_iter)
 
125
        self.assertEqual(result, s)
 
126
        if s is not None:
 
127
            for tag, value in s.iter_pairs():
 
128
                self.assertIsInstance(tag, str)
 
129
                self.assertIsInstance(value, str)
 
130
 
 
131
    def assertReadStanzaRaises(self, exception, line_iter):
 
132
        self.assertRaises(exception, self.module._read_stanza_unicode,
 
133
                          line_iter)
 
134
 
 
135
    def test_no_string(self):
 
136
        self.assertReadStanzaRaises(TypeError, [21323])
 
137
 
 
138
    def test_empty(self):
 
139
        self.assertReadStanza(None, [])
 
140
 
 
141
    def test_none(self):
 
142
        self.assertReadStanza(None, [u""])
 
143
 
 
144
    def test_simple(self):
 
145
        self.assertReadStanza(rio.Stanza(foo="bar"), [u"foo: bar\n", u""])
 
146
 
 
147
    def test_multi_line(self):
 
148
        self.assertReadStanza(rio.Stanza(foo="bar\nbla"),
 
149
                              [u"foo: bar\n", u"\tbla\n"])
 
150
 
 
151
    def test_repeated(self):
 
152
        s = rio.Stanza()
 
153
        s.add("foo", "bar")
 
154
        s.add("foo", "foo")
 
155
        self.assertReadStanza(s, [u"foo: bar\n", u"foo: foo\n"])
 
156
 
 
157
    def test_invalid_early_colon(self):
 
158
        self.assertReadStanzaRaises(ValueError, [u"f:oo: bar\n"])
 
159
 
 
160
    def test_invalid_tag(self):
 
161
        self.assertReadStanzaRaises(ValueError, [u"f%oo: bar\n"])
 
162
 
 
163
    def test_continuation_too_early(self):
 
164
        self.assertReadStanzaRaises(ValueError, [u"\tbar\n"])
 
165
 
 
166
    def test_large(self):
 
167
        value = u"bla" * 9000
 
168
        self.assertReadStanza(rio.Stanza(foo=value),
 
169
                              [u"foo: %s\n" % value])
 
170
 
 
171
    def test_non_ascii_char(self):
 
172
        self.assertReadStanza(rio.Stanza(foo=u"n\xe5me"), [u"foo: n\xe5me\n"])