133
_revision_utf8_v5 = """<revision committer="Erik Bågfors <erik@foo.net>"
134
inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
135
revision_id="erik@bågfors-02"
136
timestamp="1125907235.212"
138
<message>Include µnicode characters
141
<revision_ref revision_id="erik@bågfors-01"/>
146
_inventory_utf8_v5 = """<inventory file_id="TREé_ROOT" format="5"
147
revision_id="erik@bågfors-02">
148
<file file_id="bår-01"
149
name="bår" parent_id="TREé_ROOT"
150
revision="erik@bågfors-01"/>
151
<directory name="sµbdir"
152
file_id="sµbdir-01"
153
parent_id="TREé_ROOT"
154
revision="erik@bågfors-01"/>
155
<file executable="yes" file_id="bår-02"
156
name="bår" parent_id="sµbdir-01"
157
revision="erik@bågfors-02"/>
133
162
class TestSerializer(TestCase):
134
163
"""Test XML serialization"""
135
164
def test_canned_inventory(self):
259
288
txt = s_v5.write_revision_to_string(rev)
260
289
new_rev = s_v5.read_revision_from_string(txt)
261
290
self.assertEqual(props, new_rev.properties)
292
def test_revision_ids_are_utf8(self):
293
"""Parsed revision_ids should all be utf-8 strings, not unicode."""
294
s_v5 = bzrlib.xml5.serializer_v5
295
rev = s_v5.read_revision_from_string(_revision_utf8_v5)
296
self.assertEqual('erik@b\xc3\xa5gfors-02', rev.revision_id)
297
self.assertIsInstance(rev.revision_id, str)
298
self.assertEqual(['erik@b\xc3\xa5gfors-01'], rev.parent_ids)
299
for parent_id in rev.parent_ids:
300
self.assertIsInstance(parent_id, str)
301
self.assertEqual(u'Include \xb5nicode characters\n', rev.message)
302
self.assertIsInstance(rev.message, unicode)
304
# ie.revision should either be None or a utf-8 revision id
305
inv = s_v5.read_inventory_from_string(_inventory_utf8_v5)
306
rev_id_1 = u'erik@b\xe5gfors-01'.encode('utf8')
307
rev_id_2 = u'erik@b\xe5gfors-02'.encode('utf8')
308
fid_root = u'TRE\xe9_ROOT'.encode('utf8')
309
fid_bar1 = u'b\xe5r-01'.encode('utf8')
310
fid_sub = u's\xb5bdir-01'.encode('utf8')
311
fid_bar2 = u'b\xe5r-02'.encode('utf8')
312
expected = [(u'', fid_root, None, None),
313
(u'b\xe5r', fid_bar1, fid_root, rev_id_1),
314
(u's\xb5bdir', fid_sub, fid_root, rev_id_1),
315
(u's\xb5bdir/b\xe5r', fid_bar2, fid_sub, rev_id_2),
317
self.assertEqual(rev_id_2, inv.revision_id)
318
self.assertIsInstance(inv.revision_id, str)
320
actual = list(inv.iter_entries_by_dir())
321
for ((exp_path, exp_file_id, exp_parent_id, exp_rev_id),
322
(act_path, act_ie)) in zip(expected, actual):
323
self.assertEqual(exp_path, act_path)
324
self.assertIsInstance(act_path, unicode)
325
self.assertEqual(exp_file_id, act_ie.file_id)
326
self.assertIsInstance(act_ie.file_id, str)
327
self.assertEqual(exp_parent_id, act_ie.parent_id)
328
if exp_parent_id is not None:
329
self.assertIsInstance(act_ie.parent_id, str)
330
self.assertEqual(exp_rev_id, act_ie.revision)
331
if exp_rev_id is not None:
332
self.assertIsInstance(act_ie.revision, str)
334
self.assertEqual(len(expected), len(actual))
337
class TestEncodeAndEscape(TestCase):
338
"""Whitebox testing of the _encode_and_escape function."""
341
# Keep the cache clear before and after the test
342
bzrlib.xml5._ensure_utf8_re()
343
bzrlib.xml5._clear_cache()
344
self.addCleanup(bzrlib.xml5._clear_cache)
346
def test_simple_ascii(self):
347
# _encode_and_escape always appends a final ", because these parameters
348
# are being used in xml attributes, and by returning it now, we have to
349
# do fewer string operations later.
350
val = bzrlib.xml5._encode_and_escape('foo bar')
351
self.assertEqual('foo bar"', val)
352
# The second time should be cached
353
val2 = bzrlib.xml5._encode_and_escape('foo bar')
354
self.assertIs(val2, val)
356
def test_ascii_with_xml(self):
357
self.assertEqual('&'"<>"',
358
bzrlib.xml5._encode_and_escape('&\'"<>'))
360
def test_utf8_with_xml(self):
362
utf8_str = '\xc2\xb5\xc3\xa5&\xd8\xac'
363
self.assertEqual('µå&ج"',
364
bzrlib.xml5._encode_and_escape(utf8_str))
366
def test_unicode(self):
367
uni_str = u'\xb5\xe5&\u062c'
368
self.assertEqual('µå&ج"',
369
bzrlib.xml5._encode_and_escape(uni_str))