tests for custom class attributes
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parent c465912 · Browse files · 2 files·+128 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+128 added−5 removed
- tests:+112 added−0 removed
- other:+16 added−5 removed
Provenance
- Commit
aff5168- Tree
2855cfe- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
c465912
2 files changed+128 added−5 removed
added:tests/test_custom_classes.py+112 added
1+import click2+3+4+def test_command_context_class():5+ """A command with a custom ``context_class`` should produce a6+ context using that type.7+ """8+9+ class CustomContext(click.Context):10+ pass11+12+ class CustomCommand(click.Command):13+ context_class = CustomContext14+15+ command = CustomCommand("test")16+ context = command.make_context("test", [])17+ assert isinstance(context, CustomContext)18+19+20+def test_context_invoke_type(runner):21+ """A command invoked from a custom context should have a new22+ context with the same type.23+ """24+25+ class CustomContext(click.Context):26+ pass27+28+ class CustomCommand(click.Command):29+ context_class = CustomContext30+31+ @click.command()32+ @click.argument("first_id", type=int)33+ @click.pass_context34+ def second(ctx, first_id):35+ assert isinstance(ctx, CustomContext)36+ assert id(ctx) != first_id37+38+ @click.command(cls=CustomCommand)39+ @click.pass_context40+ def first(ctx):41+ assert isinstance(ctx, CustomContext)42+ ctx.invoke(second, first_id=id(ctx))43+44+ assert not runner.invoke(first).exception45+46+47+def test_context_formatter_class():48+ """A context with a custom ``formatter_class`` should format help49+ using that type.50+ """51+52+ class CustomFormatter(click.HelpFormatter):53+ def write_heading(self, heading):54+ heading = click.style(heading, fg="yellow")55+ return super().write_heading(heading)56+57+ class CustomContext(click.Context):58+ formatter_class = CustomFormatter59+60+ context = CustomContext(61+ click.Command("test", params=[click.Option(["--value"])]), color=True62+ )63+ assert "\x1b[33mOptions\x1b[0m:" in context.get_help()64+65+66+def test_group_command_class(runner):67+ """A group with a custom ``command_class`` should create subcommands68+ of that type by default.69+ """70+71+ class CustomCommand(click.Command):72+ pass73+74+ class CustomGroup(click.Group):75+ command_class = CustomCommand76+77+ group = CustomGroup()78+ subcommand = group.command()(lambda: None)79+ assert type(subcommand) is CustomCommand80+ subcommand = group.command(cls=click.Command)(lambda: None)81+ assert type(subcommand) is click.Command82+83+84+def test_group_group_class(runner):85+ """A group with a custom ``group_class`` should create subgroups86+ of that type by default.87+ """88+89+ class CustomSubGroup(click.Group):90+ pass91+92+ class CustomGroup(click.Group):93+ group_class = CustomSubGroup94+95+ group = CustomGroup()96+ subgroup = group.group()(lambda: None)97+ assert type(subgroup) is CustomSubGroup98+ subgroup = group.command(cls=click.Group)(lambda: None)99+ assert type(subgroup) is click.Group100+101+102+def test_group_group_class_self(runner):103+ """A group with ``group_class = type`` should create subgroups of104+ the same type as itself.105+ """106+107+ class CustomGroup(click.Group):108+ group_class = type109+110+ group = CustomGroup()111+ subgroup = group.group()(lambda: None)112+ assert type(subgroup) is CustomGroupmodified:CHANGES.rst+16 added-5 removed
41 unmodified lines42 - Non-string objects passed to ``style()`` and ``secho()`` will be43 converted to string. :pr:`1146`44 - ``edit(require_save=True)`` will detect saves for editors that exit45 very fast on filesystems with 1 second resolution. :pr:`1050`46-- ``Command`` has a ``context_class`` attribute to quickly customize47- the ``Context``. ``Context`` has a ``formatter_class`` attribute to48- quickly customize the ``HelpFormatter``. ``Context.invoke`` creates49- child contexts of the same type as itself. Core objects use50- ``super()`` consistently. :pr:`938`46+- New class attributes make it easier to use custom core objects47+ throughout an entire application. :pr:`938`48 49+ - ``Command.context_class`` controls the context created when50+ running the command.51+ - ``Context.invoke`` creates new contexts of the same type, so a52+ custom type will persist to invoked subcommands.53+ - ``Context.formatter_class`` controls the formatter used to54+ generate help and usage.55+ - ``Group.command_class`` changes the default type for56+ subcommands with ``@group.command()``.57+ - ``Group.group_class`` changes the default type for subgroups58+ with ``@group.group()``. Setting it to ``type`` will create59+ subgroups of the same type as the group itself.60+ - Core objects use ``super()`` consistently for better support of61+ subclassing.62 63+64 Version 7.1.265 -------------66 67 Released 2020-04-27633 unmodified lines