Skip to content
Myr

test_sentinel.py

This file

Path

tests/test_utils/test_sentinel.py

97 lines · 2.3 KiB · Python

Last changed by

Stable (#3851)Rowlando13before adoption6aabf09

History of this file

Take it

1import copy2import pickle3from decimal import Decimal4from fractions import Fraction56import pytest78import click9from click._utils import Sentinel10from click._utils import UNSET111213def test_unset_sentinel():14    value = UNSET1516    assert value17    assert value is UNSET18    assert value == UNSET19    assert repr(value) == "Sentinel.UNSET"20    assert str(value) == "Sentinel.UNSET"21    assert bool(value) is True2223    # Try all native Python values that can be falsy or truthy.24    # See: https://docs.python.org/3/library/stdtypes.html#truth-value-testing25    real_values = (26        None,27        True,28        False,29        0,30        1,31        0.0,32        1.0,33        0j,34        1j,35        Decimal(0),36        Decimal(1),37        Fraction(0, 1),38        Fraction(1, 1),39        "",40        "a",41        "UNSET",42        "Sentinel.UNSET",43        [1],44        (1),45        {1: "a"},46        set(),47        set([1]),48        frozenset(),49        frozenset([1]),50        range(0),51        range(1),52    )5354    for real_value in real_values:55        assert value != real_value56        assert value is not real_value5758    assert value not in real_values596061@pytest.mark.parametrize("sentinel", tuple(Sentinel))62@pytest.mark.parametrize(63    "duplicate",64    (65        copy.copy,66        copy.deepcopy,67        lambda value: pickle.loads(pickle.dumps(value)),68    ),69    ids=("copy", "deepcopy", "pickle"),70)71def test_sentinel_duplication_preserves_identity(sentinel, duplicate):72    """Sentinels are singletons: copying or pickling one returns the member.7374    The default ``Enum`` reduction is ``(cls, (member.value,))``, which cannot75    round-trip a bare ``object()`` value: the copy or the unpickled value is a76    new object and ``Sentinel()`` rejects it.77    """78    assert duplicate(sentinel) is sentinel798081@pytest.mark.parametrize(82    "duplicate",83    (84        copy.copy,85        copy.deepcopy,86        lambda value: pickle.loads(pickle.dumps(value)),87    ),88    ids=("copy", "deepcopy", "pickle"),89)90def test_parameter_duplication(duplicate):91    """Every ``Parameter`` holds ``UNSET`` in ``default`` unless one is given."""92    option = click.Option(["--name"])93    assert option.default is UNSET9495    duplicated = duplicate(option)96    assert duplicated.default is UNSET97    assert duplicated.name == "name"