1010
1111from docx .compat import BytesIO
1212from docx .image .exceptions import InvalidImageStreamError
13- from docx .image .helpers import StreamReader
13+ from docx .image .helpers import BIG_ENDIAN , StreamReader
1414from docx .image .png import Png
1515
1616from ..unitutil import (
17- initializer_mock , class_mock , instance_mock , method_mock
17+ initializer_mock , class_mock , instance_mock , method_mock , test_file
1818)
1919
2020
@@ -45,6 +45,12 @@ def it_raises_on_png_having_no_IHDR_chunk(self, no_IHDR_fixture):
4545 with pytest .raises (InvalidImageStreamError ):
4646 Png ._parse_png_headers (stream_ )
4747
48+ def it_parses_chunk_offsets_to_help_chunk_parser (
49+ self , chunk_offset_fixture ):
50+ stream , expected_chunk_offsets = chunk_offset_fixture
51+ chunk_offsets = Png ._parse_chunk_offsets (stream )
52+ assert chunk_offsets == expected_chunk_offsets
53+
4854 # fixtures -------------------------------------------------------
4955
5056 @pytest .fixture
@@ -59,6 +65,22 @@ def attrs_(self, request):
5965 def blob_ (self , request ):
6066 return instance_mock (request , bytes )
6167
68+ @pytest .fixture (params = [
69+ ('150-dpi.png' , {
70+ 'IHDR' : 16 , 'pHYs' : 41 , 'iCCP' : 62 , 'cHRM' : 2713 , 'IDAT' : 2757 ,
71+ 'IEND' : 146888 }),
72+ ('300-dpi.png' , {
73+ 'IHDR' : 16 , 'pHYs' : 41 , 'tEXt' : 62 , 'IDAT' : 99 , 'IEND' : 39917 }),
74+ ])
75+ def chunk_offset_fixture (self , request ):
76+ filename , expected_chunk_offsets = request .param
77+ path = test_file (filename )
78+ with open (path , 'rb' ) as f :
79+ blob = f .read ()
80+ stream = BytesIO (blob )
81+ stream_rdr = StreamReader (stream , BIG_ENDIAN )
82+ return stream_rdr , expected_chunk_offsets
83+
6284 @pytest .fixture
6385 def chunk_offsets_ (self , request ):
6486 return dict ()
0 commit comments