def test_split_add_event_listener(self): """Tests how EventTarget.addEventListener is split into respective overloads. From: void addEventListener( DOMString type, EventListener? listener, optional (AddEventListenerOptions or boolean) options) produces: """ helper = IdlTestingHelper() type_dom_string = IdlType('DOMString') type_listener = IdlType('EventListener') type_options = IdlType('AddEventListenerOptions') type_boolean = IdlType('boolean') type_union = IdlUnionType([type_options, type_boolean]) idl_operation = IdlOperation() idl_operation.arguments = [ helper.make_stub_idl_argument('type', type_dom_string), helper.make_stub_idl_argument('listener', type_listener), helper.make_stub_idl_argument('options', type_union, is_optional=True) ] splitter = MethodOverloadSplitter(idl_operation) result = splitter.split_into_overloads() self.assertEqual(result, [[type_dom_string, type_listener], [type_dom_string, type_listener, type_options], [type_dom_string, type_listener, type_boolean]])
def test_enumerate_argument_types(self): splitter = MethodOverloadSplitter(IdlOperation()) nullable_and_primitive = IdlArgument() nullable_and_primitive.idl_type = IdlNullableType(IdlType('double')) with self.assertRaises(ValueError): splitter._enumerate_argument_types(nullable_and_primitive) argument = IdlArgument() foo_type = IdlType('Foo') bar_type = IdlType('Bar') argument.idl_type = foo_type self.assertEqual(splitter._enumerate_argument_types(argument), [foo_type]) argument.is_optional = True self.assertEqual(splitter._enumerate_argument_types(argument), [None, foo_type]) argument.is_optional = False argument.idl_type = IdlUnionType([foo_type, bar_type]) self.assertEqual(splitter._enumerate_argument_types(argument), [foo_type, bar_type]) argument.is_optional = True self.assertEqual(splitter._enumerate_argument_types(argument), [None, foo_type, bar_type])
def test_split_into_overloads(self): helper = IdlTestingHelper() type_double = IdlType('double') type_foo = IdlType('foo') type_double_or_foo = IdlUnionType([type_double, type_foo]) idl_operation = IdlOperation() idl_operation.arguments = [] splitter = MethodOverloadSplitter(idl_operation) result = splitter.split_into_overloads() self.assertEqual(result, [[]]) idl_operation.arguments = [ helper.make_stub_idl_argument(None, type_double, True) ] splitter = MethodOverloadSplitter(idl_operation) result = splitter.split_into_overloads() self.assertEqual(result, [[], [type_double]]) idl_operation.arguments = [ helper.make_stub_idl_argument(None, type_double_or_foo) ] splitter = MethodOverloadSplitter(idl_operation) result = splitter.split_into_overloads() self.assertEqual(result, [[type_double], [type_foo]])
def test_resolve_typedefs(self): # This is a simplification of the typedef mechanism to avoid having to # use idl_definitions and use actual nodes from //tools/idl_parser. typedefs = { 'Foo': IdlType('unsigned short'), 'MyBooleanType': IdlType('boolean'), 'SomeInterfaceT': IdlType('MyInterface'), } # (long long or MyBooleanType) union = IdlUnionType([IdlType('long long'), IdlType('MyBooleanType') ]).resolve_typedefs(typedefs) self.assertEqual(union.name, 'LongLongOrBoolean') self.assertEqual(union.member_types[0].name, 'LongLong') self.assertEqual(union.member_types[1].name, 'Boolean') # (Foo or SomeInterfaceT) union = IdlUnionType([IdlType('Foo'), IdlType('SomeInterfaceT') ]).resolve_typedefs(typedefs) self.assertEqual(union.name, 'UnsignedShortOrMyInterface') self.assertEqual(union.member_types[0].name, 'UnsignedShort') self.assertEqual(union.member_types[1].name, 'MyInterface') # (Foo or sequence<(MyBooleanType or double)>) union = IdlUnionType([ IdlType('Foo'), IdlSequenceType( IdlUnionType([IdlType('MyBooleanType'), IdlType('double')])) ]).resolve_typedefs(typedefs) self.assertEqual(union.name, 'UnsignedShortOrBooleanOrDoubleSequence') self.assertEqual(union.member_types[0].name, 'UnsignedShort') self.assertEqual(union.member_types[1].name, 'BooleanOrDoubleSequence') self.assertEqual(union.member_types[1].element_type.name, 'BooleanOrDouble') self.assertEqual( union.member_types[1].element_type.member_types[0].name, 'Boolean') self.assertEqual( union.member_types[1].element_type.member_types[1].name, 'Double') self.assertEqual(2, len(union.flattened_member_types))
def union_type_node_to_idl_union_type(node): member_types = [ type_node_to_type(member_type_node) for member_type_node in node.GetChildren() ] return IdlUnionType(member_types)
def union_type_node_to_idl_union_type(node, is_nullable=False): member_types = [ type_node_to_type(member_type_node) for member_type_node in node.GetChildren() ] return IdlUnionType(member_types, is_nullable=is_nullable)
def test_flattened_member_types(self): # We are only testing the algorithm here, so we do create some ambiguous union types. def compare_flattened_members(actual, expected): """Compare a set of IDL types by name, as the objects are different""" actual_names = set([member.name for member in actual]) expected_names = set([member.name for member in expected]) self.assertEqual(actual_names, expected_names) idl_type = IdlUnionType([IdlType('long'), IdlType('SomeInterface')]) compare_flattened_members( idl_type.flattened_member_types, set([IdlType('long'), IdlType('SomeInterface')])) idl_type = IdlUnionType([ IdlUnionType([IdlType('ByteString'), IdlType('float')]), IdlType('boolean') ]) compare_flattened_members( idl_type.flattened_member_types, set([IdlType('float'), IdlType('boolean'), IdlType('ByteString')])) idl_type = IdlUnionType([ IdlUnionType([IdlType('ByteString'), IdlType('DOMString')]), IdlType('DOMString') ]) compare_flattened_members( idl_type.flattened_member_types, set([IdlType('DOMString'), IdlType('ByteString')])) idl_type = IdlUnionType( [IdlNullableType(IdlType('ByteString')), IdlType('byte')]) compare_flattened_members( idl_type.flattened_member_types, set([IdlType('ByteString'), IdlType('byte')])) idl_type = IdlUnionType([ IdlNullableType(IdlType('ByteString')), IdlType('byte'), IdlUnionType([IdlType('ByteString'), IdlType('float')]) ]) self.assertEqual(len(idl_type.flattened_member_types), 3) compare_flattened_members( idl_type.flattened_member_types, set([IdlType('ByteString'), IdlType('byte'), IdlType('float')])) # From the example in the spec: "the flattened member types of the union type (Node or (sequence<long> or Event) or # (XMLHttpRequest or DOMString)? or sequence<(sequence<double> or NodeList)>) are the six types Node, sequence<long>, # Event, XMLHttpRequest, DOMString and sequence<(sequence<double> or NodeList)>" idl_type = IdlUnionType([ IdlType('Node'), IdlUnionType([IdlSequenceType(IdlType('long')), IdlType('Event')]), IdlNullableType( IdlUnionType([IdlType('XMLHttpRequest'), IdlType('DOMString')])), IdlSequenceType( IdlUnionType( [IdlSequenceType(IdlType('double')), IdlType('NodeList')])) ]) self.assertEqual(len(idl_type.flattened_member_types), 6) compare_flattened_members( idl_type.flattened_member_types, set([ IdlType('Node'), IdlSequenceType(IdlType('long')), IdlType('Event'), IdlType('XMLHttpRequest'), IdlType('DOMString'), IdlSequenceType( IdlUnionType([ IdlSequenceType(IdlType('double')), IdlType('NodeList') ])) ]))
def test_invalid_inner_type_union_dictionary_member(self): inner_type = IdlUnionType([IdlType(self._TEST_DICTIONARY)]) with self.assertRaises(ValueError): IdlNullableType(inner_type)
def test_invalid_inner_type_union_nullable_member(self): inner_type = IdlUnionType([IdlNullableType(IdlType('long'))]) with self.assertRaises(ValueError): IdlNullableType(inner_type)
def test_valid_inner_type_union(self): inner_type = IdlUnionType([IdlType('long')]) self.assertTrue(IdlNullableType(inner_type).is_nullable)