o
    >]H                     @   s   d Z ddl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	m
Z
 ddlZddlmZ ddlmZ ddlZdd Zejd	d
G dd deZejd	d
G dd deZejd	ddG dd deZdd ZdS )z=
Python representations of the JSON Schema Test Suite tests.
    )partialN)FilePath)PY3)
validatorsc                  C   sD   t jd} | d urt| S ttj d} |  s t	d| S )NJSON_SCHEMA_TEST_SUITEjsonzCan't find the JSON-Schema-Test-Suite directory. Set the 'JSON_SCHEMA_TEST_SUITE' environment variable or run the tests from alongside a checkout of the suite.)
osenvirongetr   
jsonschema__file__parentsiblingisdir
ValueError)root r   9/usr/lib/python3/dist-packages/jsonschema/tests/_suite.py_find_suite   s   r   T)hashc                   @   s6   e Zd ZejeedZdd Zdd Z	dd Z
dS )	Suite)defaultc                 C   sB   | j ddg}ttj|jdg}dd t|	d
 D S )Nbinjsonschema_suiteremotesc                 S   s   i | ]	\}}d | |qS )zhttp://localhost:1234/r   ).0nameschemar   r   r   
<dictcomp>1   s    z"Suite._remotes.<locals>.<dictcomp>utf-8)_root
descendant
subprocesscheck_outputsys
executablepathr   loadsdecodeitems)selfr   r   r   r   r   _remotes,   s   zSuite._remotesc                 C   s"   t D ]}| j|dj|d qd S )N)r   )runner)r   version	benchmark)r*   r,   r   r   r   r   r.   6   s   zSuite.benchmarkc                 C   s   t || jd|g|  dS )Ntests)r   r&   r   )Versionr    r!   r+   r*   r   r   r   r   r-   :   s
   zSuite.versionN)__name__
__module____qualname__attribFactoryr   r    r+   r.   r-   r   r   r   r   r   '   s
    
r   c                   @   s\   e Zd Ze Ze Ze Zdd Zdd Z	dd Z
dd Zd	d
 Zdd Zdd ZdS )r0   c              	   K   s8   |   D ]}|D ]}||jt|jfi | qqd S N)r/   
bench_funcfully_qualified_namer   validate_ignoring_errors)r*   r,   kwargssuitetestr   r   r   r.   J   s   zVersion.benchmarkc                    s    fdd j dD S )Nc                 3   4    | ]} j | d d |dD ]}|V  qqd S Nsubjectr&   	_tests_inbasenamer   childr>   r*   r   r   	<genexpr>S       z Version.tests.<locals>.<genexpr>*.json)_pathglobChildrenrI   r   rI   r   r/   R   s   

zVersion.testsc                    s(    j ddg} fdd|dD S )Noptionalformatc                 3   r?   r@   rD   rG   rI   r   r   rJ   ^   rK   z'Version.format_tests.<locals>.<genexpr>rL   )rM   r!   rN   )r*   r&   r   rI   r   format_tests\   s   
zVersion.format_testsc                 C   s   | j || j|d dS )N.jsonrB   )rE   rM   rH   r1   r   r   r   tests_ofg   s   zVersion.tests_ofc                 C   s   | j || jd|d gdS )NrO   rR   rB   )rE   rM   r!   r1   r   r   r   optional_tests_ofm   s   zVersion.optional_tests_ofc                    s\     dd| j  } fdd|D }t|tjf|}zt |_W |S  ty-   Y |S w )Nr   Testc              	      s4   i | ]}|D ]}|D ]}|j |jd i  q
qqS )r   )method_nameto_unittest_method)r   r=   r/   r>   r<   r   r   r   u   s    
z0Version.to_unittest_testcase.<locals>.<dictcomp>)	popr   titletypeunittestTestCase)_someone_save_us_the_module_of_the_callerr3   	Exception)r*   suitesr<   r   methodsclsr   rX   r   to_unittest_testcases   s   

zVersion.to_unittest_testcasec                 #   s<    t | dD ]  fdd d D V  qd S )Nr   c              	   3   s4    | ]}t d d   d jd|V  qdS )descriptionr   )r-   rC   case_descriptionr   r   Nr   )_Testr+   )r   r>   eachr*   rC   r   r   rJ      s    
z$Version._tests_in.<locals>.<genexpr>r/   )r   r'   
getContentr(   )r*   rC   r&   r   rg   r   rE      s   
zVersion._tests_inN)r2   r3   r4   r5   r6   rM   r+   r   r.   r/   rQ   rS   rT   rc   rE   r   r   r   r   r0   B   s    
r0   F)r   reprc                   @   s   e Zd Ze Ze Ze Ze Ze Z	ejddZ
e Ze Zdd Zedd Zedd Zd	d
 fddZdd Zdd ZdS )rf   F)rj   c                 C   s   d | jS )Nz	<Test {}>)rP   r:   rI   r   r   r   __repr__   s   z_Test.__repr__c                 C   s   d | jj| j| j| jgS )Nz > )joinr-   r   rC   re   rd   rI   r   r   r   r:      s   z_Test.fully_qualified_namec                 C   sH   d}dt |d| jt |d| jt |d| jf }ts"|d}|S )Nz[\W\- ]+ztest_%s_%s_%s_r   )resubrC   re   rd   r   encode)r*   
delimitersr   r   r   r   rV      s   
z_Test.method_namec                 C   s   d S r8   r   )r>   r   r   r   <lambda>   s    z_Test.<lambda>c                    sH   j r fdd}n fdd}j|_|}t|d u||S )Nc                    s   j di   d S Nr   )validatethisr<   r*   r   r   fn   s   z$_Test.to_unittest_method.<locals>.fnc                    sB   |  tj jdi   W d    d S 1 sw   Y  d S rs   )assertRaisesr   ValidationErrorrt   ru   rw   r   r   rx      s   ")validrV   r2   r\   skipIf)r*   skipr<   rx   reasonr   rw   r   rW      s   z_Test.to_unittest_methodc                 K   s:   t jj| j| j|jd}t jd| j| j||d| d S )N)r   storeid_of)instancer   rb   resolverr   )r   RefResolverfrom_schemar   r+   ID_OFrt   data)r*   	Validatorr<   r   r   r   r   rt      s   
z_Test.validatec                 C   s*   z	| j |d W d S  tjy   Y d S w )N)r   )rt   r   rz   )r*   r   r   r   r   r;      s
   z_Test.validate_ignoring_errorsN)r2   r3   r4   r5   r6   r-   rC   re   rd   r   r   r{   r+   rk   propertyr:   rV   rW   rt   r;   r   r   r   r   rf      s"    


rf   c                   C   s   t djd S )a  
    The FQON of the module 2nd stack frames up from here.

    This is intended to allow us to dynamicallly return test case classes that
    are indistinguishable from being defined in the module that wants them.

    Otherwise, trial will mis-print the FQON, and copy pasting it won't re-run
    the class that really is running.

    Save us all, this is all so so so so so terrible.
       r2   )r$   	_getframe	f_globalsr   r   r   r   r^      s   r^   )__doc__	functoolsr   r   r   rn   r"   r$   r\   twisted.python.filepathr   r5   jsonschema.compatr   jsonschema.validatorsr   r   r   sobjectr   r0   rf   r^   r   r   r   r   <module>   s*    

RL