
    :Mc`                         d 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	m
Z
 ddlmZ ddlmZmZ ddlZddlmZ  G d d	ej                  ZddZdS )zBase test class for nbconvert    N)PIPEPopen)TemporaryDirectory)v4write   )_contextlib_chdirc                       e Zd ZdZ	 	 	 	 	 	 	 ddZd Zej        dd            Ze	d             Z
d	 ZddZd ZddZdS )	TestsBasezPBase tests class.  Contains useful fuzzy comparison and nbconvert
    functions.TFc
                    |	s(|                                 }|                                 }|r,|                    dd          }|                    dd          }|r,|                    dd          }|                    dd          }|r,|                    dd          }|                    dd          }|r,|                    dd          }|                    dd          }|r.|                     |dd          }|                     |dd          }|s(|                                }|                                }|                     ||           dS )a  
        Performs a fuzzy comparison of two strings.  A fuzzy comparison is a
        comparison that ignores insignificant differences in the two comparands.
        The significance of certain differences can be specified via the keyword
        parameters of this method.
        
  	z  N)stripreplacerecursive_replacelowerassertEqual)
selfabnewlines_are_spacestabs_are_spacesfuzzy_spacingignore_spacesignore_newlinescase_sensitiveleave_paddings
             4lib/python3.11/site-packages/nbconvert/tests/base.pyfuzzy_comparezTestsBase.fuzzy_compare   sR   &  			A		A 	$		$##A		$##A 	%		$$$A		$$$A 	%		$$$A		$$$A 	#		#r""A		#r""A 	5&&q$44A&&q$44A 			A		AA    c                 B    ||v r|                     ||          }||v |S )a  
        Performs a recursive replacement operation.  Replaces all instances
        of a search string in a text string with a replacement string until
        the search string no longer exists.  Recursion is needed because the
        replacement string may generate additional search strings.

        For example:
           Replace "ii" with "i" in the string "Hiiii" yields "Hii"
           Another replacement cds "Hi" (the desired output)

        Parameters
        ----------
        text : string
            Text to replace in.
        search : string
            String to search for within "text"
        replacement : string
            String to replace "search" with
        )r   )r   textsearchreplacements       r    r   zTestsBase.recursive_replaceL   s6    ( n 	5<<44D n 	5r"   Nc              #      K   t                      5 }t          j        |          5  ||                     ||           |V  d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )N)dest)r   r	   chdircopy_files_to)r   copy_filenamestds      r    create_temp_cwdzTestsBase.create_temp_cwdd   s     !! 	R):)@)D)D 	 	 <"">";;;HHH	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s4   A(AA(A	A(A	A((A,/A,c                 >    i }|D ]}|                     |           |S N)update)cls	dict_argsoutcomeds       r    merge_dictszTestsBase.merge_dictsk   s2      	 	ANN1r"   c                     t          j                    }t          |dd          5 }t          ||d           d d d            d S # 1 swxY w Y   d S )Nwzutf-8)encoding   )r   new_notebookopenr   )r   pathnbfs       r    create_empty_notebookzTestsBase.create_empty_notebooks   s    _$g... 	!"aOOO	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   AA	A	.c                    t           j                            |          st          j        |           |                                 }|D ]}t          j        t           j                            ||                    }|sJ |D ]R}t          j        |t           j                            |t           j        	                    |                               SdS )z.Copy test files into the destination directoryN)
osr<   isdirmakedirs_get_files_pathglobjoinshutilcopyfilebasename)r   r+   r(   
files_pathpatternfilesmatchs          r    r*   zTestsBase.copy_files_tox   s    w}}T"" 	K))++
% 	T 	TGIbgll:w??@@ELLL T Trw||D"':J:J5:Q:Q'R'RSSSST	T 	Tr"   c                     | j                             d          dd         }|                    d           t          j                            t          j                  }t          j        j        |g|R  S )Nr@      rM   )	
__module__splitappendrB   r<   dirname	nbconvert__file__rG   )r   namesr<   s      r    rE   zTestsBase._get_files_path   sf     %%c**1R40W wy122w|D)5))))r"   c                 b   t           j        ddg}t           j        dk    rKt          |t          f          rd                    |          dz   |z   }nHd                    ||z             }n/t          |t          f          rt          j        |          }||z  }t          |t          t          t                    }|
                    |          \  }}|j        dk    s%|s#t          |                    dd	                    |                    dd	          |                    dd	          fS )
a  
        Run nbconvert as a shell command, listening for both Errors and
        non-zero return codes. Returns the tuple (stdout, stderr) of
        output produced during the nbconvert run.

        Parameters
        ----------
        parameters : str, list(str)
            List of parameters to pass to IPython.
        ignore_return_code : optional bool (default False)
            Throw an OSError if the return code
        z-mrV   win32r   )stdoutstderrstdin)inputr   utf8r   )sys
executableplatform
isinstancestrrG   shlexrS   r   r   communicate
returncodeOSErrordecode)r   
parametersignore_return_coder]   cmdpr[   r\   s           r    rV   zTestsBase.nbconvert   s    ~t[1<7" 	*sf-- 1hhsmmc)J6hhsZ/00*sf-- 5"[44
:C#d4t<<<U33! 	<%7 	<&--	::;;;}}VY//vy1Q1QQQr"   )TTTFFFFr/   )r@   )FN)__name__rR   __qualname____doc__r!   r   
contextlibcontextmanagerr-   classmethodr5   r?   r*   rE   rV    r"   r    r   r      s          !/ / / /b  0       [  
	T 	T 	T 	T	* 	* 	*R R R R R Rr"   r   P   c           	      8   t          dt          |           |          D ]2}| |||z            }||||z            }||k    sJ d|||fz              3t          |           t          |          k    rDt          dt          |           t          |          | t          |          d         fz            t          |           t          |          k     rDt          dt          |           t          |          | t          |          d         fz            dS )zassert that large strings are equal

    Zooms in on first chunk that differs,
    to give better info than vanilla assertEqual for large text blobs.
    r   z[offset: %i]
%r != 
%rz.Length doesn't match (%i > %i). Extra text:
%rNz.Length doesn't match (%i < %i). Extra text:
%r)rangelenAssertionError)r   r   
chunk_sizeichunk_achunk_bs         r    assert_big_text_equalr~      s8    1c!ffj)) V VAJ&'AJ&''!UU#=GW@U#UUUUU
1vvA 
=QQQRSVWXSYSYS[S[Q\@]]
 
 	
 
Q#a&& 
=QQQRSVWXSYSYS[S[Q\@]]
 
 	

 
r"   )ru   )rp   rq   rF   rB   re   rH   r`   unittest
subprocessr   r   tempfiler   nbformatr   r   rV   utilsr	   TestCaser   r~   rt   r"   r    <module>r      s   # #
      				   



  " " " " " " " " ' ' ' ' ' '             % % % % % %RR RR RR RR RR! RR RR RRj
 
 
 
 
 
r"   