1
# Copyright (C) 2005-2010 Canonical Ltd
1
# Copyright (C) 2005-2011 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
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)
553
509
TestCase.setUp(self)
554
510
# 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)
511
bzrlib.xml_serializer._clear_cache()
512
self.addCleanup(bzrlib.xml_serializer._clear_cache)
559
514
def test_simple_ascii(self):
560
515
# _encode_and_escape always appends a final ", because these parameters
561
516
# are being used in xml attributes, and by returning it now, we have to
562
517
# do fewer string operations later.
563
val = bzrlib.xml8._encode_and_escape('foo bar')
518
val = bzrlib.xml_serializer.encode_and_escape('foo bar')
564
519
self.assertEqual('foo bar"', val)
565
520
# The second time should be cached
566
val2 = bzrlib.xml8._encode_and_escape('foo bar')
521
val2 = bzrlib.xml_serializer.encode_and_escape('foo bar')
567
522
self.assertIs(val2, val)
569
524
def test_ascii_with_xml(self):
570
525
self.assertEqual('&'"<>"',
571
bzrlib.xml8._encode_and_escape('&\'"<>'))
526
bzrlib.xml_serializer.encode_and_escape('&\'"<>'))
573
528
def test_utf8_with_xml(self):
574
529
# u'\xb5\xe5&\u062c'
575
530
utf8_str = '\xc2\xb5\xc3\xa5&\xd8\xac'
576
531
self.assertEqual('µå&ج"',
577
bzrlib.xml8._encode_and_escape(utf8_str))
532
bzrlib.xml_serializer.encode_and_escape(utf8_str))
579
534
def test_unicode(self):
580
535
uni_str = u'\xb5\xe5&\u062c'
581
536
self.assertEqual('µå&ج"',
582
bzrlib.xml8._encode_and_escape(uni_str))
537
bzrlib.xml_serializer.encode_and_escape(uni_str))
540
class TestMisc(TestCase):
542
def test_unescape_xml(self):
543
"""We get some kind of error when malformed entities are passed"""
544
self.assertRaises(KeyError, bzrlib.xml8._unescape_xml, 'foo&bar;')