Skip to content

Commit 7d69dce

Browse files
authored
add tests for client.dynamic (#440)
1 parent 0fecfea commit 7d69dce

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

comtypes/test/test_client_dynamic.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import ctypes
2+
import unittest as ut
3+
from unittest import mock
4+
5+
from comtypes import automation, COMError, IUnknown
6+
from comtypes.client import CreateObject, dynamic, GetModule, lazybind
7+
8+
9+
class Test_Dispatch_Function(ut.TestCase):
10+
# It is difficult to cause intentionally errors "in the regular way".
11+
# So `mock` is used to cover conditional branches.
12+
def test_returns_dynamic_Dispatch_if_takes_dynamic_Dispatch(self):
13+
obj = mock.MagicMock(spec=dynamic._Dispatch)
14+
self.assertIs(dynamic.Dispatch(obj), obj)
15+
16+
def test_returns_lazybind_Dispatch_if_takes_ptrIDispatch(self):
17+
# Conditional branches that return `lazybind.Dispatch` are also covered by
18+
# `test_dyndispatch` and others.
19+
obj = mock.MagicMock(spec=ctypes.POINTER(automation.IDispatch))
20+
self.assertIsInstance(dynamic.Dispatch(obj), lazybind.Dispatch)
21+
22+
def test_returns_dynamic_Dispatch_if_takes_ptrIDispatch_and_raised_comerr(self):
23+
obj = mock.MagicMock(spec=ctypes.POINTER(automation.IDispatch))
24+
obj.GetTypeInfo.side_effect = COMError(0, "test", ("", "", "", 0, 0))
25+
self.assertIsInstance(dynamic.Dispatch(obj), dynamic._Dispatch)
26+
27+
def test_returns_dynamic_Dispatch_if_takes_ptrIDispatch_and_raised_winerr(self):
28+
obj = mock.MagicMock(spec=ctypes.POINTER(automation.IDispatch))
29+
obj.GetTypeInfo.side_effect = WindowsError()
30+
self.assertIsInstance(dynamic.Dispatch(obj), dynamic._Dispatch)
31+
32+
def test_returns_what_is_took_if_takes_other(self):
33+
obj = object()
34+
self.assertIs(dynamic.Dispatch(obj), obj)
35+
36+
37+
class Test_Dispatch_Class(ut.TestCase):
38+
# `MethodCaller` and `_Collection` are indirectly covered in this.
39+
def test_dict(self):
40+
# The following conditional branches are not covered;
41+
# - not `hresult in ERRORS_BAD_CONTEXT`
42+
# - not `0 != enum.Skip(index)`
43+
# - other than `COMError` raises in `__getattr__`
44+
orig = CreateObject("Scripting.Dictionary", interface=automation.IDispatch)
45+
d = dynamic._Dispatch(orig)
46+
d.CompareMode = 42
47+
d.Item["foo"] = 1
48+
d.Item["bar"] = "spam foo"
49+
d.Item["baz"] = 3.14
50+
self.assertEqual(d[0], "foo")
51+
self.assertEqual(d.Item["foo"], 1)
52+
self.assertEqual([k for k in iter(d)], ["foo", "bar", "baz"])
53+
self.assertIsInstance(hash(d), int)
54+
d._FlagAsMethod("_NewEnum")
55+
self.assertIs(type(d._NewEnum()), ctypes.POINTER(IUnknown))
56+
scrrun = GetModule("scrrun.dll")
57+
scr_dict = d.QueryInterface(scrrun.IDictionary)
58+
self.assertIsInstance(scr_dict, scrrun.IDictionary)
59+
d.Item["qux"] = scr_dict
60+
with self.assertRaises(IndexError):
61+
d[4]
62+
with self.assertRaises(AttributeError):
63+
d.__foo__
64+
65+
66+
if __name__ == "__main__":
67+
ut.main()

0 commit comments

Comments
 (0)