diff -r 629852f6d186 Lib/test/test_imghdr.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Lib/test/test_imghdr.py Mon Dec 16 09:53:40 2013 +0200 @@ -0,0 +1,64 @@ +import unittest +from test.support import TESTFN, unlink +import imghdr + +FORMATS = { + 'png': (b'\211PNG\r\n\032\n', ), + 'rgb': (b'\001\332', ), + 'rast': (b'\x59\xA6\x6A\x95', ), + 'xbm': (b'#define ', ), + 'bmp': (b'BM', ), + 'jpeg': (b'000000JFIF', b'000000Exif'), + 'gif': (b'GIF87a', b'GIF89a'), + 'tiff': (b'MM', b'II'), + 'pbm': (b'P1\t', b'P1\n', b'P1\r', + b'P4\t', b'P4\n', b'P4\r'), + 'pgm': (b'P2\t', b'P2\n', b'P2\r', + b'P5\t', b'P5\n', b'P5\r'), + 'ppm': (b'P3\t', b'P6\n', b'P6\r', + b'P6\t', b'P6\n', b'P6\r') +} + + +class TestImghdr(unittest.TestCase): + def tearDown(self): + unlink(TESTFN) + + def test_formats_without_file(self): + for format, parts in FORMATS.items(): + for part in parts: + self.assertEqual(imghdr.what(None, part), + format) + + def test_format_with_files(self): + for format, parts in FORMATS.items(): + for part in parts: + with open(TESTFN, 'wb') as stream: + stream.write(part) + with open(TESTFN, 'rb') as stream: + self.assertEqual(imghdr.what(stream), + format) + self.assertEqual(imghdr.what(TESTFN), + format) + + 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_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)) + + +if __name__ == '__main__': + unittest.main() diff -r 629852f6d186 Lib/test/test_sundry.py --- a/Lib/test/test_sundry.py Sun Dec 15 21:49:17 2013 +0100 +++ b/Lib/test/test_sundry.py Mon Dec 16 09:53:40 2013 +0200 @@ -6,7 +6,7 @@ class TestUntestedModules(unittest.TestCase): def test_untested_modules_can_be_imported(self): - untested = ('bdb', 'encodings', 'formatter', 'imghdr', + untested = ('bdb', 'encodings', 'formatter', 'nturl2path', 'tabnanny') with support.check_warnings(quiet=True): for name in untested: