1# coding=utf-8 2""" 3Test each of the extensions base classes (if needed) and sometimes provide 4specialised test classes for testers to use. 5""" 6import inkex 7from inkex.tester import ComparisonMixin, TestCase 8 9class TurnGreenEffect(inkex.ColorExtension): 10 """Turn everything the purest green!""" 11 def modify_color(self, name, color): 12 return inkex.Color('green') 13 def modify_opacity(self, name, opacity): 14 if name == 'opacity': 15 return 1.0 16 return opacity 17 18class ColorEffectTest(ComparisonMixin, TestCase): 19 """Direct tests for color mechanisms""" 20 effect_class = TurnGreenEffect 21 effect_name = 'inkex_extensions_color' 22 compare_file = 'svg/colors.svg' 23 python3_only = True 24 25 comparisons = [ 26 ('--id=r1',), # One shape only 27 ('--id=r2',), # CSS Styles 28 ('--id=r3',), # Element Attributes 29 ('--id=r4',), # Gradient stops 30 ('--id=r1', '--id=r2'), # Two shapes 31 ('--id=color_svg',), # Recursive group/children 32 (), # Process all shapes 33 ] 34 35class ColorBaseCase(TestCase): 36 """Base class for all color effect extensions""" 37 color_tests = [] 38 opacity_tests = [] 39 40 def _test_list(self, tsts): 41 for tst in tsts: 42 inp, outp, args = (list(tst) + [[]])[:3] 43 self.effect.parse_arguments([self.empty_svg] + args) 44 yield inp, outp 45 46 def test_colors(self): 47 """Run all color tests""" 48 for x, (inp, outp) in enumerate(self._test_list(self.color_tests)): 49 outp = inkex.Color(outp) 50 got = self.effect._modify_color('fill', inkex.Color(inp)) 51 self.assertTrue(isinstance(got, inkex.Color),\ 52 "Bad output type: {}".format(type(got).__name__)) 53 outp, got = str(outp), str(got.to(outp.space)) 54 self.assertEqual(outp, got,\ 55 "Color mismatch, test:{} {} != {}".format(x, outp, got)) 56 for x, (inp, outp) in enumerate(self._test_list(self.opacity_tests)): 57 got = self.effect.modify_opacity('opacity', inp) 58 self.assertTrue(isinstance(got, float)) 59 self.assertAlmostEqual(got, outp, delta=0.1) 60