Debian Codegraph @

tests/test_categories.py

raw symbols SCIP
1 """Tests help categories for Cmd and CommandSet objects."""
2
3 import argparse
4
5 import cmd2
6 from cmd2 import (
7 Cmd2ArgumentParser,
8 CommandSet,
9 with_argparser,
10 with_category,
11 )
12
13
14 class NoCategoryCmd(cmd2.Cmd):
15 """Example to demonstrate a Cmd-based class which does not define its own DEFAULT_CATEGORY.
16
17 Its commands will inherit the parent class's DEFAULT_CATEGORY.
18 """
19
20 def do_inherit(self, _: cmd2.Statement) -> None:
21 """This function has a docstring.
22
23 Since this class does NOT define its own DEFAULT_CATEGORY,
24 this command will show in cmd2.Cmd.DEFAULT_CATEGORY
25 """
26
27
28 class CategoryCmd(cmd2.Cmd):
29 """Example to demonstrate custom DEFAULT_CATEGORY in a Cmd-based class.
30
31 It also includes functions to fully exercise Cmd._build_command_info.
32 """
33
34 DEFAULT_CATEGORY = "CategoryCmd Commands"
35
36 def do_cmd_command(self, _: cmd2.Statement) -> None:
37 """The cmd command.
38
39 Since this class DOES define its own DEFAULT_CATEGORY,
40 this command will show in CategoryCmd.DEFAULT_CATEGORY
41 """
42
43 @with_argparser(Cmd2ArgumentParser(description="Overridden quit command"))
44 def do_quit(self, _: argparse.Namespace) -> None:
45 """This function overrides the cmd2.Cmd quit command.
46
47 Since this override does not use the with_category decorator,
48 it will be in CategoryCmd.DEFAULT_CATEGORY and not cmd2.Cmd.DEFAULT_CATEGORY.
49 """
50
51 @with_category(cmd2.Cmd.DEFAULT_CATEGORY)
52 @with_argparser(Cmd2ArgumentParser(description="Overridden shortcuts command"))
53 def do_shortcuts(self, _: argparse.Namespace) -> None:
54 """This function overrides the cmd2.Cmd shortcut command.
55
56 It also uses the with_category decorator to keep shortcuts in
57 cmd2.Cmd.DEFAULT_CATEGORY for the parent class.
58 """
59
60 def do_has_help_func(self, _: cmd2.Statement) -> None:
61 """This command has a help function."""
62
63 def help_has_help_func(self) -> None:
64 """Help function for the has_help_func command."""
65 self.poutput("has_help_func help text.")
66
67 def help_coding(self) -> None:
68 """This help function not tied to a command.
69
70 It will be in help topics.
71 """
72 self.poutput("Read a book.")
73
74
75 def test_no_category_cmd() -> None:
76 app = NoCategoryCmd()
77 cmds_cats, _help_topics = app._build_command_info()
78 assert "inherit" in cmds_cats[cmd2.Cmd.DEFAULT_CATEGORY]
79
80
81 def test_category_cmd() -> None:
82 app = CategoryCmd()
83 cmds_cats, help_topics = app._build_command_info()
84
85 assert "cmd_command" in cmds_cats[CategoryCmd.DEFAULT_CATEGORY]
86 assert "quit" in cmds_cats[CategoryCmd.DEFAULT_CATEGORY]
87 assert "shortcuts" in cmds_cats[cmd2.Cmd.DEFAULT_CATEGORY]
88 assert "has_help_func" in cmds_cats[CategoryCmd.DEFAULT_CATEGORY]
89 assert "coding" in help_topics
90
91
92 class NoCategoryCommandSet(CommandSet[cmd2.Cmd]):
93 """Example to demonstrate a CommandSet which does not define its own DEFAULT_CATEGORY.
94
95 Its commands will inherit the parent class's DEFAULT_CATEGORY.
96 """
97
98 def do_inherit(self, _: cmd2.Statement) -> None:
99 """This function has a docstring.
100
101 Since this class does NOT define its own DEFAULT_CATEGORY,
102 this command will show in CommandSet.DEFAULT_CATEGORY
103 """
104
105
106 class CategoryCommandSet(CommandSet[cmd2.Cmd]):
107 """Example to demonstrate custom DEFAULT_CATEGORY in a CommandSet."""
108
109 DEFAULT_CATEGORY = "CategoryCommandSet Commands"
110
111 def do_cmdset_command(self, _: cmd2.Statement) -> None:
112 """The cmdset command.
113
114 Since this class DOES define its own DEFAULT_CATEGORY,
115 this command will show in CategoryCommandSet.DEFAULT_CATEGORY
116 """
117
118
119 def test_no_category_command_set() -> None:
120 app = cmd2.Cmd()
121 app.register_command_set(NoCategoryCommandSet())
122 cmds_cats, _help_topics = app._build_command_info()
123 assert "inherit" in cmds_cats[CommandSet.DEFAULT_CATEGORY]
124
125
126 def test_category_command_set() -> None:
127 app = cmd2.Cmd()
128 app.register_command_set(CategoryCommandSet())
129 cmds_cats, _help_topics = app._build_command_info()
130 assert "cmdset_command" in cmds_cats[CategoryCommandSet.DEFAULT_CATEGORY]