Update of /cvsroot/pywin32/pywin32/com/win32com/test
In directory ddv4jf1.ch3.sourceforge.com:/tmp/cvs-serv7136/com/win32com/test
Modified Files:
Tag: py3k
testClipboard.py testShell.py testStreams.py testvb.py util.py
Log Message:
merge recent test changes from the trunk
Index: testStreams.py
===================================================================
RCS file: /cvsroot/pywin32/pywin32/com/win32com/test/testStreams.py,v
retrieving revision 1.6.2.2
retrieving revision 1.6.2.3
diff -C2 -d -r1.6.2.2 -r1.6.2.3
*** testStreams.py 5 Jan 2009 12:51:27 -0000 1.6.2.2
--- testStreams.py 7 Jan 2009 06:25:15 -0000 1.6.2.3
***************
*** 4,7 ****
--- 4,8 ----
import unittest
+ from pywin32_testutil import str2bytes
class Persists:
***************
*** 10,14 ****
_com_interfaces_ = [ pythoncom.IID_IPersistStreamInit ]
def __init__(self):
! self.data = "abcdefg".encode("ascii")
self.dirty = 1
def GetClassID(self):
--- 11,15 ----
_com_interfaces_ = [ pythoncom.IID_IPersistStreamInit ]
def __init__(self):
! self.data = str2bytes("abcdefg")
self.dirty = 1
def GetClassID(self):
***************
*** 67,71 ****
"""
def Read(self, amount):
! return 'x'.encode('ascii')*(amount+1)
class StreamTest(win32com.test.util.TestCase):
--- 68,72 ----
"""
def Read(self, amount):
! return str2bytes('x')*(amount+1)
class StreamTest(win32com.test.util.TestCase):
***************
*** 81,85 ****
def testit(self):
! mydata = 'abcdefghijklmnopqrstuvwxyz'.encode('ascii')
# First test the objects just as Python objects...
--- 82,86 ----
def testit(self):
! mydata = str2bytes('abcdefghijklmnopqrstuvwxyz')
# First test the objects just as Python objects...
***************
*** 101,105 ****
self._readWrite(mydata, s2, s2)
! self._readWrite("string with\0a NULL".encode('ascii'), s2, s2)
# reset the stream
s.Write(mydata)
--- 102,106 ----
self._readWrite(mydata, s2, s2)
! self._readWrite(str2bytes("string with\0a NULL"), s2, s2)
# reset the stream
s.Write(mydata)
Index: testvb.py
===================================================================
RCS file: /cvsroot/pywin32/pywin32/com/win32com/test/testvb.py,v
retrieving revision 1.20.4.6
retrieving revision 1.20.4.7
diff -C2 -d -r1.20.4.6 -r1.20.4.7
*** testvb.py 5 Jan 2009 12:51:27 -0000 1.20.4.6
--- testvb.py 7 Jan 2009 06:25:15 -0000 1.20.4.7
***************
*** 9,21 ****
from win32com.server.util import NewCollection, wrap
from win32com.test import util
import traceback
- def string_to_buffer(s):
- if sys.version_info < (3,0):
- return buffer(s)
- else:
- return memoryview(s.encode("ascii"))
-
# for debugging
useDispatcher = None
--- 9,16 ----
from win32com.server.util import NewCollection, wrap
from win32com.test import util
+ from pywin32_testutil import str2memory
import traceback
# for debugging
useDispatcher = None
***************
*** 74,79 ****
if vbtest.VariantProperty != 10:
raise error("Could not set the variant integer property correctly.")
! vbtest.VariantProperty = string_to_buffer('raw\0data')
! if vbtest.VariantProperty != string_to_buffer('raw\0data'):
raise error("Could not set the variant buffer property correctly.")
vbtest.StringProperty = "Hello from Python"
--- 69,74 ----
if vbtest.VariantProperty != 10:
raise error("Could not set the variant integer property correctly.")
! vbtest.VariantProperty = str2memory('raw\0data')
! if vbtest.VariantProperty != str2memory('raw\0data'):
raise error("Could not set the variant buffer property correctly.")
vbtest.StringProperty = "Hello from Python"
Index: util.py
===================================================================
RCS file: /cvsroot/pywin32/pywin32/com/win32com/test/util.py,v
retrieving revision 1.9.2.7
retrieving revision 1.9.2.8
diff -C2 -d -r1.9.2.7 -r1.9.2.8
*** util.py 5 Jan 2009 12:51:27 -0000 1.9.2.7
--- util.py 7 Jan 2009 06:25:15 -0000 1.9.2.8
***************
*** 12,15 ****
--- 12,18 ----
import io as StringIO
+ import pywin32_testutil
+ from pywin32_testutil import TestLoader, TestResult, TestRunner, LeakTestCase
+
def CheckClean():
# Ensure no lingering exceptions - Python should have zero outstanding
***************
*** 143,259 ****
return len("".join(self.captured).split("\n"))
- class LeakTestCase(unittest.TestCase):
- def __init__(self, real_test):
- unittest.TestCase.__init__(self)
- self.real_test = real_test
- self.num_test_cases = 1
- self.num_leak_iters = 2 # seems to be enough!
- if hasattr(sys, "gettotalrefcount"):
- self.num_test_cases = self.num_test_cases + self.num_leak_iters
- def countTestCases(self):
- return self.num_test_cases
- def runTest(self):
- assert 0, "not used"
- def __call__(self, result = None):
- # Always ensure we don't leak gateways/interfaces
- gc.collect()
- ni = _GetInterfaceCount()
- ng = _GetGatewayCount()
- self.real_test(result)
- # Failed - no point checking anything else
- if result.shouldStop or not result.wasSuccessful():
- return
- self._do_leak_tests(result)
- gc.collect()
- lost_i = _GetInterfaceCount() - ni
- lost_g = _GetGatewayCount() - ng
- if lost_i or lost_g:
- msg = "%d interface objects and %d gateway objects leaked" \
- % (lost_i, lost_g)
- result.addFailure(self.real_test, (AssertionError, msg, None))
- def _do_leak_tests(self, result = None):
- try:
- gtrc = sys.gettotalrefcount
- except AttributeError:
- return # can't do leak tests in this build
- def gtrc():
- return 0
- # Assume already called once, to prime any caches etc
- trc = gtrc()
- for i in range(self.num_leak_iters):
- self.real_test(result)
- if result.shouldStop:
- break
- del i # created after we remembered the refcount!
- # int division here means one or 2 stray references won't force
- # failure, but one per loop
- gc.collect()
- lost = (gtrc() - trc) // self.num_leak_iters
- if lost < 0:
- msg = "LeakTest: %s appeared to gain %d references!!" % (self.real_test, -lost)
- result.addFailure(self.real_test, (AssertionError, msg, None))
- if lost > 0:
- msg = "LeakTest: %s lost %d references" % (self.real_test, lost)
- result.addFailure(self.real_test, (AssertionError, msg, None))
-
- class TestLoader(unittest.TestLoader):
- def loadTestsFromTestCase(self, testCaseClass):
- """Return a suite of all tests cases contained in testCaseClass"""
- leak_tests = []
- for name in self.getTestCaseNames(testCaseClass):
- real_test = testCaseClass(name)
- leak_test = self._getTestWrapper(real_test)
- leak_tests.append(leak_test)
- return self.suiteClass(leak_tests)
- def _getTestWrapper(self, test):
- no_leak_tests = getattr(test, "no_leak_tests", False)
- if no_leak_tests:
- print("Test says it doesn't want leak tests!")
- return test
- return LeakTestCase(test)
- def loadTestsFromModule(self, mod):
- if hasattr(mod, "suite"):
- return mod.suite()
- else:
- return unittest.TestLoader.loadTestsFromModule(self, mod)
- def loadTestsFromName(self, name, module=None):
- test = unittest.TestLoader.loadTestsFromName(self, name, module)
- if isinstance(test, unittest.TestSuite):
- pass # hmmm? print "Don't wrap suites yet!", test._tests
- elif isinstance(test, unittest.TestCase):
- test = self._getTestWrapper(test)
- else:
- print("XXX - what is", test)
- return test
-
- class TestResult(unittest._TextTestResult):
- def __init__(self, *args, **kw):
- super(TestResult, self).__init__(*args, **kw)
- self.num_invalid_clsid = 0
-
- def addError(self, test, err):
- """Called when an error has occurred. 'err' is a tuple of values as
- returned by sys.exc_info().
- """
- if isinstance(err[1], pythoncom.com_error) and \
- err[1].hresult==winerror.CO_E_CLASSSTRING:
- self.num_invalid_clsid += 1
- if self.showAll:
- self.stream.writeln("SKIP")
- elif self.dots:
- self.stream.write('S')
- self.stream.flush()
- return
- super(TestResult, self).addError(test, err)
-
- def printErrors(self):
- super(TestResult, self).printErrors()
- if self.num_invalid_clsid:
- self.stream.writeln("SKIPPED: %d tests due to missing COM objects used for testing" %
- self.num_invalid_clsid)
-
- class TestRunner(unittest.TextTestRunner):
- def _makeResult(self):
- return TestResult(self.stream, self.descriptions, self.verbosity)
# Utilities to set the win32com logger to something what just captures
--- 146,149 ----
***************
*** 331,344 ****
return "exec: " + cmd_repr
! class TestProgram(unittest.TestProgram):
! def runTests(self):
! if self.testRunner is None:
! self.testRunner = TestRunner(verbosity=self.verbosity)
! unittest.TestProgram(self)
!
def testmain(*args, **kw):
! new_kw = kw.copy()
! if 'testLoader' not in new_kw:
! new_kw['testLoader'] = TestLoader()
! unittest.main(*args, **new_kw)
CheckClean()
--- 221,226 ----
return "exec: " + cmd_repr
!
def testmain(*args, **kw):
! pywin32_testutil.testmain(*args, **new_kw)
CheckClean()
Index: testShell.py
===================================================================
RCS file: /cvsroot/pywin32/pywin32/com/win32com/test/testShell.py,v
retrieving revision 1.10.4.4
retrieving revision 1.10.4.5
diff -C2 -d -r1.10.4.4 -r1.10.4.5
*** testShell.py 27 Nov 2008 11:31:05 -0000 1.10.4.4
--- testShell.py 7 Jan 2009 06:25:15 -0000 1.10.4.5
***************
*** 16,19 ****
--- 16,20 ----
import win32com.test.util
+ from pywin32_testutil import str2bytes
class ShellTester(win32com.test.util.TestCase):
***************
*** 75,94 ****
def testPIDL(self):
# A PIDL of "\1" is: cb pidl cb
! expect = b"\03\00" b"\1" b"\0\0"
! self.assertEqual(shell.PIDLAsString([b"\1"]), expect)
! self._rtPIDL([b"\0"])
! self._rtPIDL([b"\1", b"\2", b"\3"])
! self._rtPIDL([b"\0" * 2048] * 2048)
# PIDL must be a list
self.assertRaises(TypeError, shell.PIDLAsString, "foo")
def testCIDA(self):
! self._rtCIDA([b"\0"], [ [b"\0"] ])
! self._rtCIDA([b"\1"], [ [b"\2"] ])
! self._rtCIDA([b"\0"], [ [b"\0"], [b"\1"], [b"\2"] ])
def testBadShortPIDL(self):
# A too-short child element: cb pidl cb
! pidl = b"\01\00" b"\1"
self.assertRaises(ValueError, shell.StringAsPIDL, pidl)
--- 76,95 ----
def testPIDL(self):
# A PIDL of "\1" is: cb pidl cb
! expect = str2bytes("\03\00" "\1" "\0\0")
! self.assertEqual(shell.PIDLAsString([str2bytes("\1")]), expect)
! self._rtPIDL([str2bytes("\0")])
! self._rtPIDL([str2bytes("\1"), str2bytes("\2"), str2bytes("\3")])
! self._rtPIDL([str2bytes("\0") * 2048] * 2048)
# PIDL must be a list
self.assertRaises(TypeError, shell.PIDLAsString, "foo")
def testCIDA(self):
! self._rtCIDA([str2bytes("\0")], [ [str2bytes("\0")] ])
! self._rtCIDA([str2bytes("\1")], [ [str2bytes("\2")] ])
! self._rtCIDA([str2bytes("\0")], [ [str2bytes("\0")], [str2bytes("\1")], [str2bytes("\2")] ])
def testBadShortPIDL(self):
# A too-short child element: cb pidl cb
! pidl = str2bytes("\01\00" "\1")
self.assertRaises(ValueError, shell.StringAsPIDL, pidl)
***************
*** 182,186 ****
self.src_name = os.path.join(tempfile.gettempdir(), "pywin32_testshell")
self.dest_name = os.path.join(tempfile.gettempdir(), "pywin32_testshell_dest")
! self.test_data = b"Hello from\0Python"
f=open(self.src_name, "wb")
f.write(self.test_data)
--- 183,187 ----
self.src_name = os.path.join(tempfile.gettempdir(), "pywin32_testshell")
self.dest_name = os.path.join(tempfile.gettempdir(), "pywin32_testshell_dest")
! self.test_data = str2bytes("Hello from\0Python")
f=open(self.src_name, "wb")
f.write(self.test_data)
Index: testClipboard.py
===================================================================
RCS file: /cvsroot/pywin32/pywin32/com/win32com/test/testClipboard.py,v
retrieving revision 1.2.4.2
retrieving revision 1.2.4.3
diff -C2 -d -r1.2.4.2 -r1.2.4.3
*** testClipboard.py 27 Nov 2008 04:58:41 -0000 1.2.4.2
--- testClipboard.py 7 Jan 2009 06:25:15 -0000 1.2.4.3
***************
*** 6,9 ****
--- 6,11 ----
import win32clipboard
+ from pywin32_testutil import str2bytes
+
from win32com.server.util import NewEnum, wrap
from win32com.server.exception import COMException
***************
*** 47,51 ****
ret_stg = pythoncom.STGMEDIUM()
# ensure always 'bytes' by encoding string.
! ret_stg.set(pythoncom.TYMED_HGLOBAL, self.strval.encode("ascii"))
elif cf == win32con.CF_UNICODETEXT:
ret_stg = pythoncom.STGMEDIUM()
--- 49,53 ----
ret_stg = pythoncom.STGMEDIUM()
# ensure always 'bytes' by encoding string.
! ret_stg.set(pythoncom.TYMED_HGLOBAL, str2bytes(self.strval))
elif cf == win32con.CF_UNICODETEXT:
ret_stg = pythoncom.STGMEDIUM()
***************
*** 111,116 ****
win32clipboard.OpenClipboard()
got = win32clipboard.GetClipboardData(win32con.CF_TEXT)
! # CF_TEXT gives bytes on py3k - use encode() to ensure that's true.
! expected = "Hello from Python".encode("ascii")
self.assertEqual(got, expected)
# Now check unicode
--- 113,118 ----
win32clipboard.OpenClipboard()
got = win32clipboard.GetClipboardData(win32con.CF_TEXT)
! # CF_TEXT gives bytes on py3k - use str2bytes() to ensure that's true.
! expected = str2bytes("Hello from Python")
self.assertEqual(got, expected)
# Now check unicode
***************
*** 121,125 ****
def testWin32ToCom(self):
# Set the data via the std win32 clipboard functions.
! val = "Hello again!".encode("ascii") # ensure always bytes, even in py3k
win32clipboard.OpenClipboard()
win32clipboard.SetClipboardData(win32con.CF_TEXT, val)
--- 123,127 ----
def testWin32ToCom(self):
# Set the data via the std win32 clipboard functions.
! val = str2bytes("Hello again!") # ensure always bytes, even in py3k
win32clipboard.OpenClipboard()
win32clipboard.SetClipboardData(win32con.CF_TEXT, val)
***************
*** 133,137 ****
# knowing if it meant to be a string, or a binary buffer, so
# it must return it too.
! self.failUnlessEqual(got, "Hello again!\0".encode("ascii"))
def testDataObjectFlush(self):
--- 135,139 ----
# knowing if it meant to be a string, or a binary buffer, so
# it must return it too.
! self.failUnlessEqual(got, str2bytes("Hello again!\0"))
def testDataObjectFlush(self):
|