Skip to content

Commit 8fec088

Browse files
authored
Refactor test_basic.py to use public APIs. (#936)
* refactor: Move `CreateTypeLib` import to top-level in `test_basic.py`. Relocate `CreateTypeLib` from `BasicTest.test_guid` to the top level of `test/test_basic.py` to follow project conventions. * refactor: Use `SYS_WIN32` constant in `test_basic.py`. Replace magic number `1` with `SYS_WIN32` in `_CreateTypeLib2` calls. * refactor: Use public `CreateTypeLib` in `test_basic.py`. Replace internal `_CreateTypeLib2` calls with public `CreateTypeLib`. The primary goal of these tests is to verify the basic mechanics of COM pointers, such as reference counting and `QueryInterface` behavior, which are common to all COM interfaces. Testing low-level, private functions like `_CreateTypeLib2` is not the objective here. * refactor: Remove redundant `IUnknown` import in `test_basic.py`. Remove the redundant inline import of `IUnknown` from the `test_IUnknown` method, as it is already imported at the top level of `test/test_basic.py`. * refactor: Remove commented-out code in `test_basic.py`. Remove a redundant commented-out `import` statement from the top of `test/test_basic.py`. * test: Verify `IUnknown` instance in `test_basic.py`. Add assertions to `BasicTest.test_refcounts` and `BasicTest.test_qi` to verify that the object returned by `CreateTypeLib` is an instance of `IUnknown`.
1 parent 80ed427 commit 8fec088

1 file changed

Lines changed: 8 additions & 11 deletions

File tree

comtypes/test/test_basic.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
##import ut
21
import unittest as ut
3-
from ctypes import HRESULT, POINTER, byref
2+
from ctypes import HRESULT, POINTER
43

54
from comtypes import GUID, STDMETHOD, IUnknown
6-
from comtypes.typeinfo import ICreateTypeLib2, _CreateTypeLib2
5+
from comtypes.typeinfo import SYS_WIN32, CreateTypeLib, ICreateTypeLib2
76

87
# XXX leaks references!
98

@@ -14,8 +13,6 @@ def method_count(interface):
1413

1514
class BasicTest(ut.TestCase):
1615
def test_IUnknown(self):
17-
from comtypes import IUnknown
18-
1916
self.assertEqual(method_count(IUnknown), 3)
2017

2118
def test_release(self):
@@ -24,8 +21,9 @@ def test_release(self):
2421
def test_refcounts(self):
2522
# Since all COM interfaces derive from IUnknown and have the same reference counting behavior, any interface
2623
# — whether ICreateTypeLib2 or otherwise — could be used for this test.
27-
p = POINTER(ICreateTypeLib2)()
28-
_CreateTypeLib2(1, "blabla", byref(p))
24+
p = CreateTypeLib("blabla", syskind=SYS_WIN32)
25+
self.assertIsInstance(p, ICreateTypeLib2)
26+
self.assertIsInstance(p, IUnknown)
2927
# initial refcount is 2
3028
for i in range(2, 10):
3129
self.assertEqual(p.AddRef(), i)
@@ -35,8 +33,9 @@ def test_refcounts(self):
3533
def test_qi(self):
3634
# Since all COM interfaces derive from IUnknown and have the same QueryInterface behavior, any interface
3735
# — whether ICreateTypeLib2 or otherwise — could be used for this test.
38-
p = POINTER(ICreateTypeLib2)()
39-
_CreateTypeLib2(1, "blabla", byref(p))
36+
p = CreateTypeLib("blabla", syskind=SYS_WIN32)
37+
self.assertIsInstance(p, ICreateTypeLib2)
38+
self.assertIsInstance(p, IUnknown)
4039
self.assertEqual(p.AddRef(), 2)
4140
self.assertEqual(p.Release(), 1)
4241

@@ -115,8 +114,6 @@ def test_identity(self):
115114
self.assertEqual(a, b)
116115
self.assertEqual(hash(a), hash(b))
117116

118-
from comtypes.typeinfo import CreateTypeLib
119-
120117
# we do not save the lib, so no file will be created.
121118
# these should NOT be identical
122119
a = CreateTypeLib("blahblah")

0 commit comments

Comments
 (0)