-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocale_test.py
More file actions
69 lines (49 loc) · 2.12 KB
/
locale_test.py
File metadata and controls
69 lines (49 loc) · 2.12 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
"""Output formatting using `locale`."""
import locale
from datetime import datetime
from typing import Generator
import pytest
@pytest.fixture(name="zh_cn")
def fixture_zh_cn() -> Generator[None, None, None]:
"""Set current locale to `zh_CN.UTF-8`."""
locale.setlocale(locale.LC_ALL, ("zh_CN", "UTF-8"))
yield
locale.resetlocale()
@pytest.fixture(name="de_de")
def fixture_de_de() -> Generator[None, None, None]:
"""Set current locale to `de_DE.UTF-8`."""
locale.setlocale(locale.LC_ALL, ("de_DE", "UTF-8"))
yield
locale.resetlocale()
def test_number_zh(zh_cn: None) -> None:
"""Format number using the Chinese locale."""
assert locale.format_string("Int: %d", 1234567, grouping=True) == "Int: 1,234,567"
assert locale.atoi("1,234,567") == 1234567
assert (
locale.format_string("Float: %.2f", 12345.67, grouping=True)
== "Float: 12,345.67"
)
assert locale.atof("12,345.67") == 12345.67
def test_number_de(de_de: None) -> None:
"""Format number using the German locale."""
assert locale.format_string("Int: %d", 1234567, grouping=True) == "Int: 1.234.567"
assert locale.atoi("1.234.567") == 1234567
assert (
locale.format_string("Float: %.2f", 12345.67, grouping=True)
== "Float: 12.345,67"
)
assert locale.atof("12.345,67") == 12345.67
def test_currency_zh(zh_cn: None) -> None:
"""Format number with currency using the Chinese locale."""
assert locale.currency(12345.67, grouping=True) == "¥12,345.67"
def test_currency_de(de_de: None) -> None:
"""Format number with currency using the German locale."""
assert locale.currency(12345.67, grouping=True) == "12.345,67 €"
def test_datetime_zh(zh_cn: None) -> None:
"""Format date and time using the Chinese locale."""
date_time = datetime(2020, 7, 15, 13, 10, 5)
assert date_time.strftime("%c") == "2020年07月15日 星期三 13时10分05秒"
def test_datetime_de(de_de: None) -> None:
"""Format date and time using the German locale."""
date_time = datetime(2020, 7, 15, 13, 10, 5)
assert date_time.strftime("%c") == "Mi 15 Jul 2020 13:10:05 "