Debian Codegraph @

tests/test_custom_classes.py

raw
1 import click
2
3
4 def test_command_context_class():
5 """A command with a custom ``context_class`` should produce a
6 context using that type.
7 """
8
9 class CustomContext(click.Context):
10 pass
11
12 class CustomCommand(click.Command):
13 context_class = CustomContext
14
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 new
22 context with the same type.
23 """
24
25 class CustomContext(click.Context):
26 pass
27
28 class CustomCommand(click.Command):
29 context_class = CustomContext
30
31 @click.command()
32 @click.argument("first_id", type=int)
33 @click.pass_context
34 def second(ctx, first_id):
35 assert isinstance(ctx, CustomContext)
36 assert id(ctx) != first_id
37
38 @click.command(cls=CustomCommand)
39 @click.pass_context
40 def first(ctx):
41 assert isinstance(ctx, CustomContext)
42 ctx.invoke(second, first_id=id(ctx))
43
44 assert not runner.invoke(first).exception
45
46
47 def test_context_formatter_class():
48 """A context with a custom ``formatter_class`` should format help
49 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 = CustomFormatter
59
60 context = CustomContext(
61 click.Command("test", params=[click.Option(["--value"])]), color=True
62 )
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 subcommands
68 of that type by default.
69 """
70
71 class CustomCommand(click.Command):
72 pass
73
74 class CustomGroup(click.Group):
75 command_class = CustomCommand
76
77 group = CustomGroup()
78 subcommand = group.command()(lambda: None)
79 assert type(subcommand) is CustomCommand
80 subcommand = group.command(cls=click.Command)(lambda: None)
81 assert type(subcommand) is click.Command
82
83
84 def test_group_group_class(runner):
85 """A group with a custom ``group_class`` should create subgroups
86 of that type by default.
87 """
88
89 class CustomSubGroup(click.Group):
90 pass
91
92 class CustomGroup(click.Group):
93 group_class = CustomSubGroup
94
95 group = CustomGroup()
96 subgroup = group.group()(lambda: None)
97 assert type(subgroup) is CustomSubGroup
98 subgroup = group.command(cls=click.Group)(lambda: None)
99 assert type(subgroup) is click.Group
100
101
102 def test_group_group_class_self(runner):
103 """A group with ``group_class = type`` should create subgroups of
104 the same type as itself.
105 """
106
107 class CustomGroup(click.Group):
108 group_class = type
109
110 group = CustomGroup()
111 subgroup = group.group()(lambda: None)
112 assert type(subgroup) is CustomGroup