File: test_values.py

package info (click to toggle)
python-enable 4.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 7,280 kB
  • ctags: 13,899
  • sloc: cpp: 48,447; python: 28,502; ansic: 9,004; makefile: 315; sh: 44
file content (70 lines) | stat: -rw-r--r-- 2,083 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import unittest
from pyparsing import ParseException

import enable.savage.svg.css.values as values

class FailTest(Exception):
    pass

class ParseTester(object):
    def testValidValues(self):
        #~ self.parser.debug = True
        try:
            for string, expected in self.valid:
                self.assertEqual(expected, self.parser.parseString(string)[0])
        except ParseException:
            raise FailTest("expected %r to be valid" % string)

class TestInteger(unittest.TestCase, ParseTester):
    parser = values.integer
    valid = [(x, int(x)) for x in ["01", "1"]]


class TestNumber(unittest.TestCase, ParseTester):
    parser = values.number
    valid = [(x, float(x)) for x in ["1.1", "2.3", ".3535"]]
    valid += TestInteger.valid

class TestSignedNumber(unittest.TestCase, ParseTester):
    parser = values.signedNumber
    valid = [(x, float(x)) for x in ["+1.1", "-2.3"]]
    valid += TestNumber.valid

class TestLengthUnit(unittest.TestCase, ParseTester):
    parser = values.lengthUnit
    valid = [(x,x.lower()) for x in ["em", "ex", "px", "PX", "EX", "EM", "%"]]

class TestLength(unittest.TestCase):
    parser = values.length
    valid = [
        ("1.2em", (1.2, "em")),
        ("0", (0, None)),
        ("10045px", (10045, "px")),
        ("300%", (300, "%")),
    ]

    def testValidValues(self):
        for string, expected in self.valid:
            #~ print string, expected
            got = self.parser.parseString(string)
            self.assertEqual(expected, tuple(got))

    def testIntegersIfPossible(self):
        results = self.parser.parseString("100px")[0]
        self.assertTrue(isinstance(results, int))

    def testNoSpaceBetweenValueAndUnit(self):
        """ CSS spec section 4.3.2 requires that the
        length identifier immediately follow the value
        """
        self.assertRaises(
            ParseException,
            self.parser.parseString,
            "300 %"
        )
        self.assertRaises(
            ParseException,
            self.parser.parseString,
            "300 px"
        )