File: test_derived_props.py

package info (click to toggle)
python-precis-i18n 1.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,836 kB
  • sloc: python: 1,825; sh: 28; makefile: 3
file content (74 lines) | stat: -rw-r--r-- 2,208 bytes parent folder | download
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
71
72
73
74
import os
import sys
import unittest

from precis_i18n.derived import derived_property
from precis_i18n.unicode import UnicodeData

try:
    import unicodedata2
except ImportError:
    unicodedata2 = None


class TestDerivedProperties(unittest.TestCase):
    """Test output of derived_property function."""

    def test_derived_props(self):
        """Compare derived properties against a "golden" file using built-in
        unicodedata module.
        """
        self._test_derived_props(UnicodeData())

    @unittest.skipIf(unicodedata2 is None, "unicodedata2 not available")
    def test_derived_props_unicodedata2(self):
        """Compare derived properties against a "golden" file using
        pip-installed unicodedata2 module.
        """

        ucd = UnicodeData(unicodedata2)
        assert ucd.version in {10.0, 12.0, 14.0, 15.0, 15.1}
        self._test_derived_props(ucd)

    def _test_derived_props(self, ucd):
        """Compare derived properties against a "golden" file."""

        goldname = "derived-props-%.1f.txt" % ucd.version
        goldpath = os.path.join(os.path.dirname(__file__), goldname)

        with open(goldpath) as golden:
            derived_props = enumerate_derived_props(ucd)
            for line in golden:
                line = line.strip()
                if not line:
                    continue
                expected = "%04X-%04X %s" % next(derived_props)
                self.assertEqual(line, expected)


def enumerate_derived_props(ucd, begin=0, end=0x00110000):
    """Iterable producing all derived_property values for a particular range.

    Produces a sequence of tuples (begin, end, property).
    """
    start = begin
    prev = "/".join(derived_property(start, ucd))
    for cp in range(start + 1, end):
        prop = "/".join(derived_property(cp, ucd))
        if prop != prev:
            yield (start, cp - 1, prev)
            start = cp
            prev = prop
    yield (start, cp, prev)


def main():
    ucd = UnicodeData()
    print("Unicode %.1f" % ucd.version, file=sys.stderr)
    # Dump gold file to stdout.
    for p in enumerate_derived_props(ucd):
        print("%04X-%04X %s" % p)


if __name__ == "__main__":
    main()