/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4354.3.12 by Jelmer Vernooij
Add tests for _valid_tag.
1
# Copyright (C) 2009 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 bzrlib import (
20
    rio,
21
    tests,
22
    )
23
24
25
def load_tests(standard_tests, module, loader):
26
    # parameterize all tests in this module
27
    suite = loader.suiteClass()
28
    import bzrlib._rio_py as py_module
29
    scenarios = [('python', {'module': py_module})]
4913.2.9 by John Arbash Meinel
Switch CompiledRio
30
    if compiled_rio.available():
31
        scenarios.append(('C', {'module': compiled_rio.module}))
4354.3.12 by Jelmer Vernooij
Add tests for _valid_tag.
32
    else:
33
        # the compiled module isn't available, so we add a failing test
34
        class FailWithoutFeature(tests.TestCase):
35
            def test_fail(self):
4913.2.9 by John Arbash Meinel
Switch CompiledRio
36
                self.requireFeature(compiled_rio)
4354.3.12 by Jelmer Vernooij
Add tests for _valid_tag.
37
        suite.addTest(loader.loadTestsFromTestCase(FailWithoutFeature))
38
    tests.multiply_tests(standard_tests, scenarios, suite)
39
    return suite
40
41
4913.2.9 by John Arbash Meinel
Switch CompiledRio
42
compiled_rio = tests.ModuleAvailableFeature('bzrlib._rio_pyx')
4354.3.12 by Jelmer Vernooij
Add tests for _valid_tag.
43
44
45
class TestValidTag(tests.TestCase):
46
47
    module = None # Filled in by test parameterization
48
49
    def test_ok(self):
50
        self.assertTrue(self.module._valid_tag("foo"))
51
52
    def test_no_spaces(self):
53
        self.assertFalse(self.module._valid_tag("foo bla"))
54
4354.3.13 by Jelmer Vernooij
Add more RIO tests, fix bugs in pyrex implementation.
55
    def test_numeric(self):
56
        self.assertTrue(self.module._valid_tag("3foo423"))
57
4354.3.12 by Jelmer Vernooij
Add tests for _valid_tag.
58
    def test_no_colon(self):
59
        self.assertFalse(self.module._valid_tag("foo:bla"))
60
    
61
    def test_type_error(self):
62
        self.assertRaises(TypeError, self.module._valid_tag, 423)
63
64
    def test_empty(self):
65
        self.assertFalse(self.module._valid_tag(""))
4354.3.13 by Jelmer Vernooij
Add more RIO tests, fix bugs in pyrex implementation.
66
4354.3.15 by Jelmer Vernooij
Extend valid_tags tests a bit, test that stanza pairs contain the right types.
67
    def test_unicode(self):
68
        self.assertRaises(TypeError, self.module._valid_tag, u"foo")
69
70
    def test_non_ascii_char(self):
71
        self.assertFalse(self.module._valid_tag("\xb5"))
72
4354.3.13 by Jelmer Vernooij
Add more RIO tests, fix bugs in pyrex implementation.
73
74
class TestReadUTF8Stanza(tests.TestCase):
75
76
    module = None # Filled in by test parameterization
77
78
    def assertReadStanza(self, result, line_iter):
4354.3.15 by Jelmer Vernooij
Extend valid_tags tests a bit, test that stanza pairs contain the right types.
79
        s = self.module._read_stanza_utf8(line_iter)
80
        self.assertEquals(result, s)
81
        if s is not None:
82
            for tag, value in s.iter_pairs():
83
                self.assertIsInstance(tag, str)
84
                self.assertIsInstance(value, unicode)
4354.3.13 by Jelmer Vernooij
Add more RIO tests, fix bugs in pyrex implementation.
85
86
    def assertReadStanzaRaises(self, exception, line_iter):
87
        self.assertRaises(exception, self.module._read_stanza_utf8, line_iter)
88
89
    def test_no_string(self):
90
        self.assertReadStanzaRaises(TypeError, [21323])
91
92
    def test_empty(self):
93
        self.assertReadStanza(None, [])
94
95
    def test_none(self):
96
        self.assertReadStanza(None, [""])
97
98
    def test_simple(self):
99
        self.assertReadStanza(rio.Stanza(foo="bar"), ["foo: bar\n", ""])
100
101
    def test_multi_line(self):
102
        self.assertReadStanza(rio.Stanza(foo="bar\nbla"), 
103
                ["foo: bar\n", "\tbla\n"])
104
105
    def test_repeated(self):
106
        s = rio.Stanza()
107
        s.add("foo", "bar")
108
        s.add("foo", "foo")
109
        self.assertReadStanza(s, ["foo: bar\n", "foo: foo\n"])
110
111
    def test_invalid_early_colon(self):
112
        self.assertReadStanzaRaises(ValueError, ["f:oo: bar\n"])
113
114
    def test_invalid_tag(self):
115
        self.assertReadStanzaRaises(ValueError, ["f%oo: bar\n"])
116
117
    def test_continuation_too_early(self):
118
        self.assertReadStanzaRaises(ValueError, ["\tbar\n"])
119
120
    def test_large(self):
121
        value = "bla" * 9000
122
        self.assertReadStanza(rio.Stanza(foo=value),
123
            ["foo: %s\n" % value])
124
125
    def test_non_ascii_char(self):
126
        self.assertReadStanza(rio.Stanza(foo=u"n\xe5me"),
127
            [u"foo: n\xe5me\n".encode("utf-8")])
128
129
130
class TestReadUnicodeStanza(tests.TestCase):
131
132
    module = None # Filled in by test parameterization
133
134
    def assertReadStanza(self, result, line_iter):
4354.3.15 by Jelmer Vernooij
Extend valid_tags tests a bit, test that stanza pairs contain the right types.
135
        s = self.module._read_stanza_unicode(line_iter)
136
        self.assertEquals(result, s)
137
        if s is not None:
138
            for tag, value in s.iter_pairs():
139
                self.assertIsInstance(tag, str)
140
                self.assertIsInstance(value, unicode)
4354.3.13 by Jelmer Vernooij
Add more RIO tests, fix bugs in pyrex implementation.
141
142
    def assertReadStanzaRaises(self, exception, line_iter):
143
        self.assertRaises(exception, self.module._read_stanza_unicode,
144
                          line_iter)
145
146
    def test_no_string(self):
147
        self.assertReadStanzaRaises(TypeError, [21323])
148
149
    def test_empty(self):
150
        self.assertReadStanza(None, [])
151
152
    def test_none(self):
153
        self.assertReadStanza(None, [u""])
154
155
    def test_simple(self):
156
        self.assertReadStanza(rio.Stanza(foo="bar"), [u"foo: bar\n", u""])
157
158
    def test_multi_line(self):
159
        self.assertReadStanza(rio.Stanza(foo="bar\nbla"), 
160
                [u"foo: bar\n", u"\tbla\n"])
161
162
    def test_repeated(self):
163
        s = rio.Stanza()
164
        s.add("foo", "bar")
165
        s.add("foo", "foo")
166
        self.assertReadStanza(s, [u"foo: bar\n", u"foo: foo\n"])
167
168
    def test_invalid_early_colon(self):
169
        self.assertReadStanzaRaises(ValueError, [u"f:oo: bar\n"])
170
171
    def test_invalid_tag(self):
172
        self.assertReadStanzaRaises(ValueError, [u"f%oo: bar\n"])
173
174
    def test_continuation_too_early(self):
175
        self.assertReadStanzaRaises(ValueError, [u"\tbar\n"])
176
177
    def test_large(self):
178
        value = u"bla" * 9000
179
        self.assertReadStanza(rio.Stanza(foo=value),
180
            [u"foo: %s\n" % value])
181
182
    def test_non_ascii_char(self):
183
        self.assertReadStanza(rio.Stanza(foo=u"n\xe5me"), [u"foo: n\xe5me\n"])