~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_xml.py

  • Committer: Vincent Ladeuil
  • Date: 2013-05-25 17:12:43 UTC
  • mto: (6437.77.1 2.5)
  • mto: This revision was merged to the branch mainline in revision 6577.
  • Revision ID: v.ladeuil+lp@free.fr-20130525171243-au0073fnspecl3kg
Empty arguments in EDITOR are now properly preserved

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005-2011 Canonical Ltd
2
2
#
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
23
23
    xml6,
24
24
    xml7,
25
25
    xml8,
26
 
    serializer,
27
26
    )
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
31
29
import bzrlib.xml5
32
30
 
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" />
37
 
</inventory>"""
38
 
 
39
 
 
40
 
_revision_v4 = """<revision committer="Martin Pool &lt;mbp@sourcefrog.net&gt;"
41
 
    inventory_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
42
 
    inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
43
 
    revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
44
 
    timestamp="1125907235.212"
45
 
    timezone="36000">
46
 
<message>- start splitting code for xml (de)serialization away from objects
47
 
  preparatory to supporting multiple formats by a single library
48
 
</message>
49
 
<parents>
50
 
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92" revision_sha1="7bdf4cc8c5bdac739f8cf9b10b78cf4b68f915ff" />
51
 
</parents>
52
 
</revision>
53
 
"""
54
 
 
55
31
_revision_v5 = """<revision committer="Martin Pool &lt;mbp@sourcefrog.net&gt;"
56
32
    inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
57
33
    revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
218
194
class TestSerializer(TestCase):
219
195
    """Test XML serialization"""
220
196
 
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)
227
 
 
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
233
 
        eq(rev.committer,
234
 
           "Martin Pool <mbp@sourcefrog.net>")
235
 
        eq(rev.inventory_id,
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")
240
 
 
241
197
    def test_unpack_revision_5(self):
242
198
        """Test unpacking a canned revision v5"""
243
199
        inp = StringIO(_revision_v5)
552
508
    def setUp(self):
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)
558
513
 
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)
568
523
 
569
524
    def test_ascii_with_xml(self):
570
525
        self.assertEqual('&amp;&apos;&quot;&lt;&gt;"',
571
 
                         bzrlib.xml8._encode_and_escape('&\'"<>'))
 
526
                         bzrlib.xml_serializer.encode_and_escape('&\'"<>'))
572
527
 
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('&#181;&#229;&amp;&#1580;"',
577
 
                         bzrlib.xml8._encode_and_escape(utf8_str))
 
532
                         bzrlib.xml_serializer.encode_and_escape(utf8_str))
578
533
 
579
534
    def test_unicode(self):
580
535
        uni_str = u'\xb5\xe5&\u062c'
581
536
        self.assertEqual('&#181;&#229;&amp;&#1580;"',
582
 
                         bzrlib.xml8._encode_and_escape(uni_str))
 
537
                         bzrlib.xml_serializer.encode_and_escape(uni_str))
 
538
 
 
539
 
 
540
class TestMisc(TestCase):
 
541
 
 
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;')