From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from pigeon.gentoo.org ([208.92.234.80] helo=lists.gentoo.org) by finch.gentoo.org with esmtp (Exim 4.60) (envelope-from ) id 1QOPjD-0005eU-HB for garchives@archives.gentoo.org; Mon, 23 May 2011 07:39:03 +0000 Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 569F81C009; Mon, 23 May 2011 07:38:55 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by pigeon.gentoo.org (Postfix) with ESMTP id 167261C009 for ; Mon, 23 May 2011 07:38:55 +0000 (UTC) Received: from pelican.gentoo.org (unknown [66.219.59.40]) (using TLSv1 with cipher ADH-CAMELLIA256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 834F01B401C for ; Mon, 23 May 2011 07:38:54 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by pelican.gentoo.org (Postfix) with ESMTP id E423580505 for ; Mon, 23 May 2011 07:38:48 +0000 (UTC) From: "Michał Górny" To: gentoo-commits@lists.gentoo.org Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Michał Górny" Message-ID: <5df1c8e9c28b7beabf523cea81f1aa5f7cc2a3c3.mgorny@gentoo> Subject: [gentoo-commits] proj/pms-test-suite:master commit in: PMSTestSuite/library/test/, PMSTestSuite/library/ X-VCS-Repository: proj/pms-test-suite X-VCS-Files: PMSTestSuite/library/__init__.py PMSTestSuite/library/case.py PMSTestSuite/library/test/__init__.py PMSTestSuite/library/test/random_test.py X-VCS-Directories: PMSTestSuite/library/test/ PMSTestSuite/library/ X-VCS-Committer: mgorny X-VCS-Committer-Name: Michał Górny X-VCS-Revision: 5df1c8e9c28b7beabf523cea81f1aa5f7cc2a3c3 Date: Mon, 23 May 2011 07:38:48 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: quoted-printable X-Archives-Salt: X-Archives-Hash: b8c24fb2d9b185f141fc9479f9487a5d commit: 5df1c8e9c28b7beabf523cea81f1aa5f7cc2a3c3 Author: Micha=C5=82 G=C3=B3rny gentoo org> AuthorDate: Mon May 23 07:38:46 2011 +0000 Commit: Micha=C5=82 G=C3=B3rny gentoo org> CommitDate: Mon May 23 07:38:46 2011 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=3Dproj/pms-test-suit= e.git;a=3Dcommit;h=3D5df1c8e9 Support loading test cases. --- PMSTestSuite/library/__init__.py | 85 ++++++++++++++= +----- PMSTestSuite/library/case.py | 7 ++ PMSTestSuite/library/test/__init__.py | 5 +- .../library/test/{__init__.py =3D> random_test.py} | 6 +- 4 files changed, 78 insertions(+), 25 deletions(-) diff --git a/PMSTestSuite/library/__init__.py b/PMSTestSuite/library/__in= it__.py index 40db438..322edbd 100644 --- a/PMSTestSuite/library/__init__.py +++ b/PMSTestSuite/library/__init__.py @@ -2,38 +2,81 @@ # (c) 2011 Micha=C5=82 G=C3=B3rny # Released under the terms of the 2-clause BSD license. =20 -class TestLibrary(object): - """ Base class for a test library. """ - pass - -def load_library(name): - """ - Try to load a test library . Instiantiate the first TestLibrary - subclass found there. +""" +>>> l =3D load_library('test') +>>> l # doctest: +ELLIPSIS + +>>> t =3D [x for x in l][0] +>>> t # doctest: +ELLIPSIS + +""" =20 - Returns a new TestLibrary subclass instance or raises an ImportError. +from PMSTestSuite.library.case import TestCase =20 - >>> load_library('test') # doctest: +ELLIPSIS - +def grabmodule(modname, baseclass): """ - # XXX: which exceptions can it raise? + Import Python module and find subclasses of there= . =09 - modname =3D 'PMSTestSuite.library.%s' % name + Returns an iterator over subclasses or raises one of the following + exceptions: + - ImportError if module import fails, + - TypeError if no matching class is found. + """ mod =3D __import__(modname, {}, {}, ['.'], 0) -=09 + fail =3D True + for k in dir(mod): modvar =3D getattr(mod, k) # the !issubclass() check is necessary to omit TestLibrary class # imported for the subclass try: - if issubclass(modvar, TestLibrary) and \ - not issubclass(TestLibrary, modvar): - cls =3D modvar - break + if issubclass(modvar, baseclass) and \ + not issubclass(baseclass, modvar): + fail =3D False + yield modvar except TypeError: pass - else: - raise ImportError('Unable to find a TestLibrary subclass in %s' + + if fail: + raise TypeError('Unable to find a %s subclass in %s' + % (baseclass.__name__, modname)) + +class TestLibrary(object): + """ Base class for a test library. """ + + def __iter__(self): + """ + Iterate over all the tests in a test library, loading its modules + as necessary. Uses to get the module names. + + Returns an iterator over TestCase subclass instances or raises one + of the following exceptions: + - ImportError if submodule import fails, + - TypeError if no TestCase subclass is found in a submodule. + """ + for t in self.test_names: + cls =3D grabmodule('%s.%s' % (self.modname, t), TestCase) + for c in cls: + yield c() + + def __init__(self, modname): + self.modname =3D modname + +def load_library(name): + """ + Try to load a test library . Instiantiate the first TestLibrary + subclass found there. + + Returns a new TestLibrary subclass instance or raises one of the follow= ing + exceptions: + - ImportError if module import fails, + - TypeError if no matching class is found. + """ + + modname =3D 'PMSTestSuite.library.%s' % name + cls =3D list(grabmodule(modname, TestLibrary)) + if len(cls) > 1: + raise TypeError('%s ambiguous - more than a single TestLibrary subclas= s found.' % modname) =20 - return cls() + return cls[0](modname) diff --git a/PMSTestSuite/library/case.py b/PMSTestSuite/library/case.py new file mode 100644 index 0000000..a2e60f2 --- /dev/null +++ b/PMSTestSuite/library/case.py @@ -0,0 +1,7 @@ +# vim:fileencoding=3Dutf-8 +# (c) 2011 Micha=C5=82 G=C3=B3rny +# Released under the terms of the 2-clause BSD license. + +class TestCase(object): + """ Base class for a test case. """ + pass diff --git a/PMSTestSuite/library/test/__init__.py b/PMSTestSuite/library= /test/__init__.py index 91afc38..9cfbe39 100644 --- a/PMSTestSuite/library/test/__init__.py +++ b/PMSTestSuite/library/test/__init__.py @@ -8,4 +8,7 @@ class ExampleLibrary(TestLibrary): """ Absolutely random TestLibrary subclass to test it. """ - pass + + test_names=3D[ + 'random_test' + ] diff --git a/PMSTestSuite/library/test/__init__.py b/PMSTestSuite/library= /test/random_test.py similarity index 52% copy from PMSTestSuite/library/test/__init__.py copy to PMSTestSuite/library/test/random_test.py index 91afc38..50acc45 100644 --- a/PMSTestSuite/library/test/__init__.py +++ b/PMSTestSuite/library/test/random_test.py @@ -2,10 +2,10 @@ # (c) 2011 Micha=C5=82 G=C3=B3rny # Released under the terms of the 2-clause BSD license. =20 -from PMSTestSuite.library import TestLibrary +from PMSTestSuite.library.case import TestCase =20 -class ExampleLibrary(TestLibrary): +class ExampleCase(TestCase): """ - Absolutely random TestLibrary subclass to test it. + Absolutely random TestCase subclass to test it. """ pass