Update of /cvsroot/ctypes/ctypes/comtypes/unittests
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv28228
Added Files:
test_basic.py runtests.py .cvsignore
Log Message:
comtypes is advanced enough to be checked in.
Not that it really works, though ;-)
--- NEW FILE: .cvsignore ---
test_basic.pyc
--- NEW FILE: test_basic.py ---
import unittest
from ctypes import windll, POINTER, byref
from comtypes import IUnknown
class BasicTest(unittest.TestCase):
def test_IUnknown(self):
from comtypes import IUnknown
self.failUnlessEqual(IUnknown._nummethods_, 3)
def test_release(self):
POINTER(IUnknown)()
def test_refcounts(self):
p = POINTER(IUnknown)()
windll.oleaut32.CreateTypeLib(1, u"blabla", byref(p))
# initial refcount is 2
for i in range(2, 10):
self.failUnlessEqual(p.AddRef(), i)
for i in range(8, 0, -1):
self.failUnlessEqual(p.Release(), i)
def test_qi(self):
p = POINTER(IUnknown)()
windll.oleaut32.CreateTypeLib(1, u"blabla", byref(p))
self.failUnlessEqual(p.AddRef(), 2)
self.failUnlessEqual(p.Release(), 1)
other = p.QueryInterface(IUnknown)
self.failUnlessEqual(other.AddRef(), 3)
self.failUnlessEqual(p.AddRef(), 4)
self.failUnlessEqual(p.Release(), 3)
self.failUnlessEqual(other.Release(), 2)
del p # calls p.Release()
self.failUnlessEqual(other.AddRef(), 2)
self.failUnlessEqual(other.Release(), 1)
def test_derived(self):
class IMyInterface(IUnknown):
pass
self.failUnlessEqual(IMyInterface._nummethods_, 3)
IMyInterface._methods_ = []
self.failUnlessEqual(IMyInterface._nummethods_, 3)
def test_mro(self):
mro = POINTER(IUnknown).__mro__
self.failUnlessEqual(mro[0], POINTER(IUnknown))
self.failUnlessEqual(mro[1], IUnknown)
# the IUnknown class has the actual methods:
self.failUnless(IUnknown.__dict__.get("QueryInterface"))
if __name__ == "__main__":
unittest.main()
--- NEW FILE: runtests.py ---
"""Usage: runtests.py [-q] [-v] [mask]
Run all tests found in this directory, and print a summary of the results.
Command line flags:
-v verbose mode: print the test currently executed
-q quiet mode: don't prnt anything while the tests are running
mask mask to select filenames containing testcases, wildcards allowed
"""
import glob, os, sys, unittest, getopt
def get_suite(mask):
if not mask.endswith(".py"):
mask += ".py"
test_suites = []
for fname in glob.glob(mask):
try:
mod = __import__(os.path.splitext(fname)[0])
except Exception, detail:
print "Warning: could not import %s: %s" % (fname, detail)
continue
for name in dir(mod):
if name.startswith("_"):
continue
o = getattr(mod, name)
if type(o) is type(unittest.TestCase) and issubclass(o, unittest.TestCase):
test_suites.append(unittest.makeSuite(o))
return unittest.TestSuite(test_suites)
def usage():
print __doc__
return 1
def main():
verbosity = 1
mask = "test_*.py"
try:
opts, args = getopt.getopt(sys.argv[1:], "qv")
except getopt.error:
return usage()
for flag, value in opts:
if flag == "-q":
verbosity -= 1
elif flag == "-v":
verbosity += 1
if args:
mask = args[0]
suite = get_suite(mask)
runner = unittest.TextTestRunner(verbosity=verbosity)
return bool(runner.run(suite).errors)
if __name__ == "__main__":
sys.exit(main())
|