Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
junkmd committed May 26, 2024
1 parent 8b2fe44 commit ddff7aa
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions comtypes/test/test_dispifc_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@
class Test(unittest.TestCase):
"""Test dispmethods with record pointer parameters."""

def test(self):
EXPECTED_INITED_QUESTIONS = "The meaning of life, the universe and everything?"

def _create_dispifc(self) -> "ComtypesTestLib.IComtypesTest":
# Explicitely ask for the dispinterface of the component.
dispifc = CreateObject(
return CreateObject(
ProgID, clsctx=CLSCTX_LOCAL_SERVER, interface=ComtypesTestLib.IComtypesTest
)

def test_byref(self):
dispifc = self._create_dispifc()
# Passing a record by reference to a method that has declared the parameter
# as [in, out] we expect modifications of the record on the server side to
# also change the record on the client side.
Expand All @@ -36,47 +40,52 @@ def test(self):
self.assertEqual(test_record.answer, 0)
self.assertEqual(test_record.needs_clarification, False)
dispifc.InitRecord(byref(test_record))
self.assertEqual(
test_record.question, "The meaning of life, the universe and everything?"
)
self.assertEqual(test_record.question, self.EXPECTED_INITED_QUESTIONS)
self.assertEqual(test_record.answer, 42)
self.assertEqual(test_record.needs_clarification, True)

def test_pointer(self):
dispifc = self._create_dispifc()
# Passing a record pointer to a method that has declared the parameter
# as [in, out] we expect modifications of the record on the server side to
# also change the record on the client side.
test_record = ComtypesTestLib.T_TEST_RECORD()
self.assertEqual(test_record.question, None)
self.assertEqual(test_record.answer, 0)
self.assertEqual(test_record.needs_clarification, False)
test_record_pointer = pointer(test_record)
dispifc.InitRecord(test_record_pointer)
dispifc.InitRecord(pointer(test_record))
self.assertEqual(
test_record.question, "The meaning of life, the universe and everything?"
)
self.assertEqual(test_record.answer, 42)
self.assertEqual(test_record.needs_clarification, True)

def test_record(self):
# Passing a record to a method that has declared the parameter just as [in]
# we expect modifications of the record on the server side NOT to change
# the record on the client side.
# We also need to test if the record gets properly passed to the method on
# the server side. For this, the 'VerifyRecord' method returns 'True' if
# all record fields have the initialization values provided by 'InitRecord'.
self.assertTrue(dispifc.VerifyRecord(test_record))
# Check if the 'answer' field is unchanged although the method modifies this
# field on the server side.
self.assertEqual(test_record.answer, 42)
# all record fields have values equivalent to the initialization values
# provided by 'InitRecord'.
inited_record = ComtypesTestLib.T_TEST_RECORD()
inited_record.question = self.EXPECTED_INITED_QUESTIONS
inited_record.answer = 42
inited_record.needs_clarification = True
inited_vals = (inited_record, True, (self.EXPECTED_INITED_QUESTIONS, 42, True))
# Also perform the inverted test.
# For this, first create a blank record.
test_record = ComtypesTestLib.T_TEST_RECORD()
self.assertEqual(test_record.question, None)
self.assertEqual(test_record.answer, 0)
self.assertEqual(test_record.needs_clarification, False)
# Perform the check on initialization values.
self.assertFalse(dispifc.VerifyRecord(test_record))
# The record on the client side should be unchanged.
self.assertEqual(test_record.answer, 0)
uninited_record = ComtypesTestLib.T_TEST_RECORD()
uninited_vals = (uninited_record, False, (None, 0, False))
for rec, expected, (q, a, nc) in [inited_vals, uninited_vals]:
with self.subTest(expected=expected, q=q, a=a, nc=nc):
# Perform the check on initialization values.
self.assertEqual(self._create_dispifc().VerifyRecord(rec), expected)
self.assertEqual(rec.question, q)
# Check if the 'answer' field is unchanged although the method
# modifies this field on the server side.
self.assertEqual(rec.answer, a)
self.assertEqual(rec.needs_clarification, nc)


if __name__ == "__main__":
Expand Down

0 comments on commit ddff7aa

Please sign in to comment.