~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_pack.py

  • Committer: Robert Collins
  • Date: 2007-08-08 02:57:22 UTC
  • mto: This revision was merged to the branch mainline in revision 2687.
  • Revision ID: robertc@robertcollins.net-20070808025722-26wvnolkzmnse7s1
* The ``bzrlib.pack`` interface has changed to use tuples of bytestrings
  rather than just bytestrings, making it easier to represent multiple
  element names. As this interface was not used by any internal facilities
  since it was introduced in 0.18 no API compatibility is being preserved.
  The serialised form of these packs is identical with 0.18 when a single
  element tuple is in use. (Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
64
64
        output = StringIO()
65
65
        writer = pack.ContainerWriter(output.write)
66
66
        writer.begin()
67
 
        offset, length = writer.add_bytes_record('abc', names=['name1'])
 
67
        offset, length = writer.add_bytes_record('abc', names=[('name1', )])
68
68
        self.assertEqual((42, 13), (offset, length))
69
69
        self.assertEqual(
70
70
            'Bazaar pack format 1 (introduced in 0.18)\n'
76
76
        output = StringIO()
77
77
        writer = pack.ContainerWriter(output.write)
78
78
        writer.begin()
79
 
        offset, length = writer.add_bytes_record('abc', names=['name1', 'name2'])
80
 
        self.assertEqual((42, 19), (offset, length))
81
 
        self.assertEqual(
82
 
            'Bazaar pack format 1 (introduced in 0.18)\n'
83
 
            'B3\nname1\nname2\n\nabc',
 
79
        offset, length = writer.add_bytes_record('abc', names=[('name1', ), ('name2', )])
 
80
        self.assertEqual((42, 19), (offset, length))
 
81
        self.assertEqual(
 
82
            'Bazaar pack format 1 (introduced in 0.18)\n'
 
83
            'B3\nname1\nname2\n\nabc',
 
84
            output.getvalue())
 
85
 
 
86
    def test_add_bytes_record_two_names(self):
 
87
        """Add a bytes record with two names."""
 
88
        output = StringIO()
 
89
        writer = pack.ContainerWriter(output.write)
 
90
        writer.begin()
 
91
        offset, length = writer.add_bytes_record('abc', names=[('name1', ), ('name2', )])
 
92
        self.assertEqual((42, 19), (offset, length))
 
93
        self.assertEqual(
 
94
            'Bazaar pack format 1 (introduced in 0.18)\n'
 
95
            'B3\nname1\nname2\n\nabc',
 
96
            output.getvalue())
 
97
 
 
98
    def test_add_bytes_record_two_element_name(self):
 
99
        """Add a bytes record with a two-element name."""
 
100
        output = StringIO()
 
101
        writer = pack.ContainerWriter(output.write)
 
102
        writer.begin()
 
103
        offset, length = writer.add_bytes_record('abc', names=[('name1', 'name2')])
 
104
        self.assertEqual((42, 19), (offset, length))
 
105
        self.assertEqual(
 
106
            'Bazaar pack format 1 (introduced in 0.18)\n'
 
107
            'B3\nname1\x00name2\n\nabc',
84
108
            output.getvalue())
85
109
 
86
110
    def test_add_second_bytes_record_gets_higher_offset(self):
105
129
        writer.begin()
106
130
        self.assertRaises(
107
131
            errors.InvalidRecordError,
108
 
            writer.add_bytes_record, 'abc', names=['bad name'])
 
132
            writer.add_bytes_record, 'abc', names=[('bad name', )])
109
133
 
110
134
 
111
135
class TestContainerReader(tests.TestCase):
263
287
        """
264
288
        reader = self.get_reader_for("5\nname1\n\naaaaa")
265
289
        names, get_bytes = reader.read()
266
 
        self.assertEqual(['name1'], names)
 
290
        self.assertEqual([('name1', )], names)
267
291
        self.assertEqual('aaaaa', get_bytes(None))
268
292
 
269
293
    def test_record_with_two_names(self):
271
295
        """
272
296
        reader = self.get_reader_for("5\nname1\nname2\n\naaaaa")
273
297
        names, get_bytes = reader.read()
274
 
        self.assertEqual(['name1', 'name2'], names)
 
298
        self.assertEqual([('name1', ), ('name2', )], names)
 
299
        self.assertEqual('aaaaa', get_bytes(None))
 
300
 
 
301
    def test_record_with_two_part_names(self):
 
302
        """Reading a Bytes record with a two_part name reads both."""
 
303
        reader = self.get_reader_for("5\nname1\x00name2\n\naaaaa")
 
304
        names, get_bytes = reader.read()
 
305
        self.assertEqual([('name1', 'name2', )], names)
275
306
        self.assertEqual('aaaaa', get_bytes(None))
276
307
 
277
308
    def test_invalid_length(self):
399
430
        writer.begin()
400
431
        memos = []
401
432
        memos.append(writer.add_bytes_record('abc', names=[]))
402
 
        memos.append(writer.add_bytes_record('def', names=['name1']))
403
 
        memos.append(writer.add_bytes_record('ghi', names=['name2']))
 
433
        memos.append(writer.add_bytes_record('def', names=[('name1', )]))
 
434
        memos.append(writer.add_bytes_record('ghi', names=[('name2', )]))
404
435
        memos.append(writer.add_bytes_record('jkl', names=[]))
405
436
        writer.end()
406
437
        transport = self.get_transport()
410
441
        result = []
411
442
        for names, reader_func in reader.iter_records():
412
443
            result.append((names, reader_func(None)))
413
 
        self.assertEqual([([], 'abc'), (['name2'], 'ghi')], result)
 
444
        self.assertEqual([([], 'abc'), ([('name2', )], 'ghi')], result)
414
445
 
415
446
 
416
447
class TestReadvFile(tests.TestCaseWithTransport):