Add more tests on `Enum` rendering (#3053)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Rowlando13 · committed by GitHub · parents 10b77f9, d36de6f · 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.
Diff stats
1 file+58 added−26 removed
- tests:+58 added−26 removed
Provenance
- Commit
16fe802- Tree
bbbff1c- Author
Rowlando13 <67291205+Rowlando13@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent 1
10b77f9- Parent 2
d36de6f
1 file changed+58 added−26 removed
modified:tests/test_options.py+58 added-26 removed
1665 unmodified lines1666 assert result.exit_code == 01667 1668 1669 class HashType(enum.Enum):1670- MD5 = enum.auto()1671- SHA1 = enum.auto()1670+ MD5 = "MD5"1671+ SHA1 = "SHA1"1672+ SHA256 = "SHA-256"1673 1674 1675 class Number(enum.IntEnum):1676 ONE = enum.auto()1677 TWO = enum.auto()1678 1679 1680 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"1684 1685 1686 class Color(enum.Flag):1687 RED = enum.auto()7 unmodified lines1695 BLUE = enum.auto()1696 1697 1698 @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 )1711-def test_usage_show_choices(runner, choices, metavars):1712- """When show_choices=False is set, the --help output1713- 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 of1717+ values.1718+1719+ Also check that usage error message always suggests the actual values.1720 """1721 1722 @click.command()1723 @click.option("-g", type=click.Choice(choices))4 unmodified lines1728 @click.option("-g", type=click.Choice(choices), show_choices=False)1729 def cli_without_choices(g):1730 pass1731 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.output1732+ display_values = tuple(1733+ i.name if isinstance(i, enum.Enum) else str(i) for i in choices1734 )1735 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.stdout1739+ assert not result.stderr1740+ assert result.exit_code == 01741+1742+ # 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 == 01747 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.stderr1754+ )1755+ assert not result.stdout1756+ assert result.exit_code == 21757 1758+1759 @pytest.mark.parametrize(1760 ("choices", "default", "default_string"),1761 [1762 (["foo", "bar"], "bar", "bar"),10 unmodified lines1773 ([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 )1797 def test_choice_default_rendering(runner, choices, default, default_string):1798 @click.command()443 unmodified lines