Skip to content

Commit d36de6f

Browse files
committed
Add more tests on Enum rendering their item's names and not values
Refs: #2911 #3004 #3028
1 parent 10b77f9 commit d36de6f

1 file changed

Lines changed: 58 additions & 26 deletions

File tree

tests/test_options.py

Lines changed: 58 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1667,8 +1667,9 @@ def cmd(foo):
16671667

16681668

16691669
class HashType(enum.Enum):
1670-
MD5 = enum.auto()
1671-
SHA1 = enum.auto()
1670+
MD5 = "MD5"
1671+
SHA1 = "SHA1"
1672+
SHA256 = "SHA-256"
16721673

16731674

16741675
class Number(enum.IntEnum):
@@ -1677,8 +1678,9 @@ class Number(enum.IntEnum):
16771678

16781679

16791680
class Letter(enum.StrEnum):
1680-
A = enum.auto()
1681-
B = enum.auto()
1681+
NAME_1 = "Value-1"
1682+
NAME_2 = "Value_2"
1683+
NAME_3 = "42_value"
16821684

16831685

16841686
class Color(enum.Flag):
@@ -1694,23 +1696,27 @@ class ColorInt(enum.IntFlag):
16941696

16951697

16961698
@pytest.mark.parametrize(
1697-
("choices", "metavars"),
1699+
("choices", "metavar"),
16981700
[
1699-
pytest.param(["foo", "bar"], "[TEXT]", id="text choices"),
1700-
pytest.param([1, 2], "[INTEGER]", id="int choices"),
1701-
pytest.param([1.0, 2.0], "[FLOAT]", id="float choices"),
1702-
pytest.param([True, False], "[BOOLEAN]", id="bool choices"),
1703-
pytest.param(["foo", 1], "[TEXT|INTEGER]", id="text/int choices"),
1704-
pytest.param(HashType, "[HASHTYPE]", id="enum choices"),
1705-
pytest.param(Number, "[NUMBER]", id="int enum choices"),
1706-
pytest.param(Letter, "[LETTER]", id="str enum choices"),
1707-
pytest.param(Color, "[COLOR]", id="flag enum choices"),
1708-
pytest.param(ColorInt, "[COLORINT]", id="int flag enum choices"),
1701+
(["foo", "bar"], "[TEXT]"),
1702+
([1, 2], "[INTEGER]"),
1703+
([1.0, 2.0], "[FLOAT]"),
1704+
([True, False], "[BOOLEAN]"),
1705+
(["foo", 1], "[TEXT|INTEGER]"),
1706+
(HashType, "[HASHTYPE]"),
1707+
(Number, "[NUMBER]"),
1708+
(Letter, "[LETTER]"),
1709+
(Color, "[COLOR]"),
1710+
(ColorInt, "[COLORINT]"),
17091711
],
17101712
)
1711-
def test_usage_show_choices(runner, choices, metavars):
1712-
"""When show_choices=False is set, the --help output
1713-
should print choice metavars instead of values.
1713+
def test_choice_usage_rendering(runner, choices, metavar):
1714+
"""BY default ``--help`` prints choice's values in the usage message.
1715+
1716+
But ``show_choices=False`` makes ``--help`` prints choice's METAVAR instead of
1717+
values.
1718+
1719+
Also check that usage error message always suggests the actual values.
17141720
"""
17151721

17161722
@click.command()
@@ -1723,14 +1729,31 @@ def cli_with_choices(g):
17231729
def cli_without_choices(g):
17241730
pass
17251731

1726-
result = runner.invoke(cli_with_choices, ["--help"])
1727-
assert (
1728-
f"[{'|'.join(i.name if isinstance(i, enum.Enum) else str(i) for i in choices)}]"
1729-
in result.output
1732+
display_values = tuple(
1733+
i.name if isinstance(i, enum.Enum) else str(i) for i in choices
17301734
)
17311735

1736+
# Check that the choices values are rendered as-is in the usage message.
1737+
result = runner.invoke(cli_with_choices, ["--help"])
1738+
assert f"[{'|'.join(display_values)}]" in result.stdout
1739+
assert not result.stderr
1740+
assert result.exit_code == 0
1741+
1742+
# Check that the metavar is rendered instead of the choices values themselves.
17321743
result = runner.invoke(cli_without_choices, ["--help"])
1733-
assert metavars in result.output
1744+
assert metavar in result.stdout
1745+
assert not result.stderr
1746+
assert result.exit_code == 0
1747+
1748+
# Check the usage error message suggests the actual accepted values.
1749+
for cli in (cli_with_choices, cli_without_choices):
1750+
result = runner.invoke(cli, ["-g", "random"])
1751+
assert (
1752+
"\n\nError: Invalid value for '-g': 'random' is not one of "
1753+
f"{', '.join(map(repr, display_values))}.\n" in result.stderr
1754+
)
1755+
assert not result.stdout
1756+
assert result.exit_code == 2
17341757

17351758

17361759
@pytest.mark.parametrize(
@@ -1751,15 +1774,24 @@ def cli_without_choices(g):
17511774
([True, False], False, "False"),
17521775
(["foo", 1], "foo", "foo"),
17531776
(["foo", 1], 1, "1"),
1754-
# Enum choices are rendered as their names.
1777+
# Enum choices are rendered as their names, not values.
17551778
# See: https://github.com/pallets/click/issues/2911
17561779
(HashType, HashType.SHA1, "SHA1"),
17571780
# Enum choices allow defaults strings that are their names.
1758-
(HashType, "SHA1", "SHA1"),
1781+
(HashType, HashType.SHA256, "SHA256"),
1782+
(HashType, "SHA256", "SHA256"),
17591783
(Number, Number.TWO, "TWO"),
1760-
(Letter, Letter.B, "B"),
1784+
(Number, "TWO", "TWO"),
1785+
(Letter, Letter.NAME_1, "NAME_1"),
1786+
(Letter, Letter.NAME_2, "NAME_2"),
1787+
(Letter, Letter.NAME_3, "NAME_3"),
1788+
(Letter, "NAME_1", "NAME_1"),
1789+
(Letter, "NAME_2", "NAME_2"),
1790+
(Letter, "NAME_3", "NAME_3"),
17611791
(Color, Color.GREEN, "GREEN"),
1792+
(Color, "GREEN", "GREEN"),
17621793
(ColorInt, ColorInt.GREEN, "GREEN"),
1794+
(ColorInt, "GREEN", "GREEN"),
17631795
],
17641796
)
17651797
def test_choice_default_rendering(runner, choices, default, default_string):

0 commit comments

Comments
 (0)