From 89057f249ff61b3a099c2979fd57a7396a69751e Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Tue, 10 Dec 2024 09:57:03 +0900 Subject: [PATCH] Move some tests to `test_comserver`. (#695) * Add `VariantTest` to `test_comserver`. * Add `SafeArrayTest` to `test_comserver`. * Add `PropPutRefTest` to `test_comserver`. * Remove duplicated tests from `test_propputref`, `test_safearray` and `test_variant`. * Improve `VariantTest.test_UDT` in `test_comserver`. * Improve `PropPutRefTest` in `test_comserver`. --- comtypes/test/test_comserver.py | 72 ++++++++++++++++++++++++++++++++ comtypes/test/test_propputref.py | 40 ------------------ comtypes/test/test_safearray.py | 23 ---------- comtypes/test/test_variant.py | 17 -------- 4 files changed, 72 insertions(+), 80 deletions(-) delete mode 100644 comtypes/test/test_propputref.py diff --git a/comtypes/test/test_comserver.py b/comtypes/test/test_comserver.py index 56dcc85d..23fd0c35 100644 --- a/comtypes/test/test_comserver.py +++ b/comtypes/test/test_comserver.py @@ -5,6 +5,7 @@ import comtypes.test.TestComServer from comtypes import BSTR +from comtypes.automation import VARIANT, _midlSAFEARRAY from comtypes.client import CreateObject from comtypes.server.register import register, unregister from comtypes.test.find_memleak import find_memleak @@ -170,6 +171,77 @@ def test_mixedinout(self): pass +class VariantTest(unittest.TestCase): + def test_UDT(self): + from comtypes.gen.TestComServerLib import MYCOLOR + + v = VARIANT(MYCOLOR(red=1.0, green=2.0, blue=3.0)) + value = v.value + self.assertEqual(1.0, value.red) # type: ignore + self.assertEqual(2.0, value.green) # type: ignore + self.assertEqual(3.0, value.blue) # type: ignore + + def func(): + v = VARIANT(MYCOLOR(red=1.0, green=2.0, blue=3.0)) + return v.value + + bytes = find_memleak(func) + self.assertFalse(bytes, "Leaks %d bytes" % bytes) + + +class SafeArrayTest(unittest.TestCase): + def test_UDT(self): + from comtypes.gen.TestComServerLib import MYCOLOR + + t = _midlSAFEARRAY(MYCOLOR) + self.assertTrue(t is _midlSAFEARRAY(MYCOLOR)) + + sa = t.from_param([MYCOLOR(0, 0, 0), MYCOLOR(1, 2, 3)]) + + self.assertEqual( + [(x.red, x.green, x.blue) for x in sa[0]], + [(0.0, 0.0, 0.0), (1.0, 2.0, 3.0)], + ) + + def doit(): + t.from_param([MYCOLOR(0, 0, 0), MYCOLOR(1, 2, 3)]) + + bytes = find_memleak(doit) + self.assertFalse(bytes, "Leaks %d bytes" % bytes) + + +class PropPutRefTest(unittest.TestCase): + def doit(self, dynamic: bool): + d = CreateObject("Scripting.Dictionary", dynamic=dynamic) + s = CreateObject("TestComServerLib.TestComServer", dynamic=dynamic) + s.name = "the value" + + # This calls propputref, since we assign an Object + d.Item["object"] = s + # This calls propput, since we assing a Value + d.Item["value"] = s.name + + self.assertEqual(d.Item["object"], s) + self.assertEqual(d.Item["object"].name, "the value") + self.assertEqual(d.Item["value"], "the value") + + # Changing the default property of the object + s.name = "foo bar" + self.assertEqual(d.Item["object"], s) + self.assertEqual(d.Item["object"].name, "foo bar") + self.assertEqual(d.Item["value"], "the value") + + # This also calls propputref since we assign an Object + d.Item["var"] = VARIANT(s) + self.assertEqual(d.Item["var"], s) + + def test_earlybind(self): + self.doit(dynamic=False) + + def test_latebind(self): + self.doit(dynamic=True) + + class TestEvents(unittest.TestCase): def test(self): import comtypes.test.test_comserver diff --git a/comtypes/test/test_propputref.py b/comtypes/test/test_propputref.py deleted file mode 100644 index 8f6c7537..00000000 --- a/comtypes/test/test_propputref.py +++ /dev/null @@ -1,40 +0,0 @@ -# There are also propputref tests in test_sapi.py! -import unittest -from comtypes.client import CreateObject -from comtypes.automation import VARIANT - - -class Test(unittest.TestCase): - @unittest.skip( - "Fails on creating `TestComServerLib.TestComServer`. Figure out why." - ) - def test(self, dynamic=False): - d = CreateObject("Scripting.Dictionary", dynamic=dynamic) - s = CreateObject("TestComServerLib.TestComServer", dynamic=dynamic) - s.name = "the value" - - # This calls propputref, since we assign an Object - d.Item["object"] = s - # This calls propput, since we assing a Value - d.Item["value"] = s.name - - self.assertEqual(d.Item["object"], s) - self.assertEqual(d.Item["object"].name, "the value") - self.assertEqual(d.Item["value"], "the value") - - # Changing the default property of the object - s.name = "foo bar" - self.assertEqual(d.Item["object"], s) - self.assertEqual(d.Item["object"].name, "foo bar") - self.assertEqual(d.Item["value"], "the value") - - # This also calls propputref since we assign an Object - d.Item["var"] = VARIANT(s) - self.assertEqual(d.Item["var"], s) - - def test_dispatch(self): - return self.test(dynamic=True) - - -if __name__ == "__main__": - unittest.main() diff --git a/comtypes/test/test_safearray.py b/comtypes/test/test_safearray.py index 3c9bd18e..49238020 100644 --- a/comtypes/test/test_safearray.py +++ b/comtypes/test/test_safearray.py @@ -221,29 +221,6 @@ def test_VT_UNKNOWN_multi(self): del sa self.assertEqual((a, b), (com_refcnt(plib), com_refcnt(punk))) - @unittest.skip( - "This fails with a 'library not registered' error. Need to figure out how to " - "register TestComServerLib (without admin if possible)." - ) - def test_UDT(self): - from comtypes.gen.TestComServerLib import MYCOLOR - - t = _midlSAFEARRAY(MYCOLOR) - self.assertTrue(t is _midlSAFEARRAY(MYCOLOR)) - - sa = t.from_param([MYCOLOR(0, 0, 0), MYCOLOR(1, 2, 3)]) - - self.assertEqual( - [(x.red, x.green, x.blue) for x in sa[0]], - [(0.0, 0.0, 0.0), (1.0, 2.0, 3.0)], - ) - - def doit(): - t.from_param([MYCOLOR(0, 0, 0), MYCOLOR(1, 2, 3)]) - - bytes = find_memleak(doit) - self.assertFalse(bytes, "Leaks %d bytes" % bytes) - if __name__ == "__main__": unittest.main() diff --git a/comtypes/test/test_variant.py b/comtypes/test/test_variant.py index f4d2a992..88db242e 100644 --- a/comtypes/test/test_variant.py +++ b/comtypes/test/test_variant.py @@ -194,23 +194,6 @@ def test_empty_BSTR(self): v.value = "" self.assertEqual(v.vt, VT_BSTR) - @unittest.skip( - "Fails on creating `TestComServerLib.TestComServer`. Library not registered." - ) - def test_UDT(self): - from comtypes.gen.TestComServerLib import MYCOLOR - - v = VARIANT(MYCOLOR(red=1.0, green=2.0, blue=3.0)) - value = v.value - self.assertEqual((1.0, 2.0, 3.0), (value.red, value.green, value.blue)) - - def func(): - v = VARIANT(MYCOLOR(red=1.0, green=2.0, blue=3.0)) - return v.value - - bytes = find_memleak(func) - self.assertFalse(bytes, "Leaks %d bytes" % bytes) - def test_ctypes_in_variant(self): v = VARIANT() objs = [