summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/test_command.py62
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__':