~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test__rio.py

  • Committer: Max Bowsher
  • Date: 2010-10-31 14:31:17 UTC
  • mto: This revision was merged to the branch mainline in revision 5521.
  • Revision ID: maxb@f2s.com-20101031143117-57vrfu630yq8a3k2
Fix a slightly odd style of ConfigObj construction to match the prevailing style in bzrlib.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2009, 2010 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
    suite, _ = tests.permute_tests_for_extension(standard_tests, loader,
 
27
        'bzrlib._rio_py', 'bzrlib._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
        self.assertRaises(TypeError, self.module._valid_tag, u"foo")
 
55
 
 
56
    def test_non_ascii_char(self):
 
57
        self.assertFalse(self.module._valid_tag("\xb5"))
 
58
 
 
59
 
 
60
class TestReadUTF8Stanza(tests.TestCase):
 
61
 
 
62
    module = None # Filled in by test parameterization
 
63
 
 
64
    def assertReadStanza(self, result, line_iter):
 
65
        s = self.module._read_stanza_utf8(line_iter)
 
66
        self.assertEquals(result, s)
 
67
        if s is not None:
 
68
            for tag, value in s.iter_pairs():
 
69
                self.assertIsInstance(tag, str)
 
70
                self.assertIsInstance(value, unicode)
 
71
 
 
72
    def assertReadStanzaRaises(self, exception, line_iter):
 
73
        self.assertRaises(exception, self.module._read_stanza_utf8, line_iter)
 
74
 
 
75
    def test_no_string(self):
 
76
        self.assertReadStanzaRaises(TypeError, [21323])
 
77
 
 
78
    def test_empty(self):
 
79
        self.assertReadStanza(None, [])
 
80
 
 
81
    def test_none(self):
 
82
        self.assertReadStanza(None, [""])
 
83
 
 
84
    def test_simple(self):
 
85
        self.assertReadStanza(rio.Stanza(foo="bar"), ["foo: bar\n", ""])
 
86
 
 
87
    def test_multi_line(self):
 
88
        self.assertReadStanza(rio.Stanza(foo="bar\nbla"), 
 
89
                ["foo: bar\n", "\tbla\n"])
 
90
 
 
91
    def test_repeated(self):
 
92
        s = rio.Stanza()
 
93
        s.add("foo", "bar")
 
94
        s.add("foo", "foo")
 
95
        self.assertReadStanza(s, ["foo: bar\n", "foo: foo\n"])
 
96
 
 
97
    def test_invalid_early_colon(self):
 
98
        self.assertReadStanzaRaises(ValueError, ["f:oo: bar\n"])
 
99
 
 
100
    def test_invalid_tag(self):
 
101
        self.assertReadStanzaRaises(ValueError, ["f%oo: bar\n"])
 
102
 
 
103
    def test_continuation_too_early(self):
 
104
        self.assertReadStanzaRaises(ValueError, ["\tbar\n"])
 
105
 
 
106
    def test_large(self):
 
107
        value = "bla" * 9000
 
108
        self.assertReadStanza(rio.Stanza(foo=value),
 
109
            ["foo: %s\n" % value])
 
110
 
 
111
    def test_non_ascii_char(self):
 
112
        self.assertReadStanza(rio.Stanza(foo=u"n\xe5me"),
 
113
            [u"foo: n\xe5me\n".encode("utf-8")])
 
114
 
 
115
 
 
116
class TestReadUnicodeStanza(tests.TestCase):
 
117
 
 
118
    module = None # Filled in by test parameterization
 
119
 
 
120
    def assertReadStanza(self, result, line_iter):
 
121
        s = self.module._read_stanza_unicode(line_iter)
 
122
        self.assertEquals(result, s)
 
123
        if s is not None:
 
124
            for tag, value in s.iter_pairs():
 
125
                self.assertIsInstance(tag, str)
 
126
                self.assertIsInstance(value, unicode)
 
127
 
 
128
    def assertReadStanzaRaises(self, exception, line_iter):
 
129
        self.assertRaises(exception, self.module._read_stanza_unicode,
 
130
                          line_iter)
 
131
 
 
132
    def test_no_string(self):
 
133
        self.assertReadStanzaRaises(TypeError, [21323])
 
134
 
 
135
    def test_empty(self):
 
136
        self.assertReadStanza(None, [])
 
137
 
 
138
    def test_none(self):
 
139
        self.assertReadStanza(None, [u""])
 
140
 
 
141
    def test_simple(self):
 
142
        self.assertReadStanza(rio.Stanza(foo="bar"), [u"foo: bar\n", u""])
 
143
 
 
144
    def test_multi_line(self):
 
145
        self.assertReadStanza(rio.Stanza(foo="bar\nbla"), 
 
146
                [u"foo: bar\n", u"\tbla\n"])
 
147
 
 
148
    def test_repeated(self):
 
149
        s = rio.Stanza()
 
150
        s.add("foo", "bar")
 
151
        s.add("foo", "foo")
 
152
        self.assertReadStanza(s, [u"foo: bar\n", u"foo: foo\n"])
 
153
 
 
154
    def test_invalid_early_colon(self):
 
155
        self.assertReadStanzaRaises(ValueError, [u"f:oo: bar\n"])
 
156
 
 
157
    def test_invalid_tag(self):
 
158
        self.assertReadStanzaRaises(ValueError, [u"f%oo: bar\n"])
 
159
 
 
160
    def test_continuation_too_early(self):
 
161
        self.assertReadStanzaRaises(ValueError, [u"\tbar\n"])
 
162
 
 
163
    def test_large(self):
 
164
        value = u"bla" * 9000
 
165
        self.assertReadStanza(rio.Stanza(foo=value),
 
166
            [u"foo: %s\n" % value])
 
167
 
 
168
    def test_non_ascii_char(self):
 
169
        self.assertReadStanza(rio.Stanza(foo=u"n\xe5me"), [u"foo: n\xe5me\n"])