added more case insensitivity tests

This commit is contained in:
homm 2016-09-19 12:12:16 +03:00
parent 9453129a57
commit f07c42f719

View File

@ -7,51 +7,68 @@ from PIL import ImageColor
class TestImageColor(PillowTestCase):
def test_hash(self):
# short 3 components
self.assertEqual((255, 0, 0), ImageColor.getrgb("#f00"))
self.assertEqual((0, 255, 0), ImageColor.getrgb("#0f0"))
self.assertEqual((0, 0, 255), ImageColor.getrgb("#00f"))
# short 4 components
self.assertEqual((255, 0, 0, 0), ImageColor.getrgb("#f000"))
self.assertEqual((0, 255, 0, 0), ImageColor.getrgb("#0f00"))
self.assertEqual((0, 0, 255, 0), ImageColor.getrgb("#00f0"))
self.assertEqual((0, 0, 0, 255), ImageColor.getrgb("#000f"))
# long 3 components
self.assertEqual((222, 0, 0), ImageColor.getrgb("#de0000"))
self.assertEqual((0, 222, 0), ImageColor.getrgb("#00de00"))
self.assertEqual((0, 0, 222), ImageColor.getrgb("#0000de"))
# long 4 components
self.assertEqual((222, 0, 0, 0), ImageColor.getrgb("#de000000"))
self.assertEqual((0, 222, 0, 0), ImageColor.getrgb("#00de0000"))
self.assertEqual((0, 0, 222, 0), ImageColor.getrgb("#0000de00"))
self.assertEqual((0, 0, 0, 222), ImageColor.getrgb("#000000de"))
# case insensitivity
self.assertEqual(ImageColor.getrgb("#DEF"), ImageColor.getrgb("#def"))
self.assertEqual(ImageColor.getrgb("#CDEF"), ImageColor.getrgb("#cdef"))
self.assertEqual(ImageColor.getrgb("#DEFDEF"),
ImageColor.getrgb("#defdef"))
self.assertEqual(ImageColor.getrgb("#CDEFCDEF"),
ImageColor.getrgb("#cdefcdef"))
# not a number
self.assertRaises(ValueError, ImageColor.getrgb, "#fo0")
self.assertRaises(ValueError, ImageColor.getrgb, "#fo00")
self.assertRaises(ValueError, ImageColor.getrgb, "#fo0000")
self.assertRaises(ValueError, ImageColor.getrgb, "#fo000000")
# wrong number of components
self.assertRaises(ValueError, ImageColor.getrgb, "#f0000")
self.assertRaises(ValueError, ImageColor.getrgb, "#f000000")
self.assertRaises(ValueError, ImageColor.getrgb, "#f00000000")
self.assertRaises(ValueError, ImageColor.getrgb, "#f000000000")
self.assertRaises(ValueError, ImageColor.getrgb, "#f00000 ")
def test_colormap(self):
self.assertEqual((0, 0, 0), ImageColor.getrgb("black"))
self.assertEqual((255, 255, 255), ImageColor.getrgb("white"))
self.assertEqual((255, 255, 255), ImageColor.getrgb("WHITE"))
self.assertRaises(ValueError, ImageColor.getrgb, "black0")
self.assertRaises(ValueError, ImageColor.getrgb, "black ")
def test_functions(self):
# rgb numbers
self.assertEqual((255, 0, 0), ImageColor.getrgb("rgb(255,0,0)"))
self.assertEqual((0, 255, 0), ImageColor.getrgb("rgb(0,255,0)"))
self.assertEqual((0, 0, 255), ImageColor.getrgb("rgb(0,0,255)"))
# percents
self.assertEqual((255, 0, 0), ImageColor.getrgb("rgb(100%,0%,0%)"))
self.assertEqual((0, 255, 0), ImageColor.getrgb("rgb(0%,100%,0%)"))
self.assertEqual((0, 0, 255), ImageColor.getrgb("rgb(0%,0%,100%)"))
# rgba numbers
self.assertEqual((255, 0, 0, 0), ImageColor.getrgb("rgba(255,0,0,0)"))
self.assertEqual((0, 255, 0, 0), ImageColor.getrgb("rgba(0,255,0,0)"))
self.assertEqual((0, 0, 255, 0), ImageColor.getrgb("rgba(0,0,255,0)"))
@ -61,15 +78,27 @@ class TestImageColor(PillowTestCase):
self.assertEqual((255, 0, 0), ImageColor.getrgb("hsl(360,100%,50%)"))
self.assertEqual((0, 255, 255), ImageColor.getrgb("hsl(180,100%,50%)"))
actual = ImageColor.getrgb("rgb( 255 , 0 , 0 )")
self.assertEqual((255, 0, 0), actual)
actual = ImageColor.getrgb("rgb( 100% , 0% , 0% )")
self.assertEqual((255, 0, 0), actual)
actual = ImageColor.getrgb("rgba( 255 , 0 , 0 , 0 )")
self.assertEqual((255, 0, 0, 0), actual)
actual = ImageColor.getrgb("hsl( 0 , 100% , 50% )")
self.assertEqual((255, 0, 0), actual)
# case insensitivity
self.assertEqual(ImageColor.getrgb("RGB(255,0,0)"),
ImageColor.getrgb("rgb(255,0,0)"))
self.assertEqual(ImageColor.getrgb("RGB(100%,0%,0%)"),
ImageColor.getrgb("rgb(100%,0%,0%)"))
self.assertEqual(ImageColor.getrgb("RGBA(255,0,0,0)"),
ImageColor.getrgb("rgba(255,0,0,0)"))
self.assertEqual(ImageColor.getrgb("HSL(0,100%,50%)"),
ImageColor.getrgb("hsl(0,100%,50%)"))
# space agnosticism
self.assertEqual((255, 0, 0),
ImageColor.getrgb("rgb( 255 , 0 , 0 )"))
self.assertEqual((255, 0, 0),
ImageColor.getrgb("rgb( 100% , 0% , 0% )"))
self.assertEqual((255, 0, 0, 0),
ImageColor.getrgb("rgba( 255 , 0 , 0 , 0 )"))
self.assertEqual((255, 0, 0),
ImageColor.getrgb("hsl( 0 , 100% , 50% )"))
# wrong number of components
self.assertRaises(ValueError, ImageColor.getrgb, "rgb(255,0)")
self.assertRaises(ValueError, ImageColor.getrgb, "rgb(255,0,0,0)")