U
    1e9                     @   s  d Z ddlmZmZmZmZmZ ddlZddlZddl	Z	ddl
Z
ddlZddlZddlZddlmZ ddlmZ G dd deZG dd	 d	eZG d
d deZG dd deZG dd deZG dd deZde_G dd deZG dd deZG dd deZdd Zdd Zdd Zej !de"e# e$ e#dge$dge#dd ge$dd ge#d!d"d#ggd$d%d&d'd(d)d*gd+d, Z%d-d. Z&d/d0 Z'd1d2 Z(d3d4 Z)d5d6 Z*G d7d8 d8e+Z,G d9d: d:eZ-d;d< Z.G d=d> d>eZ/G d?d@ d@e/Z0G dAdB dBej1Z2G dCdD dDe3Z4e4dEZ5dFdG Z6dHdI Z7dJdK Z8dLdM Z9dNdO Z:dPdQ Z;dRdS Z<dTdU Z=dVdW Z>dXdY Z?dZd[ Z@d\d] ZAG d^d_ d_eeZBG d`da dae#ZCdbdc ZDdS )dzTests for IPython.lib.pretty.    )CounterdefaultdictdequeOrderedDictUserListN)pretty)StringIOc                   @   s   e Zd Zdd Zdd ZdS )MyListc                 C   s
   || _ d S N)content)selfr    r   A/tmp/pip-unpacked-wheel-3hxk2k58/IPython/lib/tests/test_pretty.py__init__   s    zMyList.__init__c              	   C   sn   |r| d nZ|dddF t| jD ]4\}}|rJ| d |  n
|d || q*W 5 Q R X d S )NzMyList(...)   zMyList(), )textgroup	enumerater   Z	breakabler   )r   pcycleichildr   r   r   _repr_pretty_   s    


zMyList._repr_pretty_N)__name__
__module____qualname__r   r   r   r   r   r   r	      s   r	   c                   @   s   e Zd Zdd ZdS )MyDictc                 C   s   | d d S )NMyDict(...)r   r   r   r   r   r   r   r   )   s    zMyDict._repr_pretty_Nr   r   r   r   r   r   r   r   r   (   s   r   c                   @   s   e Zd Zdd ZdS )MyObjc                 C   s   d S r
   r   r   r   r   r   
somemethod-   s    MyObj.somemethodN)r   r   r   r&   r   r   r   r   r$   ,   s   r$   c                   @   s   e Zd Zdd ZdS )Dummy1c                 C   s   | d d S )NDummy1(...)r!   r"   r   r   r   r   2   s    zDummy1._repr_pretty_Nr#   r   r   r   r   r(   1   s   r(   c                   @   s   e Zd ZdZdS )Dummy2Nr#   r   r   r   r   r*   5   s   r*   c                   @   s   e Zd ZdS )NoModuleNr   r   r   r   r   r   r   r+   8   s   r+   c                   @   s   e Zd Zdd ZdS )Breakingc              	   C   s:   | ddd" |d |  |d W 5 Q R X d S )N   zTG: :z	Breaking(r   )r   r   Zbreak_r"   r   r   r   r   >   s    
zBreaking._repr_pretty_Nr#   r   r   r   r   r-   =   s   r-   c                   @   s   e Zd Zdd ZdS )BreakingReprc                 C   s   dS )NzBreaking(
)r   r%   r   r   r   __repr__E   s    zBreakingRepr.__repr__Nr   r   r   r1   r   r   r   r   r0   D   s   r0   c                   @   s   e Zd Zdd ZdS )BadReprc                 C   s   dd S )N   r   r   r%   r   r   r   r1   I   s    zBadRepr.__repr__Nr2   r   r   r   r   r3   H   s   r3   c                  C   sF   d} t  tt| }dddd t| D  d }||ksBtdS )z"Test correct indentation in groups(   zMyList(
z,
c                 s   s   | ]}d | V  qdS )z   %dNr   .0r   r   r   r   	<genexpr>Q   s     z#test_indentation.<locals>.<genexpr>r   N)r   r	   rangejoinAssertionError)count	gotoutputexpectedoutputr   r   r   test_indentationM   s     r?   c                  C   s    t  t } d}| |kstdS )z
    Test correct dispatching: The _repr_pretty_ method for MyDict
    must be found before the registered printer for dict.
    r    N)r   r   r;   r=   r>   r   r   r   test_dispatchV   s    rA   c                  C   s    t  t } d}| |kstdS )z^
    Test that the _repr_pretty_ method is tested for callability and skipped if
    not.
    r)   N)r   r*   r;   r@   r   r   r   test_callability_checkinga   s    rB   zobj,expected_outputr4      zset()zfrozenset()z{1}zfrozenset({1})z{1, 2}zfrozenset({1, 2})z{-3, -2, -1}c                 C   s   t  | }||kstdS )z>
    Test that set and frozenset use Python 3 formatting.
    N)r   r;   )objexpected_outputZ
got_outputr   r   r   	test_setsl   s    
rI   c                  C   sJ   t jdk rdnd} t jdk r dnd}t| }t|j}||ksFtdS )z:
    Test that pprint works for heap allocated types.
    )r   
   	xxlimitedZxxlimited_35)r   rJ      zxxlimited.Nullzxxlimited_35.NullN)sysversion_infopytestZimportorskipr   ZNullr;   )module_namerH   rK   outputr   r   r   test_pprint_heap_allocated_type   s    
rR   c                  C   s   t  t} | dkstdS )z@
    Test that pprint works for classes with no __module__.
    r+   N)r   r+   r;   rQ   r   r   r   test_pprint_nomod   s    
rT   c                  C   s    t  t } d}| |kstdS )z5
    Test that p.break_ produces expected output
    zTG: Breaking(
    ):N)r   r-   r;   rQ   expectedr   r   r   test_pprint_break   s    rW   c                  C   sH   t  t gg} d}| |ks tt  t gd g} d}| |ksDtdS )z,
    Test that p.break_ is used in repr
    z[[Breaking(
  )]]rC   z"[[Breaking(
  ),
  Breaking(
  )]]N)r   r0   r;   rU   r   r   r   test_pprint_break_repr   s    rX   c                	   C   s&   t t tt  W 5 Q R X dS )zDon't catch bad repr errorsN)rO   raisesZeroDivisionErrorr   r3   r   r   r   r   test_bad_repr   s    r[   c                   @   s   e Zd Zdd ZdS )BadExceptionc                 C   s   dS )NrD   r   r%   r   r   r   __str__   s    zBadException.__str__N)r   r   r   r]   r   r   r   r   r\      s   r\   c                   @   s$   e Zd ZdZedd Zdd ZdS )ReallyBadReprr4   c                 C   s   t dd S )NzI am horrible)
ValueErrorr%   r   r   r   	__class__   s    zReallyBadRepr.__class__c                 C   s
   t  d S r
   )r\   r%   r   r   r   r1      s    zReallyBadRepr.__repr__N)r   r   r   propertyr`   r1   r   r   r   r   r^      s   
r^   c                	   C   s&   t t tt  W 5 Q R X d S r
   )rO   rY   r\   r   r^   r   r   r   r   test_really_bad_repr   s    rb   c                   @   s   e Zd ZdS )SANr,   r   r   r   r   rc      s   rc   c                   @   s   e Zd ZdS )SBNr,   r   r   r   r   rd      s   rd   c                       sD   e Zd Z fddZdd Zdd Zdd Zd	d
 Zdd Z  Z	S )TestsPrettyc                    s@   t  tt}| |d t }t  tt|}| |d d S )Nz<super: \S+.SA, None>z$<super: \S+.SA,\s+<\S+.SB at 0x\S+>>)r   superrc   assertRegexrd   )r   rQ   Zsbr`   r   r   test_super_repr   s
    zTestsPretty.test_super_reprc                 C   s>   t td}t|}|dddd  }| |ddg d S )N'  
rC   rE    999,z ...])listr9   r   rsplitassertEqual)r   Zlisr   last2r   r   r   test_long_list   s    
zTestsPretty.test_long_listc                 C   s>   t td}t|}|dddd  }| |ddg d S )Nrj   rk   rC   rE   rl    ...})setr9   r   rn   ro   )r   sr   rp   r   r   r   test_long_set   s    
zTestsPretty.test_long_setc                 C   s>   t td}t|}|dddd  }| |ddg d S )Nrj   rk   rC   rE   rl   z ...))tupler9   r   rn   ro   )r   tupr   rp   r   r   r   test_long_tuple   s    
zTestsPretty.test_long_tuplec                 C   sD   dd t dD }t|}|dddd  }| |ddg d S )	Nc                 S   s   i | ]
}||qS r   r   )r7   nr   r   r   
<dictcomp>   s      z.TestsPretty.test_long_dict.<locals>.<dictcomp>rj   rk   rC   rE   z
 999: 999,rr   )r9   r   rn   ro   )r   dr   rp   r   r   r   test_long_dict   s    
zTestsPretty.test_long_dictc                 C   s   t  tj}| d| d S )Nr'   )r   r$   r&   ZassertIn)r   rQ   r   r   r   test_unbound_method   s    zTestsPretty.test_unbound_method)
r   r   r   ri   rq   ru   rx   r|   r}   __classcell__r   r   rh   r   re      s   re   c                   @   s   e Zd Zdd Zdd ZdS )	MetaClassc                 C   s   t | |tfd|iS )Nname)type__new__object)clsr   r   r   r   r      s    zMetaClass.__new__c                 C   s
   d| j  S )Nz[CUSTOM REPR FOR CLASS %s])r   r%   r   r   r   r1      s    zMetaClass.__repr__N)r   r   r   r   r1   r   r   r   r   r      s   r   ClassWithMetac                  C   s   t  t} | dkstd S )Nz%[CUSTOM REPR FOR CLASS ClassWithMeta])r   r   r;   rS   r   r   r   test_metaclass_repr  s    
r   c                     sX   d} |  G  fdddt }| }t|}|| ks8tt|g}|d|  ksTtd S )Nu
   üniçodéc                       s   e Zd Z fddZdS )ztest_unicode_repr.<locals>.Cc                    s    S r
   r   r%   Zustrr   r   r1     s    z%test_unicode_repr.<locals>.C.__repr__Nr2   r   r   r   r   C  s   r   z[%s])r   r   r;   )ur   cr   r   r   r   test_unicode_repr  s    
r   c                     sh    fdd d _ t } t| } |jt< |t |  |  }|dt	 ksVt
 j dksdt
d S )Nc                    s   | t krd _t| ||S )NT)r$   calledr   Z_type_pprint)rG   r   r   type_pprint_wrapperr   r   r     s    z-test_basic_class.<locals>.type_pprint_wrapperFz%s.MyObjT)r   r   r   ZRepresentationPrinterZtype_pprintersr   r$   flushgetvaluer   r;   )streamprinterrQ   r   r   r   test_basic_class  s    


r   c                  C   s`   t  } | |  t  dft dd tddD df| dfg}|D ]\}}t||ks@tq@d S )NzUserList([])c                 s   s   | ]
}|V  qd S r
   r   r6   r   r   r   r8   4  s     z,test_collections_userlist.<locals>.<genexpr>    a@  UserList([1000,
          1001,
          1002,
          1003,
          1004,
          1005,
          1006,
          1007,
          1008,
          1009,
          1010,
          1011,
          1012,
          1013,
          1014,
          1015,
          1016,
          1017,
          1018,
          1019])zUserList([UserList(...)]))r   appendr9   r   r;   acasesrG   rV   r   r   r   test_collections_userlist,  s    
r   c                  C   sj   t  } | | _t t}||d< t tdft tddidf| df|dfg}|D ]\}}t||ksJtqJd S )Nkeyzdefaultdict(list, {})z2--------------------------------------------------z\defaultdict(list,
            {'key': '--------------------------------------------------'})z!defaultdict(defaultdict(...), {})z,defaultdict(list, {'key': defaultdict(...)}))r   default_factoryrm   r   r;   )r   br   rG   rV   r   r   r   test_collections_defaultdictQ  s    
r   c                  C   s^   t  } | | d< t  dft dd tddD df| dfg}|D ]\}}t||ks>tq>d S )	Nr   zOrderedDict()c                 s   s   | ]}||fV  qd S r
   r   r6   r   r   r   r8   m  s     z/test_collections_ordereddict.<locals>.<genexpr>r   i  a  OrderedDict([(1000, 1000),
             (1001, 1001),
             (1002, 1002),
             (1003, 1003),
             (1004, 1004),
             (1005, 1005),
             (1006, 1006),
             (1007, 1007),
             (1008, 1008),
             (1009, 1009)])z(OrderedDict([('key', OrderedDict(...))]))r   r9   r   r;   r   r   r   r   test_collections_ordereddictf  s    r   c                  C   s`   t  } | |  t  dft dd tddD df| dfg}|D ]\}}t||ks@tq@d S )Nz	deque([])c                 s   s   | ]
}|V  qd S r
   r   r6   r   r   r   r8     s     z)test_collections_deque.<locals>.<genexpr>r   r   a  deque([1000,
       1001,
       1002,
       1003,
       1004,
       1005,
       1006,
       1007,
       1008,
       1009,
       1010,
       1011,
       1012,
       1013,
       1014,
       1015,
       1016,
       1017,
       1018,
       1019])zdeque([deque(...)]))r   r   r9   r   r;   r   r   r   r   test_collections_deque  s    
r   c                  C   sX   G dd dt } t  dft dddf| dddfg}|D ]\}}t||ks8tq8d S )Nc                   @   s   e Zd ZdS )z+test_collections_counter.<locals>.MyCounterNr,   r   r   r   r   	MyCounter  s   r   z	Counter()r4   )r   zCounter({'a': 1})zMyCounter({'a': 1}))r   r   r;   )r   r   rG   rV   r   r   r   test_collections_counter  s    r   c                  C   s   t j} i }| |}||d< ||d< | i df| d | i idf| dd tjD df|df|d	fg}|D ]\}}t||kshtqhd S )
NrC   r   zmappingproxy({})z&mappingproxy({None: mappingproxy({})})c                 S   s   i | ]}||  qS r   )upper)r7   kr   r   r   rz     s      z%test_mappingproxy.<locals>.<dictcomp>ap  mappingproxy({'a': 'A',
              'b': 'B',
              'c': 'C',
              'd': 'D',
              'e': 'E',
              'f': 'F',
              'g': 'G',
              'h': 'H',
              'i': 'I',
              'j': 'J',
              'k': 'K',
              'l': 'L',
              'm': 'M',
              'n': 'N',
              'o': 'O',
              'p': 'P',
              'q': 'Q',
              'r': 'R',
              's': 'S',
              't': 'T',
              'u': 'U',
              'v': 'V',
              'w': 'W',
              'x': 'X',
              'y': 'Y',
              'z': 'Z'})z1mappingproxy({2: {...}, 3: {2: {...}, 3: {...}}})z1{2: mappingproxy({2: {...}, 3: {...}}), 3: {...}})typesMappingProxyTypestringascii_lowercaser   r;   )ZMPZunderlying_dictZmp_recursiver   rG   rV   r   r   r   test_mappingproxy  s"    
"r   c                  C   sx   t j} |  }||_||_|  df| |  ddf| | tjdgd d ddf|dfg}|D ]\}}t||ksXtqXd S )	Nznamespace())xznamespace(x=namespace()))rt   r   )Za_long_nameZa_short_nameznamespace(a_long_name=[namespace(s='abcdefghijklmnopqrstuvwxyz'),
                       namespace(s='abcdefghijklmnopqrstuvwxyz'),
                       namespace(s='abcdefghijklmnopqrstuvwxyz')],
          a_short_name=None)z6namespace(first=namespace(...), second=namespace(...)))r   SimpleNamespacefirstsecondr   r   r   r;   )ZSNZsn_recursiver   rG   rV   r   r   r   test_simplenamespace  s    
r   c                  C   sH   t  ttj} | dddtd  }t  tj}|d| ksDtd S )Nrk    environ)r   dictosr   replacelenr;   )Z	dict_reprZdict_indentedZenv_reprr   r   r   test_pretty_environ  s    r   c                  C   s<   ddl } t| jdkstddd}dt|ks8tdS )zTest pretty print of functionr   Nz <function posixpath.join(a, *p)>c                 S   s   | rdS dS )N*   zDon't panicr   )Zquestionr   r   r   meaning_of_life  s    z-test_function_pretty.<locals>.meaning_of_lifezmeaning_of_life(question=None))N)	posixpathr   r:   r;   )r   r   r   r   r   test_function_pretty  s    
r   c                   @   s    e Zd ZdZdd Zdd ZdS )OrderedCounterz?Counter that remembers the order elements are first encounteredc                 C   s   d| j jt| f S )Nz%s(%r))r`   r   r   r%   r   r   r   r1     s    zOrderedCounter.__repr__c                 C   s   | j t| ffS r
   )r`   r   r%   r   r   r   
__reduce__  s    zOrderedCounter.__reduce__N)r   r   r   __doc__r1   r   r   r   r   r   r   	  s   r   c                   @   s   e Zd Zdd ZdS )MySetc                 C   s   dS )Nminer   r%   r   r   r   r1     s    zMySet.__repr__Nr2   r   r   r   r   r     s   r   c                  C   s2   t d} dt| ksttt dks.tdS )z@A custom repr should override a pretty printer for a parent typeZabracadabrazOrderedCounter(OrderedDictr   N)r   r   r;   r   )ocr   r   r   test_custom_repr  s    r   )Er   collectionsr   r   r   r   r   r   rO   r   r   rM   ZunittestZIPython.libr   ior   r   r	   r   r   r$   r(   r*   r+   r   r-   r0   r3   r?   rA   rB   markZparametrizeziprs   	frozensetrI   rR   rT   rW   rX   r[   	Exceptionr\   r^   rb   rc   rd   ZTestCasere   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s   	



	*%#.	