def test_failed_creation(self): shell_function = ShellFunction("test_function", dummyFunction) with self.assertRaises(ValueError): shell_function.setParent(None) with self.assertRaises(ValueError): ShellFunction("test_function", None)
def test_completion(self): target = TestTarget() def completer(model, text, line, begidx, endidx): text = text.strip().lower() if text == "s": return ["Large"] elif text == "l": return ["Small"] elif text == "m": return ["Medium"] else: return [] shell_function = ShellFunction("test_function", dummyFunction, completer) shell_function.setParent(target) result = target.complete_test_function(*completeMock("s")) self.assertEqual(result, ["Large"]) result = target.complete_test_function(*completeMock("l")) self.assertEqual(result, ["Small"]) result = target.complete_test_function(*completeMock("")) self.assertEqual(result, [])
def test_creation(self): target = TestTarget() name = "test_function" shell_function = ShellFunction(name, dummyFunction, None) shell_function.setParent(target) self.assertTrue(hasattr(target, "do_%s" % name)) self.assertTrue(hasattr(target, "complete_%s" % name)) self.assertTrue(hasattr(target, "help_tuple_%s" % name))
def test_function_with_target_as_model(self): target = TestTarget() def checkModel(model, line): self.assertIsInstance(model, TestTarget) self.assertEqual(line, "some text to parse") shell_function = ShellFunction("test_function", checkModel) shell_function.setParent(target) target.do_test_function("some text to parse")
def test_no_parent(self): target = TestTarget() name = "test_function" shell_function = ShellFunction(name, dummyFunction, None) self.assertFalse(hasattr(target, "do_%s" % name)) self.assertFalse(hasattr(target, "complete_%s" % name)) self.assertFalse(hasattr(target, "help_tuple_%s" % name)) with self.assertRaises(AttributeError): shell_function.doFunction("nonsense")
def test_function_with_custom_model(self): target = TestTarget() def checkModel(model, line): self.assertIsInstance(model, dict) self.assertTrue("key" in model) self.assertEqual(line, "some other text to parse") shell_function = ShellFunction("other_test_function", checkModel) shell_function.setParent(target) target.do_other_test_function("some other text to parse")
def test_duplicate_function(self): target = TestTarget() shell_function = ShellFunction("duplicate", dummyFunction) shell_function.setParent(target) with self.assertRaises(ValueError): duplicate = ShellFunction("duplicate", dummyFunction) duplicate.setParent(target)
def test_addition_of_help(self): target = TestTarget() name = "test_function" help_arguments = "%s args" % name help_message = "%s msg" % name shell_function = ShellFunction(name, str, None, help_arguments=help_arguments, help_message=help_message) shell_function.setParent(target) help_function = getattr(target, "help_tuple_%s" % name) cmd_name, args, msg = help_function() self.assertEqual(cmd_name, name) self.assertEqual(args, help_arguments) self.assertEqual(msg, help_message) self.assertFalse(target.didLastCommandFail())
def addShellFunction(self, name, function, completer=None, help_arguments=None, help_message=None, model=None): """ @rtype: ShellFunction """ func = ShellFunction(name, function, completer, help_arguments, help_message) self.addFunction(func) if model is None: model = self self.__model_tracker[name] = model return func