14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Tests for bzrlib.container."""
17
"""Tests for bzrlib.pack."""
20
20
from cStringIO import StringIO
22
from bzrlib import container, errors
22
from bzrlib import pack, errors
23
23
from bzrlib.tests import TestCase
31
31
This uses None as the output stream to show that the constructor doesn't
32
32
try to use the output stream.
34
writer = container.ContainerWriter(None)
34
writer = pack.ContainerWriter(None)
36
36
def test_begin(self):
37
37
"""Test the begin() method."""
38
38
output = StringIO()
39
writer = container.ContainerWriter(output.write)
39
writer = pack.ContainerWriter(output.write)
41
41
self.assertEqual('bzr pack format 1\n', output.getvalue())
43
43
def test_end(self):
44
44
"""Test the end() method."""
45
45
output = StringIO()
46
writer = container.ContainerWriter(output.write)
46
writer = pack.ContainerWriter(output.write)
49
49
self.assertEqual('bzr pack format 1\nE', output.getvalue())
83
83
This uses None as the output stream to show that the constructor doesn't
84
84
try to use the input stream.
86
reader = container.ContainerReader(None)
86
reader = pack.ContainerReader(None)
88
88
def test_empty_container(self):
89
89
"""Read an empty container."""
90
90
input = StringIO("bzr pack format 1\nE")
91
reader = container.ContainerReader(input.read)
91
reader = pack.ContainerReader(input.read)
92
92
self.assertEqual([], list(reader.iter_records()))
94
94
def test_unknown_format(self):
95
95
"""Unrecognised container formats raise UnknownContainerFormatError."""
96
96
input = StringIO("unknown format\n")
97
reader = container.ContainerReader(input.read)
97
reader = pack.ContainerReader(input.read)
99
99
errors.UnknownContainerFormatError, reader.iter_records)
111
111
def test_unknown_record_type(self):
112
112
"""Unknown record types cause UnknownRecordTypeError to be raised."""
113
113
input = StringIO("bzr pack format 1\nX")
114
reader = container.ContainerReader(input.read)
114
reader = pack.ContainerReader(input.read)
115
115
iterator = reader.iter_records()
116
116
self.assertRaises(
117
117
errors.UnknownRecordTypeError, iterator.next)
119
# XXX: refactor Bytes record parsing into a seperate BytesRecordReader for
120
# better unit testing.
121
def test_one_unnamed_record(self):
122
"""Read a container with one Bytes record."""
119
def test_container_with_one_unnamed_record(self):
120
"""Read a container with one Bytes record.
122
Parsing Bytes records is more thoroughly exercised by XXX. This test is
123
here to ensure that ContainerReader's integration with BytesRecordReader
123
126
input = StringIO("bzr pack format 1\nB5\n\naaaaaE")
124
reader = container.ContainerReader(input.read)
127
reader = pack.ContainerReader(input.read)
125
128
expected_records = [([], 'aaaaa')]
126
129
self.assertEqual(expected_records, list(reader.iter_records()))
128
def test_one_named_record(self):
129
"""Read a container with one Bytes record with a single name."""
130
input = StringIO("bzr pack format 1\nB5\nname1\n\naaaaaE")
131
reader = container.ContainerReader(input.read)
132
expected_records = [(['name1'], 'aaaaa')]
133
self.assertEqual(expected_records, list(reader.iter_records()))
132
class TestBytesRecordReader(TestCase):
133
"""Tests for parsing Bytes records with BytesRecordReader."""
135
def test_record_with_no_name(self):
136
"""Reading a Bytes record with no name returns an empty list of
139
input = StringIO("5\n\naaaaa")
140
reader = pack.BytesRecordReader(input.read)
141
names, bytes = reader.read()
142
self.assertEqual([], names)
143
self.assertEqual('aaaaa', bytes)
145
def test_record_with_one_name(self):
146
"""Reading a Bytes record with one name returns a list of just that
149
input = StringIO("5\nname1\n\naaaaa")
150
reader = pack.BytesRecordReader(input.read)
151
names, bytes = reader.read()
152
self.assertEqual(['name1'], names)
153
self.assertEqual('aaaaa', bytes)
155
def test_record_with_two_names(self):
156
"""Reading a Bytes record with two names returns a list of both names.
158
input = StringIO("5\nname1\nname2\n\naaaaa")
159
reader = pack.BytesRecordReader(input.read)
160
names, bytes = reader.read()
161
self.assertEqual(['name1', 'name2'], names)
162
self.assertEqual('aaaaa', bytes)
164
def test_invalid_length(self):
165
"""If the length-prefix is not a number, parsing raises
168
input = StringIO("not a number\n")
169
reader = pack.BytesRecordReader(input.read)
170
self.assertRaises(errors.InvalidRecordError, reader.read)
136
172
# Other Bytes record parsing cases to test:
137
# - invalid length value
138
173
# - incomplete bytes (i.e. stream ends before $length bytes read)
139
174
# - _read_line encountering end of stream (at any time; during length,
140
175
# names, end of headers...)