|
8 | 8 |
|
9 | 9 | import pytest |
10 | 10 |
|
| 11 | +from mock import call |
| 12 | + |
11 | 13 | from docx.compat import BytesIO |
12 | | -from docx.image.constants import MIME_TYPE, TAG |
| 14 | +from docx.image.constants import MIME_TYPE, PNG_CHUNK_TYPE, TAG |
13 | 15 | from docx.image.exceptions import InvalidImageStreamError |
14 | 16 | from docx.image.helpers import BIG_ENDIAN, StreamReader |
15 | | -from docx.image.png import _Chunks, _ChunkParser, Png, _PngParser |
| 17 | +from docx.image.png import _Chunk, _Chunks, _ChunkParser, Png, _PngParser |
16 | 18 |
|
17 | 19 | from ..unitutil import ( |
18 | | - initializer_mock, class_mock, instance_mock, method_mock, test_file |
| 20 | + function_mock, class_mock, initializer_mock, instance_mock, method_mock, |
| 21 | + test_file |
19 | 22 | ) |
20 | 23 |
|
21 | 24 |
|
@@ -338,16 +341,72 @@ def it_can_construct_from_a_stream(self, from_stream_fixture): |
338 | 341 | _ChunkParser__init_.assert_called_once_with(stream_rdr_) |
339 | 342 | assert isinstance(chunk_parser, _ChunkParser) |
340 | 343 |
|
| 344 | + def it_can_iterate_over_the_chunks_in_its_png_stream(self, iter_fixture): |
| 345 | + # fixture ---------------------- |
| 346 | + chunk_parser, _iter_chunk_offsets_, _ChunkFactory_ = iter_fixture[:3] |
| 347 | + stream_rdr_, offsets, chunk_lst = iter_fixture[3:] |
| 348 | + # exercise --------------------- |
| 349 | + chunks = [chunk for chunk in chunk_parser.iter_chunks()] |
| 350 | + # verify ----------------------- |
| 351 | + _iter_chunk_offsets_.assert_called_once_with() |
| 352 | + assert _ChunkFactory_.call_args_list == [ |
| 353 | + call(PNG_CHUNK_TYPE.IHDR, stream_rdr_, offsets[0]), |
| 354 | + call(PNG_CHUNK_TYPE.pHYs, stream_rdr_, offsets[1]), |
| 355 | + ] |
| 356 | + assert chunks == chunk_lst |
| 357 | + |
341 | 358 | # fixtures ------------------------------------------------------- |
342 | 359 |
|
| 360 | + @pytest.fixture |
| 361 | + def chunk_(self, request): |
| 362 | + return instance_mock(request, _Chunk) |
| 363 | + |
| 364 | + @pytest.fixture |
| 365 | + def chunk_2_(self, request): |
| 366 | + return instance_mock(request, _Chunk) |
| 367 | + |
| 368 | + @pytest.fixture |
| 369 | + def _ChunkFactory_(self, request, chunk_lst_): |
| 370 | + return function_mock( |
| 371 | + request, 'docx.image.png._ChunkFactory', |
| 372 | + side_effect=chunk_lst_ |
| 373 | + ) |
| 374 | + |
| 375 | + @pytest.fixture |
| 376 | + def chunk_lst_(self, chunk_, chunk_2_): |
| 377 | + return [chunk_, chunk_2_] |
| 378 | + |
| 379 | + @pytest.fixture |
| 380 | + def _ChunkParser__init_(self, request): |
| 381 | + return initializer_mock(request, _ChunkParser) |
| 382 | + |
343 | 383 | @pytest.fixture |
344 | 384 | def from_stream_fixture( |
345 | 385 | self, stream_, StreamReader_, stream_rdr_, _ChunkParser__init_): |
346 | 386 | return stream_, StreamReader_, stream_rdr_, _ChunkParser__init_ |
347 | 387 |
|
348 | 388 | @pytest.fixture |
349 | | - def _ChunkParser__init_(self, request): |
350 | | - return initializer_mock(request, _ChunkParser) |
| 389 | + def _iter_chunk_offsets_(self, request): |
| 390 | + chunk_offsets = ( |
| 391 | + (PNG_CHUNK_TYPE.IHDR, 2), |
| 392 | + (PNG_CHUNK_TYPE.pHYs, 4), |
| 393 | + ) |
| 394 | + return method_mock( |
| 395 | + request, _ChunkParser, '_iter_chunk_offsets', |
| 396 | + return_value=iter(chunk_offsets) |
| 397 | + ) |
| 398 | + |
| 399 | + @pytest.fixture |
| 400 | + def iter_fixture( |
| 401 | + self, _iter_chunk_offsets_, _ChunkFactory_, stream_rdr_, chunk_, |
| 402 | + chunk_2_): |
| 403 | + chunk_parser = _ChunkParser(stream_rdr_) |
| 404 | + offsets = [2, 4, 6] |
| 405 | + chunk_lst = [chunk_, chunk_2_] |
| 406 | + return ( |
| 407 | + chunk_parser, _iter_chunk_offsets_, _ChunkFactory_, stream_rdr_, |
| 408 | + offsets, chunk_lst |
| 409 | + ) |
351 | 410 |
|
352 | 411 | @pytest.fixture |
353 | 412 | def StreamReader_(self, request, stream_rdr_): |
|
0 commit comments