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)
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))
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("")
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)
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())
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