test.test_pycnnum API documentation

# test.test_pycnnum

All tests

View Source

     1"""All tests"""
     2
     3from collections import namedtuple
     4
     5import pytest
     6
     7from pycnnum import cn2num, num2cn
     8
     9TestData = namedtuple(
    10    "TestData",
    11    ["cn", "num", "num_type", "alt_0", "alt_2", "num2cn"],
    12    defaults=["一", 0, "mid", False, False, None],
    13)
    14"""Test data for cn2num and num2cn
    15
    16- cn: Chinese number, default to `"一"`
    17- num: Arabic number, default to `0`
    18- alt_0: Use alternative format zero, default to `False`
    19- alt_2: Use alternative format two, default to `False`
    20"""
    21
    22test_data = [
    23    TestData("一", 1),  # https://github.com/zcold/pycnnum/issues/7, https://github.com/zcold/pycnnum/issues/9
    24    TestData("十六", 16),  # https://github.com/zcold/pycnnum/issues/2
    25    TestData("一百一十六", 116),  # https://github.com/zcold/pycnnum/issues/2
    26    TestData("一百零一", 101),  # https://github.com/zcold/pycnnum/issues/4
    27    TestData("一百二十三", 123),
    28    TestData("一千二百三十四", 1234),
    29    TestData("一千两百三十四", 1234, alt_2=True),
    30    TestData("两千四百零一", 2401, alt_2=True),  # https://github.com/zcold/pycnnum/issues/5
    31    TestData("二千四百零一", 2401),  # https://github.com/zcold/pycnnum/issues/5
    32    TestData("一万二千三百四十五", 12345),
    33    TestData("一百二十三万四千五百六十七", 1234567),
    34    TestData("一千二百三十四万五千六百七十八", 12345678),
    35    TestData("一亿二千三百四十五万六千七百八十九", 123456789),
    36    TestData("一百二十三亿四千五百六十七万八千九百零一", 12345678901),
    37    TestData("一千两百三十四亿五千六百七十八万九千零一", 123456789001, alt_2=True),
    38    TestData("点四五", 0.45, num2cn="零点四五"),  # https://github.com/zcold/pycnnum/issues/1
    39    TestData("零点四五", 0.45),  # https://github.com/zcold/pycnnum/issues/1
    40    TestData("三点四", 3.4),  # https://github.com/zcold/pycnnum/issues/1
    41    TestData("二千万零一百八十五", 20000185),
    42    TestData("两千万一百八十五", 20000185, num2cn="二千万零一百八十五"),
    43]
    44"""Test data for `pycnnum.pycnnum.cn2num` and `pycnnum.pycnnum.num2cn`"""
    45
    46
    47@pytest.mark.parametrize("td", test_data)
    48def test_cn2num(td: TestData) -> None:
    49    """Test cn2num
    50
    51    - Test if `td.cn` is converted to `td.num`
    52    - If `td.num2cn` is not `None`, test if `td.num2cn` is converted to `td.num`
    53
    54    Args:
    55        td (TestData): Test data
    56    """
    57    assert cn2num(td.cn, numbering_type=td.num_type) == td.num
    58    if td.num2cn:
    59        assert cn2num(td.num2cn, numbering_type=td.num_type) == td.num
    60
    61
    62@pytest.mark.parametrize("td", test_data)
    63def test_num2cn(td: TestData):
    64    """Test num2cn
    65
    66    - Test if `td.num` is converted to `td.cn`
    67    - Test if `str(td.num)` is converted to `td.cn`
    68
    69    Args:
    70        td (TestData): Test data
    71    """
    72    cn = td.num2cn if td.num2cn else td.cn
    73    assert num2cn(td.num, numbering_type=td.num_type, alt_0=td.alt_0, alt_2=td.alt_2) == cn
    74    assert num2cn(str(td.num), numbering_type=td.num_type, alt_0=td.alt_0, alt_2=td.alt_2) == cn

class TestData(builtins.tuple):

Test data for cn2num and num2cn

- cn: Chinese number, default to `"一"`
- num: Arabic number, default to `0`
- alt_0: Use alternative format zero, default to `False`
- alt_2: Use alternative format two, default to `False`

TestData(cn='一', num=0, num_type='mid', alt_0=False, alt_2=False, num2cn=None)

Create new instance of TestData(cn, num, num_type, alt_0, alt_2, num2cn)

cn

Alias for field number 0

num

Alias for field number 1

num_type

Alias for field number 2

alt_0

Alias for field number 3

alt_2

Alias for field number 4

num2cn

Alias for field number 5

##### Inherited Members

builtins.tuple
  index
  count

test_data = [TestData(cn='一', num=1, num_type='mid', alt_0=False, alt_2=False, num2cn=None), TestData(cn='十六', num=16, num_type='mid', alt_0=False, alt_2=False, num2cn=None), TestData(cn='一百一十六', num=116, num_type='mid', alt_0=False, alt_2=False, num2cn=None), TestData(cn='一百零一', num=101, num_type='mid', alt_0=False, alt_2=False, num2cn=None), TestData(cn='一百二十三', num=123, num_type='mid', alt_0=False, alt_2=False, num2cn=None), TestData(cn='一千二百三十四', num=1234, num_type='mid', alt_0=False, alt_2=False, num2cn=None), TestData(cn='一千两百三十四', num=1234, num_type='mid', alt_0=False, alt_2=True, num2cn=None), TestData(cn='两千四百零一', num=2401, num_type='mid', alt_0=False, alt_2=True, num2cn=None), TestData(cn='二千四百零一', num=2401, num_type='mid', alt_0=False, alt_2=False, num2cn=None), TestData(cn='一万二千三百四十五', num=12345, num_type='mid', alt_0=False, alt_2=False, num2cn=None), TestData(cn='一百二十三万四千五百六十七', num=1234567, num_type='mid', alt_0=False, alt_2=False, num2cn=None), TestData(cn='一千二百三十四万五千六百七十八', num=12345678, num_type='mid', alt_0=False, alt_2=False, num2cn=None), TestData(cn='一亿二千三百四十五万六千七百八十九', num=123456789, num_type='mid', alt_0=False, alt_2=False, num2cn=None), TestData(cn='一百二十三亿四千五百六十七万八千九百零一', num=12345678901, num_type='mid', alt_0=False, alt_2=False, num2cn=None), TestData(cn='一千两百三十四亿五千六百七十八万九千零一', num=123456789001, num_type='mid', alt_0=False, alt_2=True, num2cn=None), TestData(cn='点四五', num=0.45, num_type='mid', alt_0=False, alt_2=False, num2cn='零点四五'), TestData(cn='零点四五', num=0.45, num_type='mid', alt_0=False, alt_2=False, num2cn=None), TestData(cn='三点四', num=3.4, num_type='mid', alt_0=False, alt_2=False, num2cn=None), TestData(cn='二千万零一百八十五', num=20000185, num_type='mid', alt_0=False, alt_2=False, num2cn=None), TestData(cn='两千万一百八十五', num=20000185, num_type='mid', alt_0=False, alt_2=False, num2cn='二千万零一百八十五')]

Test data for `pycnnum.pycnnum.cn2num` and `pycnnum.pycnnum.num2cn`

@pytest.mark.parametrize('td', test_data)
def test_cn2num(td: TestData) -> None: View Source

    48@pytest.mark.parametrize("td", test_data)
    49def test_cn2num(td: TestData) -> None:
    50    """Test cn2num
    51
    52    - Test if `td.cn` is converted to `td.num`
    53    - If `td.num2cn` is not `None`, test if `td.num2cn` is converted to `td.num`
    54
    55    Args:
    56        td (TestData): Test data
    57    """
    58    assert cn2num(td.cn, numbering_type=td.num_type) == td.num
    59    if td.num2cn:
    60        assert cn2num(td.num2cn, numbering_type=td.num_type) == td.num

Test cn2num

- Test if `td.cn` is converted to `td.num`
- If `td.num2cn` is not `None`, test if `td.num2cn` is converted to `td.num`

###### Arguments:

- td (TestData): Test data

@pytest.mark.parametrize('td', test_data)
def test_num2cn(td: TestData): View Source

    63@pytest.mark.parametrize("td", test_data)
    64def test_num2cn(td: TestData):
    65    """Test num2cn
    66
    67    - Test if `td.num` is converted to `td.cn`
    68    - Test if `str(td.num)` is converted to `td.cn`
    69
    70    Args:
    71        td (TestData): Test data
    72    """
    73    cn = td.num2cn if td.num2cn else td.cn
    74    assert num2cn(td.num, numbering_type=td.num_type, alt_0=td.alt_0, alt_2=td.alt_2) == cn
    75    assert num2cn(str(td.num), numbering_type=td.num_type, alt_0=td.alt_0, alt_2=td.alt_2) == cn

Test num2cn

- Test if `td.num` is converted to `td.cn`
- Test if `str(td.num)` is converted to `td.cn`

###### Arguments:

- td (TestData): Test data