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

Unified Diff: Lib/test/test_imghdr.py

Issue 19990: Add unittests for imghdr module
Patch Set: Created 5 years, 7 months ago
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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Lib/test/imghdrdata/python.xbm ('k') | Lib/test/test_sundry.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
new file mode 100644
--- /dev/null
+++ b/Lib/test/test_imghdr.py
@@ -0,0 +1,113 @@
+import imghdr
+import io
+import unittest
+from test.support import findfile, TESTFN, unlink
+
+TEST_FILES = (
+ ('python.png', 'png'),
+ ('python.gif', 'gif'),
+ ('python.bmp', 'bmp'),
+ ('python.ppm', 'ppm'),
+ ('python.pgm', 'pgm'),
+ ('python.pbm', 'pbm'),
+ ('python.jpg', 'jpeg'),
+ ('python.ras', 'rast'),
+ ('python.sgi', 'rgb'),
+ ('python.tiff', 'tiff'),
+ ('python.xbm', 'xbm')
+)
+
+class UnseekableIO(io.FileIO):
+ def tell(self):
+ raise io.UnsupportedOperation
+
+ def seek(self, *args, **kwargs):
+ raise io.UnsupportedOperation
+
+class TestImghdr(unittest.TestCase):
+ def tearDown(self):
+ unlink(TESTFN)
+
+ def test_data(self):
+ for filename, expected in TEST_FILES:
+ filename = findfile(filename, subdir="imghdrdata")
+ self.assertEqual(imghdr.what(filename), expected)
+ with open(filename, 'rb') as stream:
+ self.assertEqual(imghdr.what(stream), expected)
+ with open(filename, 'rb') as stream:
+ data = stream.read()
+ self.assertEqual(imghdr.what(None, data), expected)
+
+ def test_register_test(self):
+ def test_jumbo(h, file):
+ if h.startswith(b'eggs'):
+ return 'ham'
+ imghdr.tests.append(test_jumbo)
+ self.addCleanup(imghdr.tests.pop)
+ self.assertEqual(imghdr.what(None, b'eggs'), 'ham')
+
+ def test_missing_file(self):
+ with self.assertRaises(FileNotFoundError):
+ imghdr.what('missing')
+
+ def test_closed_file(self):
+ with open(TESTFN, "wb") as stream:
+ stream.close()
+ with self.assertRaises(ValueError) as cm:
+ imghdr.what(stream)
+ self.assertEqual(str(cm.exception),
+ "I/O operation on closed file")
+
+ def test_text_stream(self):
+ with open(TESTFN, "w") as stream:
+ stream.write("text")
+ with open(TESTFN) as stream:
+ # certain tests uses `.startswith`
+ with self.assertRaises(TypeError):
+ imghdr.what(stream)
+
+ def test_unseekable(self):
+ with open(TESTFN, "wb") as stream:
+ stream.write(b"binary")
+ with UnseekableIO(TESTFN, "rb") as stream:
+ with self.assertRaises(io.UnsupportedOperation):
+ imghdr.what(stream)
+
+ def test_output_stream(self):
+ with open(TESTFN, "wb") as stream:
+ with self.assertRaises(io.UnsupportedOperation) as cm:
+ imghdr.what(stream)
+ self.assertEqual(str(cm.exception), "read")
+
+ def test_second_arg_bytearray(self):
+ for filename, expected in TEST_FILES:
+ filename = findfile(filename, subdir="imghdrdata")
+ with open(filename, 'rb') as stream:
+ data = bytearray(stream.read())
+ self.assertEqual(expected, imghdr.what(None, data))
+
+ def test_second_arg_str(self):
+ with self.assertRaises(TypeError):
+ imghdr.what(None, "test")
+
+ def test_invalid_headers(self):
+ for header in (b'\211PN\r\n',
+ b'\001\331',
+ b'\x59\xA6',
+ b'cutecat',
+ b'000000JFI',
+ b'GIF80'):
+ self.assertIsNone(imghdr.what(None, header))
+
+ def test_first_argument(self):
+ for obj, type in ((1, 'int'),
+ (b'', 'bytes'),
+ (None, 'NoneType')):
+ with self.assertRaises(AttributeError) as cm:
+ imghdr.what(obj)
+ self.assertEqual(str(cm.exception),
+ "{!r} object has no attribute 'tell'"
+ .format(type))
+
+if __name__ == '__main__':
+ unittest.main()
« 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+