Plan 9 from Bell Labs’s /usr/web/sources/contrib/jas/root/sys/lib/python2.7/site-packages/nose/plugins/errorclass.pyc

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


�9�c@s�dZddlmZddlmZddlmZddlmZde	fd��YZ
defd	��YZd
efd��YZ
d�Zd
�Zd�Zd�Zd�Zedkr�ddlZej�ndS(s�
ErrorClass Plugins
------------------

ErrorClass plugins provide an easy way to add support for custom
handling of particular classes of exceptions.

An ErrorClass plugin defines one or more ErrorClasses and how each is
handled and reported on. Each error class is stored in a different
attribute on the result, and reported separately. Each error class must
indicate the exceptions that fall under that class, the label to use
for reporting, and whether exceptions of the class should be
considered as failures for the whole test run.

ErrorClasses use a declarative syntax. Assign an ErrorClass to the
attribute you wish to add to the result object, defining the
exceptions, label and isfailure attributes. For example, to declare an
ErrorClassPlugin that defines TodoErrors (and subclasses of TodoError)
as an error class with the label 'TODO' that is considered a failure,
do this:

    >>> class Todo(Exception):
    ...     pass
    >>> class TodoError(ErrorClassPlugin):
    ...     todo = ErrorClass(Todo, label='TODO', isfailure=True)

The MetaErrorClass metaclass translates the ErrorClass declarations
into the tuples used by the error handling and reporting functions in
the result. This is an internal format and subject to change; you
should always use the declarative syntax for attaching ErrorClasses to
an ErrorClass plugin.

    >>> TodoError.errorClasses # doctest: +ELLIPSIS
    ((<class ...Todo...>, ('todo', 'TODO', True)),)

Let's see the plugin in action. First some boilerplate.

    >>> import sys
    >>> import unittest
    >>> try:
    ...     # 2.7+
    ...     from unittest.runner import _WritelnDecorator
    ... except ImportError:
    ...     from unittest import _WritelnDecorator
    ...
    >>> buf = _WritelnDecorator(sys.stdout)

Now define a test case that raises a Todo.

    >>> class TestTodo(unittest.TestCase):
    ...     def runTest(self):
    ...         raise Todo("I need to test something")
    >>> case = TestTodo()

Prepare the result using our plugin. Normally this happens during the
course of test execution within nose -- you won't be doing this
yourself. For the purposes of this testing document, I'm stepping
through the internal process of nose so you can see what happens at
each step.

    >>> plugin = TodoError()
    >>> from nose.result import _TextTestResult
    >>> result = _TextTestResult(stream=buf, descriptions=0, verbosity=2)
    >>> plugin.prepareTestResult(result)

Now run the test. TODO is printed.

    >>> _ = case(result) # doctest: +ELLIPSIS
    runTest (....TestTodo) ... TODO: I need to test something

Errors and failures are empty, but todo has our test:

    >>> result.errors
    []
    >>> result.failures
    []
    >>> result.todo # doctest: +ELLIPSIS
    [(<....TestTodo testMethod=runTest>, '...Todo: I need to test something\n')]
    >>> result.printErrors() # doctest: +ELLIPSIS
    <BLANKLINE>
    ======================================================================
    TODO: runTest (....TestTodo)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
    ...
    ...Todo: I need to test something
    <BLANKLINE>

Since we defined a Todo as a failure, the run was not successful.

    >>> result.wasSuccessful()
    False
i�(tmake_instancemethod(tPlugin(tTextTestResult(tisclasstMetaErrorClasscBseZdZd�ZRS(shMetaclass for ErrorClassPlugins that allows error classes to be
    set up in a declarative manner.
    cCs�g}xl|j�D]^\}}t|t�r|j|�x3|D](}|j|||j|jff�qBWqqWtt|�j	|||�t
|�|_dS(N(titemst
isinstancet
ErrorClasstpoptappendtlabelt	isfailuretsuperRt__init__ttupleterrorClasses(tselftnametbasestattrRtdetailtcls((s;/sys/lib/python2.7/site-packages/nose/plugins/errorclass.pyR
hs

'(t__name__t
__module__t__doc__R
(((s;/sys/lib/python2.7/site-packages/nose/plugins/errorclass.pyRdsRcBseZd�Zd�ZRS(cOs_||_y.x'dD]}t|||j|��qWWn!tk
rZtd|��nXdS(NR
Rs.%r is a required named argument for ErrorClass(slabels	isfailure(RtsetattrRtKeyErrort	TypeError(RRtkwtkey((s;/sys/lib/python2.7/site-packages/nose/plugins/errorclass.pyR
us	
!
cCs
t|j�S(N(titerR(R((s;/sys/lib/python2.7/site-packages/nose/plugins/errorclass.pyt__iter__~s(RRR
R(((s;/sys/lib/python2.7/site-packages/nose/plugins/errorclass.pyRts		tErrorClassPlugincBs;eZdZeZdZdZd�Zd�Zd�Z	RS(s�
    Base class for ErrorClass plugins. Subclass this class and declare the
    exceptions that you wish to handle as attributes of the subclass.
    i�cs_|\�}}t��sdSg|jD]}|d^q)}t�fd�|�r[tSdS(Nics
t�|�S(N(t
issubclass(tc(terr_cls(s;/sys/lib/python2.7/site-packages/nose/plugins/errorclass.pyt<lambda>�s(RRtfiltertTrue(Rttestterrtatbtetclasses((R#s;/sys/lib/python2.7/site-packages/nose/plugins/errorclass.pytaddError�s cCs�t|d�s|j|�nxj|jD]_\}\}}}||jkr)t||g�}t|||�|||f|j|<q)q)WdS(NR(thasattrtpatchResultRtgetattrR(RtresultRtstorage_attrR
tisfailtstorage((s;/sys/lib/python2.7/site-packages/nose/plugins/errorclass.pytprepareTestResult�scCs�t|�|_|jt|�|_|_|jt|�|_|_t|d�ru|j	t
|�|_|_	nt|d�r�|jt
|�|_|_ni|_dS(NtprintErrorstaddSkip(tprint_label_patcht
printLabelR-tadd_error_patcht_orig_addErrort
wasSuccessfultwassuccessful_patcht_orig_wasSuccessfulR.R6tprint_errors_patcht_orig_printErrorsR7tadd_skip_patcht
_orig_addSkipR(RR1((s;/sys/lib/python2.7/site-packages/nose/plugins/errorclass.pyR/�s((
RRRRt
__metaclass__tscoreRR-R5R/(((s;/sys/lib/python2.7/site-packages/nose/plugins/errorclass.pyR �s			cCsttj|�S(s�Create a new addError method to patch into a result instance
    that recognizes the errorClasses attribute and deals with
    errorclasses correctly.
    (RRR-(R1((s;/sys/lib/python2.7/site-packages/nose/plugins/errorclass.pyR:�scCsttj|�S(sPCreate a new printErrors method that prints errorClasses items
    as well.
    (RRR6(R1((s;/sys/lib/python2.7/site-packages/nose/plugins/errorclass.pyR?�scCsttj|�S(sOCreate a new printLabel method that prints errorClasses items
    as well.
    (RRR9(R1((s;/sys/lib/python2.7/site-packages/nose/plugins/errorclass.pyR8�scCsttj|�S(s�Create a new wasSuccessful method that checks errorClasses for
    exceptions that were put into other slots than error or failure
    but that still count as not success.
    (RRR<(R1((s;/sys/lib/python2.7/site-packages/nose/plugins/errorclass.pyR=�scCsttj|�S(s`Create a new addSkip method to patch into a result instance
    that delegates to addError.
    (RRR7(R1((s;/sys/lib/python2.7/site-packages/nose/plugins/errorclass.pyRA�st__main__N(Rtnose.pyversionRtnose.plugins.baseRtnose.resultRt	nose.utilRttypeRtobjectRR R:R?R8R=RARtdoctestttestmod(((s;/sys/lib/python2.7/site-packages/nose/plugins/errorclass.pyt<module>]s)					

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].