Esempio n. 1
0
    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)
Esempio n. 2
0
    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, [])
Esempio n. 3
0
    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))
Esempio n. 4
0
    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")
Esempio n. 5
0
    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")
Esempio n. 6
0
    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")
Esempio n. 7
0
    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")
Esempio n. 8
0
    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")
Esempio n. 9
0
    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")
Esempio n. 10
0
    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)
Esempio n. 11
0
    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())
Esempio n. 12
0
    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())
Esempio n. 13
0
    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)
Esempio n. 14
0
    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