def test_creation(self): @command def test(): test.has_been_called = True self.assertTrue("test" in context.commands()) context.exec("test") self.assertTrue(hasattr(test, "has_been_called"))
def test_basic_argument(self): @command @argument("arg") def foo(arg): foo.passed_arg = arg context.exec("foo arrrrg") self.assertTrue(hasattr(foo, "passed_arg")) self.assertEqual("arrrrg", foo.passed_arg)
def test_command_with_multiple_arguments(self): @command @argument("arg1") @argument("arg2") def foo(arg1, arg2): foo.passed_arg = [arg1, arg2] context.exec("foo bar baz") self.assertTrue("bar" in foo.passed_arg) self.assertTrue("baz" in foo.passed_arg)
def test_named_commands(self): @command(name = "foo") def bar(): bar.has_been_called = True self.assertTrue("foo" in context.commands()) self.assertFalse("bar" in context.commands()) context.exec("foo") self.assertTrue(hasattr(bar, "has_been_called"))
def test_named_parent(self): @command(name = "baz") def foo(): foo.has_been_called = True @command(parent = foo) def bar(): bar.has_been_called = True context.exec("baz bar") self.assertFalse(hasattr(foo, "has_been_called")) self.assertTrue(hasattr(bar, "has_been_called"))
def run(self): self.running = True while self.running: try: text = prompt(self._prompt_generator, completer=cmd_completer, history=history.FileHistory('.reptyle.hist'), complete_style=CompleteStyle.READLINE_LIKE) context.exec(text) except exception.ParserException as e: print(f"ERROR {e}") except KeyboardInterrupt: self.stop()
def test_command_called_with_too_few_arguments(self): @command @argument("arg") def foo(arg): pass exception_raised = False try: context.exec("foo") except exception.ParserException as e: exception_raised = True self.assertTrue("arguments count missmatch" in str(e)) self.assertTrue(exception_raised)
def test_unknown_argument_raises_exception(self): exception_raised = False @command @argument("arg", opt="a") def foo(arg="foo"): pass try: context.exec("foo -b") except exception.ParserException: exception_raised = True self.assertTrue(exception_raised)
def test_command_with_flags(self): verb = False @command @argument("verbose", opt="v") def foo(verbose=False): nonlocal verb verb = verbose context.exec("foo -v") self.assertTrue(verb, "Short flagnames does not work") verb = False context.exec("foo --verbose") self.assertTrue(verb, "Long flagnames does not work")
def test_quit(self): # Need to lazyimport this module size previous testcases # might have destroyed root() during their tearDown q = lazy_import.lazy_module("reptyle.builtins.quit") # Test command exists self.assertTrue(q.quit.__name__ in root.childs) self.assertEqual(q.quit, root.childs["quit"]) # Test to call it via CLI call context.quit = Mock() context.exec("quit") # Test to call it as a function context.quit = Mock() q.quit() context.quit.assert_called()
def test_that_opts_can_have_multiple_args(self): n = 2 v = False @command @argument("num", opt="n") @argument("verbose", opt="v") def foo(num=3, verbose=False): nonlocal n nonlocal v n = num v = verbose context.exec("foo -n 4 --verbose") self.assertTrue(v) self.assertEqual(n, 4)
def test_multi_level_hierarchy(self): @command def foo(): foo.has_been_called = True @command(parent=foo) def bar(): bar.has_been_called = True @command(parent=bar) def baz(): baz.has_been_called = True context.exec("foo bar baz") self.assertFalse(hasattr(foo, "has_been_called")) self.assertFalse(hasattr(bar, "has_been_called")) self.assertTrue(hasattr(baz, "has_been_called"))
def test_unknown_command(self): exception_raised = False try: context.exec("unknown") except exception.ParserException: exception_raised = True self.assertTrue(exception_raised) exception_raised = False @command def test(): pass try: context.exec("testa") except exception.ParserException: exception_raised = True self.assertTrue(exception_raised)
def test_parent_command(self): @command def foo(): foo.has_been_called = True @command(parent = foo) def bar(): bar.has_been_called = True self.assertTrue("foo" in context.commands()) self.assertFalse("bar" in context.commands()) # Make sure only bar has been called context.exec("foo bar") self.assertFalse(hasattr(foo, "has_been_called")) self.assertTrue(hasattr(bar, "has_been_called")) # And that it still is possible to call foo context.exec("foo") self.assertTrue(hasattr(foo, "has_been_called"))
def test_that_opts_can_have_arguments(self): arg = None @command @argument("num", opt="n") def foo(num=3): nonlocal arg arg = num context.exec("foo -n 4") self.assertEqual(arg, 4) context.exec("foo") self.assertEqual(arg, 3) context.exec("foo --num 5") self.assertEqual(arg, 5)