/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1
# Copyright (C) 2005, 2006, 2007, 2009, 2010, 2011, 2016 Canonical Ltd
1185.47.1 by Martin Pool
[broken] start converting basic_io to more rfc822-like format
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1185.47.1 by Martin Pool
[broken] start converting basic_io to more rfc822-like format
16
17
"""Tests for rio serialization
18
19
A simple, reproducible structured IO format.
20
21
rio itself works in Unicode strings.  It is typically encoded to UTF-8,
22
but this depends on the transport.
23
"""
24
1551.12.1 by Aaron Bentley
Basic RIO patch-compatible format is working
25
import re
1185.47.1 by Martin Pool
[broken] start converting basic_io to more rfc822-like format
26
from tempfile import TemporaryFile
27
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
28
from breezy import (
2030.1.5 by John Arbash Meinel
Create a 'read_stanza_unicode' to handle unicode processing
29
    rio,
30
    )
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
31
from breezy.tests import TestCase
32
from breezy.rio import (
5579.3.1 by Jelmer Vernooij
Remove unused imports.
33
    RioReader,
34
    Stanza,
35
    read_stanza,
36
    read_stanzas,
37
    rio_file,
38
    )
1185.47.1 by Martin Pool
[broken] start converting basic_io to more rfc822-like format
39
40
41
class TestRio(TestCase):
42
43
    def test_stanza(self):
44
        """Construct rio stanza in memory"""
1185.47.2 by Martin Pool
Finish rio format and tests.
45
        s = Stanza(number='42', name="fred")
1185.47.1 by Martin Pool
[broken] start converting basic_io to more rfc822-like format
46
        self.assertTrue('number' in s)
47
        self.assertFalse('color' in s)
1185.47.2 by Martin Pool
Finish rio format and tests.
48
        self.assertFalse('42' in s)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
49
        self.assertEqual(list(s.iter_pairs()),
7143.15.2 by Jelmer Vernooij
Run autopep8.
50
                         [('name', 'fred'), ('number', '42')])
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
51
        self.assertEqual(s.get('number'), '42')
52
        self.assertEqual(s.get('name'), 'fred')
1185.47.1 by Martin Pool
[broken] start converting basic_io to more rfc822-like format
53
54
    def test_empty_value(self):
55
        """Serialize stanza with empty field"""
56
        s = Stanza(empty='')
6684.1.7 by Martin
Update rio tests to pass on Python 3
57
        self.assertEquals(s.to_string(),
7143.15.2 by Jelmer Vernooij
Run autopep8.
58
                          b"empty: \n")
1185.47.1 by Martin Pool
[broken] start converting basic_io to more rfc822-like format
59
60
    def test_to_lines(self):
61
        """Write simple rio stanza to string"""
1185.47.2 by Martin Pool
Finish rio format and tests.
62
        s = Stanza(number='42', name='fred')
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
63
        self.assertEqual(list(s.to_lines()),
7143.15.2 by Jelmer Vernooij
Run autopep8.
64
                         [b'name: fred\n',
65
                          b'number: 42\n'])
1185.47.1 by Martin Pool
[broken] start converting basic_io to more rfc822-like format
66
1553.5.8 by Martin Pool
New Rio.as_dict method
67
    def test_as_dict(self):
68
        """Convert rio Stanza to dictionary"""
69
        s = Stanza(number='42', name='fred')
70
        sd = s.as_dict()
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
71
        self.assertEqual(sd, dict(number='42', name='fred'))
1553.5.8 by Martin Pool
New Rio.as_dict method
72
1185.47.1 by Martin Pool
[broken] start converting basic_io to more rfc822-like format
73
    def test_to_file(self):
74
        """Write rio to file"""
75
        tmpf = TemporaryFile()
7143.15.2 by Jelmer Vernooij
Run autopep8.
76
        s = Stanza(a_thing='something with "quotes like \\"this\\""',
77
                   number='42', name='fred')
1185.47.1 by Martin Pool
[broken] start converting basic_io to more rfc822-like format
78
        s.write(tmpf)
79
        tmpf.seek(0)
6684.1.7 by Martin
Update rio tests to pass on Python 3
80
        self.assertEqual(tmpf.read(), b'''\
81
a_thing: something with "quotes like \\"this\\""
1185.47.1 by Martin Pool
[broken] start converting basic_io to more rfc822-like format
82
name: fred
83
number: 42
6684.1.7 by Martin
Update rio tests to pass on Python 3
84
''')
1185.47.1 by Martin Pool
[broken] start converting basic_io to more rfc822-like format
85
86
    def test_multiline_string(self):
87
        tmpf = TemporaryFile()
7143.15.2 by Jelmer Vernooij
Run autopep8.
88
        s = Stanza(
89
            motto="war is peace\nfreedom is slavery\nignorance is strength")
1185.47.1 by Martin Pool
[broken] start converting basic_io to more rfc822-like format
90
        s.write(tmpf)
91
        tmpf.seek(0)
6684.1.7 by Martin
Update rio tests to pass on Python 3
92
        self.assertEqual(tmpf.read(), b'''\
1185.47.1 by Martin Pool
[broken] start converting basic_io to more rfc822-like format
93
motto: war is peace
94
\tfreedom is slavery
95
\tignorance is strength
96
''')
97
        tmpf.seek(0)
98
        s2 = read_stanza(tmpf)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
99
        self.assertEqual(s, s2)
1185.47.1 by Martin Pool
[broken] start converting basic_io to more rfc822-like format
100
101
    def test_read_stanza(self):
102
        """Load stanza from string"""
6684.1.7 by Martin
Update rio tests to pass on Python 3
103
        lines = b"""\
1185.47.1 by Martin Pool
[broken] start converting basic_io to more rfc822-like format
104
revision: mbp@sourcefrog.net-123-abc
105
timestamp: 1130653962
106
timezone: 36000
107
committer: Martin Pool <mbp@test.sourcefrog.net>
108
""".splitlines(True)
109
        s = read_stanza(lines)
110
        self.assertTrue('revision' in s)
6684.1.7 by Martin
Update rio tests to pass on Python 3
111
        self.assertEqual(s.get('revision'), 'mbp@sourcefrog.net-123-abc')
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
112
        self.assertEqual(list(s.iter_pairs()),
7143.15.2 by Jelmer Vernooij
Run autopep8.
113
                         [('revision', 'mbp@sourcefrog.net-123-abc'),
114
                          ('timestamp', '1130653962'),
115
                          ('timezone', '36000'),
116
                          ('committer', "Martin Pool <mbp@test.sourcefrog.net>")])
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
117
        self.assertEqual(len(s), 4)
1185.47.1 by Martin Pool
[broken] start converting basic_io to more rfc822-like format
118
119
    def test_repeated_field(self):
120
        """Repeated field in rio"""
121
        s = Stanza()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
122
        for k, v in [('a', '10'), ('b', '20'), ('a', '100'), ('b', '200'),
1185.47.1 by Martin Pool
[broken] start converting basic_io to more rfc822-like format
123
                     ('a', '1000'), ('b', '2000')]:
124
            s.add(k, v)
125
        s2 = read_stanza(s.to_lines())
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
126
        self.assertEqual(s, s2)
6631.3.1 by Martin
Run 2to3 map fixer and refactor after
127
        self.assertEqual(s.get_all('a'), ['10', '100', '1000'])
128
        self.assertEqual(s.get_all('b'), ['20', '200', '2000'])
1185.47.1 by Martin Pool
[broken] start converting basic_io to more rfc822-like format
129
1185.47.2 by Martin Pool
Finish rio format and tests.
130
    def test_backslash(self):
1185.47.1 by Martin Pool
[broken] start converting basic_io to more rfc822-like format
131
        s = Stanza(q='\\')
132
        t = s.to_string()
6684.1.7 by Martin
Update rio tests to pass on Python 3
133
        self.assertEqual(t, b'q: \\\n')
1185.47.2 by Martin Pool
Finish rio format and tests.
134
        s2 = read_stanza(s.to_lines())
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
135
        self.assertEqual(s, s2)
1185.47.2 by Martin Pool
Finish rio format and tests.
136
137
    def test_blank_line(self):
138
        s = Stanza(none='', one='\n', two='\n\n')
6684.1.7 by Martin
Update rio tests to pass on Python 3
139
        self.assertEqual(s.to_string(), b"""\
3943.8.2 by Marius Kruger
fix tests relying on trailing whitespace by replacing it with \x20.
140
none:\x20
141
one:\x20
1185.47.2 by Martin Pool
Finish rio format and tests.
142
\t
3943.8.2 by Marius Kruger
fix tests relying on trailing whitespace by replacing it with \x20.
143
two:\x20
1185.47.2 by Martin Pool
Finish rio format and tests.
144
\t
145
\t
146
""")
147
        s2 = read_stanza(s.to_lines())
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
148
        self.assertEqual(s, s2)
1185.47.2 by Martin Pool
Finish rio format and tests.
149
150
    def test_whitespace_value(self):
151
        s = Stanza(space=' ', tabs='\t\t\t', combo='\n\t\t\n')
6684.1.7 by Martin
Update rio tests to pass on Python 3
152
        self.assertEqual(s.to_string(), b"""\
3943.8.2 by Marius Kruger
fix tests relying on trailing whitespace by replacing it with \x20.
153
combo:\x20
1185.47.2 by Martin Pool
Finish rio format and tests.
154
\t\t\t
155
\t
3943.8.2 by Marius Kruger
fix tests relying on trailing whitespace by replacing it with \x20.
156
space:\x20\x20
1185.47.2 by Martin Pool
Finish rio format and tests.
157
tabs: \t\t\t
158
""")
159
        s2 = read_stanza(s.to_lines())
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
160
        self.assertEqual(s, s2)
1534.10.2 by Aaron Bentley
Implemented rio_file to produce a light file object from stanzas
161
        self.rio_file_stanzas([s])
1185.47.2 by Martin Pool
Finish rio format and tests.
162
1185.47.1 by Martin Pool
[broken] start converting basic_io to more rfc822-like format
163
    def test_quoted(self):
164
        """rio quoted string cases"""
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
165
        s = Stanza(q1='"hello"',
166
                   q2=' "for',
1185.47.1 by Martin Pool
[broken] start converting basic_io to more rfc822-like format
167
                   q3='\n\n"for"\n',
168
                   q4='for\n"\nfor',
169
                   q5='\n',
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
170
                   q6='"',
1185.47.1 by Martin Pool
[broken] start converting basic_io to more rfc822-like format
171
                   q7='""',
172
                   q8='\\',
173
                   q9='\\"\\"',
174
                   )
175
        s2 = read_stanza(s.to_lines())
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
176
        self.assertEqual(s, s2)
1534.10.2 by Aaron Bentley
Implemented rio_file to produce a light file object from stanzas
177
        # apparent bug in read_stanza
178
        # s3 = read_stanza(self.stanzas_to_str([s]))
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
179
        # self.assertEqual(s, s3)
1185.47.1 by Martin Pool
[broken] start converting basic_io to more rfc822-like format
180
181
    def test_read_empty(self):
182
        """Detect end of rio file"""
183
        s = read_stanza([])
184
        self.assertEqual(s, None)
185
        self.assertTrue(s is None)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
186
4634.161.1 by Andrew Bennetts
Add LockCorrupt error, and use it to provide nicer handling of unparseable lock/held/info files.
187
    def test_read_nul_byte(self):
188
        """File consisting of a nul byte causes an error."""
6684.1.7 by Martin
Update rio tests to pass on Python 3
189
        self.assertRaises(ValueError, read_stanza, [b'\0'])
4634.161.1 by Andrew Bennetts
Add LockCorrupt error, and use it to provide nicer handling of unparseable lock/held/info files.
190
191
    def test_read_nul_bytes(self):
192
        """File consisting of many nul bytes causes an error."""
6684.1.7 by Martin
Update rio tests to pass on Python 3
193
        self.assertRaises(ValueError, read_stanza, [b'\0' * 100])
4634.161.1 by Andrew Bennetts
Add LockCorrupt error, and use it to provide nicer handling of unparseable lock/held/info files.
194
1185.47.1 by Martin Pool
[broken] start converting basic_io to more rfc822-like format
195
    def test_read_iter(self):
196
        """Read several stanzas from file"""
197
        tmpf = TemporaryFile()
6684.1.7 by Martin
Update rio tests to pass on Python 3
198
        tmpf.write(b"""\
1185.47.2 by Martin Pool
Finish rio format and tests.
199
version_header: 1
200
201
name: foo
202
val: 123
203
204
name: bar
205
val: 129319
1185.47.1 by Martin Pool
[broken] start converting basic_io to more rfc822-like format
206
""")
207
        tmpf.seek(0)
208
        reader = read_stanzas(tmpf)
209
        read_iter = iter(reader)
210
        stuff = list(reader)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
211
        self.assertEqual(stuff,
7143.15.2 by Jelmer Vernooij
Run autopep8.
212
                         [Stanza(version_header='1'),
213
                          Stanza(name="foo", val='123'),
214
                             Stanza(name="bar", val='129319'), ])
1185.47.1 by Martin Pool
[broken] start converting basic_io to more rfc822-like format
215
216
    def test_read_several(self):
217
        """Read several stanzas from file"""
218
        tmpf = TemporaryFile()
6684.1.7 by Martin
Update rio tests to pass on Python 3
219
        tmpf.write(b"""\
1185.47.2 by Martin Pool
Finish rio format and tests.
220
version_header: 1
221
222
name: foo
223
val: 123
224
225
name: quoted
226
address:   "Willowglen"
227
\t  42 Wallaby Way
228
\t  Sydney
229
230
name: bar
231
val: 129319
1185.47.1 by Martin Pool
[broken] start converting basic_io to more rfc822-like format
232
""")
233
        tmpf.seek(0)
234
        s = read_stanza(tmpf)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
235
        self.assertEqual(s, Stanza(version_header='1'))
1185.47.1 by Martin Pool
[broken] start converting basic_io to more rfc822-like format
236
        s = read_stanza(tmpf)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
237
        self.assertEqual(s, Stanza(name="foo", val='123'))
1185.47.1 by Martin Pool
[broken] start converting basic_io to more rfc822-like format
238
        s = read_stanza(tmpf)
6684.1.7 by Martin
Update rio tests to pass on Python 3
239
        self.assertEqual(s.get('name'), 'quoted')
240
        self.assertEqual(
241
            s.get('address'), '  "Willowglen"\n  42 Wallaby Way\n  Sydney')
1185.47.1 by Martin Pool
[broken] start converting basic_io to more rfc822-like format
242
        s = read_stanza(tmpf)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
243
        self.assertEqual(s, Stanza(name="bar", val='129319'))
1185.47.1 by Martin Pool
[broken] start converting basic_io to more rfc822-like format
244
        s = read_stanza(tmpf)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
245
        self.assertEqual(s, None)
1534.10.2 by Aaron Bentley
Implemented rio_file to produce a light file object from stanzas
246
        self.check_rio_file(tmpf)
247
248
    def check_rio_file(self, real_file):
249
        real_file.seek(0)
250
        read_write = rio_file(RioReader(real_file)).read()
251
        real_file.seek(0)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
252
        self.assertEqual(read_write, real_file.read())
1534.10.2 by Aaron Bentley
Implemented rio_file to produce a light file object from stanzas
253
254
    @staticmethod
255
    def stanzas_to_str(stanzas):
256
        return rio_file(stanzas).read()
257
258
    def rio_file_stanzas(self, stanzas):
259
        new_stanzas = list(RioReader(rio_file(stanzas)))
260
        self.assertEqual(new_stanzas, stanzas)
1185.47.1 by Martin Pool
[broken] start converting basic_io to more rfc822-like format
261
262
    def test_tricky_quoted(self):
263
        tmpf = TemporaryFile()
6684.1.7 by Martin
Update rio tests to pass on Python 3
264
        tmpf.write(b'''\
1185.47.2 by Martin Pool
Finish rio format and tests.
265
s: "one"
266
3943.8.2 by Marius Kruger
fix tests relying on trailing whitespace by replacing it with \x20.
267
s:\x20
1185.47.2 by Martin Pool
Finish rio format and tests.
268
\t"one"
269
\t
270
271
s: "
272
273
s: ""
274
275
s: """
276
3943.8.2 by Marius Kruger
fix tests relying on trailing whitespace by replacing it with \x20.
277
s:\x20
1185.47.2 by Martin Pool
Finish rio format and tests.
278
\t
279
280
s: \\
281
3943.8.2 by Marius Kruger
fix tests relying on trailing whitespace by replacing it with \x20.
282
s:\x20
1185.47.2 by Martin Pool
Finish rio format and tests.
283
\t\\
284
\t\\\\
285
\t
286
287
s: word\\
288
289
s: quote"
290
291
s: backslashes\\\\\\
292
293
s: both\\\"
294
295
''')
1185.47.1 by Martin Pool
[broken] start converting basic_io to more rfc822-like format
296
        tmpf.seek(0)
297
        expected_vals = ['"one"',
7143.15.2 by Jelmer Vernooij
Run autopep8.
298
                         '\n"one"\n',
299
                         '"',
300
                         '""',
301
                         '"""',
302
                         '\n',
303
                         '\\',
304
                         '\n\\\n\\\\\n',
305
                         'word\\',
306
                         'quote\"',
307
                         'backslashes\\\\\\',
308
                         'both\\\"',
309
                         ]
1185.47.1 by Martin Pool
[broken] start converting basic_io to more rfc822-like format
310
        for expected in expected_vals:
311
            stanza = read_stanza(tmpf)
1534.10.2 by Aaron Bentley
Implemented rio_file to produce a light file object from stanzas
312
            self.rio_file_stanzas([stanza])
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
313
            self.assertEqual(len(stanza), 1)
6684.1.7 by Martin
Update rio tests to pass on Python 3
314
            self.assertEqual(stanza.get('s'), expected)
1185.47.1 by Martin Pool
[broken] start converting basic_io to more rfc822-like format
315
316
    def test_write_empty_stanza(self):
317
        """Write empty stanza"""
318
        l = list(Stanza().to_lines())
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
319
        self.assertEqual(l, [])
1553.5.7 by Martin Pool
rio.Stanza.add should raise TypeError on invalid types.
320
321
    def test_rio_raises_type_error(self):
322
        """TypeError on adding invalid type to Stanza"""
323
        s = Stanza()
324
        self.assertRaises(TypeError, s.add, 'foo', {})
325
326
    def test_rio_raises_type_error_key(self):
327
        """TypeError on adding invalid type to Stanza"""
328
        s = Stanza()
329
        self.assertRaises(TypeError, s.add, 10, {})
1553.5.32 by Martin Pool
rio files are always externalized in utf-8. test this.
330
7490.74.1 by Jelmer Vernooij
Use surrogateescape in rio.
331
    def test_rio_surrogateescape(self):
332
        raw_bytes = b'\xcb'
333
        self.assertRaises(UnicodeDecodeError, raw_bytes.decode, 'utf-8')
7490.74.3 by Jelmer Vernooij
Fix tests.
334
        try:
335
            uni_data = raw_bytes.decode('utf-8', 'surrogateescape')
336
        except LookupError:
337
            self.skipTest('surrogateescape is not available on Python < 3')
7490.74.1 by Jelmer Vernooij
Use surrogateescape in rio.
338
        s = Stanza(foo=uni_data)
339
        self.assertEqual(s.get('foo'), uni_data)
340
        raw_lines = s.to_lines()
341
        self.assertEqual(raw_lines,
342
                         [b'foo: ' + uni_data.encode('utf-8', 'surrogateescape') + b'\n'])
343
        new_s = read_stanza(raw_lines)
344
        self.assertEqual(new_s.get('foo'), uni_data)
345
1553.5.32 by Martin Pool
rio files are always externalized in utf-8. test this.
346
    def test_rio_unicode(self):
347
        uni_data = u'\N{KATAKANA LETTER O}'
348
        s = Stanza(foo=uni_data)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
349
        self.assertEqual(s.get('foo'), uni_data)
1553.5.32 by Martin Pool
rio files are always externalized in utf-8. test this.
350
        raw_lines = s.to_lines()
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
351
        self.assertEqual(raw_lines,
7143.15.2 by Jelmer Vernooij
Run autopep8.
352
                         [b'foo: ' + uni_data.encode('utf-8') + b'\n'])
1553.5.32 by Martin Pool
rio files are always externalized in utf-8. test this.
353
        new_s = read_stanza(raw_lines)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
354
        self.assertEqual(new_s.get('foo'), uni_data)
1553.5.32 by Martin Pool
rio files are always externalized in utf-8. test this.
355
2030.1.1 by John Arbash Meinel
Make it easier to nest Stanzas with Unicode contents
356
    def test_rio_to_unicode(self):
357
        uni_data = u'\N{KATAKANA LETTER O}'
358
        s = Stanza(foo=uni_data)
2030.1.5 by John Arbash Meinel
Create a 'read_stanza_unicode' to handle unicode processing
359
        unicode_str = s.to_unicode()
360
        self.assertEqual(u'foo: %s\n' % (uni_data,), unicode_str)
361
        new_s = rio.read_stanza_unicode(unicode_str.splitlines(True))
362
        self.assertEqual(uni_data, new_s.get('foo'))
2030.1.1 by John Arbash Meinel
Make it easier to nest Stanzas with Unicode contents
363
364
    def test_nested_rio_unicode(self):
365
        uni_data = u'\N{KATAKANA LETTER O}'
366
        s = Stanza(foo=uni_data)
367
        parent_stanza = Stanza(child=s.to_unicode())
368
        raw_lines = parent_stanza.to_lines()
6684.1.7 by Martin
Update rio tests to pass on Python 3
369
        self.assertEqual([b'child: foo: ' + uni_data.encode('utf-8') + b'\n',
370
                          b'\t\n',
7143.15.2 by Jelmer Vernooij
Run autopep8.
371
                          ], raw_lines)
2030.1.1 by John Arbash Meinel
Make it easier to nest Stanzas with Unicode contents
372
        new_parent = read_stanza(raw_lines)
373
        child_text = new_parent.get('child')
374
        self.assertEqual(u'foo: %s\n' % uni_data, child_text)
2030.1.5 by John Arbash Meinel
Create a 'read_stanza_unicode' to handle unicode processing
375
        new_child = rio.read_stanza_unicode(child_text.splitlines(True))
2030.1.1 by John Arbash Meinel
Make it easier to nest Stanzas with Unicode contents
376
        self.assertEqual(uni_data, new_child.get('foo'))
1551.12.1 by Aaron Bentley
Basic RIO patch-compatible format is working
377
1551.12.22 by Aaron Bentley
Fix handling of whitespace-stripping without newline munging
378
    def mail_munge(self, lines, dos_nl=True):
1551.12.1 by Aaron Bentley
Basic RIO patch-compatible format is working
379
        new_lines = []
380
        for line in lines:
6684.1.7 by Martin
Update rio tests to pass on Python 3
381
            line = re.sub(b' *\n', b'\n', line)
1551.12.22 by Aaron Bentley
Fix handling of whitespace-stripping without newline munging
382
            if dos_nl:
6684.1.7 by Martin
Update rio tests to pass on Python 3
383
                line = re.sub(b'([^\r])\n', b'\\1\r\n', line)
1551.12.1 by Aaron Bentley
Basic RIO patch-compatible format is working
384
            new_lines.append(line)
385
        return new_lines
386
387
    def test_patch_rio(self):
1551.12.21 by Aaron Bentley
Patch-RIO does line breaks in slightly more readable places
388
        stanza = Stanza(data='#\n\r\\r ', space=' ' * 255, hash='#' * 255)
1551.12.1 by Aaron Bentley
Basic RIO patch-compatible format is working
389
        lines = rio.to_patch_lines(stanza)
390
        for line in lines:
6684.1.7 by Martin
Update rio tests to pass on Python 3
391
            self.assertContainsRe(line, b'^# ')
1551.12.10 by Aaron Bentley
Reduce max width to 72
392
            self.assertTrue(72 >= len(line))
393
        for line in rio.to_patch_lines(stanza, max_width=12):
394
            self.assertTrue(12 >= len(line))
1551.12.22 by Aaron Bentley
Fix handling of whitespace-stripping without newline munging
395
        new_stanza = rio.read_patch_stanza(self.mail_munge(lines,
396
                                                           dos_nl=False))
1551.12.1 by Aaron Bentley
Basic RIO patch-compatible format is working
397
        lines = self.mail_munge(lines)
398
        new_stanza = rio.read_patch_stanza(lines)
399
        self.assertEqual('#\n\r\\r ', new_stanza.get('data'))
7143.15.2 by Jelmer Vernooij
Run autopep8.
400
        self.assertEqual(' ' * 255, new_stanza.get('space'))
401
        self.assertEqual('#' * 255, new_stanza.get('hash'))
1551.12.21 by Aaron Bentley
Patch-RIO does line breaks in slightly more readable places
402
403
    def test_patch_rio_linebreaks(self):
7143.15.2 by Jelmer Vernooij
Run autopep8.
404
        stanza = Stanza(breaktest='linebreak -/' * 30)
1551.12.21 by Aaron Bentley
Patch-RIO does line breaks in slightly more readable places
405
        self.assertContainsRe(rio.to_patch_lines(stanza, 71)[0],
6684.1.7 by Martin
Update rio tests to pass on Python 3
406
                              b'linebreak\\\\\n')
7143.15.2 by Jelmer Vernooij
Run autopep8.
407
        stanza = Stanza(breaktest='linebreak-/' * 30)
1551.12.21 by Aaron Bentley
Patch-RIO does line breaks in slightly more readable places
408
        self.assertContainsRe(rio.to_patch_lines(stanza, 70)[0],
6684.1.7 by Martin
Update rio tests to pass on Python 3
409
                              b'linebreak-\\\\\n')
7143.15.2 by Jelmer Vernooij
Run autopep8.
410
        stanza = Stanza(breaktest='linebreak/' * 30)
1551.12.21 by Aaron Bentley
Patch-RIO does line breaks in slightly more readable places
411
        self.assertContainsRe(rio.to_patch_lines(stanza, 70)[0],
6684.1.7 by Martin
Update rio tests to pass on Python 3
412
                              b'linebreak\\\\\n')