28
27
from bzrlib.tests import TestCase
29
from bzrlib.inventory import Inventory, InventoryEntry
30
from bzrlib.xml4 import serializer_v4
28
from bzrlib.inventory import Inventory
33
_working_inventory_v4 = """<inventory file_id="TREE_ROOT">
34
<entry file_id="bar-20050901064931-73b4b1138abc9cd2" kind="file" name="bar" parent_id="TREE_ROOT" />
35
<entry file_id="foo-20050801201819-4139aa4a272f4250" kind="directory" name="foo" parent_id="TREE_ROOT" />
36
<entry file_id="bar-20050824000535-6bc48cfad47ed134" kind="file" name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" />
40
_revision_v4 = """<revision committer="Martin Pool <mbp@sourcefrog.net>"
41
inventory_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
42
inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
43
revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
44
timestamp="1125907235.212"
46
<message>- start splitting code for xml (de)serialization away from objects
47
preparatory to supporting multiple formats by a single library
50
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92" revision_sha1="7bdf4cc8c5bdac739f8cf9b10b78cf4b68f915ff" />
55
31
_revision_v5 = """<revision committer="Martin Pool <mbp@sourcefrog.net>"
56
32
inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
57
33
revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
218
194
class TestSerializer(TestCase):
219
195
"""Test XML serialization"""
221
def test_canned_inventory(self):
222
"""Test unpacked a canned inventory v4 file."""
223
inp = StringIO(_working_inventory_v4)
224
inv = serializer_v4.read_inventory(inp)
225
self.assertEqual(len(inv), 4)
226
self.assert_('bar-20050901064931-73b4b1138abc9cd2' in inv)
228
def test_unpack_revision(self):
229
"""Test unpacking a canned revision v4"""
230
inp = StringIO(_revision_v4)
231
rev = serializer_v4.read_revision(inp)
232
eq = self.assertEqual
234
"Martin Pool <mbp@sourcefrog.net>")
236
"mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9")
237
eq(len(rev.parent_ids), 1)
238
eq(rev.parent_ids[0],
239
"mbp@sourcefrog.net-20050905063503-43948f59fa127d92")
241
197
def test_unpack_revision_5(self):
242
198
"""Test unpacking a canned revision v5"""
243
199
inp = StringIO(_revision_v5)
458
414
self.assertEqual('tree-root-321', inv2['nested-id'].parent_id)
459
415
self.assertEqual('rev-outer', inv2['nested-id'].revision)
460
416
self.assertEqual('rev-inner', inv2['nested-id'].reference_revision)
461
self.assertRaises(errors.UnsupportedInventoryKind,
462
s_v6.read_inventory_from_string,
463
txt.replace('format="7"', 'format="6"'))
464
self.assertRaises(errors.UnsupportedInventoryKind,
465
s_v5.read_inventory_from_string,
466
txt.replace('format="7"', 'format="5"'))
468
418
def test_roundtrip_inventory_v8(self):
469
419
inv = self.get_sample_inventory()
550
500
"""Whitebox testing of the _encode_and_escape function."""
503
super(TestEncodeAndEscape, self).setUp()
554
504
# Keep the cache clear before and after the test
555
bzrlib.xml8._ensure_utf8_re()
556
bzrlib.xml8._clear_cache()
557
self.addCleanup(bzrlib.xml8._clear_cache)
505
bzrlib.xml_serializer._clear_cache()
506
self.addCleanup(bzrlib.xml_serializer._clear_cache)
559
508
def test_simple_ascii(self):
560
509
# _encode_and_escape always appends a final ", because these parameters
561
510
# are being used in xml attributes, and by returning it now, we have to
562
511
# do fewer string operations later.
563
val = bzrlib.xml8._encode_and_escape('foo bar')
512
val = bzrlib.xml_serializer.encode_and_escape('foo bar')
564
513
self.assertEqual('foo bar"', val)
565
514
# The second time should be cached
566
val2 = bzrlib.xml8._encode_and_escape('foo bar')
515
val2 = bzrlib.xml_serializer.encode_and_escape('foo bar')
567
516
self.assertIs(val2, val)
569
518
def test_ascii_with_xml(self):
570
519
self.assertEqual('&'"<>"',
571
bzrlib.xml8._encode_and_escape('&\'"<>'))
520
bzrlib.xml_serializer.encode_and_escape('&\'"<>'))
573
522
def test_utf8_with_xml(self):
574
523
# u'\xb5\xe5&\u062c'
575
524
utf8_str = '\xc2\xb5\xc3\xa5&\xd8\xac'
576
525
self.assertEqual('µå&ج"',
577
bzrlib.xml8._encode_and_escape(utf8_str))
526
bzrlib.xml_serializer.encode_and_escape(utf8_str))
579
528
def test_unicode(self):
580
529
uni_str = u'\xb5\xe5&\u062c'
581
530
self.assertEqual('µå&ج"',
582
bzrlib.xml8._encode_and_escape(uni_str))
531
bzrlib.xml_serializer.encode_and_escape(uni_str))
534
class TestMisc(TestCase):
536
def test_unescape_xml(self):
537
"""We get some kind of error when malformed entities are passed"""
538
self.assertRaises(KeyError, bzrlib.xml8._unescape_xml, 'foo&bar;')