/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-05-24 00:39:50 UTC
  • mto: This revision was merged to the branch mainline in revision 7504.
  • Revision ID: jelmer@jelmer.uk-20200524003950-bbc545r76vc5yajg
Add github action.

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