~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_xml.py

  • Committer: Vincent Ladeuil
  • Date: 2016-02-01 18:06:32 UTC
  • mto: (6614.2.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 6615.
  • Revision ID: v.ladeuil+lp@free.fr-20160201180632-xgp7k7z5bda9ap1w
Fix assertEquals being deprecated by using assertEqual.

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)
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"'))
467
417
 
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."""
551
501
 
552
502
    def setUp(self):
553
 
        TestCase.setUp(self)
 
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)
558
507
 
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)
568
517
 
569
518
    def test_ascii_with_xml(self):
570
519
        self.assertEqual('&amp;&apos;&quot;&lt;&gt;"',
571
 
                         bzrlib.xml8._encode_and_escape('&\'"<>'))
 
520
                         bzrlib.xml_serializer.encode_and_escape('&\'"<>'))
572
521
 
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('&#181;&#229;&amp;&#1580;"',
577
 
                         bzrlib.xml8._encode_and_escape(utf8_str))
 
526
                         bzrlib.xml_serializer.encode_and_escape(utf8_str))
578
527
 
579
528
    def test_unicode(self):
580
529
        uni_str = u'\xb5\xe5&\u062c'
581
530
        self.assertEqual('&#181;&#229;&amp;&#1580;"',
582
 
                         bzrlib.xml8._encode_and_escape(uni_str))
 
531
                         bzrlib.xml_serializer.encode_and_escape(uni_str))
 
532
 
 
533
 
 
534
class TestMisc(TestCase):
 
535
 
 
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;')