B
    v9a{  ã               @   sf   d dl Z d dlmZ d dlmZ d dlmZmZ G dd„ dƒZG dd„ deeƒZ	G d	d
„ d
eeƒZ
dS )é    N)ÚStringIO)ÚOrderedDict)ÚPyTestÚCTestc               @   sl   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ ZdS )Ú
TestDecodec             C   s8   | j dtjd}|  t|tjƒ¡ |  |t d¡¡ d S )Nz1.1)Zparse_float)ÚloadsÚdecimalZDecimalÚ
assertTrueÚ
isinstanceÚassertEqual)ÚselfÚrval© r   ú!/usr/lib/python3.7/test_decode.pyÚtest_decimal   s    zTestDecode.test_decimalc             C   s.   | j dtd}|  t|tƒ¡ |  |d¡ d S )NÚ1)Z	parse_intg      ð?)r   Úfloatr	   r
   r   )r   r   r   r   r   Ú
test_float   s    zTestDecode.test_floatc             C   s:   |   |  d¡i ¡ |   |  d¡g ¡ |   |  d¡d¡ d S )Nz{}z[]z""Ú )r   r   )r   r   r   r   Útest_empty_objects   s    zTestDecode.test_empty_objectsc             C   sò   d}dddddddg}|   |  |¡t|ƒ¡ |   | j|d	d
„ d|¡ |   | jjt|ƒdd
„ d|¡ | j|td}|   |t|ƒ¡ |   t|ƒt¡ |   | j|tdd
„ dt|ƒ¡ |   | jdtdtƒ ¡ |   | jdtdtdtƒ fgƒ¡ d S )Nz?{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7})Zxkdé   )Zkcwé   )Zarté   )Zhxmé   )Zqrté   )Zpadé   )Zhoyé   c             S   s   | S )Nr   )Úxr   r   r   Ú<lambda>   ó    z3TestDecode.test_object_pairs_hook.<locals>.<lambda>)Úobject_pairs_hookc             S   s   | S )Nr   )r   r   r   r   r      r   c             S   s   d S )Nr   )r   r   r   r   r   $   r   )r    Zobject_hookz{}z{"empty": {}}Úempty)r   r   ÚevalÚjsonÚloadr   r   Útype)r   ÚsÚpZodr   r   r   Útest_object_pairs_hook   s$    



z!TestDecode.test_object_pairs_hookc             C   s    |   d¡}|  |dddœ¡ d S )Nz,{   "key"    :    "value"    ,  "k":"v"    }ÚvalueÚv)ÚkeyÚk)r   r   )r   r   r   r   r   Útest_decoder_optimizations-   s    
z%TestDecode.test_decoder_optimizationsc             C   sF   ||ƒ}t |d ƒt |d ƒ \}}\}}|  ||¡ |  ||¡ d S )Nr   r   )ÚsortedZassertIs)r   Úsourcer   r   ÚaÚbÚcÚdr   r   r   Úcheck_keys_reuse4   s    "zTestDecode.check_keys_reusec             C   s<   d}|   || j¡ | jj ¡ }|   ||j¡ |  |j¡ d S )Nu2   [{"a_key": 1, "b_Ã©": 2}, {"a_key": 3, "b_Ã©": 4}])r4   r   r#   ÚdecoderÚJSONDecoderÚdecodeZassertFalseZmemo)r   r&   r5   r   r   r   Útest_keys_reuse:   s
    zTestDecode.test_keys_reusec             C   s    d}d}|   | j|| j|¡ d S )Nz
[1, 2, 3]5z
Extra data)ÚassertRaisesRegexÚJSONDecodeErrorr   )r   r&   Úmsgr   r   r   Útest_extra_dataA   s    zTestDecode.test_extra_datac             C   s    d}d}|   | j|| j|¡ d S )Nz	["abc\y"]Úescape)r9   r:   r   )r   r&   r;   r   r   r   Útest_invalid_escapeF   s    zTestDecode.test_invalid_escapec             C   s2   d}x(ddg i d gD ]}|   t|| j|¡ qW d S )Nzthe JSON object must be strr   g…ëQ¸	@)r9   Ú	TypeErrorr   )r   r;   r)   r   r   r   Útest_invalid_input_typeK   s    z"TestDecode.test_invalid_input_typec          	   C   sÂ   d  d¡ d¡}|  | j¡}|  |¡ W d Q R X |  dt|jƒ¡ |  | j¡}| j 	t
|ƒ¡ W d Q R X |  dt|jƒ¡ d d  d¡ d¡¡}|  |  |¡d¡ |  | j 	t
|ƒ¡d¡ d S )Nz[1,2,3]z	utf-8-sigzutf-8ÚBOMz"{}"r   u   ï»¿)Úencoder7   ÚassertRaisesr:   r   ZassertInÚstrZ	exceptionr#   r$   r   Úformatr   )r   Zbom_jsonÚcmZ
bom_in_strr   r   r   Útest_string_with_utf8_bomP   s    z$TestDecode.test_string_with_utf8_bomc             C   s    | j  ¡ }|  t|jdd¡ d S )NZ*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaai°<ÿÿ)r#   r6   rC   Ú
ValueErrorZ
raw_decode)r   r3   r   r   r   Útest_negative_index^   s    
zTestDecode.test_negative_indexN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r(   r-   r4   r8   r<   r>   r@   rG   rI   r   r   r   r   r      s   r   c               @   s   e Zd ZdS )ÚTestPyDecodeN)rJ   rK   rL   r   r   r   r   rM   b   s    rM   c               @   s   e Zd ZdS )ÚTestCDecodeN)rJ   rK   rL   r   r   r   r   rN   c   s    rN   )r   Úior   Úcollectionsr   Ztest.test_jsonr   r   r   rM   rN   r   r   r   r   Ú<module>   s   [