def test_mergeUnresolvedIID(self): """ Test that merging a typelib with an unresolved definition of an interface that's also unresolved in this typelib, but one has a valid IID copies the IID value to the resulting typelib. """ # Unresolved in both, but t1 has an IID value t1 = xpt.Typelib() # add an unresolved interface with a valid IID t1.interfaces.append( xpt.Interface("IFoo", iid="11223344-5566-7788-9900-aabbccddeeff")) t2 = xpt.Typelib() # add an unresolved interface, no IID t2.interfaces.append(xpt.Interface("IFoo")) t1.merge(t2) self.assertEqual(1, len(t1.interfaces)) self.assertEqual("IFoo", t1.interfaces[0].name) self.assertEqual("11223344-5566-7788-9900-aabbccddeeff", t1.interfaces[0].iid) # Unresolved in both, but t2 has an IID value t1 = xpt.Typelib() # add an unresolved interface, no IID t1.interfaces.append(xpt.Interface("IFoo")) t2 = xpt.Typelib() # add an unresolved interface with a valid IID t2.interfaces.append( xpt.Interface("IFoo", iid="11223344-5566-7788-9900-aabbccddeeff")) t1.merge(t2) self.assertEqual(1, len(t1.interfaces)) self.assertEqual("IFoo", t1.interfaces[0].name) self.assertEqual("11223344-5566-7788-9900-aabbccddeeff", t1.interfaces[0].iid)
def test_mergeConflict(self): """ Test that merging two typelibs with conflicting interface definitions raises an error. """ # Same names, different IIDs t1 = xpt.Typelib() # add an unresolved interface t1.interfaces.append( xpt.Interface("IFoo", iid="11223344-5566-7788-9900-aabbccddeeff")) t2 = xpt.Typelib() # add an unresolved interface, same name different IID t2.interfaces.append( xpt.Interface("IFoo", iid="44332211-6655-8877-0099-aabbccddeeff")) self.assertRaises(xpt.DataError, xpt.xpt_link, [t1, t2]) # Same IIDs, different names t1 = xpt.Typelib() # add an unresolved interface t1.interfaces.append( xpt.Interface("IFoo", iid="11223344-5566-7788-9900-aabbccddeeff")) t2 = xpt.Typelib() # add an unresolved interface, same IID different name t2.interfaces.append( xpt.Interface("IBar", iid="11223344-5566-7788-9900-aabbccddeeff")) self.assertRaises(xpt.DataError, xpt.xpt_link, [t1, t2])
def test_mergeDifferent(self): """ Test that merging two typelibs with completely different interfaces produces the correctly merged typelib. """ t1 = xpt.Typelib() # add an unresolved interface t1.interfaces.append(xpt.Interface("IFoo")) t2 = xpt.Typelib() # add an unresolved interface t2.interfaces.append(xpt.Interface("IBar")) t1.merge(t2) self.assertEqual(2, len(t1.interfaces)) # Interfaces should wind up sorted self.assertEqual("IBar", t1.interfaces[0].name) self.assertEqual("IFoo", t1.interfaces[1].name) # Add some IID values t1 = xpt.Typelib() # add an unresolved interface t1.interfaces.append( xpt.Interface("IFoo", iid="11223344-5566-7788-9900-aabbccddeeff")) t2 = xpt.Typelib() # add an unresolved interface t2.interfaces.append( xpt.Interface("IBar", iid="44332211-6655-8877-0099-aabbccddeeff")) t1.merge(t2) self.assertEqual(2, len(t1.interfaces)) # Interfaces should wind up sorted self.assertEqual("IFoo", t1.interfaces[0].name) self.assertEqual("IBar", t1.interfaces[1].name)
def test_simple(self): t = xpt.Typelib() # add an unresolved interface t.interfaces.append(xpt.Interface("IFoo")) self.checkRoundtrip(t) t = xpt.Typelib() # add an unresolved interface with an IID t.interfaces.append(xpt.Interface("IBar", "11223344-5566-7788-9900-aabbccddeeff")) self.checkRoundtrip(t)
def test_mergeResolvedUnresolved(self): """ Test that merging two typelibs, one of which contains an unresolved reference to an interface, and the other of which contains a resolved reference to the same interface results in keeping the resolved reference. """ # t1 has an unresolved interface, t2 has a resolved version t1 = xpt.Typelib() # add an unresolved interface t1.interfaces.append(xpt.Interface("IFoo")) t2 = xpt.Typelib() # add a resolved interface p = xpt.Param(xpt.SimpleType(xpt.Type.Tags.void)) m = xpt.Method("Bar", p) t2.interfaces.append( xpt.Interface("IFoo", iid="11223344-5566-7788-9900-aabbccddeeff", methods=[m], scriptable=True)) t3 = xpt.xpt_link([t1, t2]) self.assertEqual(1, len(t3.interfaces)) self.assertEqual("IFoo", t3.interfaces[0].name) self.assertEqual("11223344-5566-7788-9900-aabbccddeeff", t3.interfaces[0].iid) self.assert_(t3.interfaces[0].resolved) self.assertEqual(1, len(t3.interfaces[0].methods)) self.assertEqual("Bar", t3.interfaces[0].methods[0].name) # t1 has a resolved interface, t2 has an unresolved version t1 = xpt.Typelib() # add a resolved interface p = xpt.Param(xpt.SimpleType(xpt.Type.Tags.void)) m = xpt.Method("Bar", p) t1.interfaces.append( xpt.Interface("IFoo", iid="11223344-5566-7788-9900-aabbccddeeff", methods=[m], scriptable=True)) t2 = xpt.Typelib() # add an unresolved interface t2.interfaces.append(xpt.Interface("IFoo")) t3 = xpt.xpt_link([t1, t2]) self.assertEqual(1, len(t3.interfaces)) self.assertEqual("IFoo", t3.interfaces[0].name) self.assertEqual("11223344-5566-7788-9900-aabbccddeeff", t3.interfaces[0].iid) self.assert_(t3.interfaces[0].resolved) self.assertEqual(1, len(t3.interfaces[0].methods)) self.assertEqual("Bar", t3.interfaces[0].methods[0].name)
def test_mergeReplaceArrayTypeParams(self): """ Test that merging an interface correctly updates ArrayType params whose element_type is an InterfaceType on methods of other interfaces. """ # t1 has an unresolved interface and an interface that uses the # unresolved interface as a type in an ArrayType in a parameter # of a method. t2 has a resolved version of the unresolved interface. t1 = xpt.Typelib() # add an unresolved interface i = xpt.Interface("IFoo") t1.interfaces.append(i) # add an interface that uses the unresolved interface # as a type in an ArrayType in a param value in a method. vp = xpt.Param(xpt.SimpleType(xpt.Type.Tags.void)) intp = xpt.Param(xpt.SimpleType(xpt.Type.Tags.int32)) p = xpt.Param(xpt.ArrayType(xpt.InterfaceType(i), 1, 2)) m = xpt.Method("ArrayIfaceParam", vp, params=[p, intp, intp]) t1.interfaces.append( xpt.Interface("IParam", iid="11111111-1111-1111-1111-111111111111", methods=[m], scriptable=True)) t2 = xpt.Typelib() # add a resolved interface m = xpt.Method("Bar", vp) t2.interfaces.append( xpt.Interface("IFoo", iid="11223344-5566-7788-9900-aabbccddeeff", methods=[m], scriptable=True)) t3 = xpt.xpt_link([t1, t2]) self.assertEqual(2, len(t3.interfaces)) self.assertEqual("IParam", t3.interfaces[0].name) self.assertEqual("11111111-1111-1111-1111-111111111111", t3.interfaces[0].iid) self.assertEqual("IFoo", t3.interfaces[1].name) self.assertEqual("11223344-5566-7788-9900-aabbccddeeff", t3.interfaces[1].iid) self.assert_(t3.interfaces[1].resolved) # Ensure that IRetval's method's param type has been updated. self.assertEqual(1, len(t3.interfaces[0].methods)) self.assert_(t3.interfaces[0].methods[0].params[0].type.element_type. iface.resolved) self.assertEqual( t3.interfaces[1], t3.interfaces[0].methods[0].params[0].type.element_type.iface)
def test_xpt_link(self): """ Test the xpt_link method. """ t1 = xpt.Typelib() # add an unresolved interface t1.interfaces.append(xpt.Interface("IFoo")) f1 = self.gettempfile() t1.write(f1) t2 = xpt.Typelib() # add an unresolved interface t2.interfaces.append(xpt.Interface("IBar")) f2 = self.gettempfile() t2.write(f2) f3 = self.gettempfile() xpt.xpt_link(f3, [f1, f2]) t3 = xpt.Typelib.read(f3) self.assertEqual(2, len(t3.interfaces)) # Interfaces should wind up sorted self.assertEqual("IBar", t3.interfaces[0].name) self.assertEqual("IFoo", t3.interfaces[1].name) # Add some IID values t1 = xpt.Typelib() # add an unresolved interface t1.interfaces.append( xpt.Interface("IFoo", iid="11223344-5566-7788-9900-aabbccddeeff")) f1 = self.gettempfile() t1.write(f1) t2 = xpt.Typelib() # add an unresolved interface t2.interfaces.append( xpt.Interface("IBar", iid="44332211-6655-8877-0099-aabbccddeeff")) f2 = self.gettempfile() t2.write(f2) f3 = self.gettempfile() xpt.xpt_link(f3, [f1, f2]) t3 = xpt.Typelib.read(f3) self.assertEqual(2, len(t3.interfaces)) # Interfaces should wind up sorted self.assertEqual("IFoo", t3.interfaces[0].name) self.assertEqual("IBar", t3.interfaces[1].name)
def write_typelib(idl, fd, filename): """ Generate the typelib. """ # We only care about interfaces that are scriptable. ifaces = [] for p in idl.productions: if p.kind == 'interface' and p.attributes.scriptable: ifaces.append(build_interface(p, ifaces)) typelib = xpt.Typelib(interfaces=ifaces) typelib.writefd(fd)
def write_typelib(idl, fd, filename): """ Generate the typelib. """ # We only care about interfaces ifaces = [] for p in idl.productions: if p.kind == 'interface': ifaces.append(build_interface(p, ifaces)) typelib = xpt.Typelib(interfaces=ifaces) typelib.writefd(fd)
def test_ifaceFlags(self): """ Test that an interface's flags are correctly serialized and deserialized. """ t = xpt.Typelib() t.interfaces.append(xpt.Interface("IFlags", iid="11223344-5566-7788-9900-aabbccddeeff", resolved=True, scriptable=True, function=True)) self.checkRoundtrip(t)
def test_parent(self): """ Test that an interface's parent property is correctly serialized and deserialized. """ t = xpt.Typelib() pi = xpt.Interface("IParent") t.interfaces.append(pi) t.interfaces.append(xpt.Interface("IChild", iid="11223344-5566-7788-9900-aabbccddeeff", parent=pi, resolved=True)) self.checkRoundtrip(t)
def test_read_file(self): """ Test that a Typelib can be read/written from/to a file. """ t = xpt.Typelib() # add an unresolved interface t.interfaces.append(xpt.Interface("IFoo")) fd, f = tempfile.mkstemp() os.close(fd) t.write(f) t2 = xpt.Typelib.read(f) os.remove(f) self.assert_(t2 is not None) self.assertEqualTypelibs(t, t2)
def test_constants(self): c = xpt.Constant("X", xpt.SimpleType(xpt.Type.Tags.uint32), 0xF000F000) i = xpt.Interface("IFoo", iid="11223344-5566-7788-9900-aabbccddeeff", constants=[c]) t = xpt.Typelib(interfaces=[i]) self.checkRoundtrip(t) # tack on some more constants i.constants.append( xpt.Constant("Y", xpt.SimpleType(xpt.Type.Tags.int16), -30000)) i.constants.append( xpt.Constant("Z", xpt.SimpleType(xpt.Type.Tags.uint16), 0xB0B0)) i.constants.append( xpt.Constant("A", xpt.SimpleType(xpt.Type.Tags.int32), -1000000)) self.checkRoundtrip(t)
def test_mergeReplaceRetval(self): """ Test that merging an interface correctly updates InterfaceType return values on methods of other interfaces. """ # t1 has an unresolved interface and an interface that uses the # unresolved interface as a return value from a method. t2 # has a resolved version of the unresolved interface. t1 = xpt.Typelib() # add an unresolved interface i = xpt.Interface("IFoo") t1.interfaces.append(i) # add an interface that uses the unresolved interface # as a return value in a method. p = xpt.Param(xpt.InterfaceType(i)) m = xpt.Method("ReturnIface", p) t1.interfaces.append( xpt.Interface("IRetval", iid="11111111-1111-1111-1111-111111111111", methods=[m], scriptable=True)) t2 = xpt.Typelib() # add a resolved interface p = xpt.Param(xpt.SimpleType(xpt.Type.Tags.void)) m = xpt.Method("Bar", p) t2.interfaces.append( xpt.Interface("IFoo", iid="11223344-5566-7788-9900-aabbccddeeff", methods=[m], scriptable=True)) t3 = xpt.xpt_link([t1, t2]) self.assertEqual(2, len(t3.interfaces)) self.assertEqual("IRetval", t3.interfaces[0].name) self.assertEqual("11111111-1111-1111-1111-111111111111", t3.interfaces[0].iid) self.assertEqual("IFoo", t3.interfaces[1].name) self.assertEqual("11223344-5566-7788-9900-aabbccddeeff", t3.interfaces[1].iid) self.assert_(t3.interfaces[1].resolved) # Ensure that IRetval's method's return value type has been updated. self.assertEqual(1, len(t3.interfaces[0].methods)) self.assert_(t3.interfaces[0].methods[0].result.type.iface.resolved) self.assertEqual(t3.interfaces[1], t3.interfaces[0].methods[0].result.type.iface) # t1 has a resolved interface. t2 has an unresolved version and # an interface that uses the unresolved interface as a return value # from a method. t1 = xpt.Typelib() # add a resolved interface p = xpt.Param(xpt.SimpleType(xpt.Type.Tags.void)) m = xpt.Method("Bar", p) t1.interfaces.append( xpt.Interface("IFoo", iid="11223344-5566-7788-9900-aabbccddeeff", methods=[m], scriptable=True)) t2 = xpt.Typelib() # add an unresolved interface i = xpt.Interface("IFoo") t2.interfaces.append(i) # add an interface that uses the unresolved interface # as a return value in a method. p = xpt.Param(xpt.InterfaceType(i)) m = xpt.Method("ReturnIface", p) t2.interfaces.append( xpt.Interface("IRetval", iid="11111111-1111-1111-1111-111111111111", methods=[m], scriptable=True)) t3 = xpt.xpt_link([t1, t2]) self.assertEqual(2, len(t3.interfaces)) self.assertEqual("IRetval", t3.interfaces[0].name) self.assertEqual("11111111-1111-1111-1111-111111111111", t3.interfaces[0].iid) self.assertEqual("IFoo", t3.interfaces[1].name) self.assertEqual("11223344-5566-7788-9900-aabbccddeeff", t3.interfaces[1].iid) self.assert_(t3.interfaces[1].resolved) # Ensure that IRetval's method's return value type has been updated. self.assertEqual(1, len(t3.interfaces[0].methods)) self.assert_(t3.interfaces[0].methods[0].result.type.iface.resolved) self.assertEqual(t3.interfaces[1], t3.interfaces[0].methods[0].result.type.iface)
def test_mergeReplaceParents(self): """ Test that merging an interface results in other interfaces' parent member being updated properly. """ # t1 has an unresolved interface, t2 has a resolved version, # but t1 also has another interface whose parent is the unresolved # interface. t1 = xpt.Typelib() # add an unresolved interface pi = xpt.Interface("IFoo") t1.interfaces.append(pi) # add a child of the unresolved interface t1.interfaces.append( xpt.Interface("IChild", iid="11111111-1111-1111-1111-111111111111", resolved=True, parent=pi, scriptable=True)) t2 = xpt.Typelib() # add a resolved interface p = xpt.Param(xpt.SimpleType(xpt.Type.Tags.void)) m = xpt.Method("Bar", p) t2.interfaces.append( xpt.Interface("IFoo", iid="11223344-5566-7788-9900-aabbccddeeff", methods=[m], scriptable=True)) t3 = xpt.xpt_link([t1, t2]) self.assertEqual(2, len(t3.interfaces)) self.assertEqual("IChild", t3.interfaces[0].name) self.assertEqual("11111111-1111-1111-1111-111111111111", t3.interfaces[0].iid) self.assertEqual("IFoo", t3.interfaces[1].name) self.assertEqual("11223344-5566-7788-9900-aabbccddeeff", t3.interfaces[1].iid) self.assert_(t3.interfaces[0].resolved) # Ensure that IChild's parent has been updated self.assertEqual(t3.interfaces[1], t3.interfaces[0].parent) self.assert_(t3.interfaces[0].parent.resolved) # t1 has a resolved interface, t2 has an unresolved version, # but t2 also has another interface whose parent is the unresolved # interface. t1 = xpt.Typelib() # add a resolved interface p = xpt.Param(xpt.SimpleType(xpt.Type.Tags.void)) m = xpt.Method("Bar", p) t1.interfaces.append( xpt.Interface("IFoo", iid="11223344-5566-7788-9900-aabbccddeeff", methods=[m], scriptable=True)) t2 = xpt.Typelib() # add an unresolved interface pi = xpt.Interface("IFoo") t2.interfaces.append(pi) # add a child of the unresolved interface t2.interfaces.append( xpt.Interface("IChild", iid="11111111-1111-1111-1111-111111111111", resolved=True, parent=pi, scriptable=True)) t3 = xpt.xpt_link([t1, t2]) self.assertEqual(2, len(t3.interfaces)) self.assertEqual("IChild", t3.interfaces[0].name) self.assertEqual("11111111-1111-1111-1111-111111111111", t3.interfaces[0].iid) self.assertEqual("IFoo", t3.interfaces[1].name) self.assertEqual("11223344-5566-7788-9900-aabbccddeeff", t3.interfaces[1].iid) self.assert_(t3.interfaces[0].resolved) # Ensure that IChild's parent has been updated self.assertEqual(t3.interfaces[1], t3.interfaces[0].parent) self.assert_(t3.interfaces[0].parent.resolved)
def test_methods(self): p = xpt.Param(xpt.SimpleType(xpt.Type.Tags.void)) m = xpt.Method("Bar", p) i = xpt.Interface("IFoo", iid="11223344-5566-7788-9900-aabbccddeeff", methods=[m]) t = xpt.Typelib(interfaces=[i]) self.checkRoundtrip(t) # add some more methods i.methods.append( xpt.Method("One", xpt.Param(xpt.SimpleType(xpt.Type.Tags.int32)), params=[ xpt.Param(xpt.SimpleType(xpt.Type.Tags.int64)), xpt.Param( xpt.SimpleType(xpt.Type.Tags.float, pointer=True)) ])) self.checkRoundtrip(t) # test some other types (should really be more thorough) i.methods.append( xpt.Method("Two", xpt.Param(xpt.SimpleType(xpt.Type.Tags.int32)), params=[ xpt.Param( xpt.SimpleType(xpt.Type.Tags.UTF8String, pointer=True)), xpt.Param( xpt.SimpleType(xpt.Type.Tags.wchar_t_ptr, pointer=True)) ])) self.checkRoundtrip(t) # add a method with an InterfaceType argument bar = xpt.Interface("IBar") t.interfaces.append(bar) i.methods.append( xpt.Method("IFaceMethod", xpt.Param(xpt.SimpleType(xpt.Type.Tags.int32)), params=[xpt.Param(xpt.InterfaceType(bar))])) self.checkRoundtrip(t) # add a method with an InterfaceIsType argument i.methods.append( xpt.Method("IFaceIsMethod", xpt.Param(xpt.SimpleType(xpt.Type.Tags.void)), params=[ xpt.Param(xpt.InterfaceIsType(1)), xpt.Param(xpt.SimpleType(xpt.Type.Tags.nsIID)) ])) self.checkRoundtrip(t) # add a method with an ArrayType argument i.methods.append( xpt.Method("ArrayMethod", xpt.Param(xpt.SimpleType(xpt.Type.Tags.void)), params=[ xpt.Param( xpt.ArrayType( xpt.SimpleType(xpt.Type.Tags.int32), 1, 2)), xpt.Param(xpt.SimpleType(xpt.Type.Tags.int32)), xpt.Param(xpt.SimpleType(xpt.Type.Tags.int32)), ])) self.checkRoundtrip(t) # add a method with a StringWithSize and WideStringWithSize arguments i.methods.append( xpt.Method("StringWithSizeMethod", xpt.Param(xpt.SimpleType(xpt.Type.Tags.void)), params=[ xpt.Param(xpt.StringWithSizeType(1, 2)), xpt.Param(xpt.SimpleType(xpt.Type.Tags.int32)), xpt.Param(xpt.SimpleType(xpt.Type.Tags.int32)), xpt.Param(xpt.WideStringWithSizeType(4, 5)), xpt.Param(xpt.SimpleType(xpt.Type.Tags.int32)), xpt.Param(xpt.SimpleType(xpt.Type.Tags.int32)), ])) self.checkRoundtrip(t)
def test_mergeReplaceParams(self): """ Test that merging an interface correctly updates InterfaceType params on methods of other interfaces. """ # t1 has an unresolved interface and an interface that uses the # unresolved interface as a param value in a method. t2 # has a resolved version of the unresolved interface. t1 = xpt.Typelib() # add an unresolved interface i = xpt.Interface("IFoo") t1.interfaces.append(i) # add an interface that uses the unresolved interface # as a param value in a method. vp = xpt.Param(xpt.SimpleType(xpt.Type.Tags.void)) p = xpt.Param(xpt.InterfaceType(i)) m = xpt.Method("IfaceParam", vp, params=[p]) t1.interfaces.append( xpt.Interface("IParam", iid="11111111-1111-1111-1111-111111111111", methods=[m])) t2 = xpt.Typelib() # add a resolved interface m = xpt.Method("Bar", vp) t2.interfaces.append( xpt.Interface("IFoo", iid="11223344-5566-7788-9900-aabbccddeeff", methods=[m])) t1.merge(t2) self.assertEqual(2, len(t1.interfaces)) self.assertEqual("IParam", t1.interfaces[0].name) self.assertEqual("11111111-1111-1111-1111-111111111111", t1.interfaces[0].iid) self.assertEqual("IFoo", t1.interfaces[1].name) self.assertEqual("11223344-5566-7788-9900-aabbccddeeff", t1.interfaces[1].iid) self.assert_(t1.interfaces[1].resolved) # Ensure that IRetval's method's param type has been updated. self.assertEqual(1, len(t1.interfaces[0].methods)) self.assert_(t1.interfaces[0].methods[0].params[0].type.iface.resolved) self.assertEqual(t1.interfaces[1], t1.interfaces[0].methods[0].params[0].type.iface) # t1 has a resolved interface. t2 has an unresolved version # and an interface that uses the unresolved interface as a # param value in a method. t1 = xpt.Typelib() # add a resolved interface m = xpt.Method("Bar", vp) t1.interfaces.append( xpt.Interface("IFoo", iid="11223344-5566-7788-9900-aabbccddeeff", methods=[m])) t2 = xpt.Typelib() # add an unresolved interface i = xpt.Interface("IFoo") t2.interfaces.append(i) # add an interface that uses the unresolved interface # as a param value in a method. vp = xpt.Param(xpt.SimpleType(xpt.Type.Tags.void)) p = xpt.Param(xpt.InterfaceType(i)) m = xpt.Method("IfaceParam", vp, params=[p]) t2.interfaces.append( xpt.Interface("IParam", iid="11111111-1111-1111-1111-111111111111", methods=[m])) t1.merge(t2) self.assertEqual(2, len(t1.interfaces)) self.assertEqual("IParam", t1.interfaces[0].name) self.assertEqual("11111111-1111-1111-1111-111111111111", t1.interfaces[0].iid) self.assertEqual("IFoo", t1.interfaces[1].name) self.assertEqual("11223344-5566-7788-9900-aabbccddeeff", t1.interfaces[1].iid) self.assert_(t1.interfaces[1].resolved) # Ensure that IRetval's method's param type has been updated. self.assertEqual(1, len(t1.interfaces[0].methods)) self.assert_(t1.interfaces[0].methods[0].params[0].type.iface.resolved) self.assertEqual(t1.interfaces[1], t1.interfaces[0].methods[0].params[0].type.iface)