~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_xml.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-04-17 07:55:35 UTC
  • mfrom: (3311.3.8 pack-1.4)
  • Revision ID: pqm@pqm.ubuntu.com-20080417075535-shpsll72xkrz5l3m
Implement xml8 serializer (abentley)

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
    errors, 
21
21
    inventory, 
22
22
    xml7,
 
23
    xml8,
23
24
    xml_serializer,
24
25
    )
25
26
from bzrlib.tests import TestCase
147
148
</inventory>
148
149
"""
149
150
 
 
151
_expected_rev_v8 = """<revision committer="Martin Pool &lt;mbp@sourcefrog.net&gt;" 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
 
154
</message>
 
155
<parents>
 
156
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92" />
 
157
</parents>
 
158
</revision>
 
159
"""
 
160
 
 
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" />
 
166
</inventory>
 
167
"""
 
168
 
150
169
_revision_utf8_v5 = """<revision committer="Erik B&#229;gfors &lt;erik@foo.net&gt;"
151
170
    inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
152
171
    revision_id="erik@b&#229;gfors-02"
330
349
        new_rev = s_v5.read_revision_from_string(txt)
331
350
        self.assertEqual(props, new_rev.properties)
332
351
 
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'
 
365
        return inv
 
366
 
 
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"'))
391
414
 
 
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])
 
422
 
 
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)
 
429
 
 
430
    def test_revision_text_v6(self):
 
431
        """Pack revision to XML v6"""
 
432
        rev = bzrlib.xml6.serializer_v6.read_revision_from_string(
 
433
            _expected_rev_v5)
 
434
        serialized = bzrlib.xml6.serializer_v6.write_revision_to_string(rev)
 
435
        self.assertEqualDiff(serialized, _expected_rev_v5)
 
436
 
 
437
    def test_revision_text_v7(self):
 
438
        """Pack revision to XML v7"""
 
439
        rev = bzrlib.xml7.serializer_v7.read_revision_from_string(
 
440
            _expected_rev_v5)
 
441
        serialized = bzrlib.xml7.serializer_v7.write_revision_to_string(rev)
 
442
        self.assertEqualDiff(serialized, _expected_rev_v5)
 
443
 
 
444
    def test_revision_text_v8(self):
 
445
        """Pack revision to XML v8"""
 
446
        rev = bzrlib.xml8.serializer_v8.read_revision_from_string(
 
447
            _expected_rev_v8)
 
448
        serialized = bzrlib.xml8.serializer_v8.write_revision_to_string(rev)
 
449
        self.assertEqualDiff(serialized, _expected_rev_v8)
 
450
 
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'))
445
506
 
446
507
 
447
508
class TestEncodeAndEscape(TestCase):
449
510
 
450
511
    def setUp(self):
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)
455
516
 
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)
465
526
 
466
527
    def test_ascii_with_xml(self):
467
528
        self.assertEqual('&amp;&apos;&quot;&lt;&gt;"',
468
 
                         bzrlib.xml5._encode_and_escape('&\'"<>'))
 
529
                         bzrlib.xml8._encode_and_escape('&\'"<>'))
469
530
 
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('&#181;&#229;&amp;&#1580;"',
474
 
                         bzrlib.xml5._encode_and_escape(utf8_str))
 
535
                         bzrlib.xml8._encode_and_escape(utf8_str))
475
536
 
476
537
    def test_unicode(self):
477
538
        uni_str = u'\xb5\xe5&\u062c'
478
539
        self.assertEqual('&#181;&#229;&amp;&#1580;"',
479
 
                         bzrlib.xml5._encode_and_escape(uni_str))
 
540
                         bzrlib.xml8._encode_and_escape(uni_str))