~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test__rio.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-05-15 01:21:40 UTC
  • mfrom: (4354.3.18 rio-c)
  • Revision ID: pqm@pqm.ubuntu.com-20090515012140-stwx16f974x0zogp
(Jelmer) Add Pyrex RIO implementation.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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})]
 
30
    if CompiledRioFeature.available():
 
31
        import bzrlib._rio_pyx as c_module
 
32
        scenarios.append(('C', {'module': c_module}))
 
33
    else:
 
34
        # the compiled module isn't available, so we add a failing test
 
35
        class FailWithoutFeature(tests.TestCase):
 
36
            def test_fail(self):
 
37
                self.requireFeature(CompiledRioFeature)
 
38
        suite.addTest(loader.loadTestsFromTestCase(FailWithoutFeature))
 
39
    tests.multiply_tests(standard_tests, scenarios, suite)
 
40
    return suite
 
41
 
 
42
 
 
43
class _CompiledRioFeature(tests.Feature):
 
44
 
 
45
    def _probe(self):
 
46
        try:
 
47
            import bzrlib._rio_pyx
 
48
        except ImportError:
 
49
            return False
 
50
        return True
 
51
 
 
52
    def feature_name(self):
 
53
        return 'bzrlib._rio_pyx'
 
54
 
 
55
CompiledRioFeature = _CompiledRioFeature()
 
56
 
 
57
 
 
58
class TestValidTag(tests.TestCase):
 
59
 
 
60
    module = None # Filled in by test parameterization
 
61
 
 
62
    def test_ok(self):
 
63
        self.assertTrue(self.module._valid_tag("foo"))
 
64
 
 
65
    def test_no_spaces(self):
 
66
        self.assertFalse(self.module._valid_tag("foo bla"))
 
67
 
 
68
    def test_numeric(self):
 
69
        self.assertTrue(self.module._valid_tag("3foo423"))
 
70
 
 
71
    def test_no_colon(self):
 
72
        self.assertFalse(self.module._valid_tag("foo:bla"))
 
73
    
 
74
    def test_type_error(self):
 
75
        self.assertRaises(TypeError, self.module._valid_tag, 423)
 
76
 
 
77
    def test_empty(self):
 
78
        self.assertFalse(self.module._valid_tag(""))
 
79
 
 
80
    def test_unicode(self):
 
81
        self.assertRaises(TypeError, self.module._valid_tag, u"foo")
 
82
 
 
83
    def test_non_ascii_char(self):
 
84
        self.assertFalse(self.module._valid_tag("\xb5"))
 
85
 
 
86
 
 
87
class TestReadUTF8Stanza(tests.TestCase):
 
88
 
 
89
    module = None # Filled in by test parameterization
 
90
 
 
91
    def assertReadStanza(self, result, line_iter):
 
92
        s = self.module._read_stanza_utf8(line_iter)
 
93
        self.assertEquals(result, s)
 
94
        if s is not None:
 
95
            for tag, value in s.iter_pairs():
 
96
                self.assertIsInstance(tag, str)
 
97
                self.assertIsInstance(value, unicode)
 
98
 
 
99
    def assertReadStanzaRaises(self, exception, line_iter):
 
100
        self.assertRaises(exception, self.module._read_stanza_utf8, line_iter)
 
101
 
 
102
    def test_no_string(self):
 
103
        self.assertReadStanzaRaises(TypeError, [21323])
 
104
 
 
105
    def test_empty(self):
 
106
        self.assertReadStanza(None, [])
 
107
 
 
108
    def test_none(self):
 
109
        self.assertReadStanza(None, [""])
 
110
 
 
111
    def test_simple(self):
 
112
        self.assertReadStanza(rio.Stanza(foo="bar"), ["foo: bar\n", ""])
 
113
 
 
114
    def test_multi_line(self):
 
115
        self.assertReadStanza(rio.Stanza(foo="bar\nbla"), 
 
116
                ["foo: bar\n", "\tbla\n"])
 
117
 
 
118
    def test_repeated(self):
 
119
        s = rio.Stanza()
 
120
        s.add("foo", "bar")
 
121
        s.add("foo", "foo")
 
122
        self.assertReadStanza(s, ["foo: bar\n", "foo: foo\n"])
 
123
 
 
124
    def test_invalid_early_colon(self):
 
125
        self.assertReadStanzaRaises(ValueError, ["f:oo: bar\n"])
 
126
 
 
127
    def test_invalid_tag(self):
 
128
        self.assertReadStanzaRaises(ValueError, ["f%oo: bar\n"])
 
129
 
 
130
    def test_continuation_too_early(self):
 
131
        self.assertReadStanzaRaises(ValueError, ["\tbar\n"])
 
132
 
 
133
    def test_large(self):
 
134
        value = "bla" * 9000
 
135
        self.assertReadStanza(rio.Stanza(foo=value),
 
136
            ["foo: %s\n" % value])
 
137
 
 
138
    def test_non_ascii_char(self):
 
139
        self.assertReadStanza(rio.Stanza(foo=u"n\xe5me"),
 
140
            [u"foo: n\xe5me\n".encode("utf-8")])
 
141
 
 
142
 
 
143
class TestReadUnicodeStanza(tests.TestCase):
 
144
 
 
145
    module = None # Filled in by test parameterization
 
146
 
 
147
    def assertReadStanza(self, result, line_iter):
 
148
        s = self.module._read_stanza_unicode(line_iter)
 
149
        self.assertEquals(result, s)
 
150
        if s is not None:
 
151
            for tag, value in s.iter_pairs():
 
152
                self.assertIsInstance(tag, str)
 
153
                self.assertIsInstance(value, unicode)
 
154
 
 
155
    def assertReadStanzaRaises(self, exception, line_iter):
 
156
        self.assertRaises(exception, self.module._read_stanza_unicode,
 
157
                          line_iter)
 
158
 
 
159
    def test_no_string(self):
 
160
        self.assertReadStanzaRaises(TypeError, [21323])
 
161
 
 
162
    def test_empty(self):
 
163
        self.assertReadStanza(None, [])
 
164
 
 
165
    def test_none(self):
 
166
        self.assertReadStanza(None, [u""])
 
167
 
 
168
    def test_simple(self):
 
169
        self.assertReadStanza(rio.Stanza(foo="bar"), [u"foo: bar\n", u""])
 
170
 
 
171
    def test_multi_line(self):
 
172
        self.assertReadStanza(rio.Stanza(foo="bar\nbla"), 
 
173
                [u"foo: bar\n", u"\tbla\n"])
 
174
 
 
175
    def test_repeated(self):
 
176
        s = rio.Stanza()
 
177
        s.add("foo", "bar")
 
178
        s.add("foo", "foo")
 
179
        self.assertReadStanza(s, [u"foo: bar\n", u"foo: foo\n"])
 
180
 
 
181
    def test_invalid_early_colon(self):
 
182
        self.assertReadStanzaRaises(ValueError, [u"f:oo: bar\n"])
 
183
 
 
184
    def test_invalid_tag(self):
 
185
        self.assertReadStanzaRaises(ValueError, [u"f%oo: bar\n"])
 
186
 
 
187
    def test_continuation_too_early(self):
 
188
        self.assertReadStanzaRaises(ValueError, [u"\tbar\n"])
 
189
 
 
190
    def test_large(self):
 
191
        value = u"bla" * 9000
 
192
        self.assertReadStanza(rio.Stanza(foo=value),
 
193
            [u"foo: %s\n" % value])
 
194
 
 
195
    def test_non_ascii_char(self):
 
196
        self.assertReadStanza(rio.Stanza(foo=u"n\xe5me"), [u"foo: n\xe5me\n"])