forked from lucaswerkmeister/tool-lexeme-forms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_formatters.py
286 lines (242 loc) · 10.8 KB
/
test_formatters.py
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
from markupsafe import Markup
import pytest
import formatters
@pytest.mark.parametrize('count, expected', [
(1, 'I ate 1 apple.'),
(2, 'I ate 2 apples.'),
(0, 'I ate no apples.'),
])
def test_PluralFormatter_en(count, expected):
plural_formatter = formatters.PluralFormatter(locale_identifier='en')
assert plural_formatter.format(
'I ate {count!p:0=no apples:one={count} apple:other={count} apples}.',
count=count
) == expected
@pytest.mark.parametrize('size, expected', [
(1, '1 (0x0001) bajt'),
(2, '2 (0x0002) bajtaj'),
(3, '3 (0x0003) bajty'),
(4, '4 (0x0004) bajty'),
(5, '5 (0x0005) bajtow'),
])
def test_PluralFormatter_hsb(size, expected):
plural_formatter = formatters.PluralFormatter(locale_identifier='hsb')
assert plural_formatter.format(
'{size} (0x{size:04X}) {size!p:one=bajt:two=bajtaj:few=bajty:other=bajtow}',
size=size
) == expected
@pytest.mark.parametrize('num, expected', [
(0, 'zero'),
(1, 'one'),
(2, 'two'),
(3, 'other'),
(0.0, 'other'),
])
def test_PluralFormatter_explicit(num, expected):
plural_formatter = formatters.PluralFormatter(locale_identifier='en')
assert plural_formatter.format(
'{num!p:0=zero:1=one:2=two:other=other}',
num=num
) == expected
def test_PluralFormatter_explicit_takes_precedence():
plural_formatter = formatters.PluralFormatter(locale_identifier='en')
assert plural_formatter.format(
'{num!p:other=other:0=zero}',
num=0
) == 'zero'
@pytest.mark.parametrize('format_spec, type', [
('{!p}', KeyError),
('{!p:}', KeyError),
('{!p:0=missing plural for other}', KeyError),
])
def test_PluralFormatter_invalid_format_spec(format_spec, type):
plural_formatter = formatters.PluralFormatter(locale_identifier='en')
with pytest.raises(type):
plural_formatter.format(format_spec, 1)
def test_PluralFormatter_fallback():
plural_formatter = formatters.PluralFormatter(locale_identifier='en')
formatted = plural_formatter.format('prefix {val!p:0=zero:other="{val}"} suffix', val=1)
assert formatted == 'prefix "1" suffix'
@pytest.mark.parametrize('format_type, adjective_type, expected_str', [
(str, str, 'A single "<strong>cool</strong>" item.'),
(Markup, Markup, 'A single "<strong>cool</strong>" item.'),
(str, Markup, 'A single "<strong>cool</strong>" item.'),
(Markup, str, 'A single "<strong>cool</strong>" item.'),
])
def test_PluralFormatter_MarkupSafe(format_type, adjective_type, expected_str):
plural_formatter = formatters.PluralFormatter(locale_identifier='en')
formatted = plural_formatter.format(
format_type('{count!p:one=A single "{adjective}" item.:other=Several "{adjective}" items.}'),
count=1,
adjective=adjective_type('<strong>cool</strong>'),
)
assert formatted == expected_str
assert type(formatted) == format_type
@pytest.mark.parametrize('list, expected', [
(['FLAC'], 'FLAC.'),
(['FLAC', 'OGG'], 'FLAC and OGG.'),
(['FLAC', 'OGG', 'OPUS'], 'FLAC, OGG, and OPUS.'),
])
def test_CommaSeparatedListFormatter_en(list, expected):
comma_separated_list_formatter = formatters.CommaSeparatedListFormatter(locale_identifier='en')
assert comma_separated_list_formatter.format(
'{list!l}.',
list=list
) == expected
@pytest.mark.parametrize('list, expected', [
(['FLAC'], 'FLAC.'),
(['FLAC', 'OGG'], 'FLAC和OGG.'),
(['FLAC', 'OGG', 'OPUS'], 'FLAC、OGG和OPUS.'),
])
def test_CommaSeparatedListFormatter_zh(list, expected):
comma_separated_list_formatter = formatters.CommaSeparatedListFormatter(locale_identifier='zh')
assert comma_separated_list_formatter.format(
'{list!l}.',
list=list
) == expected
def test_CommaSeparatedListFormatter_formats_list_items_with_format_spec():
comma_separated_list_formatter = formatters.CommaSeparatedListFormatter(locale_identifier='en')
assert comma_separated_list_formatter.format(
'The binaries are {sizes!l:04d} bytes large.',
sizes=[64, 128, 256, 1024, 4096]
) == 'The binaries are 0064, 0128, 0256, 1024, and 4096 bytes large.'
@pytest.mark.parametrize('format_type, item_type, expected_str', [
(str, str, 'I like <i>Python</i>.'),
(Markup, Markup, 'I like <i>Python</i>.'),
(str, Markup, 'I like <i>Python</i>.'),
(Markup, str, 'I like <i>Python</i>.'),
])
def test_CommaSeparatedListFormatter_MarkupSafe_one_item(format_type, item_type, expected_str):
comma_separated_list_formatter = formatters.CommaSeparatedListFormatter(locale_identifier='en')
formatted = comma_separated_list_formatter.format(
format_type('I like {list!l}.'),
list=[
item_type('<i>Python</i>'),
],
)
assert formatted == expected_str
assert type(formatted) == format_type
@pytest.mark.parametrize('format_type, item_one_type, item_two_type, expected_str', [
(str, str, str, 'I like <i>Python</i> and <i>Wikidata</i>.'),
(Markup, Markup, Markup, 'I like <i>Python</i> and <i>Wikidata</i>.'),
(str, Markup, Markup, 'I like <i>Python</i> and <i>Wikidata</i>.'),
(Markup, str, str, 'I like <i>Python</i> and <i>Wikidata</i>.'),
(Markup, Markup, str, 'I like <i>Python</i> and <i>Wikidata</i>.'),
(Markup, str, Markup, 'I like <i>Python</i> and <i>Wikidata</i>.'),
])
def test_CommaSeparatedListFormatter_MarkupSafe_two_items(format_type, item_one_type, item_two_type, expected_str):
comma_separated_list_formatter = formatters.CommaSeparatedListFormatter(locale_identifier='en')
formatted = comma_separated_list_formatter.format(
format_type('I like {list!l}.'),
list=[
item_one_type('<i>Python</i>'),
item_two_type('<i>Wikidata</i>'),
],
)
assert formatted == expected_str
assert type(formatted) == format_type
@pytest.mark.parametrize('format_type, item_one_type, item_two_type, item_three_type, expected_str', [
(str, str, str, str, 'I like <i>Python</i>, <i>Wikidata</i>, and <i>Music</i>.'),
(Markup, Markup, Markup, Markup, 'I like <i>Python</i>, <i>Wikidata</i>, and <i>Music</i>.'),
(Markup, str, Markup, str, 'I like <i>Python</i>, <i>Wikidata</i>, and <i>Music</i>.'),
])
def test_CommaSeparatedListFormatter_MarkupSafe_three_items(format_type, item_one_type, item_two_type, item_three_type, expected_str):
comma_separated_list_formatter = formatters.CommaSeparatedListFormatter(locale_identifier='en')
formatted = comma_separated_list_formatter.format(
format_type('I like {list!l}.'),
list=[
item_one_type('<i>Python</i>'),
item_two_type('<i>Wikidata</i>'),
item_three_type('<i>Music</i>'),
],
)
assert formatted == expected_str
assert type(formatted) == format_type
@pytest.mark.parametrize('gender, expected', [
('m', 'Thank him?'),
('f', 'Thank her?'),
('n', 'Thank them?'),
])
def test_GenderFormatter(gender, expected):
user = 'opaque value'
def get_gender(value):
assert value == user
return gender
gender_formatter = formatters.GenderFormatter(get_gender=get_gender)
assert gender_formatter.format(
'Thank {user!g:m=him:f=her:n=them}?',
user=user
) == expected
@pytest.mark.parametrize('gender', [
('m'),
('f'),
('n'),
])
def test_GenderFormatter_fallback(gender):
user = 'opaque value'
def get_gender(value):
assert value == user
return gender
gender_formatter = formatters.GenderFormatter(get_gender=get_gender)
assert gender_formatter.format(
'Thank {user!g:m=you}!',
user=user
) == 'Thank you!'
@pytest.mark.parametrize('format_type, adjective_type, expected_str', [
(str, str, 'Please respect <em>their</em> <strong>correct</strong> pronouns.'),
(Markup, Markup, 'Please respect <em>their</em> <strong>correct</strong> pronouns.'),
(str, Markup, 'Please respect <em>their</em> <strong>correct</strong> pronouns.'),
(Markup, str, 'Please respect <em>their</em> <strong>correct</strong> pronouns.'),
])
def test_GenderFormatter_MarkupSafe(format_type, adjective_type, expected_str):
gender_formatter = formatters.GenderFormatter(get_gender=lambda _: 'n')
formatted = gender_formatter.format(
format_type('Please respect {user!g:m=<em>his</em> {adjective}:f=<em>her</em> {adjective}:n=<em>their</em> {adjective}} pronouns.'),
user='user',
adjective=adjective_type('<strong>correct</strong>'),
)
assert formatted == expected_str
assert type(formatted) == format_type
def test_HyperlinkFormatter():
hyperlink_formatter = formatters.HyperlinkFormatter()
assert hyperlink_formatter.format(
'You need to {url!h:log in} before you can edit.',
url='/login',
) == 'You need to <a href="/login">log in</a> before you can edit.'
@pytest.mark.parametrize('format_type, url_type, expected_str', [
(str, str, 'You need to <a href="/login"">log <em>in</em></a> before you can edit.'),
(Markup, Markup, 'You need to <a href="/login"">log <em>in</em></a> before you can edit.'),
(str, Markup, 'You need to <a href="/login"">log <em>in</em></a> before you can edit.'),
(Markup, str, 'You need to <a href="/login"">log <em>in</em></a> before you can edit.'),
])
def test_HyperlinkFormatter_MarkupSafe(format_type, url_type, expected_str):
hyperlink_formatter = formatters.HyperlinkFormatter()
formatted = hyperlink_formatter.format(
format_type('You need to {url!h:log <em>in</em>} before you can edit.'),
url=url_type('/login"'),
)
assert formatted == expected_str
assert type(formatted) == format_type
flac = '<abbr title="Free Lossless Audio Codec">FLAC</abbr>'
@pytest.mark.parametrize('formats, user, expected', [
([Markup(flac)], 'Keith', f'His preferred <a href="/wiki/Format">format</a> is {flac}.'),
([Markup(flac), '"OGG"'], 'Keira', f'Her preferred <a href="/wiki/Format">formats</a> are {flac} and "OGG".'),
([Markup(flac), '"OGG"', 'OPUS'], 'Kim', f'Their preferred <a href="/wiki/Format">formats</a> are {flac}, "OGG", and OPUS.'),
])
def test_I18nFormatter_en(formats, user, expected):
genders = {
'Keith': 'm',
'Keira': 'f',
'Kim': 'n',
}
i18n_formatter = formatters.I18nFormatter(locale_identifier='en',
get_gender=lambda value: genders[value])
formatted = i18n_formatter.format(
Markup('{user!g:m=His:f=Her:n=Their} preferred {count!p:one={url!h:format} is:other={url!h:formats} are} {formats!l}.'),
user=user,
count=len(formats),
formats=formats,
url='/wiki/Format',
)
assert formatted == expected
assert isinstance(formatted, Markup)