Skip to content

Commit e2c65cc

Browse files
authored
Add CreateTypeLib and ICreateTypeInfo tests. (#915)
* test: Add tests for `CreateTypeLib` documentation and `CreateTypeInfo`. This commit introduces `Test_CreateTypeLib` to `test_typeinfo.py`, verifying `ICreateTypeLib`'s ability to set type library documentation. * test: Add `test_CreateTypeInfo` to `Test_CreateTypeLib`. This adds a test for the `ICreateTypeLib.CreateTypeInfo` method, ensuring that it correctly creates an `ICreateTypeInfo` instance. * test: Add `test_LibAttr` to verify `ICreateTypeLib` attribute setters. This test ensures that `ICreateTypeLib` methods like `SetGuid`, `SetLcid`, `SetVersion`, and `SetLibFlags` correctly persist library attributes. It verifies these attributes by loading the created type library and comparing the `LIBATTR` fields. * test: Add `Test_ICreateTypeInfo`. This commit introduces `Test_ICreateTypeInfo` to `test_typeinfo.py`, specifically testing the `ICreateTypeInfo.SetDocString` and `ICreateTypeInfo.SetHelpContext` methods. It ensures that documentation set on a type information object is correctly persisted and can be retrieved after the type library is saved and reloaded.
1 parent c2c2fe5 commit e2c65cc

1 file changed

Lines changed: 84 additions & 0 deletions

File tree

comtypes/test/test_typeinfo.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import ctypes
22
import os
33
import sys
4+
import tempfile
45
import unittest
56
from _ctypes import COMError
67
from ctypes.wintypes import MAX_PATH
8+
from pathlib import Path
79

810
from comtypes import GUID, hresult, typeinfo
911
from comtypes.typeinfo import (
12+
CreateTypeLib,
1013
GetModuleFileName,
1114
LoadRegTypeLib,
1215
LoadTypeLibEx,
@@ -209,6 +212,87 @@ def test_non_existent_name(self):
209212
tcomp.Bind("NonExistentNameForTest")
210213

211214

215+
class Test_CreateTypeLib(unittest.TestCase):
216+
def setUp(self):
217+
td = tempfile.TemporaryDirectory()
218+
self.addCleanup(td.cleanup)
219+
self.tmpdir = Path(td.name)
220+
self.typelib_path = self.tmpdir / "test.tlb"
221+
222+
def test_Documentation(self):
223+
ctlib = CreateTypeLib(str(self.typelib_path))
224+
libname = "MyTestTypeLib"
225+
docstring = "This is a test type library docstring."
226+
helpctx = 123
227+
helpfile = "myhelp.chm"
228+
ctlib.SetName(libname)
229+
ctlib.SetDocString(docstring)
230+
ctlib.SetHelpContext(helpctx)
231+
ctlib.SetHelpFileName(helpfile)
232+
ctlib.SaveAllChanges()
233+
# Verify by loading the created typelib
234+
tlib = LoadTypeLibEx(str(self.typelib_path))
235+
doc = tlib.GetDocumentation(-1)
236+
self.assertEqual(
237+
doc[:-1] + (Path(doc[-1].strip("\0")).name,), # type: ignore
238+
(libname, docstring, helpctx, helpfile),
239+
)
240+
241+
def test_LibAttr(self):
242+
ctlib = CreateTypeLib(str(self.typelib_path))
243+
libid = GUID.create_new()
244+
lcid = 1033 # English (United States)
245+
LIBFLAG_FRESTRICTED = 0x1
246+
major_version = 1
247+
minor_version = 2
248+
ctlib.SetGuid(libid)
249+
ctlib.SetLcid(lcid)
250+
ctlib.SetVersion(major_version, minor_version)
251+
ctlib.SetLibFlags(LIBFLAG_FRESTRICTED)
252+
ctlib.SaveAllChanges()
253+
# Verify by loading the created typelib
254+
tlib = LoadTypeLibEx(str(self.typelib_path))
255+
la = tlib.GetLibAttr()
256+
self.assertEqual(la.guid, libid)
257+
self.assertEqual(la.lcid, lcid)
258+
self.assertTrue(la.wLibFlags & LIBFLAG_FRESTRICTED)
259+
self.assertEqual(la.wMajorVerNum, major_version)
260+
self.assertEqual(la.wMinorVerNum, minor_version)
261+
262+
def test_CreateTypeInfo(self):
263+
ctlib = CreateTypeLib(str(self.typelib_path))
264+
# Create a type info
265+
self.assertIsInstance(
266+
ctlib.CreateTypeInfo("IMyInterface", typeinfo.TKIND_INTERFACE),
267+
typeinfo.ICreateTypeInfo,
268+
)
269+
270+
271+
class Test_ICreateTypeInfo(unittest.TestCase):
272+
def setUp(self):
273+
td = tempfile.TemporaryDirectory()
274+
self.addCleanup(td.cleanup)
275+
self.tmpdir = Path(td.name)
276+
self.typelib_path = self.tmpdir / "test.tlb"
277+
self.ctlib = CreateTypeLib(str(self.typelib_path))
278+
279+
def test_Documentaion(self):
280+
name = "IMyInterface"
281+
docstring = "My test interface"
282+
helpctx = 123
283+
ctinfo = self.ctlib.CreateTypeInfo(name, typeinfo.TKIND_INTERFACE)
284+
ctinfo.SetDocString(docstring)
285+
ctinfo.SetHelpContext(helpctx)
286+
# `Layout` must be called before `SaveAllChanges`.
287+
ctinfo.LayOut()
288+
self.ctlib.SaveAllChanges()
289+
# Load the typelib and verify the type info
290+
tlib = LoadTypeLibEx(str(self.typelib_path))
291+
_, tinfo = tlib.FindName(name) # type: ignore
292+
doc = tinfo.GetDocumentation(-1)
293+
self.assertEqual(doc, (name, docstring, helpctx, None))
294+
295+
212296
class Test_GetModuleFileName(unittest.TestCase):
213297
@unittest.skipUnless(
214298
sys.prefix == sys.base_prefix,

0 commit comments

Comments
 (0)