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

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

Issue 19990: Add unittests for imghdr module
Left Patch Set: Created 5 years, 9 months ago
Right Patch Set: Created 5 years, 8 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
2 import io
1 import unittest 3 import unittest
2 from test.support import TESTFN, unlink 4 from test.support import findfile, TESTFN, unlink
3 import imghdr
4 5
5 FORMATS = { 6 TEST_FILES = (
6 'png': (b'\211PNG\r\n\032\n', ), 7 ('python.png', 'png'),
7 'rgb': (b'\001\332', ), 8 ('python.gif', 'gif'),
8 'rast': (b'\x59\xA6\x6A\x95', ), 9 ('python.bmp', 'bmp'),
9 'xbm': (b'#define ', ), 10 ('python.ppm', 'ppm'),
10 'bmp': (b'BM', ), 11 ('python.pgm', 'pgm'),
11 'jpeg': (b'000000JFIF', b'000000Exif'), 12 ('python.pbm', 'pbm'),
12 'gif': (b'GIF87a', b'GIF89a'), 13 ('python.jpg', 'jpeg'),
13 'tiff': (b'MM', b'II'), 14 ('python.ras', 'rast'),
14 'pbm': (b'P1\t', b'P1\n', b'P1\r', 15 ('python.sgi', 'rgb'),
15 b'P4\t', b'P4\n', b'P4\r'), 16 ('python.tiff', 'tiff'),
16 'pgm': (b'P2\t', b'P2\n', b'P2\r', 17 ('python.xbm', 'xbm')
17 b'P5\t', b'P5\n', b'P5\r'), 18 )
18 'ppm': (b'P3\t', b'P6\n', b'P6\r', 19
19 b'P6\t', b'P6\n', b'P6\r') 20 class UnseekableIO(io.FileIO):
20 } 21 def tell(self):
22 raise io.UnsupportedOperation
23
24 def seek(self, *args, **kwargs):
25 raise io.UnsupportedOperation
21 26
vajrasky 2013/12/16 04:37:48 2 blank lines, please.
22 class TestImghdr(unittest.TestCase): 27 class TestImghdr(unittest.TestCase):
23 def tearDown(self): 28 def tearDown(self):
24 unlink(TESTFN) 29 unlink(TESTFN)
25 30
26 def test_formats_without_file(self): 31 def test_data(self):
27 for format, parts in FORMATS.items(): 32 for filename, expected in TEST_FILES:
28 for part in parts: 33 filename = findfile(filename, subdir="imghdrdata")
29 self.assertEqual(imghdr.what(None, part), 34 self.assertEqual(imghdr.what(filename), expected)
30 format) 35 with open(filename, 'rb') as stream:
31 36 self.assertEqual(imghdr.what(stream), expected)
32 def test_format_with_files(self): 37 with open(filename, 'rb') as stream:
33 for format, parts in FORMATS.items(): 38 data = stream.read()
34 for part in parts: 39 self.assertEqual(imghdr.what(None, data), expected)
35 with open(TESTFN, 'wb') as stream:
36 stream.write(part)
37 self.assertEqual(imghdr.what(TESTFN),
vajrasky 2013/12/16 04:37:48 We need one kind of test that tests the file objec
38 format)
39 40
40 def test_register_test(self): 41 def test_register_test(self):
41 def test_jumbo(h, file): 42 def test_jumbo(h, file):
42 if h.startswith(b'eggs'): 43 if h.startswith(b'eggs'):
43 return 'ham' 44 return 'ham'
44 imghdr.tests.append(test_jumbo) 45 imghdr.tests.append(test_jumbo)
45 self.addCleanup(imghdr.tests.pop) 46 self.addCleanup(imghdr.tests.pop)
47 self.assertEqual(imghdr.what(None, b'eggs'), 'ham')
46 48
47 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")
92
93 def test_invalid_headers(self):
94 for header in (b'\211PN\r\n',
95 b'\001\331',
96 b'\x59\xA6',
97 b'cutecat',
98 b'000000JFI',
99 b'GIF80'):
100 self.assertIsNone(imghdr.what(None, header))
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+