B
     \                 @   s   d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
Z
d dlZd dlmZ yd dlmZ d	d
 ZW n( ek
r   d dlmZ dd
 ZY nX G dd deZdS )    )unittest_support)TestCase)utils)convert)readsN)copy)Emptyc             C   s
   t | tS )N)
isinstancestr)s r   5lib/python3.7/site-packages/numba/testing/notebook.pyisstr   s    r   c             C   s
   t | tS )N)r	   Z
basestring)r   r   r   r   r      s    c               @   sl   e Zd ZdZddddgZdddd	g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S )NotebookTestaV  Validate a notebook. All code cells are executed in order. The output is either checked
    for errors (if no reference output is present), or is compared against expected output.


    Useful references:
    http://nbformat.readthedocs.org/en/latest/format_description.html
    http://jupyter-client.readthedocs.org/en/latest/messaging.html
Zexecute_requestZexecute_inputstatusZpyinZexecution_count	tracebackZprompt_numbersource   c       	   	   C   sp   t |}tt| | j}W d Q R X t \}}x6tdd |jD D ]\}}| 	| 
|||| qJW d S )Nc             S   s   g | ]}|j d kr|qS )code)Z	cell_type).0cr   r   r   
<listcomp>.   s    z/NotebookTest._test_notebook.<locals>.<listcomp>)openr   r   readNBFORMAT_VERSIONr   Zstart_new_kernel	enumerateZcells_test_notebook_cellsanitize_cell)	selfZnotebooktestfZnb_kernelir   r   r   r   _test_notebook)   s
    
zNotebookTest._test_notebookc                s<  t |dr|j}n|j}|j}|| g }d }d}	d}
d}x |ry|jddd}W n tk
rp   w>Y nX |d  jkr>|d dkrd	|d
 d  d |d
 d  }d}	|
dkr|}
t	|}|t	|j
k r|j
| pg } ||}|| q>W |dkr |	d|
|f  n fdd}||}||j
}d S )Nr   T    )blockZtimeoutmsg_typeerrorz  contentZenamez
  ZevalueFZcheck_errorz*Executing cell %d resulted in an error:
%sc                s     t | S )N)dump_canonicallistscrub_outputs)x)r   r   r   <lambda>T   s    z2NotebookTest._test_notebook_cell.<locals>.<lambda>)hasattrr   inputZiopub_channelZexecuteshould_continueZget_msgr   IGNORE_TYPESlenoutputstransform_messageappendZ
assertTrue)r   cellr#   r"   r   r   Ziopubr6   msgZno_errorZfirst_errorZ	error_msgexpectedoscrubZscrubbedr   )r   r   r   1   s<    

 
z NotebookTest._test_notebook_cellc             C   s   t j|dddS )N   T)indentZ	sort_keys)jsondumps)r   objr   r   r   r,   [   s    zNotebookTest.dump_canonicalc             #   s   x|D ]}t |}xdD ]\  fdd}d|krF||d |d< d|krt|d trx:|d  D ]\}}|||d |< qjW q||d |d< qW |V  qW dS )z=
        remove all scrubs from output data and text
        r   c                s.   t | rt | S  fdd| D S d S )Nc                s   g | ]}t  |qS r   )resub)r   line)r=   rD   r   r   r   j   s    zCNotebookTest.scrub_outputs.<locals>._scrubLines.<locals>.<listcomp>)r   rC   rD   )lines)r=   rD   r   r   _scrubLinesf   s    z/NotebookTest.scrub_outputs.<locals>._scrubLinestextdataN)r   r	   dictitems)r   r6   outputoutrG   ZmimerI   r   )r=   rD   r   r.   ^   s    
zNotebookTest.scrub_outputsc             C   s    x| j D ]}||d qW |S )zE
        remove keys from STRIP_KEYS to ensure comparability
        N)
STRIP_KEYSpop)r   dkeyr   r   r   
strip_keysw   s    zNotebookTest.strip_keysc             C   s   x|j D ]}| | qW |S )z0
        remove non-reproducible things
        )r6   rR   )r   r9   rL   r   r   r   r      s    zNotebookTest.sanitize_cellc             C   s   ddddi}d|d i}| |d  | |}x8| D ],\}}||kr<|| |kr<|||  ||< q<W d|krd|kr|d |d< |d= |S )	zF
        transform a message into something like the notebook
        Zoutput_typeZexecute_resultr*   )ZpyoutZpyerrr)   r+   rI   rH   )updaterR   rK   )r   r:   r;   Z	SWAP_KEYSrL   rQ   Zswapsr   r   r   r7      s    

zNotebookTest.transform_messagec             C   s*   |dkrdS |d dko&|d d dk S )zQ
        determine whether the current message is the last for this cell
        NTr)   r   r+   Zexecution_stateZidler   )r   r:   r   r   r   r3      s    zNotebookTest.should_continueN)__name__
__module____qualname____doc__r4   rN   r   r$   r   r,   r.   rR   r   r7   r3   r   r   r   r   r      s   *r   )Znumbar   Zunittestr   Zipykernel.testsr   Znbformat.converterr   Znbformat.readerr   rC   r@   r   Zqueuer   r   ImportErrorZQueuer   r   r   r   r   <module>   s   