File: config_test.py

package info (click to toggle)
fonttools 4.61.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,588 kB
  • sloc: python: 145,091; xml: 103; makefile: 24
file content (135 lines) | stat: -rw-r--r-- 4,325 bytes parent folder | download | duplicates (5)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
from fontTools.misc.configTools import AbstractConfig, Options
import pytest
from fontTools.config import (
    OPTIONS,
    Config,
    ConfigUnknownOptionError,
    ConfigValueParsingError,
    ConfigValueValidationError,
)
from fontTools.ttLib import TTFont


def test_can_register_option():
    MY_OPTION = Config.register_option(
        name="tests:MY_OPTION",
        help="Test option, value should be True or False, default = True",
        default=True,
        parse=lambda v: v in ("True", "true", 1, True),
        validate=lambda v: v == True or v == False,
    )

    assert MY_OPTION.name == "tests:MY_OPTION"
    assert (
        MY_OPTION.help == "Test option, value should be True or False, default = True"
    )
    assert MY_OPTION.default == True
    assert MY_OPTION.parse("true") == True
    assert MY_OPTION.validate("hello") == False

    ttFont = TTFont(cfg={"tests:MY_OPTION": True})
    assert True == ttFont.cfg.get("tests:MY_OPTION")
    assert True == ttFont.cfg.get(MY_OPTION)


# to parametrize tests of Config mapping interface accepting either a str or Option
@pytest.fixture(
    params=[
        pytest.param("fontTools.otlLib.optimize.gpos:COMPRESSION_LEVEL", id="str"),
        pytest.param(
            OPTIONS["fontTools.otlLib.optimize.gpos:COMPRESSION_LEVEL"], id="Option"
        ),
    ]
)
def COMPRESSION_LEVEL(request):
    return request.param


def test_ttfont_has_config(COMPRESSION_LEVEL):
    ttFont = TTFont(cfg={COMPRESSION_LEVEL: 8})
    assert 8 == ttFont.cfg.get(COMPRESSION_LEVEL)


def test_ttfont_can_take_superset_of_fonttools_config(COMPRESSION_LEVEL):
    # Create MyConfig with all options from fontTools.config plus some
    my_options = Options(Config.options)
    MY_OPTION = my_options.register("custom:my_option", "help", "default", str, any)

    class MyConfig(AbstractConfig):
        options = my_options

    ttFont = TTFont(cfg=MyConfig({"custom:my_option": "my_value"}))
    assert 0 == ttFont.cfg.get(COMPRESSION_LEVEL)
    assert "my_value" == ttFont.cfg.get(MY_OPTION)

    # but the default Config doens't know about MY_OPTION
    with pytest.raises(ConfigUnknownOptionError):
        TTFont(cfg={MY_OPTION: "my_value"})


def test_no_config_returns_default_values(COMPRESSION_LEVEL):
    ttFont = TTFont()
    assert 0 == ttFont.cfg.get(COMPRESSION_LEVEL)
    assert 3 == ttFont.cfg.get(COMPRESSION_LEVEL, 3)


def test_can_set_config(COMPRESSION_LEVEL):
    ttFont = TTFont()
    ttFont.cfg.set(COMPRESSION_LEVEL, 5)
    assert 5 == ttFont.cfg.get(COMPRESSION_LEVEL)
    ttFont.cfg.set(COMPRESSION_LEVEL, 6)
    assert 6 == ttFont.cfg.get(COMPRESSION_LEVEL)


def test_different_ttfonts_have_different_configs(COMPRESSION_LEVEL):
    cfg = Config({COMPRESSION_LEVEL: 5})
    ttFont1 = TTFont(cfg=cfg)
    ttFont2 = TTFont(cfg=cfg)
    ttFont2.cfg.set(COMPRESSION_LEVEL, 6)
    assert 5 == ttFont1.cfg.get(COMPRESSION_LEVEL)
    assert 6 == ttFont2.cfg.get(COMPRESSION_LEVEL)


def test_cannot_set_inexistent_key():
    with pytest.raises(ConfigUnknownOptionError):
        TTFont(cfg={"notALib.notAModule.inexistent": 4})


def test_value_not_parsed_by_default(COMPRESSION_LEVEL):
    # Note: value given as a string
    with pytest.raises(ConfigValueValidationError):
        TTFont(cfg={COMPRESSION_LEVEL: "8"})


def test_value_gets_parsed_if_asked(COMPRESSION_LEVEL):
    # Note: value given as a string
    ttFont = TTFont(cfg=Config({COMPRESSION_LEVEL: "8"}, parse_values=True))
    assert 8 == ttFont.cfg.get(COMPRESSION_LEVEL)


def test_value_parsing_can_error(COMPRESSION_LEVEL):
    with pytest.raises(ConfigValueParsingError):
        TTFont(
            cfg=Config(
                {COMPRESSION_LEVEL: "not an int"},
                parse_values=True,
            )
        )


def test_value_gets_validated(COMPRESSION_LEVEL):
    # Note: 12 is not a valid value for GPOS compression level (must be in 0-9)
    with pytest.raises(ConfigValueValidationError):
        TTFont(cfg={COMPRESSION_LEVEL: 12})


def test_implements_mutable_mapping(COMPRESSION_LEVEL):
    cfg = Config()
    cfg[COMPRESSION_LEVEL] = 2
    assert 2 == cfg[COMPRESSION_LEVEL]
    assert list(iter(cfg))
    assert 1 == len(cfg)
    del cfg[COMPRESSION_LEVEL]
    assert 0 == cfg[COMPRESSION_LEVEL]
    assert not list(iter(cfg))
    assert 0 == len(cfg)