-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathtest_types.py
More file actions
345 lines (289 loc) · 12.4 KB
/
Copy pathtest_types.py
File metadata and controls
345 lines (289 loc) · 12.4 KB
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import re
import warnings
from datetime import date, datetime, time
from decimal import Decimal
import pytest
import pytz
from babel import Locale
from fluent.runtime.types import (
FluentDateType,
FluentNumber,
fluent_date,
fluent_number,
)
en_US = Locale.parse("en_US")
cur_pos = fluent_number(123456.78123, currency="USD", style="currency")
cur_neg = fluent_number(-123456.78123, currency="USD", style="currency")
class TestFluentNumber:
def test_int(self):
i = fluent_number(1)
assert isinstance(i, int)
assert isinstance(i, FluentNumber)
assert i + 1 == 2
def test_float(self):
f = fluent_number(1.1)
assert isinstance(f, float)
assert isinstance(f, FluentNumber)
assert f + 1 == 2.1
def test_decimal(self):
d = Decimal("1.1")
assert isinstance(fluent_number(d), Decimal)
assert isinstance(fluent_number(d), FluentNumber)
assert d + 1 == Decimal("2.1")
def test_disallow_nonexistant_options(self):
with pytest.raises(TypeError):
fluent_number(
1,
not_a_real_option=True,
)
def test_style_validation(self):
with pytest.raises(ValueError):
fluent_number(1, style="xyz")
def test_use_grouping(self):
f1 = fluent_number(123456.78, useGrouping=True)
f2 = fluent_number(123456.78, useGrouping=False)
assert f1.format(en_US) == "123,456.78"
assert f2.format(en_US) == "123456.78"
# ensure we didn't mutate anything when we created the new
# NumberPattern:
assert f1.format(en_US) == "123,456.78"
def test_use_grouping_decimal(self):
d = Decimal("123456.78")
f1 = fluent_number(d, useGrouping=True)
f2 = fluent_number(d, useGrouping=False)
assert f1.format(en_US) == "123,456.78"
assert f2.format(en_US) == "123456.78"
def test_minimum_integer_digits(self):
f = fluent_number(1.23, minimumIntegerDigits=3)
assert f.format(en_US) == "001.23"
def test_minimum_integer_digits_decimal(self):
f = fluent_number(Decimal("1.23"), minimumIntegerDigits=3)
assert f.format(en_US) == "001.23"
def test_minimum_fraction_digits(self):
f = fluent_number(1.2, minimumFractionDigits=3)
assert f.format(en_US) == "1.200"
def test_maximum_fraction_digits(self):
f1 = fluent_number(1.23456)
assert f1.format(en_US) == "1.235"
f2 = fluent_number(1.23456, maximumFractionDigits=5)
assert f2.format(en_US) == "1.23456"
def test_minimum_significant_digits(self):
f1 = fluent_number(123, minimumSignificantDigits=5)
assert f1.format(en_US) == "123.00"
f2 = fluent_number(12.3, minimumSignificantDigits=5)
assert f2.format(en_US) == "12.300"
def test_maximum_significant_digits(self):
f1 = fluent_number(123456, maximumSignificantDigits=3)
assert f1.format(en_US) == "123,000"
f2 = fluent_number(12.3456, maximumSignificantDigits=3)
assert f2.format(en_US) == "12.3"
f3 = fluent_number(12, maximumSignificantDigits=5)
assert f3.format(en_US) == "12"
def test_currency(self):
# This test the default currencyDisplay value
assert cur_pos.format(en_US) == "$123,456.78"
def test_currency_display_validation(self):
with pytest.raises(ValueError):
fluent_number(1234, currencyDisplay="junk")
def test_currency_display_symbol(self):
cur_pos_sym = fluent_number(cur_pos, currencyDisplay="symbol")
cur_neg_sym = fluent_number(cur_neg, currencyDisplay="symbol")
assert cur_pos_sym.format(en_US) == "$123,456.78"
assert cur_neg_sym.format(en_US) == "-$123,456.78"
def test_currency_display_code(self):
# Outputs here were determined by comparing with Javascrpt
# Intl.NumberFormat in Firefox.
cur_pos_code = fluent_number(cur_pos, currencyDisplay="code")
cur_neg_code = fluent_number(cur_neg, currencyDisplay="code")
assert cur_pos_code.format(en_US) == "USD123,456.78"
assert cur_neg_code.format(en_US) == "-USD123,456.78"
@pytest.mark.skip("Babel doesn't provide support for this yet")
def test_currency_display_name(self):
cur_pos_name = fluent_number(cur_pos, currencyDisplay="name")
cur_neg_name = fluent_number(cur_neg, currencyDisplay="name")
assert cur_pos_name.format(en_US) == "123,456.78 US dollars"
assert cur_neg_name.format(en_US) == "-123,456.78 US dollars"
# Some others locales:
hr_BA = Locale.parse("hr_BA")
assert cur_pos_name.format(hr_BA) == "123.456,78 američkih dolara"
es_GT = Locale.parse("es_GT")
assert cur_pos_name.format(es_GT) == "dólares estadounidenses 123,456.78"
def test_copy_attributes(self):
f1 = fluent_number(123456.78, useGrouping=False)
assert f1.options.useGrouping is False
# Check we didn't mutate anything
assert FluentNumber.default_number_format_options.useGrouping is True
f2 = fluent_number(f1, style="percent")
assert f2.options.style == "percent"
# Check we copied
assert f2.options.useGrouping is False
# and didn't mutate anything
assert f1.options.style == "decimal"
assert FluentNumber.default_number_format_options.style == "decimal"
a_date = date(2018, 2, 1)
a_datetime = datetime(2018, 2, 1, 14, 15, 16, 123456, tzinfo=pytz.UTC)
a_time = time(10, 31, 00, 333, tzinfo=pytz.UTC)
class TestFluentDate:
def test_date(self):
fd = fluent_date(a_date)
assert isinstance(fd, date)
assert isinstance(fd, FluentDateType)
assert fd.year == a_date.year
assert fd.month == a_date.month
assert fd.day == a_date.day
def test_time(self):
fd = fluent_date(a_time)
assert isinstance(fd, time)
assert isinstance(fd, FluentDateType)
assert fd.hour == a_time.hour
assert fd.minute == a_time.minute
assert fd.second == a_time.second
assert fd.microsecond == a_time.microsecond
assert fd.tzinfo == a_time.tzinfo
def test_datetime(self):
fd = fluent_date(a_datetime)
assert isinstance(fd, datetime)
assert isinstance(fd, FluentDateType)
assert fd.year == a_datetime.year
assert fd.month == a_datetime.month
assert fd.day == a_datetime.day
assert fd.hour == a_datetime.hour
assert fd.minute == a_datetime.minute
assert fd.second == a_datetime.second
assert fd.microsecond == a_datetime.microsecond
assert fd.tzinfo == a_datetime.tzinfo
def test_date_format_defaults(self):
fd = fluent_date(a_date)
en_US = Locale.parse("en_US")
en_GB = Locale.parse("en_GB")
assert fd.format(en_GB) == "1 Feb 2018"
assert fd.format(en_US) == "Feb 1, 2018"
def test_time_format_defaults(self):
fd = fluent_date(a_time)
en_US = Locale.parse('en_US')
en_GB = Locale.parse('en_GB')
assert fd.format(en_GB) == '10:31'
assert re.search('^10:31\\sAM$', fd.format(en_US))
def test_datetime_format_defaults(self):
fd = fluent_date(a_datetime)
en_US = Locale.parse('en_US')
en_GB = Locale.parse('en_GB')
assert fd.format(en_GB) == '1 Feb 2018'
assert re.search('Feb 1, 2018', fd.format(en_US))
def test_dateStyle_date(self):
fd = fluent_date(a_date, dateStyle="long")
en_US = Locale.parse("en_US")
en_GB = Locale.parse("en_GB")
assert fd.format(en_GB) == "1 February 2018"
assert fd.format(en_US) == "February 1, 2018"
def test_dateStyle_datetime(self):
fd = fluent_date(a_datetime, dateStyle="long")
en_US = Locale.parse("en_US")
en_GB = Locale.parse("en_GB")
assert fd.format(en_GB) == "1 February 2018"
assert fd.format(en_US) == "February 1, 2018"
def test_timeStyle_datetime(self):
fd = fluent_date(a_datetime, timeStyle="short")
en_US = Locale.parse("en_US")
en_GB = Locale.parse("en_GB")
assert re.search("^2:15\\sPM$", fd.format(en_US))
assert fd.format(en_GB) == "14:15"
def test_timeStyle_time(self):
fd = fluent_date(a_datetime.time(), timeStyle='short')
en_US = Locale.parse('en_US')
en_GB = Locale.parse('en_GB')
assert re.search('^2:15\\sPM$', fd.format(en_US))
assert fd.format(en_GB) == '14:15'
def test_dateStyle_and_timeStyle_datetime(self):
fd = fluent_date(a_datetime, timeStyle="short", dateStyle="short")
en_US = Locale.parse("en_US")
en_GB = Locale.parse("en_GB")
assert re.search("^2/1/18, 2:15\\sPM$", fd.format(en_US))
assert fd.format(en_GB) == "01/02/2018, 14:15"
def test_validate_dateStyle(self):
with pytest.raises(ValueError):
fluent_date(a_date, dateStyle="nothing")
def test_validate_timeStyle(self):
with pytest.raises(ValueError):
fluent_date(a_datetime, timeStyle="nothing")
def test_timeZone(self):
en_GB = Locale.parse("en_GB")
LondonTZ = pytz.timezone("Europe/London")
# 1st July is a date in British Summer Time
# datetime object with tzinfo set to BST
dt1 = datetime(2018, 7, 1, 23, 30, 0, tzinfo=pytz.UTC).astimezone(LondonTZ)
fd1 = fluent_date(dt1, dateStyle="short", timeStyle="short")
assert fd1.format(en_GB) == "02/07/2018, 00:30"
fd1b = fluent_date(dt1, dateStyle="full", timeStyle="full")
assert re.search(
"^Monday,? 2 July 2018(,| at) 00:30:00 British Summer Time$",
fd1b.format(en_GB),
)
fd1c = fluent_date(dt1, dateStyle="short")
assert fd1c.format(en_GB) == "02/07/2018"
fd1d = fluent_date(dt1, timeStyle="short")
assert fd1d.format(en_GB) == "00:30"
# datetime object with no TZ, TZ passed in to fluent_date
dt2 = datetime(2018, 7, 1, 23, 30, 0) # Assumed UTC
fd2 = fluent_date(
dt2, dateStyle="short", timeStyle="short", timeZone="Europe/London"
)
assert fd2.format(en_GB) == "02/07/2018, 00:30"
fd2b = fluent_date(
dt2, dateStyle="full", timeStyle="full", timeZone="Europe/London"
)
assert re.search(
"^Monday,? 2 July 2018(,| at) 00:30:00 British Summer Time$",
fd2b.format(en_GB),
)
fd2c = fluent_date(dt2, dateStyle="short", timeZone="Europe/London")
assert fd2c.format(en_GB) == "02/07/2018"
fd2d = fluent_date(dt1, timeStyle="short", timeZone="Europe/London")
assert fd2d.format(en_GB) == "00:30"
ft = fluent_date(a_time, timeZone="UTC")
assert ft.format(en_GB) == "10:31"
ft = fluent_date(a_time, timeZone="Europe/London")
with pytest.raises(TypeError):
ft.format(en_GB)
ft = fluent_date(time(10, 31, 00, 333), timeZone="Europe/London")
assert ft.format(en_GB) == "10:31"
def test_allow_unsupported_options(self):
# We are just checking that these don't raise exceptions
with warnings.catch_warnings():
warnings.simplefilter("ignore")
fluent_date(
a_date,
hour12=True,
weekday="narrow",
era="narrow",
year="numeric",
month="numeric",
day="numeric",
hour="numeric",
minute="numeric",
second="numeric",
timeZoneName="short",
)
def test_disallow_nonexistant_options(self):
with pytest.raises(TypeError):
fluent_date(
a_date,
not_a_real_option=True,
)
def test_dont_wrap_unnecessarily(self):
f1 = fluent_date(a_date)
f2 = fluent_date(f1)
assert f1 is f2
def test_copy_attributes(self):
f1 = fluent_date(a_date, dateStyle="long", hour12=False)
assert f1.options.dateStyle == "long"
f2 = fluent_date(f1, hour12=False)
# Check we copied other attributes:
assert f2.options.dateStyle == "long"
assert f2.options.hour12 is False
# Check we can override
f3 = fluent_date(f2, dateStyle="full")
assert f3.options.dateStyle == "full"
# and didn't mutate anything
assert f1.options.dateStyle == "long"
assert f2.options.dateStyle == "long"