def test_invalid(self): """Test the invalid types.""" msg = "Invalid DBus type of dictionary key: '{}'" with self.assertRaises(TypeError) as cm: get_dbus_type(Dict[List[Bool], Bool]) self.assertEqual(msg.format(get_type_name(List[Bool])), str(cm.exception)) with self.assertRaises(TypeError) as cm: get_dbus_type(Dict[Variant, Int]) self.assertEqual(msg.format("Variant"), str(cm.exception)) with self.assertRaises(TypeError) as cm: get_dbus_type(Tuple[Int, Double, Dict[Tuple[Int, Int], Bool]]) self.assertEqual(msg.format(get_type_name(Tuple[Int, Int])), str(cm.exception)) msg = "Invalid DBus type '{}'." with self.assertRaises(TypeError) as cm: get_dbus_type(Set[Int]) self.assertEqual( msg.format(get_type_name(Set[Int])), str(cm.exception), )
def test_invalid(self): """Test the invalid types.""" with self.assertRaises(TypeError) as cm: get_dbus_type(Dict[List[Bool], Bool]) self.assertEqual( "Invalid DBus type of dictionary key: 'typing.List[bool]'", str(cm.exception)) with self.assertRaises(TypeError) as cm: get_dbus_type(Dict[Variant, Int]) self.assertEqual("Invalid DBus type of dictionary key: 'Variant'", str(cm.exception)) with self.assertRaises(TypeError) as cm: get_dbus_type(Tuple[Int, Double, Dict[Tuple[Int, Int], Bool]]) self.assertEqual( "Invalid DBus type of dictionary key: 'typing.Tuple[int, int]'", str(cm.exception)) with self.assertRaises(TypeError) as cm: get_dbus_type(Set[Int]) self.assertEqual("Invalid DBus type 'typing.Set[int]'.", str(cm.exception))
def _generate_signal(cls, member, member_name): """Generate signal defined by a class member. :param member: a dbus_signal object. :param member_name: a name of the signal :return: a signal element raises DBusSpecificationError: if signal has defined return type """ element = cls.xml_generator.create_signal(member_name) method = member.definition if not method: return element for name, type_hint, direction in cls._iterate_parameters(method): # Only input parameters can be defined. if direction == DBusSpecification.DIRECTION_OUT: raise DBusSpecificationError( "Signal {} has defined return type.".format(member_name)) # All parameters are exported as output parameters # (see specification). direction = DBusSpecification.DIRECTION_OUT parameter = cls.xml_generator.create_parameter( name, get_dbus_type(type_hint), direction) cls.xml_generator.add_child(element, parameter) return element
def _compare(self, type_hint, expected_string): """Compare generated and expected types.""" # Generate a type string. dbus_type = get_dbus_type(type_hint) self.assertEqual(dbus_type, expected_string) self.assertTrue(GLib.VariantType.string_is_valid(dbus_type)) # Create a variant type from a type hint. variant_type = get_variant_type(type_hint) self.assertIsInstance(variant_type, GLib.VariantType) self.assertEqual(variant_type.dup_string(), expected_string) expected_type = GLib.VariantType.new(expected_string) self.assertTrue(expected_type.equal(variant_type)) # Create a variant type from a type string. variant_type = get_variant_type(expected_string) self.assertIsInstance(variant_type, GLib.VariantType) self.assertTrue(expected_type.equal(variant_type)) # Test the is_tuple_of_one function. expected_value = is_base_type(type_hint, Tuple) \ and len(get_type_arguments(type_hint)) == 1 self.assertEqual(is_tuple_of_one(type_hint), expected_value) self.assertEqual(is_tuple_of_one(expected_string), expected_value) self.assertTrue(is_tuple_of_one(Tuple[type_hint])) self.assertTrue(is_tuple_of_one("({})".format(expected_string)))
def test_invalid(self): """Test the invalid types.""" with self.assertRaises(TypeError): get_dbus_type(Dict[List[Bool], Bool]) with self.assertRaises(TypeError): get_dbus_type(Dict[Variant, Int]) with self.assertRaises(TypeError): get_dbus_type(Tuple[Int, Double, Dict[Tuple[Int, Int], Bool]]) with self.assertRaises(TypeError): get_dbus_type(Set[Int])
def test_unknown(self): """Test the unknown type.""" class UnknownType: pass with self.assertRaises(TypeError): get_dbus_type(UnknownType) with self.assertRaises(TypeError): get_dbus_type(List[UnknownType]) with self.assertRaises(TypeError): get_dbus_type(Tuple[Int, Str, UnknownType]) with self.assertRaises(TypeError): get_dbus_type(Dict[Int, UnknownType])
def _generate_method(cls, member, member_name): """Generate method defined by given class member. :param member: a method object :param member_name: a name of the method :return: a method element """ method = cls.xml_generator.create_method(member_name) # Process the parameters. for name, type_hint, direction in cls._iterate_parameters(member): # Create the parameter element. parameter = cls.xml_generator.create_parameter( name, get_dbus_type(type_hint), direction) # Add the element to the method element. cls.xml_generator.add_child(method, parameter) return method
def _compare(self, type_hint, expected_string): """Compare generated and expected types.""" # Generate a type string. dbus_type = get_dbus_type(type_hint) self.assertEqual(dbus_type, expected_string) self.assertTrue(GLib.VariantType.string_is_valid(dbus_type)) # Create a variant type from a type hint. variant_type = get_variant_type(type_hint) self.assertIsInstance(variant_type, GLib.VariantType) self.assertEqual(variant_type.dup_string(), expected_string) expected_type = GLib.VariantType.new(expected_string) self.assertTrue(expected_type.equal(variant_type)) # Create a variant type from a type string. variant_type = get_variant_type(expected_string) self.assertIsInstance(variant_type, GLib.VariantType) self.assertTrue(expected_type.equal(variant_type))
def _generate_property(cls, member, member_name): """Generate DBus property defined by class member. :param member: a property object :param member_name: a property name :return: a property element raises DBusSpecificationError: if the property is invalid """ access = None type_hint = None try: # Process the setter. if member.fset: [(_, type_hint, _)] = cls._iterate_parameters(member.fset) access = DBusSpecification.ACCESS_WRITE # Process the getter. if member.fget: [(_, type_hint, _)] = cls._iterate_parameters(member.fget) access = DBusSpecification.ACCESS_READ except ValueError: raise DBusSpecificationError( "Undefined type of DBus property '{}'.".format(member_name) ) from None # Property has both. if member.fget and member.fset: access = DBusSpecification.ACCESS_READWRITE if access is None: raise DBusSpecificationError( "DBus property '{}' is not accessible.".format(member_name) ) return cls.xml_generator.create_property( member_name, get_dbus_type(type_hint), access )
def test_unknown(self): """Test the unknown type.""" class UnknownType: pass with self.assertRaises(TypeError) as cm: get_dbus_type(UnknownType) self.assertEqual("Invalid DBus type 'UnknownType'.", str(cm.exception)) with self.assertRaises(TypeError) as cm: get_dbus_type(List[UnknownType]) self.assertEqual("Invalid DBus type 'UnknownType'.", str(cm.exception)) with self.assertRaises(TypeError) as cm: get_dbus_type(Tuple[Int, Str, UnknownType]) self.assertEqual("Invalid DBus type 'UnknownType'.", str(cm.exception)) with self.assertRaises(TypeError) as cm: get_dbus_type(Dict[Int, UnknownType]) self.assertEqual("Invalid DBus type 'UnknownType'.", str(cm.exception))