151
_expected_rev_v8 = """<revision committer="Martin Pool <mbp@sourcefrog.net>" format="8" inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41" revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9" timestamp="1125907235.212" timezone="36000">
152
<message>- start splitting code for xml (de)serialization away from objects
153
preparatory to supporting multiple formats by a single library
156
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92" />
161
_expected_inv_v8 = """<inventory format="8" revision_id="rev_outer">
162
<directory file_id="tree-root-321" name="" revision="rev_outer" />
163
<directory file_id="dir-id" name="dir" parent_id="tree-root-321" revision="rev_outer" />
164
<file file_id="file-id" name="file" parent_id="tree-root-321" revision="rev_outer" text_sha1="A" text_size="1" />
165
<symlink file_id="link-id" name="link" parent_id="tree-root-321" revision="rev_outer" symlink_target="a" />
150
169
_revision_utf8_v5 = """<revision committer="Erik Bågfors <erik@foo.net>"
151
170
inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
152
171
revision_id="erik@bågfors-02"
330
349
new_rev = s_v5.read_revision_from_string(txt)
331
350
self.assertEqual(props, new_rev.properties)
333
def test_roundtrip_inventory_v7(self):
352
def get_sample_inventory(self):
334
353
inv = Inventory('tree-root-321', revision_id='rev_outer')
335
inv.add(inventory.TreeReference('nested-id', 'nested', 'tree-root-321',
336
'rev_outer', 'rev_inner'))
337
354
inv.add(inventory.InventoryFile('file-id', 'file', 'tree-root-321'))
338
355
inv.add(inventory.InventoryDirectory('dir-id', 'dir',
339
356
'tree-root-321'))
345
362
inv['file-id'].text_size = 1
346
363
inv['link-id'].revision = 'rev_outer'
347
364
inv['link-id'].symlink_target = 'a'
367
def test_roundtrip_inventory_v7(self):
368
inv = self.get_sample_inventory()
369
inv.add(inventory.TreeReference('nested-id', 'nested', 'tree-root-321',
370
'rev_outer', 'rev_inner'))
348
371
txt = xml7.serializer_v7.write_inventory_to_string(inv)
349
372
lines = xml7.serializer_v7.write_inventory_to_lines(inv)
350
373
self.assertEqual(bzrlib.osutils.split_lines(txt), lines)
389
412
s_v5.read_inventory_from_string,
390
413
txt.replace('format="7"', 'format="5"'))
415
def test_roundtrip_inventory_v8(self):
416
inv = self.get_sample_inventory()
417
txt = xml8.serializer_v8.write_inventory_to_string(inv)
418
inv2 = xml8.serializer_v8.read_inventory_from_string(txt)
419
self.assertEqual(4, len(inv2))
420
for path, ie in inv.iter_entries():
421
self.assertEqual(ie, inv2[ie.file_id])
423
def test_inventory_text_v8(self):
424
inv = self.get_sample_inventory()
425
txt = xml8.serializer_v8.write_inventory_to_string(inv)
426
lines = xml8.serializer_v8.write_inventory_to_lines(inv)
427
self.assertEqual(bzrlib.osutils.split_lines(txt), lines)
428
self.assertEqualDiff(_expected_inv_v8, txt)
430
def test_revision_text_v6(self):
431
"""Pack revision to XML v6"""
432
rev = bzrlib.xml6.serializer_v6.read_revision_from_string(
434
serialized = bzrlib.xml6.serializer_v6.write_revision_to_string(rev)
435
self.assertEqualDiff(serialized, _expected_rev_v5)
437
def test_revision_text_v7(self):
438
"""Pack revision to XML v7"""
439
rev = bzrlib.xml7.serializer_v7.read_revision_from_string(
441
serialized = bzrlib.xml7.serializer_v7.write_revision_to_string(rev)
442
self.assertEqualDiff(serialized, _expected_rev_v5)
444
def test_revision_text_v8(self):
445
"""Pack revision to XML v8"""
446
rev = bzrlib.xml8.serializer_v8.read_revision_from_string(
448
serialized = bzrlib.xml8.serializer_v8.write_revision_to_string(rev)
449
self.assertEqualDiff(serialized, _expected_rev_v8)
392
451
def test_revision_ids_are_utf8(self):
393
452
"""Parsed revision_ids should all be utf-8 strings, not unicode."""
394
453
s_v5 = bzrlib.xml5.serializer_v5
442
501
xml_serializer.format_registry.get('6'))
443
502
self.assertIs(bzrlib.xml7.serializer_v7,
444
503
xml_serializer.format_registry.get('7'))
504
self.assertIs(bzrlib.xml8.serializer_v8,
505
xml_serializer.format_registry.get('8'))
447
508
class TestEncodeAndEscape(TestCase):
451
512
# Keep the cache clear before and after the test
452
bzrlib.xml5._ensure_utf8_re()
453
bzrlib.xml5._clear_cache()
454
self.addCleanup(bzrlib.xml5._clear_cache)
513
bzrlib.xml8._ensure_utf8_re()
514
bzrlib.xml8._clear_cache()
515
self.addCleanup(bzrlib.xml8._clear_cache)
456
517
def test_simple_ascii(self):
457
518
# _encode_and_escape always appends a final ", because these parameters
458
519
# are being used in xml attributes, and by returning it now, we have to
459
520
# do fewer string operations later.
460
val = bzrlib.xml5._encode_and_escape('foo bar')
521
val = bzrlib.xml8._encode_and_escape('foo bar')
461
522
self.assertEqual('foo bar"', val)
462
523
# The second time should be cached
463
val2 = bzrlib.xml5._encode_and_escape('foo bar')
524
val2 = bzrlib.xml8._encode_and_escape('foo bar')
464
525
self.assertIs(val2, val)
466
527
def test_ascii_with_xml(self):
467
528
self.assertEqual('&'"<>"',
468
bzrlib.xml5._encode_and_escape('&\'"<>'))
529
bzrlib.xml8._encode_and_escape('&\'"<>'))
470
531
def test_utf8_with_xml(self):
471
532
# u'\xb5\xe5&\u062c'
472
533
utf8_str = '\xc2\xb5\xc3\xa5&\xd8\xac'
473
534
self.assertEqual('µå&ج"',
474
bzrlib.xml5._encode_and_escape(utf8_str))
535
bzrlib.xml8._encode_and_escape(utf8_str))
476
537
def test_unicode(self):
477
538
uni_str = u'\xb5\xe5&\u062c'
478
539
self.assertEqual('µå&ج"',
479
bzrlib.xml5._encode_and_escape(uni_str))
540
bzrlib.xml8._encode_and_escape(uni_str))