def test_named_color(self): self.assertEqualColor('Red', rgb(0xFF, 0, 0)) self.assertEqualColor('RED', rgb(0xFF, 0, 0)) self.assertEqualColor('red', rgb(0xFF, 0, 0)) self.assertEqualColor('rEd', rgb(0xFF, 0, 0)) self.assertEqualColor('CornflowerBlue', rgb(0x64, 0x95, 0xED)) self.assertEqualColor('cornflowerblue', rgb(0x64, 0x95, 0xED)) self.assertEqualColor('CORNFLOWERBLUE', rgb(0x64, 0x95, 0xED)) self.assertEqualColor('Cornflowerblue', rgb(0x64, 0x95, 0xED)) self.assertEqualColor('CoRnFlOwErBlUe', rgb(0x64, 0x95, 0xED)) with self.assertRaises(ValueError): parser.color('not a color')
def assertEqualColor(self, value, expected): # Nothing fancy - a color is equal if actual = parser.color(value) self.assertEqual(actual.r, expected.r) self.assertEqual(actual.g, expected.g) self.assertEqual(actual.b, expected.b) self.assertAlmostEqual(actual.a, expected.a, places=3)
def assertEqualHSL(self, value, expected): # Nothing fancy - a color is equal if actual = parser.color(value) self.assertEqual(actual.h, expected.h) self.assertEqual(actual.s, expected.s) self.assertEqual(actual.l, expected.l) self.assertAlmostEqual(actual.a, expected.a, places=3)
def test_parse_border_shorthand_valid_list_1_part(self): for direction, func in { 'bottom_': border_bottom, 'left_': border_left, 'right_': border_right, 'top_': border_top, '': border }.items(): expected_outputs = { 'black': { 'border_{direction}color'.format(direction=direction): color('black') }, 'solid': { 'border_{direction}style'.format(direction=direction): 'solid' }, 'thick': { 'border_{direction}width'.format(direction=direction): 'thick' }, } perms = permutations(['black', 'solid', 'thick'], 1) for perm in perms: value = perm expected_output = expected_outputs[value[0]] output = func(value) self.assertEqual(output, expected_output)
def test_parse_border_shorthand_valid_list_2_parts(self): black = color('black') for direction, func in { 'bottom_': border_bottom, 'left_': border_left, 'right_': border_right, 'top_': border_top, '': border }.items(): expected_outputs = { ('black', 'solid'): { 'border_{direction}color'.format(direction=direction): black, 'border_{direction}style'.format(direction=direction): 'solid' }, ('black', 'thick'): { 'border_{direction}color'.format(direction=direction): black, 'border_{direction}width'.format(direction=direction): 'thick' }, ('solid', 'thick'): { 'border_{direction}style'.format(direction=direction): 'solid', 'border_{direction}width'.format(direction=direction): 'thick' }, } perms = permutations(['black', 'solid', 'thick'], 2) for perm in perms: expected_output = expected_outputs[tuple(sorted(perm))] value = perm output = func(value) self.assertEqual(output, expected_output)
def test_hsl(self): self.assertEqualHSL('hsl(1,20%,30%)', hsl(1, 0.2, 0.3)) self.assertEqualHSL('hsl(1, 20%, 30%)', hsl(1, 0.2, 0.3)) self.assertEqualHSL('hsl( 1, 20% , 30%)', hsl(1, 0.2, 0.3)) with self.assertRaises(ValueError): parser.color('hsl(1, 20%)') with self.assertRaises(ValueError): parser.color('hsl(a, 20%, 30%)') with self.assertRaises(ValueError): parser.color('hsl(1, a, 30%)') with self.assertRaises(ValueError): parser.color('hsl(1, 20%, a)') with self.assertRaises(ValueError): parser.color('hsl(1, 20%, 30%, 0.5)')
def test_parse_outline_shorthand_valid_list_3_parts(self): expected_output = { 'outline_style': 'solid', 'outline_color': color('black'), 'outline_width': 'thick', } perms = permutations(['black', 'solid', 'thick'], 3) for perm in perms: value = perm output = outline(value) self.assertEqual(output, expected_output)
def test_parse_outline_shorthand_valid_list_1_part(self): expected_outputs = { 'black': {'outline_color': color('black')}, 'solid': {'outline_style': 'solid'}, 'thick': {'outline_width': 'thick'}, } perms = permutations(['black', 'solid', 'thick'], 1) for perm in perms: value = perm expected_output = expected_outputs[value[0]] output = outline(value) self.assertEqual(output, expected_output)
def test_rgb(self): self.assertEqualColor('rgb(1,2,3)', rgb(1, 2, 3)) self.assertEqualColor('rgb(1, 2, 3)', rgb(1, 2, 3)) self.assertEqualColor('rgb( 1 , 2 , 3)', rgb(1, 2, 3)) self.assertEqualColor('#123', rgb(0x11, 0x22, 0x33)) self.assertEqualColor('#112233', rgb(0x11, 0x22, 0x33)) with self.assertRaises(ValueError): parser.color('rgb(10, 20)') with self.assertRaises(ValueError): parser.color('rgb(a, 10, 20)') with self.assertRaises(ValueError): parser.color('rgb(10, b, 20)') with self.assertRaises(ValueError): parser.color('rgb(10, 20, c)') with self.assertRaises(ValueError): parser.color('rgb(10, 20, 30, 0.5)')
def test_parse_outline_shorthand_valid_list_2_parts(self): black = color('black') expected_outputs = { ('black', 'solid'): {'outline_color': black, 'outline_style': 'solid'}, ('black', 'thick'): {'outline_color': black, 'outline_width': 'thick'}, ('solid', 'thick'): {'outline_style': 'solid', 'outline_width': 'thick'}, } perms = permutations(['black', 'solid', 'thick'], 2) for perm in perms: expected_output = expected_outputs[tuple(sorted(perm))] value = perm output = outline(value) self.assertEqual(output, expected_output)
def test_parse_border_shorthand_valid_str_3_parts(self): for direction, func in {'bottom_': border_bottom, 'left_': border_left, 'right_': border_right, 'top_': border_top, '': border}.items(): expected_output = { 'border_{direction}width'.format(direction=direction): 'thick', 'border_{direction}style'.format(direction=direction): 'solid', 'border_{direction}color'.format(direction=direction): color('black'), } perms = permutations(['black', 'solid', 'thick'], 3) for perm in perms: value = ' '.join(perm) output = func(value) self.assertEqual(output, expected_output)
def test_rgba(self): self.assertEqualColor('rgba(1,2,3,0.5)', rgb(1, 2, 3, 0.5)) self.assertEqualColor('rgba(1, 2, 3, 0.5)', rgb(1, 2, 3, 0.5)) self.assertEqualColor('rgba( 1 , 2 , 3 , 0.5)', rgb(1, 2, 3, 0.5)) self.assertEqualColor('#1234', rgb(0x11, 0x22, 0x33, 0.2666)) self.assertEqualColor('#11223344', rgb(0x11, 0x22, 0x33, 0.2666)) with self.assertRaises(ValueError): parser.color('rgba(10, 20, 30)') with self.assertRaises(ValueError): parser.color('rgba(a, 10, 20, 0.5)') with self.assertRaises(ValueError): parser.color('rgba(10, b, 20, 0.5)') with self.assertRaises(ValueError): parser.color('rgba(10, 20, c, 0.5)') with self.assertRaises(ValueError): parser.color('rgba(10, 20, 30, c)') with self.assertRaises(ValueError): parser.color('rgba(10, 20, 30, 0.5, 5)')