@@ -99,28 +99,29 @@ def test_pnm(tmp_path):
9999 assert_image_equal_tofile (im , f )
100100
101101
102- def test_plain_pbm (tmp_path ):
103- # P1
104- with Image .open ("Tests/images/hopper_1bit_plain.pbm" ) as im :
105- # P4
106- assert_image_equal_tofile (im , "Tests/images/hopper_1bit.pbm" )
107-
108-
109- def test_8bit_plain_pgm (tmp_path ):
110- # P2
111- with Image .open ("Tests/images/hopper_8bit_plain.pgm" ) as im :
112- # P5
113- assert_image_equal_tofile (im , "Tests/images/hopper_8bit.pgm" )
114-
115-
116- def test_8bit_plain_ppm (tmp_path ):
117- # P3
118- with Image .open ("Tests/images/hopper_8bit_plain.ppm" ) as im :
119- # P6
120- assert_image_equal_tofile (im , "Tests/images/hopper_8bit.ppm" )
102+ @pytest .mark .parametrize (
103+ "plain_path, raw_path" ,
104+ (
105+ (
106+ "Tests/images/hopper_1bit_plain.pbm" , # P1
107+ "Tests/images/hopper_1bit.pbm" , # P4
108+ ),
109+ (
110+ "Tests/images/hopper_8bit_plain.pgm" , # P2
111+ "Tests/images/hopper_8bit.pgm" , # P5
112+ ),
113+ (
114+ "Tests/images/hopper_8bit_plain.ppm" , # P3
115+ "Tests/images/hopper_8bit.ppm" , # P6
116+ ),
117+ ),
118+ )
119+ def test_plain (plain_path , raw_path ):
120+ with Image .open (plain_path ) as im :
121+ assert_image_equal_tofile (im , raw_path )
121122
122123
123- def test_16bit_plain_pgm (tmp_path ):
124+ def test_16bit_plain_pgm ():
124125 # P2 with maxval 2 ** 16 - 1
125126 with Image .open ("Tests/images/hopper_16bit_plain.pgm" ) as im :
126127 assert im .mode == "I"
@@ -131,86 +132,55 @@ def test_16bit_plain_pgm(tmp_path):
131132 assert_image_equal_tofile (im , "Tests/images/hopper_16bit.pgm" )
132133
133134
134- def test_plain_pbm_data_with_comments (tmp_path ):
135- path1 = str (tmp_path / "temp1.ppm" )
136- path2 = str (tmp_path / "temp2.ppm" )
137- comment = b"# veeery long comment" * 10 ** 6
138- with open (path1 , "wb" ) as f1 , open (path2 , "wb" ) as f2 :
139- f1 .write (b"P1\n 2 2\n \n 1010" )
140- f2 .write (b"P1\n 2 2\n " + comment + b"\n 1010" + comment )
141-
142- with Image .open (path1 ) as im :
143- assert_image_equal_tofile (im , path2 )
144-
145-
146- def test_plain_pbm_truncated_data (tmp_path ):
147- path = str (tmp_path / "temp.ppm" )
148- with open (path , "wb" ) as f :
149- f .write (b"P1\n 128 128\n " )
150-
151- with Image .open (path ) as im :
152- with pytest .raises (ValueError ):
153- im .load ()
154-
155-
156- def test_plain_pbm_invalid_data (tmp_path ):
157- path = str (tmp_path / "temp.ppm" )
158- with open (path , "wb" ) as f :
159- f .write (b"P1\n 128 128\n 1009" )
160-
161- with Image .open (path ) as im :
162- with pytest .raises (ValueError ):
163- im .load ()
164-
165-
166- def test_plain_ppm_data_with_comments (tmp_path ):
135+ @pytest .mark .parametrize (
136+ "header, data" ,
137+ ((b"P1\n 2 2" , b"1010" ), (b"P3\n 2 2\n 255" , b"0 0 0 001 1 1 2 2 2 255 255 255" )),
138+ )
139+ def test_plain_data_with_comments (tmp_path , header , data ):
167140 path1 = str (tmp_path / "temp1.ppm" )
168141 path2 = str (tmp_path / "temp2.ppm" )
169142 comment = b"# veeery long comment" * 10 ** 6
170143 with open (path1 , "wb" ) as f1 , open (path2 , "wb" ) as f2 :
171- f1 .write (b"P3\n 2 2\n 255\n 0 0 0 001 1 1 2 2 2 255 255 255" )
172- f2 .write (
173- b"P3\n 2 2\n 255\n " + comment + b"\n 0 0 0 001 1 1 2 2 2 255 255 255" + comment
174- )
144+ f1 .write (header + b"\n \n " + data )
145+ f2 .write (header + b"\n " + comment + b"\n " + data + comment )
175146
176147 with Image .open (path1 ) as im :
177148 assert_image_equal_tofile (im , path2 )
178149
179150
180- def test_plain_ppm_truncated_data (tmp_path ):
151+ @pytest .mark .parametrize ("data" , (b"P1\n 128 128\n " , b"P3\n 128 128\n 255\n " ))
152+ def test_plain_truncated_data (tmp_path , data ):
181153 path = str (tmp_path / "temp.ppm" )
182154 with open (path , "wb" ) as f :
183- f .write (b"P3 \n 128 128 \n 255 \n " )
155+ f .write (data )
184156
185157 with Image .open (path ) as im :
186158 with pytest .raises (ValueError ):
187159 im .load ()
188160
189161
190- def test_plain_ppm_invalid_data (tmp_path ):
162+ @pytest .mark .parametrize ("data" , (b"P1\n 128 128\n 1009" , b"P3\n 128 128\n 255\n 100A" ))
163+ def test_plain_invalid_data (tmp_path , data ):
191164 path = str (tmp_path / "temp.ppm" )
192165 with open (path , "wb" ) as f :
193- f .write (b"P3 \n 128 128 \n 255 \n 100A" )
166+ f .write (data )
194167
195168 with Image .open (path ) as im :
196169 with pytest .raises (ValueError ):
197170 im .load ()
198171
199172
200- def test_plain_ppm_half_token_too_long (tmp_path ):
201- path = str (tmp_path / "temp.ppm" )
202- with open (path , "wb" ) as f :
203- f .write (b"P3\n 128 128\n 255\n 012345678910" )
204-
205- with Image .open (path ) as im :
206- with pytest .raises (ValueError ):
207- im .load ()
208-
209-
210- def test_plain_ppm_token_too_long (tmp_path ):
173+ @pytest .mark .parametrize (
174+ "data" ,
175+ (
176+ b"P3\n 128 128\n 255\n 012345678910" , # half token too long
177+ b"P3\n 128 128\n 255\n 012345678910 0" , # token too long
178+ ),
179+ )
180+ def test_plain_ppm_token_too_long (tmp_path , data ):
211181 path = str (tmp_path / "temp.ppm" )
212182 with open (path , "wb" ) as f :
213- f .write (b"P3 \n 128 128 \n 255 \n 012345678910 0" )
183+ f .write (data )
214184
215185 with Image .open (path ) as im :
216186 with pytest .raises (ValueError ):
@@ -227,7 +197,7 @@ def test_plain_ppm_value_too_large(tmp_path):
227197 im .load ()
228198
229199
230- def test_magic (tmp_path ):
200+ def test_magic ():
231201 with pytest .raises (SyntaxError ):
232202 PpmImagePlugin .PpmImageFile (fp = BytesIO (b"PyInvalid" ))
233203
@@ -263,7 +233,7 @@ def test_header_token_too_long(tmp_path):
263233 assert str (e .value ) == "Token too long in file header: 01234567890"
264234
265235
266- def test_truncated_header (tmp_path ):
236+ def test_truncated_file (tmp_path ):
267237 # Test EOF in header
268238 path = str (tmp_path / "temp.pgm" )
269239 with open (path , "w" ) as f :
0 commit comments