~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_xml.py

  • Committer: Brad Crittenden
  • Date: 2007-02-26 20:56:10 UTC
  • mfrom: (2300 +trunk)
  • mto: (2293.1.5 bzr.dev)
  • mto: This revision was merged to the branch mainline in revision 2311.
  • Revision ID: brad.crittenden@canonical.com-20070226205610-44oatbxrjjz3ajwy
merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
130
130
"""
131
131
 
132
132
 
 
133
_revision_utf8_v5 = """<revision committer="Erik B&#229;gfors &lt;erik@foo.net&gt;"
 
134
    inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
 
135
    revision_id="erik@b&#229;gfors-02"
 
136
    timestamp="1125907235.212"
 
137
    timezone="36000">
 
138
<message>Include &#181;nicode characters
 
139
</message>
 
140
<parents>
 
141
<revision_ref revision_id="erik@b&#229;gfors-01"/>
 
142
</parents>
 
143
</revision>
 
144
"""
 
145
 
 
146
_inventory_utf8_v5 = """<inventory file_id="TRE&#233;_ROOT" format="5"
 
147
                                   revision_id="erik@b&#229;gfors-02">
 
148
<file file_id="b&#229;r-01"
 
149
      name="b&#229;r" parent_id="TRE&#233;_ROOT"
 
150
      revision="erik@b&#229;gfors-01"/>
 
151
<directory name="s&#181;bdir"
 
152
           file_id="s&#181;bdir-01"
 
153
           parent_id="TRE&#233;_ROOT"
 
154
           revision="erik@b&#229;gfors-01"/>
 
155
<file executable="yes" file_id="b&#229;r-02"
 
156
      name="b&#229;r" parent_id="s&#181;bdir-01"
 
157
      revision="erik@b&#229;gfors-02"/>
 
158
</inventory>
 
159
"""
 
160
 
 
161
 
133
162
class TestSerializer(TestCase):
134
163
    """Test XML serialization"""
135
164
    def test_canned_inventory(self):
259
288
        txt = s_v5.write_revision_to_string(rev)
260
289
        new_rev = s_v5.read_revision_from_string(txt)
261
290
        self.assertEqual(props, new_rev.properties)
 
291
 
 
292
    def test_revision_ids_are_utf8(self):
 
293
        """Parsed revision_ids should all be utf-8 strings, not unicode."""
 
294
        s_v5 = bzrlib.xml5.serializer_v5
 
295
        rev = s_v5.read_revision_from_string(_revision_utf8_v5)
 
296
        self.assertEqual('erik@b\xc3\xa5gfors-02', rev.revision_id)
 
297
        self.assertIsInstance(rev.revision_id, str)
 
298
        self.assertEqual(['erik@b\xc3\xa5gfors-01'], rev.parent_ids)
 
299
        for parent_id in rev.parent_ids:
 
300
            self.assertIsInstance(parent_id, str)
 
301
        self.assertEqual(u'Include \xb5nicode characters\n', rev.message)
 
302
        self.assertIsInstance(rev.message, unicode)
 
303
 
 
304
        # ie.revision should either be None or a utf-8 revision id
 
305
        inv = s_v5.read_inventory_from_string(_inventory_utf8_v5)
 
306
        rev_id_1 = u'erik@b\xe5gfors-01'.encode('utf8')
 
307
        rev_id_2 = u'erik@b\xe5gfors-02'.encode('utf8')
 
308
        fid_root = u'TRE\xe9_ROOT'.encode('utf8')
 
309
        fid_bar1 = u'b\xe5r-01'.encode('utf8')
 
310
        fid_sub = u's\xb5bdir-01'.encode('utf8')
 
311
        fid_bar2 = u'b\xe5r-02'.encode('utf8')
 
312
        expected = [(u'', fid_root, None, None),
 
313
                    (u'b\xe5r', fid_bar1, fid_root, rev_id_1),
 
314
                    (u's\xb5bdir', fid_sub, fid_root, rev_id_1),
 
315
                    (u's\xb5bdir/b\xe5r', fid_bar2, fid_sub, rev_id_2),
 
316
                   ]
 
317
        self.assertEqual(rev_id_2, inv.revision_id)
 
318
        self.assertIsInstance(inv.revision_id, str)
 
319
 
 
320
        actual = list(inv.iter_entries_by_dir())
 
321
        for ((exp_path, exp_file_id, exp_parent_id, exp_rev_id),
 
322
             (act_path, act_ie)) in zip(expected, actual):
 
323
            self.assertEqual(exp_path, act_path)
 
324
            self.assertIsInstance(act_path, unicode)
 
325
            self.assertEqual(exp_file_id, act_ie.file_id)
 
326
            self.assertIsInstance(act_ie.file_id, str)
 
327
            self.assertEqual(exp_parent_id, act_ie.parent_id)
 
328
            if exp_parent_id is not None:
 
329
                self.assertIsInstance(act_ie.parent_id, str)
 
330
            self.assertEqual(exp_rev_id, act_ie.revision)
 
331
            if exp_rev_id is not None:
 
332
                self.assertIsInstance(act_ie.revision, str)
 
333
 
 
334
        self.assertEqual(len(expected), len(actual))
 
335
 
 
336
 
 
337
class TestEncodeAndEscape(TestCase):
 
338
    """Whitebox testing of the _encode_and_escape function."""
 
339
 
 
340
    def setUp(self):
 
341
        # Keep the cache clear before and after the test
 
342
        bzrlib.xml5._ensure_utf8_re()
 
343
        bzrlib.xml5._clear_cache()
 
344
        self.addCleanup(bzrlib.xml5._clear_cache)
 
345
 
 
346
    def test_simple_ascii(self):
 
347
        # _encode_and_escape always appends a final ", because these parameters
 
348
        # are being used in xml attributes, and by returning it now, we have to
 
349
        # do fewer string operations later.
 
350
        val = bzrlib.xml5._encode_and_escape('foo bar')
 
351
        self.assertEqual('foo bar"', val)
 
352
        # The second time should be cached
 
353
        val2 = bzrlib.xml5._encode_and_escape('foo bar')
 
354
        self.assertIs(val2, val)
 
355
 
 
356
    def test_ascii_with_xml(self):
 
357
        self.assertEqual('&amp;&apos;&quot;&lt;&gt;"',
 
358
                         bzrlib.xml5._encode_and_escape('&\'"<>'))
 
359
 
 
360
    def test_utf8_with_xml(self):
 
361
        # u'\xb5\xe5&\u062c'
 
362
        utf8_str = '\xc2\xb5\xc3\xa5&\xd8\xac'
 
363
        self.assertEqual('&#181;&#229;&amp;&#1580;"',
 
364
                         bzrlib.xml5._encode_and_escape(utf8_str))
 
365
 
 
366
    def test_unicode(self):
 
367
        uni_str = u'\xb5\xe5&\u062c'
 
368
        self.assertEqual('&#181;&#229;&amp;&#1580;"',
 
369
                         bzrlib.xml5._encode_and_escape(uni_str))