Skip to content

Commit

Permalink
Move some tests to test_comserver. (enthought#695)
Browse files Browse the repository at this point in the history
* 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`.
  • Loading branch information
junkmd authored Dec 10, 2024
1 parent 46c34ec commit 89057f2
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 80 deletions.
72 changes: 72 additions & 0 deletions comtypes/test/test_comserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
40 changes: 0 additions & 40 deletions comtypes/test/test_propputref.py

This file was deleted.

23 changes: 0 additions & 23 deletions comtypes/test/test_safearray.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
17 changes: 0 additions & 17 deletions comtypes/test/test_variant.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down

0 comments on commit 89057f2

Please sign in to comment.