diff options
author | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2022-02-22 17:45:23 +0100 |
---|---|---|
committer | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2022-02-22 17:45:23 +0100 |
commit | 85727f9bd9b36d4936d4d19514b7cdfe4fffca43 (patch) | |
tree | c4264ad79b0e16ef7210664675fbef5e33fa18d8 /tests | |
parent | 3195e3e213084f086dec5d81447e0db16d622337 (diff) |
Subcommands
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_command.py | 62 |
1 files changed, 60 insertions, 2 deletions
diff --git a/tests/test_command.py b/tests/test_command.py index eed6e1f..d0b6437 100644 --- a/tests/test_command.py +++ b/tests/test_command.py @@ -6,19 +6,41 @@ import unittest import hazwaz +class MySubCommand(hazwaz.Command): + """ + A subcommand. + + This does very little. + """ + + def add_arguments(self, parser): + super().add_arguments(parser) + parser.add_argument( + "--bar", + action="store_true", + help="barfoo things" + ) + + def main(self): + print("Hello World") + + class MyCommand(hazwaz.MainCommand): """ A command that does things. This is a command, but honestly it doesn't really do anything. """ - commands = () + commands = ( + MySubCommand(), + ) def add_arguments(self, parser): super().add_arguments(parser) parser.add_argument( "--foo", - help="foobar things" + action="store_true", + help="foobar things", ) @@ -68,12 +90,48 @@ class testCommand(unittest.TestCase): self.assertIn("--verbose", cmd_help) self.assertIn("--foo", cmd_help) + def test_subparser(self): + cmd = MyCommand() + sub_parser = cmd.subparsers.choices["mysubcommand"] + self.assertEqual(sub_parser.description, "A subcommand.") + self.assertEqual( + sub_parser.epilog, + "This does very little.", + ) + def test_run(self): cmd = MyCommand() cmd_help = cmd.parser.format_help() stream = self._run_with_argv(cmd, ["mycommand"]) self.assertEqual(stream["stdout"].getvalue(), cmd_help) + def test_run_with_option(self): + cmd = MyCommand() + cmd_help = cmd.parser.format_help() + stream = self._run_with_argv(cmd, [ + "mycommand", + "--verbose", + ]) + self.assertEqual(stream["stdout"].getvalue(), cmd_help) + stream = self._run_with_argv(cmd, [ + "mycommand", + "--debug", + ]) + self.assertEqual(stream["stdout"].getvalue(), cmd_help) + + def test_run_subcommand(self): + cmd = MyCommand() + stream = self._run_with_argv(cmd, ["mycommand", "mysubcommand"]) + self.assertEqual(stream["stdout"].getvalue(), "Hello World\n") + + def test_run_subcommand_with_option(self): + cmd = MyCommand() + stream = self._run_with_argv(cmd, [ + "mycommand", + "mysubcommand", + "--bar", + ]) + self.assertEqual(stream["stdout"].getvalue(), "Hello World\n") if __name__ == '__main__': |