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

Side by Side Diff: Lib/test/test_imghdr.py

Issue 19990: Add unittests for imghdr module
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:
View unified diff | Download patch
« no previous file with comments | « Lib/test/imghdrdata/python.xbm ('k') | Lib/test/test_sundry.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 import imghdr
2 import io
3 import unittest
4 from test.support import findfile, TESTFN, unlink
5
6 TEST_FILES = (
7 ('python.png', 'png'),
8 ('python.gif', 'gif'),
9 ('python.bmp', 'bmp'),
10 ('python.ppm', 'ppm'),
11 ('python.pgm', 'pgm'),
12 ('python.pbm', 'pbm'),
13 ('python.jpg', 'jpeg'),
14 ('python.ras', 'rast'),
15 ('python.sgi', 'rgb'),
16 ('python.tiff', 'tiff'),
17 ('python.xbm', 'xbm')
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
27 class TestImghdr(unittest.TestCase):
28 def tearDown(self):
29 unlink(TESTFN)
30
31 def test_data(self):
32 for filename, expected in TEST_FILES:
33 filename = findfile(filename, subdir="imghdrdata")
34 self.assertEqual(imghdr.what(filename), expected)
35 with open(filename, 'rb') as stream:
36 self.assertEqual(imghdr.what(stream), expected)
37 with open(filename, 'rb') as stream:
38 data = stream.read()
39 self.assertEqual(imghdr.what(None, data), expected)
40
41 def test_register_test(self):
42 def test_jumbo(h, file):
43 if h.startswith(b'eggs'):
44 return 'ham'
45 imghdr.tests.append(test_jumbo)
46 self.addCleanup(imghdr.tests.pop)
47 self.assertEqual(imghdr.what(None, b'eggs'), 'ham')
48
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))
111
112 if __name__ == '__main__':
113 unittest.main()
OLDNEW
« no previous file with comments | « Lib/test/imghdrdata/python.xbm ('k') | Lib/test/test_sundry.py » ('j') | no next file with comments »

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