Skip to content
Myr

test_custom_classes.py

This file

Path

tests/test_custom_classes.py

112 lines · 3.0 KiB · Python

Last changed by

Not known.

History of this file

Take it

1import click234def test_command_context_class():5    """A command with a custom ``context_class`` should produce a6    context using that type.7    """89    class CustomContext(click.Context):10        pass1112    class CustomCommand(click.Command):13        context_class = CustomContext1415    command = CustomCommand("test")16    context = command.make_context("test", [])17    assert isinstance(context, CustomContext)181920def test_context_invoke_type(runner):21    """A command invoked from a custom context should have a new22    context with the same type.23    """2425    class CustomContext(click.Context):26        pass2728    class CustomCommand(click.Command):29        context_class = CustomContext3031    @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_id3738    @click.command(cls=CustomCommand)39    @click.pass_context40    def first(ctx):41        assert isinstance(ctx, CustomContext)42        ctx.invoke(second, first_id=id(ctx))4344    assert not runner.invoke(first).exception454647def test_context_formatter_class():48    """A context with a custom ``formatter_class`` should format help49    using that type.50    """5152    class CustomFormatter(click.HelpFormatter):53        def write_heading(self, heading):54            heading = click.style(heading, fg="yellow")55            return super().write_heading(heading)5657    class CustomContext(click.Context):58        formatter_class = CustomFormatter5960    context = CustomContext(61        click.Command("test", params=[click.Option(["--value"])]), color=True62    )63    assert "\x1b[33mOptions\x1b[0m:" in context.get_help()646566def test_group_command_class(runner):67    """A group with a custom ``command_class`` should create subcommands68    of that type by default.69    """7071    class CustomCommand(click.Command):72        pass7374    class CustomGroup(click.Group):75        command_class = CustomCommand7677    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.Command828384def test_group_group_class(runner):85    """A group with a custom ``group_class`` should create subgroups86    of that type by default.87    """8889    class CustomSubGroup(click.Group):90        pass9192    class CustomGroup(click.Group):93        group_class = CustomSubGroup9495    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.Group100101102def test_group_group_class_self(runner):103    """A group with ``group_class = type`` should create subgroups of104    the same type as itself.105    """106107    class CustomGroup(click.Group):108        group_class = type109110    group = CustomGroup()111    subgroup = group.group()(lambda: None)112    assert type(subgroup) is CustomGroup