File: test_font.py

package info (click to toggle)
python-pyface 8.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,944 kB
  • sloc: python: 54,107; makefile: 82
file content (297 lines) | stat: -rw-r--r-- 9,502 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# (C) Copyright 2005-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!

import unittest

from traits.api import HasStrictTraits, TraitError

from pyface.font import Font, FontSize, FontStretch, SIZES, STRETCHES
from pyface.toolkit import toolkit_object


class FontSizeDummy(HasStrictTraits):

    size = FontSize()
    size_default_1 = FontSize(14.0)
    size_default_2 = FontSize("14.0")
    size_default_3 = FontSize("14.0pt")
    size_default_4 = FontSize("large")


class TestFontSizeTrait(unittest.TestCase):

    def test_font_size_trait_defaults(self):
        dummy = FontSizeDummy()

        self.assertEqual(dummy.size, 12.0)
        self.assertEqual(dummy.size_default_1, 14.0)
        self.assertEqual(dummy.size_default_2, 14.0)
        self.assertEqual(dummy.size_default_3, 14.0)
        self.assertEqual(dummy.size_default_4, 14.0)

    def test_font_sizes(self):
        dummy = FontSizeDummy()
        for size, value in SIZES.items():
            with self.subTest(size=size):
                dummy.size = size
                self.assertEqual(dummy.size, value)

    def test_font_size_trait_invalid_default(self):
        for size in ["badvalue", -1.0, "-1.0", "0pt"]:
            with self.subTest(size=size):
                with self.assertRaises(TraitError):
                    FontSize(size)

    def test_font_size_trait_validate(self):
        dummy = FontSizeDummy()
        for size, expected in [
            (14.0, 14.0),
            ("15.0", 15.0),
            ("16pt", 16.0),
            ("17.0px", 17.0),
        ]:
            with self.subTest(size=size):
                dummy.size = size
                self.assertEqual(dummy.size, expected)

    def test_font_size_trait_invalid_validate(self):
        dummy = FontSizeDummy()
        for size in ["badvalue", -1.0, "-1.0", "0pt"]:
            with self.subTest(size=size):
                with self.assertRaises(TraitError):
                    dummy.size = size


class FontStretchDummy(HasStrictTraits):

    stretch = FontStretch()
    stretch_default_1 = FontStretch(150)
    stretch_default_2 = FontStretch("150.0")
    stretch_default_3 = FontStretch("150.0%")
    stretch_default_4 = FontStretch("expanded")


class TestFontStretchTrait(unittest.TestCase):

    def test_font_stretch_trait_defaults(self):
        dummy = FontStretchDummy()

        self.assertEqual(dummy.stretch, 100.0)
        self.assertEqual(dummy.stretch_default_1, 150.0)
        self.assertEqual(dummy.stretch_default_2, 150.0)
        self.assertEqual(dummy.stretch_default_3, 150.0)
        self.assertEqual(dummy.stretch_default_4, 125.0)

    def test_font_stretches(self):
        dummy = FontStretchDummy()
        for stretch, value in STRETCHES.items():
            with self.subTest(stretch=stretch):
                dummy.stretch = stretch
                self.assertEqual(dummy.stretch, value)

    def test_font_stretch_trait_invalid_default(self):
        for stretch in ["badvalue", 49.5, "49.5", "49.5%", 200.1]:
            with self.subTest(stretch=stretch):
                with self.assertRaises(TraitError):
                    FontStretch(stretch)

    def test_font_stretch_trait_validate(self):
        dummy = FontStretchDummy()

        for stretch, expected in [
            (150.0, 150.0),
            ("125", 125.0),
            ("50%", 50.0),
            ("ultra-expanded", 200.0)
        ]:
            with self.subTest(stretch=stretch):
                dummy.stretch = stretch
                self.assertEqual(dummy.stretch, expected)

    def test_font_stretch_trait_invalid_validate(self):
        dummy = FontStretchDummy()
        for stretch in ["badvalue", 49.5, "200.1", "49.9%"]:
            with self.subTest(stretch=stretch):
                with self.assertRaises(TraitError):
                    dummy.stretch = stretch


class TestFont(unittest.TestCase):

    def test_default(self):
        font = Font()

        self.assertEqual(font.family, ['default'])
        self.assertEqual(font.size, 12.0)
        self.assertEqual(font.weight, 'normal')
        self.assertEqual(font.stretch, 100)
        self.assertEqual(font.style, 'normal')
        self.assertEqual(font.variants, set())

    def test_typical(self):
        font = Font(
            family=['Helvetica', 'sans-serif'],
            size='large',
            weight='demi-bold',
            stretch='condensed',
            style='italic',
            variants={'small-caps'},
            decorations={'underline'},
        )

        self.assertEqual(font.family, ['Helvetica', 'sans-serif'])
        self.assertEqual(font.size, 14.0)
        self.assertEqual(font.weight, 'demi-bold')
        self.assertEqual(font.weight_, 600)
        self.assertEqual(font.stretch, 75)
        self.assertEqual(font.style, 'italic')
        self.assertEqual(font.variants, {'small-caps'})
        self.assertEqual(font.decorations, {'underline'})

    def test_family_sequence(self):
        font = Font(family=('Helvetica', 'sans-serif'))
        self.assertEqual(font.family, ['Helvetica', 'sans-serif'])

    def test_variants_frozenset(self):
        font = Font(variants=frozenset({'small-caps'}))
        self.assertEqual(font.variants, {'small-caps'})

    def test_decorations_frozenset(self):
        font = Font(decorations=frozenset({'underline'}))
        self.assertEqual(font.decorations, {'underline'})

    def test_str(self):
        font = Font()

        description = str(font)

        self.assertEqual(description, "12pt default")

    def test_str_typical(self):
        font = Font(
            family=['Comic Sans', 'decorative'],
            size='large',
            weight='demi-bold',
            stretch='condensed',
            style='italic',
            variants={'small-caps'},
            decorations={'underline'},
        )

        description = str(font)

        self.assertEqual(
            description,
            "italic small-caps underline demi-bold 75% 14pt "
            "'Comic Sans', decorative"
        )

    def test_repr(self):
        font = Font()

        text = repr(font)

        # this is little more than a smoke check, but good enough
        self.assertTrue(text.startswith('Font('))

    def test_repr_typical(self):
        font = Font(
            family=['Helvetica', 'sans-serif'],
            size='large',
            weight='demi-bold',
            stretch='condensed',
            style='italic',
            variants={'small-caps'},
            decorations={'underline'},
        )

        text = repr(font)

        # this is little more than a smoke check, but good enough
        self.assertTrue(text.startswith('Font('))

    def test_to_toolkit(self):
        font = Font()

        # smoke test
        toolkit_font = font.to_toolkit()
        self.assertIsNotNone(toolkit_font)

    def test_to_toolkit_typical(self):
        font = Font(
            family=['Helvetica', 'sans-serif'],
            size='large',
            weight='demi-bold',
            stretch='condensed',
            style='italic',
            variants={'small-caps'},
            decorations={'underline', 'strikethrough', 'overline'},
        )

        # smoke test
        toolkit_font = font.to_toolkit()
        self.assertIsNotNone(toolkit_font)

    def test_toolkit_default_roundtrip(self):
        font = Font()

        # smoke test
        result = Font.from_toolkit(font.to_toolkit())

        # defaults should round-trip
        self.assertTrue(result.family[-1], 'default')
        self.assertEqual(result.size, font.size)
        self.assertEqual(result.weight, font.weight)
        self.assertEqual(result.stretch, font.stretch)
        self.assertEqual(result.variants, font.variants)
        self.assertEqual(result.decorations, font.decorations)

    def test_from_toolkit_typical(self):
        font = Font(
            family=['Helvetica', 'sans-serif'],
            size='large',
            weight='bold',
            stretch='condensed',
            style='italic',
            variants={'small-caps'},
            decorations={'underline', 'strikethrough', 'overline'},
        )

        # smoke test
        result = Font.from_toolkit(font.to_toolkit())

        # we expect some things should round-trip no matter what system
        self.assertEqual(result.size, font.size)
        self.assertEqual(result.weight, font.weight)
        self.assertEqual(result.style, font.style)

    def test_toolkit_font_to_properties(self):
        toolkit_font_to_properties = toolkit_object(
            'font:toolkit_font_to_properties')

        font = Font(
            family=['Helvetica', 'sans-serif'],
            size='large',
            weight='demi-bold',
            stretch='condensed',
            style='italic',
            variants={'small-caps'},
            decorations={'underline', 'strikethrough', 'overline'},
        )

        properties = toolkit_font_to_properties(font.to_toolkit())

        self.assertEqual(
            set(properties.keys()),
            {
                'family', 'size', 'stretch', 'weight', 'style', 'variants',
                'decorations'
            }
        )