Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(41694)

Delta Between Two Patch Sets: Lib/test/test_imghdr.py

Issue 19990: Add unittests for imghdr module
Left Patch Set: Created 5 years, 8 months ago
Right Patch Set: Created 5 years, 7 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/imghdrdata/python.xbm ('k') | Lib/test/test_sundry.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 import imghdr 1 import imghdr
2 import io
2 import unittest 3 import unittest
3 from test.support import findfile 4 from test.support import findfile, TESTFN, unlink
4 5
5 TEST_FILES = ( 6 TEST_FILES = (
6 ('python.png', 'png'), 7 ('python.png', 'png'),
7 ('python.gif', 'gif'), 8 ('python.gif', 'gif'),
8 ('python.bmp', 'bmp'), 9 ('python.bmp', 'bmp'),
9 ('python.ppm', 'ppm'), 10 ('python.ppm', 'ppm'),
10 ('python.pgm', 'pgm'), 11 ('python.pgm', 'pgm'),
11 ('python.pbm', 'pbm'), 12 ('python.pbm', 'pbm'),
12 ('python.jpg', 'jpeg'), 13 ('python.jpg', 'jpeg'),
13 ('python.ras', 'rast'), 14 ('python.ras', 'rast'),
14 ('python.sgi', 'rgb'), 15 ('python.sgi', 'rgb'),
15 ('python.tiff', 'tiff'), 16 ('python.tiff', 'tiff'),
16 ('python.xbm', 'xbm') 17 ('python.xbm', 'xbm')
17 ) 18 )
18 19
20 class UnseekableIO(io.FileIO):
21 def tell(self):
22 raise io.UnsupportedOperation
23
24 def seek(self, *args, **kwargs):
25 raise io.UnsupportedOperation
26
19 class TestImghdr(unittest.TestCase): 27 class TestImghdr(unittest.TestCase):
28 def tearDown(self):
29 unlink(TESTFN)
30
20 def test_data(self): 31 def test_data(self):
21 for filename, expected in TEST_FILES: 32 for filename, expected in TEST_FILES:
22 filename = findfile(filename, subdir="imghdrdata") 33 filename = findfile(filename, subdir="imghdrdata")
23 self.assertEqual(imghdr.what(filename), expected) 34 self.assertEqual(imghdr.what(filename), expected)
24 with open(filename, 'rb') as stream: 35 with open(filename, 'rb') as stream:
25 self.assertEqual(imghdr.what(stream), expected) 36 self.assertEqual(imghdr.what(stream), expected)
26 with open(filename, 'rb') as stream: 37 with open(filename, 'rb') as stream:
27 data = stream.read() 38 data = stream.read()
28 self.assertEqual(imghdr.what(None, data), expected) 39 self.assertEqual(imghdr.what(None, data), expected)
29 40
30 def test_register_test(self): 41 def test_register_test(self):
31 def test_jumbo(h, file): 42 def test_jumbo(h, file):
32 if h.startswith(b'eggs'): 43 if h.startswith(b'eggs'):
33 return 'ham' 44 return 'ham'
34 imghdr.tests.append(test_jumbo) 45 imghdr.tests.append(test_jumbo)
35 self.addCleanup(imghdr.tests.pop) 46 self.addCleanup(imghdr.tests.pop)
47 self.assertEqual(imghdr.what(None, b'eggs'), 'ham')
36 48
37 self.assertEqual(imghdr.what(None, b'eggs'), 'ham') 49 def test_missing_file(self):
50 with self.assertRaises(FileNotFoundError):
51 imghdr.what('missing')
52
53 def test_closed_file(self):
54 with open(TESTFN, "wb") as stream:
55 stream.close()
56 with self.assertRaises(ValueError) as cm:
57 imghdr.what(stream)
58 self.assertEqual(str(cm.exception),
59 "I/O operation on closed file")
60
61 def test_text_stream(self):
62 with open(TESTFN, "w") as stream:
63 stream.write("text")
64 with open(TESTFN) as stream:
65 # certain tests uses `.startswith`
66 with self.assertRaises(TypeError):
67 imghdr.what(stream)
68
69 def test_unseekable(self):
70 with open(TESTFN, "wb") as stream:
71 stream.write(b"binary")
72 with UnseekableIO(TESTFN, "rb") as stream:
73 with self.assertRaises(io.UnsupportedOperation):
74 imghdr.what(stream)
75
76 def test_output_stream(self):
77 with open(TESTFN, "wb") as stream:
78 with self.assertRaises(io.UnsupportedOperation) as cm:
79 imghdr.what(stream)
80 self.assertEqual(str(cm.exception), "read")
81
82 def test_second_arg_bytearray(self):
83 for filename, expected in TEST_FILES:
84 filename = findfile(filename, subdir="imghdrdata")
85 with open(filename, 'rb') as stream:
86 data = bytearray(stream.read())
87 self.assertEqual(expected, imghdr.what(None, data))
88
89 def test_second_arg_str(self):
90 with self.assertRaises(TypeError):
91 imghdr.what(None, "test")
38 92
39 def test_invalid_headers(self): 93 def test_invalid_headers(self):
40 for header in (b'\211PN\r\n', 94 for header in (b'\211PN\r\n',
41 b'\001\331', 95 b'\001\331',
42 b'\x59\xA6', 96 b'\x59\xA6',
43 b'cutecat', 97 b'cutecat',
44 b'000000JFI', 98 b'000000JFI',
45 b'GIF80'): 99 b'GIF80'):
46 self.assertIsNone(imghdr.what(None, header)) 100 self.assertIsNone(imghdr.what(None, header))
47 101
102 def test_first_argument(self):
103 for obj, type in ((1, 'int'),
104 (b'', 'bytes'),
105 (None, 'NoneType')):
106 with self.assertRaises(AttributeError) as cm:
107 imghdr.what(obj)
108 self.assertEqual(str(cm.exception),
109 "{!r} object has no attribute 'tell'"
110 .format(type))
48 111
49 if __name__ == '__main__': 112 if __name__ == '__main__':
50 unittest.main() 113 unittest.main()
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+