Exemple #1
0
    def runPropertyTests(self, property_name, tests, completions):
        model = TestModel()
        target = TestTarget(model)

        name, getter, setter = self.properties[property_name]
        shell_property = ShellProperty(name, getter, setter, self.validators[property_name], self.completers[property_name])
        shell_property.setParent(target)

        doFunc = getattr(target, "do_%s" % property_name)
        for do_input, expected_value, fail in tests:
            doFunc(do_input)

            if isinstance(getter, property):
                model_value = getter.__get__(model)
            else:
                model_value = getter(model)

            self.assertEqual(expected_value, model_value)

            if fail:
                self.assertTrue(target.didLastCommandFail())
            else:
                self.assertFalse(target.didLastCommandFail())

            target.resetFailedFlag()

        completeFunc = getattr(target, "complete_%s" % property_name)
        for complete_input, expected_value in completions:
            choices = completeFunc(*completeMock(complete_input))
            self.assertListEqual(choices, expected_value)
Exemple #2
0
    def runPropertyTests(self, property_name, tests, completions):
        model = TestModel()
        target = TestTarget(model)

        name, getter, setter = self.properties[property_name]
        shell_property = ShellProperty(name, getter, setter,
                                       self.validators[property_name],
                                       self.completers[property_name])
        shell_property.setParent(target)

        doFunc = getattr(target, "do_%s" % property_name)
        for do_input, expected_value, fail in tests:
            doFunc(do_input)

            if isinstance(getter, property):
                model_value = getter.__get__(model)
            else:
                model_value = getter(model)

            self.assertEqual(expected_value, model_value)

            if fail:
                self.assertTrue(target.didLastCommandFail())
            else:
                self.assertFalse(target.didLastCommandFail())

            target.resetFailedFlag()

        completeFunc = getattr(target, "complete_%s" % property_name)
        for complete_input, expected_value in completions:
            choices = completeFunc(*completeMock(complete_input))
            self.assertListEqual(choices, expected_value)
Exemple #3
0
    def test_creation_and_check_existence_of_do_complete_help(self):
        target = TestTarget(TestModel())
        for key, (name, getter, setter) in self.properties.items():
            shell_property = ShellProperty(name, getter, setter)
            shell_property.setParent(target)

            self.assertTrue(hasattr(target, "do_%s" % name))
            self.assertTrue(hasattr(target, "complete_%s" % name))
            self.assertTrue(hasattr(target, "help_tuple_%s" % name))
Exemple #4
0
    def test_creation_and_check_existence_of_do_complete_help(self):
        target = TestTarget(TestModel())
        for key, (name, getter, setter) in self.properties.items():
            shell_property = ShellProperty(name, getter, setter)
            shell_property.setParent(target)

            self.assertTrue(hasattr(target, "do_%s" % name))
            self.assertTrue(hasattr(target, "complete_%s" % name))
            self.assertTrue(hasattr(target, "help_tuple_%s" % name))
Exemple #5
0
    def test_no_parent(self):
        target = TestTarget(TestModel())
        for key, (name, getter, setter) in self.properties.items():
            shell_property = ShellProperty(name, getter, setter)

            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_property.doFunction("")
Exemple #6
0
    def test_no_parent(self):
        target = TestTarget(TestModel())
        for key, (name, getter, setter) in self.properties.items():
            shell_property = ShellProperty(name, getter, setter)

            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_property.doFunction("")
Exemple #7
0
    def test_duplicate_properties(self):
        target = TestTarget(TestModel())
        shell_property = ShellProperty("test", str, None)
        shell_property.setParent(target)

        with self.assertRaises(ValueError):
            shell_property = ShellProperty("test", str, None)
            shell_property.setParent(target)
Exemple #8
0
    def test_addition_of_help(self):
        target = TestTarget(TestModel())

        for key, (name, getter, setter) in self.properties.items():
            help_arguments = "%s args" % name
            help_message = "%s msg" % name
            shell_property = ShellProperty(name, getter, setter, help_arguments=help_arguments, help_message=help_message)
            shell_property.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())
Exemple #9
0
    def test_addition_of_help(self):
        target = TestTarget(TestModel())

        for key, (name, getter, setter) in self.properties.items():
            help_arguments = "%s args" % name
            help_message = "%s msg" % name
            shell_property = ShellProperty(name,
                                           getter,
                                           setter,
                                           help_arguments=help_arguments,
                                           help_message=help_message)
            shell_property.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())
Exemple #10
0
    def test_duplicate_properties(self):
        target = TestTarget(TestModel())
        shell_property = ShellProperty("test",  str, None)
        shell_property.setParent(target)

        with self.assertRaises(ValueError):
            shell_property = ShellProperty("test", str, None)
            shell_property.setParent(target)
Exemple #11
0
    def addShellProperty(self,
                         name,
                         getter,
                         setter=None,
                         validator=None,
                         completer=None,
                         help_arguments=None,
                         help_message=None,
                         pretty_attribute=None,
                         model=None):
        """ @rtype: ShellProperty """
        shell_property = ShellProperty(name, getter, setter, validator,
                                       completer, help_arguments, help_message,
                                       pretty_attribute)
        self.addProperty(shell_property)

        if model is None:
            model = self

        self.__model_tracker[name] = model
        return shell_property