<commit_before>import unittest
import enable.savage.svg.document as document
import xml.etree.cElementTree as etree
from cStringIO import StringIO

from enable.savage.svg.backends.kiva.renderer import Renderer as KivaRenderer

minimalSVG = etree.parse(StringIO(r"""<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
  "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1"></svg>"""))

class TestBrushFromColourValue(unittest.TestCase):

    def setUp(self):
        self.document = document.SVGDocument(minimalSVG.getroot(), renderer=KivaRenderer())
        self.stateStack = [{}]

    def testNone(self):
        self.document.state["fill"] = 'none'
        self.assertEqual(
            self.document.getBrushFromState(),
            None
        )

    def testCurrentColour(self):
        self.document.state["fill"] = 'currentColor'
        self.document.state["color"] = "rgb(100,100,100)"
        self.assertEqual(
            self.document.getBrushFromState().color,
            (100,100,100,255)
        )

    def testCurrentColourNull(self):
        self.document.state["fill"] = 'currentColor'
        self.assertEqual(
            self.document.getBrushFromState(),
            None
        )

    def testOpacity(self):
        self.document.state["fill"] = 'rgb(255,100,10)'
        self.document.state["fill-opacity"] = 0.5
        self.assertEqual(
            self.document.getBrushFromState().color[-1],
            127.5
        )

    def testOpacityClampHigh(self):
        self.document.state["fill"] = 'rgb(255,100,10)'
        self.document.state["fill-opacity"] = 5
        self.assertEqual(
            self.document.getBrushFromState().color[-1],
            255
        )

    def testOpacityClampLow(self):
        self.document.state["fill"] = 'rgb(255,100,10)'
        self.document.state["fill-opacity"] = -100
        self.assertEqual(
            self.document.getBrushFromState().color[-1],
            0
        )
    def testURLFallback(self):
        self.document.state["fill"] = "url(http://google.com) red"
        self.assertEqual(
            self.document.getBrushFromState().color,
            (255,0,0)
        )

class TestValueToPixels(unittest.TestCase):
    """ Make sure that CSS length values get converted correctly to pixels"""
    def testDefault(self):
        got = document.valueToPixels("12")
        self.assertEqual(got, 12)
    def testPointConversion(self):
        got = document.valueToPixels('14pt')
        self.assertEqual(got, 22)<commit_msg>Fix test of points-to-pixel conversion.<commit_after>import unittest
import enable.savage.svg.document as document
import xml.etree.cElementTree as etree
from cStringIO import StringIO

from enable.savage.svg.backends.kiva.renderer import Renderer as KivaRenderer

minimalSVG = etree.parse(StringIO(r"""<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
  "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1"></svg>"""))


class TestBrushFromColourValue(unittest.TestCase):

    def setUp(self):
        self.document = document.SVGDocument(minimalSVG.getroot(), renderer=KivaRenderer())
        self.stateStack = [{}]

    def testNone(self):
        self.document.state["fill"] = 'none'
        self.assertEqual(
            self.document.getBrushFromState(),
            None
        )

    def testCurrentColour(self):
        self.document.state["fill"] = 'currentColor'
        self.document.state["color"] = "rgb(100,100,100)"
        self.assertEqual(
            self.document.getBrushFromState().color,
            (100, 100, 100, 255)
        )

    def testCurrentColourNull(self):
        self.document.state["fill"] = 'currentColor'
        self.assertEqual(
            self.document.getBrushFromState(),
            None
        )

    def testOpacity(self):
        self.document.state["fill"] = 'rgb(255,100,10)'
        self.document.state["fill-opacity"] = 0.5
        self.assertEqual(
            self.document.getBrushFromState().color[-1],
            127.5
        )

    def testOpacityClampHigh(self):
        self.document.state["fill"] = 'rgb(255,100,10)'
        self.document.state["fill-opacity"] = 5
        self.assertEqual(
            self.document.getBrushFromState().color[-1],
            255
        )

    def testOpacityClampLow(self):
        self.document.state["fill"] = 'rgb(255,100,10)'
        self.document.state["fill-opacity"] = -100
        self.assertEqual(
            self.document.getBrushFromState().color[-1],
            0
        )

    def testURLFallback(self):
        self.document.state["fill"] = "url(http://google.com) red"
        self.assertEqual(
            self.document.getBrushFromState().color,
            (255, 0, 0)
        )


class TestValueToPixels(unittest.TestCase):
    """ Make sure that CSS length values get converted correctly to pixels"""

    def testDefault(self):
        got = document.valueToPixels("12")
        self.assertEqual(got, 12)

    def testPointConversion(self):
        got = document.valueToPixels('14pt')
        self.assertEqual(got, 14)

    def testInchConversion(self):
        got = document.valueToPixels('2in')
        self.assertEqual(got, 144)

    def testCentimeterConversion(self):
        got = document.valueToPixels('2cm')
        self.assertAlmostEqual(got, 56.7, places=1)

    def testMillimeterConversion(self):
        got = document.valueToPixels('2mm')
        self.assertAlmostEqual(got, 5.67, places=2)
{'fraction_non_alphanumeric': 0.13807829181494663, 'fraction_numerical': 0.03523131672597865, 'mean_word_length': 2.571156289707751, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 2, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '513236', 'n_tokens_mistral': 1876, 'n_tokens_neox': 1692, 'n_words': 278}