/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: 2019-08-12 20:24:50 UTC
  • mto: (7290.1.35 work)
  • mto: This revision was merged to the branch mainline in revision 7405.
  • Revision ID: jelmer@jelmer.uk-20190812202450-vdpamxay6sebo93w
Fix path to brz.

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