summaryrefslogtreecommitdiff
path: root/test_natsort/test_final_data_transform_factory.py
blob: d256e30d693a1a5db9e35b24ee924ab0519ed0e1 (plain)
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
# -*- coding: utf-8 -*-
"""These test the utils.py functions."""
from __future__ import unicode_literals

import pytest
from hypothesis import given
from hypothesis.strategies import floats, integers, text
from natsort.compat.py23 import py23_str
from natsort.ns_enum import ns, ns_DUMB
from natsort.utils import final_data_transform_factory


@pytest.mark.parametrize("alg", [ns.DEFAULT, ns.UNGROUPLETTERS, ns.LOCALE])
@given(x=text(), y=floats(allow_nan=False, allow_infinity=False) | integers())
@pytest.mark.usefixtures("with_locale_en_us")
def test_final_data_transform_factory_default(x, y, alg):
    final_data_transform_func = final_data_transform_factory(alg, "", "::")
    value = (x, y)
    original_value = "".join(map(py23_str, value))
    result = final_data_transform_func(value, original_value)
    assert result == value


@pytest.mark.parametrize(
    "alg, func",
    [
        (ns.UNGROUPLETTERS | ns.LOCALE, lambda x: x),
        (ns.LOCALE | ns.UNGROUPLETTERS | ns_DUMB, lambda x: x),
        (ns.LOCALE | ns.UNGROUPLETTERS | ns.LOWERCASEFIRST, lambda x: x),
        (
            ns.LOCALE | ns.UNGROUPLETTERS | ns_DUMB | ns.LOWERCASEFIRST,
            lambda x: x.swapcase(),
        ),
    ],
)
@given(x=text(), y=floats(allow_nan=False, allow_infinity=False) | integers())
@pytest.mark.usefixtures("with_locale_en_us")
def test_final_data_transform_factory_ungroup_and_locale(x, y, alg, func):
    final_data_transform_func = final_data_transform_factory(alg, "", "::")
    value = (x, y)
    original_value = "".join(map(py23_str, value))
    result = final_data_transform_func(value, original_value)
    if x:
        expected = (tuple(func(original_value[:1])), value)
    else:
        expected = (("::",), value)
    assert result == expected


def test_final_data_transform_factory_ungroup_and_locale_empty_tuple():
    final_data_transform_func = final_data_transform_factory(ns.UG | ns.L, "", "::")
    assert final_data_transform_func((), "") == ((), ())