ó
[³XMc           @   sÀ   d  Z  d d l Z d d l Z d d l m Z d d l m Z d d l m Z m Z d d l	 m
 Z
 d e f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   s?   
Tests for Trial's interaction with the Python warning system.
iÿÿÿÿN(   t   StringIO(   t   FilePath(   t   TestCaset   _collectWarnings(   t
   TestResultt   Maskc           B   s$   e  Z d  Z d e f d „  ƒ  YZ RS(   sS   
    Hide a L{TestCase} definition from trial's automatic discovery mechanism.
    t	   MockTestsc           B   s,   e  Z d  Z d Z e Z d „  Z d „  Z RS(   s   
        A test case which is used by L{FlushWarningsTests} to verify behavior
        which cannot be verified by code inside a single test method.
        s   some warning textc         C   s   t  j |  j |  j ƒ d S(   s@   
            Generate a warning and don't flush it.
            N(   t   warningst   warnt   messaget   category(   t   self(    (    sC   /usr/lib/python2.7/dist-packages/twisted/trial/test/test_warning.pyt   test_unflushed   s    c         C   s6   t  j |  j |  j ƒ |  j t |  j ƒ  ƒ d ƒ d S(   s:   
            Generate a warning and flush it.
            i   N(   R   R   R	   R
   t   assertEqualt   lent   flushWarnings(   R   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/trial/test/test_warning.pyt   test_flushed"   s    (   t   __name__t
   __module__t   __doc__R	   t   UserWarningR
   R   R   (    (    (    sC   /usr/lib/python2.7/dist-packages/twisted/trial/test/test_warning.pyR      s
   	(   R   R   R   R   R   (    (    (    sC   /usr/lib/python2.7/dist-packages/twisted/trial/test/test_warning.pyR      s   t   FlushWarningsTestsc           B   sž   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   sn   
    Tests for L{TestCase.flushWarnings}, an API for examining the warnings
    emitted so far in a test.
    c         C   s5   x. | j  ƒ  D]  \ } } |  j | | | ƒ q Wd S(   s   
        Assert that all the keys present in C{subset} are also present in
        C{set} and that the corresponding values are equal.
        N(   t	   iteritemsR   (   R   t   sett   subsett   kt   v(    (    sC   /usr/lib/python2.7/dist-packages/twisted/trial/test/test_warning.pyt   assertDictSubset1   s    c         C   sP   |  j  t | ƒ t | ƒ ƒ x- t | | ƒ D] \ } } |  j | | ƒ q, Wd S(   sµ   
        For each pair of corresponding elements in C{sets} and C{subsets},
        assert that the element from C{subsets} is a subset of the element from
        C{sets}.
        N(   R   R   t   zipR   (   R   t   setst   subsetst   at   b(    (    sC   /usr/lib/python2.7/dist-packages/twisted/trial/test/test_warning.pyt   assertDictSubsets:   s    c         C   s   |  j  |  j ƒ  g  ƒ d S(   sp   
        If no warnings are emitted by a test, L{TestCase.flushWarnings} returns
        an empty list.
        N(   R   R   (   R   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/trial/test/test_warning.pyt	   test_noneE   s    c         C   s€   d } t  } t j d | d | ƒ d } t } t j d | d | ƒ |  j |  j ƒ  i | d 6| d 6i | d 6| d 6g ƒ d S(   s…   
        If several warnings are emitted by a test, L{TestCase.flushWarnings}
        returns a list containing all of them.
        s   first warning messageR	   R
   s   second warning messageN(   R   R   R   t   RuntimeWarningR!   R   (   R   t   firstMessaget   firstCategoryt   secondMessaget   secondCategory(    (    sC   /usr/lib/python2.7/dist-packages/twisted/trial/test/test_warning.pyt   test_severalM   s    	c         C   sh   d } t  } x* t d ƒ D] } t j d | d | ƒ q W|  j |  j ƒ  i | d 6| d 6g d ƒ d S(   s“   
        The same warning triggered twice from the same place is included twice
        in the list returned by L{TestCase.flushWarnings}.
        s   the messagei   R	   R
   N(   R#   t   rangeR   R   R!   R   (   R   R	   R
   t   i(    (    sC   /usr/lib/python2.7/dist-packages/twisted/trial/test/test_warning.pyt   test_repeated`   s    	c         C   sc   d } t  } t j d | d | ƒ |  j |  j ƒ  i | d 6| d 6g ƒ |  j |  j ƒ  g  ƒ d S(   s’   
        After a particular warning event has been returned by
        L{TestCase.flushWarnings}, it is not returned by subsequent calls.
        s   the messageR	   R
   N(   R#   R   R   R!   R   R   (   R   R	   R
   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/trial/test/test_warning.pyt   test_clearedo   s    	c         C   sÜ   t  ƒ  } t j d ƒ } | j | ƒ |  j t j j g ƒ } |  j | d d d ƒ |  j | d d t ƒ | j j	 j
 } | j } | j d } |  j | d d | ƒ |  j | d d | ƒ |  j t | ƒ d	 ƒ d
 S(   sx   
        Any warnings emitted by a test which are not flushed are emitted to the
        Python warning system.
        R   i    R	   s   some warning textR
   i   t   filenamet   linenoi   N(   R   R   R   t   runR   R   R   t   assertIdenticalR   t   im_funct	   func_codet   co_filenamet   co_firstlinenoR   (   R   t   resultt   caset   warningsShownt   whereR-   R.   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/trial/test/test_warning.pyR   }   s    		c         C   sg   t  ƒ  } t j d ƒ } t ƒ  } |  j t d | ƒ } | j | ƒ | j ƒ  |  j | j	 ƒ  d ƒ d S(   sx   
        Any warnings emitted by a test which are flushed are not emitted to the
        Python warning system.
        R   t   stdoutt    N(
   R   R   R   R    t   patcht   sysR/   t   restoreR   t   getvalue(   R   R5   R6   t   outputt   monkey(    (    sC   /usr/lib/python2.7/dist-packages/twisted/trial/test/test_warning.pyR   •   s    		
c         C   s½   d t  f d „  ƒ  Y} t ƒ  } t j d ƒ } | | _ t j } zj t j d ƒ | j | ƒ |  j	 t
 | j ƒ d ƒ |  j | j d d | ƒ | j d d j | ƒ Wd | t j (Xd S(   s¾   
        If a warnings filter has been installed which turns warnings into
        exceptions, tests have an error added to the reporter for them for each
        unflushed warning.
        t   CustomWarningc           B   s   e  Z RS(    (   R   R   (    (    (    sC   /usr/lib/python2.7/dist-packages/twisted/trial/test/test_warning.pyRA   ©   s   R   t   errori   i    N(   t   WarningR   R   R   R
   R   t   filterst   simplefilterR/   R   R   t   errorsR0   t   trap(   R   RA   R5   R6   t   originalWarnings(    (    sC   /usr/lib/python2.7/dist-packages/twisted/trial/test/test_warning.pyt   test_warningsConfiguredAsErrors£   s    		
c         C   s„   d t  f d „  ƒ  Y} t ƒ  } t j d ƒ } | | _ t j } z1 t j d ƒ | j | ƒ |  j	 | j
 g  ƒ Wd | t j (Xd S(   sÊ   
        If a warnings filter has been installed which turns warnings into
        exceptions, tests which emit those warnings but flush them do not have
        an error added to the reporter.
        RA   c           B   s   e  Z RS(    (   R   R   (    (    (    sC   /usr/lib/python2.7/dist-packages/twisted/trial/test/test_warning.pyRA   Á   s   R   RB   N(   RC   R   R   R   R
   R   RD   RE   R/   R   RF   (   R   RA   R5   R6   RH   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/trial/test/test_warning.pyt&   test_flushedWarningsConfiguredAsErrors»   s    		
c         C   sV   t  j d ƒ |  j t |  j ƒ  ƒ d ƒ t  j d ƒ |  j t |  j ƒ  ƒ d ƒ d S(   s•   
        Any warnings emitted after a call to L{TestCase.flushWarnings} can be
        flushed by another call to L{TestCase.flushWarnings}.
        s   first messagei   s   second messageN(   R   R   R   R   R   (   R   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/trial/test/test_warning.pyt   test_multipleFlushesÑ   s    c            s®   d ‰  t  ‰ ‡  ‡ f d †  } d ‰ t ‰ ‡ ‡ f d †  } | ƒ  | ƒ  |  j |  j d | g ƒ i ˆ d 6ˆ  d 6g ƒ |  j |  j d | g ƒ i ˆ d 6ˆ d 6g ƒ d S(	   sî   
        The list returned by L{TestCase.flushWarnings} includes only those
        warnings which refer to the source of the function passed as the value
        for C{offendingFunction}, if a value is passed for that parameter.
        s   first warning textc              s   t  j ˆ  ˆ d d ƒd  S(   Nt
   stackleveli   (   R   R   (    (   R$   R%   (    sC   /usr/lib/python2.7/dist-packages/twisted/trial/test/test_warning.pyt   oneä   s    s	   some textc              s   t  j ˆ ˆ  d d ƒd  S(   NRL   i   (   R   R   (    (   R'   R&   (    sC   /usr/lib/python2.7/dist-packages/twisted/trial/test/test_warning.pyt   twoé   s    t   offendingFunctionsR
   R	   N(   R   R#   R!   R   (   R   RM   RN   (    (   R$   R&   R'   R%   sC   /usr/lib/python2.7/dist-packages/twisted/trial/test/test_warning.pyt   test_filterOnOffendingFunctionÜ   s    c         C   s9   d „  } | ƒ  |  j  t |  j d | g ƒ ƒ d ƒ d S(   s   
        Verify that warnings emitted at the very edges of a function are still
        determined to be emitted from that function.
        c           S   s+   t  j d ƒ t  j d ƒ t  j d ƒ d  S(   Ns   first line warnings   internal line warnings   last line warning(   R   R   (    (    (    sC   /usr/lib/python2.7/dist-packages/twisted/trial/test/test_warning.pyt   warnerü   s    RO   i   N(   R   R   R   (   R   RQ   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/trial/test/test_warning.pyt   test_functionBoundaries÷   s    	c         C   sF   t  j d ƒ |  j t |  j d g ƒ |  j t |  j ƒ  ƒ d ƒ d S(   sØ   
        If an object which is neither a function nor a method is included in
        the C{offendingFunctions} list, L{TestCase.flushWarnings} raises
        L{ValueError}.  Such a call flushes no warnings.
        s   oh noi   N(   R   R   t   assertRaisest
   ValueErrorR   t   NoneR   R   (   R   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/trial/test/test_warning.pyt   test_invalidFilter  s    c         C   s  t  |  j ƒ  ƒ j d ƒ } | j ƒ  | j d ƒ j d ƒ | j d ƒ j d ƒ t j j d | j ƒ  j ƒ |  j	 t j j
 | j ƒ  j ƒ d d l m } |  j	 t j j d ƒ |  j	 t j j | j ƒ | j d ƒ j
 ƒ  | j ƒ  |  j t |  j | j g ƒ ƒ d	 ƒ d
 S(   sx   
        Warnings emitted by a function the source code of which is not
        available can still be flushed.
        t   twisted_private_helpers   __init__.pyR:   s   missingsourcefile.pys7   
import warnings
def foo():
    warnings.warn("oh no")
i    iÿÿÿÿ(   t   missingsourcefilei   N(   R   t   mktempt   childt   makedirst
   setContentR<   t   patht   insertt   parentt
   addCleanupt   removeRW   RX   t   modulest   popR   t   fooR   R   R   (   R   t   packageRX   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/trial/test/test_warning.pyt   test_missingSource  s    

c         C   s;  t  |  j ƒ  ƒ j d ƒ } | j ƒ  | j d ƒ j d ƒ | j d ƒ j d ƒ t j j d | j ƒ  j ƒ |  j	 t j j
 | j ƒ  j ƒ d d l m } t j d =t j | j =| j | j d	 ƒ ƒ d d l m } |  j	 t j j d	 ƒ |  j	 t j j | j ƒ | j ƒ  |  j t |  j | j g ƒ ƒ d
 ƒ d S(   s
  
        Warnings emitted by a function defined in a file which has been renamed
        since it was initially compiled can still be flushed.

        This is testing the code which specifically supports working around the
        unfortunate behavior of CPython to write a .py source file name into
        the .pyc files it generates and then trust that it is correct in
        various places.  If source files are renamed, .pyc files may not be
        regenerated, but they will contain incorrect filenames.
        RW   s   __init__.pyR:   s	   module.pys7   
import warnings
def foo():
    warnings.warn("oh no")
i    iÿÿÿÿ(   t   modulet   twisted_renamed_helperi   N(   R   RY   RZ   R[   R\   R<   R]   R^   R_   R`   Ra   RW   Rg   Rb   R   t   moveTot   siblingRh   Rc   Rd   R   R   R   (   R   Re   Rg   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/trial/test/test_warning.pyt   test_renamedSource)  s     


(   R   R   R   R   R!   R"   R(   R+   R,   R   R   RI   RJ   RK   RP   RR   RV   Rf   Rk   (    (    (    sC   /usr/lib/python2.7/dist-packages/twisted/trial/test/test_warning.pyR   +   s"   																t   FakeWarningc           B   s   e  Z RS(    (   R   R   (    (    (    sC   /usr/lib/python2.7/dist-packages/twisted/trial/test/test_warning.pyRl   W  s   t   CollectWarningsTestsc           B   s;   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s(   
    Tests for L{_collectWarnings}.
    c            s´   d ‰  ˆ  d d d … ‰ g  ‰ ‡  ‡ ‡ f d †  } t  ˆ j | ƒ |  j ˆ d d ƒ |  j ˆ d j ˆ  ƒ |  j ˆ d j ˆ ƒ |  j ˆ d	 d
 ƒ |  j t ˆ ƒ d ƒ d S(   sS   
        L{_collectWarnings} calls the observer with each emitted warning.
        s   dummy calls observer warningNiÿÿÿÿc              s8   ˆ j  d ƒ t j ˆ  ƒ t j ˆ ƒ ˆ j  d ƒ d  S(   Nt   callt	   returning(   t   appendR   R   (    (   R$   R&   t   events(    sC   /usr/lib/python2.7/dist-packages/twisted/trial/test/test_warning.pyt   fg  s    i    Rn   i   i   i   Ro   i   (   R   Rp   R   R	   R   (   R   Rr   (    (   R$   R&   Rq   sC   /usr/lib/python2.7/dist-packages/twisted/trial/test/test_warning.pyt   test_callsObserver`  s    c         C   sL   t  ƒ  } |  j t d | ƒ t d „  t j d ƒ |  j | j ƒ  d ƒ d S(   s”   
        Any warnings emitted by a call to a function passed to
        L{_collectWarnings} are not actually emitted to the warning system.
        R9   c         S   s   d  S(   N(   RU   (   t   x(    (    sC   /usr/lib/python2.7/dist-packages/twisted/trial/test/test_warning.pyt   <lambda>}  s    t   textR:   N(   R    R;   R<   R   R   R   R   R>   (   R   R?   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/trial/test/test_warning.pyt   test_suppressesv  s    	c      	      s€   g  ‰ t  ƒ  ‰  ‡ ‡  f d †  } t d „  | d d d d d d ƒ} |  j ˆ d
 i d d 6d d 6f g ƒ |  j | ˆ  ƒ d	 S(   s€   
        L{_collectWarnings} returns the result of calling the callable passed to
        it with the parameters given.
        c             s   ˆ  j  |  | f ƒ ˆ S(   N(   Rp   (   t   argst   kwargs(   t	   argumentst   value(    sC   /usr/lib/python2.7/dist-packages/twisted/trial/test/test_warning.pyRr   ‰  s    c         S   s   d  S(   N(   RU   (   Rt   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/trial/test/test_warning.pyRu     s    i   R   R    i   t   ct   dN(   i   R   (   t   objectR   R   R0   (   R   Rr   R5   (    (   R{   Rz   sC   /usr/lib/python2.7/dist-packages/twisted/trial/test/test_warning.pyt   test_callsFunction  s    	$'c         C   sƒ   b  d „  } t j d ƒ | ƒ  g  } t | j | ƒ |  j t | ƒ d ƒ |  j | d j d ƒ |  j t |  j ƒ  ƒ d ƒ d S(   sì   
        Subsequent emissions of a warning from a particular source site can be
        collected by L{_collectWarnings}.  In particular, the per-module
        emitted-warning cache should be bypassed (I{__warningregistry__}).
        c           S   s   t  j d ƒ d  S(   NRd   (   R   R   (    (    (    sC   /usr/lib/python2.7/dist-packages/twisted/trial/test/test_warning.pyRr      s    t   defaulti   i    Rd   N(	   t   __warningregistry__R   RE   R   Rp   R   R   R	   R   (   R   Rr   Rq   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/trial/test/test_warning.pyt   test_duplicateWarningCollected’  s    	c         C   s:   t  ƒ  } | t j | <|  j t j j | ƒ |  j ƒ  d S(   s±   
        L{_collectWarnings}'s behavior is not altered by the presence of an
        object which cannot have attributes set on it as a value in
        C{sys.modules}.
        N(   R~   R<   Rb   R`   Rc   R‚   (   R   t   key(    (    sC   /usr/lib/python2.7/dist-packages/twisted/trial/test/test_warning.pyt   test_immutableObject«  s    	(   R   R   R   Rs   Rw   R   R‚   R„   (    (    (    sC   /usr/lib/python2.7/dist-packages/twisted/trial/test/test_warning.pyRm   \  s   				(   R   R<   R   R    t   twisted.python.filepathR   t   twisted.trial.unittestR   R   t   twisted.trial.reporterR   R~   R   R   RC   Rl   Rm   (    (    (    sC   /usr/lib/python2.7/dist-packages/twisted/trial/test/test_warning.pyt   <module>   s   ÿ -