Skip to content
Myr

Add more tests on Enum rendering their item's names and not values

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Refs: https://github.com/pallets/click/issues/2911 https://github.com/pallets/click/pull/3004 https://github.com/pallets/click/issues/3028

authored by Kevin Deldycke · committed by Kevin Deldycke · parent 10b77f9 · Browse files · 1 file+58 added−26 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Refs:
  • https: //github.com/pallets/click/issues/2911
  • https: //github.com/pallets/click/pull/3004
  • https: //github.com/pallets/click/issues/3028

Diff stats

1 file+58 added−26 removed

  • tests:+58 added−26 removed

1 file+58 added−26 removed

modified:tests/test_options.py+58 added-26 removed
1665 unmodified lines
1666 assert result.exit_code == 0166716681669class HashType(enum.Enum):1670 MD5 = enum.auto()1671 SHA1 = enum.auto()1670 MD5 = "MD5"1671 SHA1 = "SHA1"1672 SHA256 = "SHA-256"167316741675class Number(enum.IntEnum):1676 ONE = enum.auto()1677 TWO = enum.auto()167816791680class 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"168416851686class Color(enum.Flag):1687 RED = enum.auto()
7 unmodified lines
1695 BLUE = enum.auto()169616971698@pytest.mark.parametrize(1697 ("choices", "metavars"),1699 ("choices", "metavar"),1700 [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]"),1711 ],1712)1711def test_usage_show_choices(runner, choices, metavars):1712 """When show_choices=False is set, the --help output1713 should print choice metavars instead of values.1713def test_choice_usage_rendering(runner, choices, metavar):1714 """BY default ``--help`` prints choice's values in the usage message.17151716 But ``show_choices=False`` makes ``--help`` prints choice's METAVAR instead of1717 values.17181719 Also check that usage error message always suggests the actual values.1720 """17211722 @click.command()1723 @click.option("-g", type=click.Choice(choices))
4 unmodified lines
1728 @click.option("-g", type=click.Choice(choices), show_choices=False)1729 def cli_without_choices(g):1730 pass17311726 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.output1732 display_values = tuple(1733 i.name if isinstance(i, enum.Enum) else str(i) for i in choices1734 )17351736 # 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.stdout1739 assert not result.stderr1740 assert result.exit_code == 017411742 # Check that the metavar is rendered instead of the choices values themselves.1743 result = runner.invoke(cli_without_choices, ["--help"])1733 assert metavars in result.output1744 assert metavar in result.stdout1745 assert not result.stderr1746 assert result.exit_code == 017471748 # 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.stderr1754 )1755 assert not result.stdout1756 assert result.exit_code == 2175717581759@pytest.mark.parametrize(1760 ("choices", "default", "default_string"),1761 [1762 (["foo", "bar"], "bar", "bar"),
10 unmodified lines
1773 ([True, False], True, "True"),1774 ([True, False], False, "False"),1775 (["foo", 1], "foo", "foo"),1776 (["foo", 1], 1, "1"),1754 # Enum choices are rendered as their names.1777 # Enum choices are rendered as their names, not values.1778 # See: https://github.com/pallets/click/issues/29111779 (HashType, HashType.SHA1, "SHA1"),1780 # Enum choices allow defaults strings that are their names.1758 (HashType, "SHA1", "SHA1"),1781 (HashType, HashType.SHA256, "SHA256"),1782 (HashType, "SHA256", "SHA256"),1783 (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"),1791 (Color, Color.GREEN, "GREEN"),1792 (Color, "GREEN", "GREEN"),1793 (ColorInt, ColorInt.GREEN, "GREEN"),1794 (ColorInt, "GREEN", "GREEN"),1795 ],1796)1797def test_choice_default_rendering(runner, choices, default, default_string):1798 @click.command()
443 unmodified lines