
    5\h	3                        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mZ ddl	m
Z
 ddlmZmZmZ ddlmZ ddlmZ  e
d          Z eed	          r
d
ej        v Znej         odej         v Zd Z ej         eed          d           G d dej                              Z G d dej                  Zedk    r ej                     dS dS )z:
Very minimal unittests for parts of the readline module.
    N)verbose)import_module)unlinktemp_dirTESTFN)run_pty)assert_python_okreadline_READLINE_LIBRARY_VERSIONzEditLine wrapperlibeditc                  F   t           rt          t          d          r:t          dt          j        d           t          dt          j        d           t          t          d          rt          dt          j                   t          dt                      d S d S )N_READLINE_VERSIONzreadline version: z#xzreadline runtime version: r   zreadline library version: zuse libedit emulation? )r   hasattrr
   printr   _READLINE_RUNTIME_VERSIONr   is_editline     </opt/python-3.11.14/usr/lib/python3.11/test/test_readline.pysetUpModuler      s     7 8011 	XFx'AFFFGGGVx/QVVVWWW8899 	WUx/QUUVVV555666667 7r   clear_historyzXThe history update test cannot be run because the clear_history method is not available.c                   f    e Zd ZdZd Z ej         eed          d          d             Z	d Z
dS )TestHistoryManipulationz
    These tests were added to check that the libedit emulation on OSX and the
    "real" readline have the same interface for history manipulation. That's
    why the tests cover only a small subset of the interface.
    c                    t                                            t                               d           t                               d           |                     t                               d          d            |                     t                               d          d           |                     t                               d          d           t                               dd           |                     t                               d          d            |                     t                               d          d           |                     t                               d          d           |                     t                                           d           t                               d           |                     t                               d          d            |                     t                               d          d           |                     t                                           d           d S )N
first linesecond liner         zreplaced line)r
   r   add_historyassertEqualget_history_itemreplace_history_itemget_current_history_lengthremove_history_item)selfs    r   testHistoryUpdatesz*TestHistoryManipulation.testHistoryUpdates/   s      \***]+++22155t<<<22155|DDD22155}EEE%%a99922155t<<<22155GGG22155}EEE<<>>BBB$$Q'''22155t<<<22155}EEE<<>>BBBBBr   append_history_filezappend_history not availablec                 $   t          j        d          }|                                 |j        }|                     t
          |           t                                           t                              d           t                              d           t          	                    |           t                                           | 
                    t                                          d           t                              |           | 
                    t                                          d           | 
                    t                              d          d           | 
                    t                              d          d           t                              d|           t                                           t                              |           | 
                    t                                          d           | 
                    t                              d          d           | 
                    t                              d          d           | 
                    t                              d          d           t          j        |           	 t                              d|           t          j        |           n# t           $ r Y nw xY wt          	                    |           d S )	NF)deleter   r   r   r   r      )tempfileNamedTemporaryFileclosename
addCleanupr   r
   r   r   write_history_filer    r#   read_history_filer!   r'   osFileNotFoundError)r%   hfile	hfilenames      r   test_write_read_appendz.TestHistoryManipulation.test_write_read_appendF   s    +5999J		*** 	   \***]+++##I...   <<>>BBB""9---<<>>BBB22155|DDD22155}EEE 	$$Q	222   ""9---<<>>BBB22155|DDD22155}EEE22155}EEE 		)	!((I666 Ii     ! 	 	 	D	 	##I.....s   6K& &
K32K3c                 D   t                                            	 t                               d           n<# t          $ r/}|                     dt          |          z              Y d }~nd }~ww xY wt                               d           t                               dd           t                               t                     | 	                    t          j        t                     t                                            t                               t                     t          rt                               d           |                     t                               d          d           |                     t                               d          d           d S )Nu	   entrée 1 Locale cannot encode test data: u	   entrée 2r   u
   entrée 22dummyr   )r
   r   r   UnicodeEncodeErrorskipTestformatr"   r0   r   r/   r2   remover1   r   r    r!   )r%   errs     r   test_nonascii_historyz-TestHistoryManipulation.test_nonascii_historyt   sg      	L  ----! 	L 	L 	LMM<vc{{JKKKKKKKK	L[)))%%a666##F+++	6***   ""6*** 	*   )))22155{CCC22155|DDDDDs   6 
A/ %A**A/N)__name__
__module____qualname____doc__r&   unittest
skipUnlessr   r
   r6   r?   r   r   r   r   r   %   s         C C C. X+@AA79 9*/ */9 9*/XE E E E Er   r   c                       e Zd Z ej        ej        dk     oe d          d             ZdZ	d Z
d Zd Zd Z ej        ej        d	k     d
           ej        ed          d                         ZdS )TestReadlinei  z%not supported in this library versionc                 ^    t          ddd          \  }}}|                     |d           d S )Nz-czimport readlinezxterm-256color)TERMr   )r	   r    )r%   rcstdoutstderrs       r   	test_initzTestReadline.test_init   sF     .d4E3CE E EFF%%%%%r   zvimport readline
readline.set_auto_history({})
input()
print("History length:", readline.get_current_history_length())
c                     t          | j                            d                    }|                     d|           d S )NTs   History length: 1r   auto_history_scriptr<   assertInr%   outputs     r   test_auto_history_enabledz&TestReadline.test_auto_history_enabled   s=    188>>?? 	*F33333r   c                     t          | j                            d                    }|                     d|           d S )NFs   History length: 0rO   rR   s     r   test_auto_history_disabledz'TestReadline.test_auto_history_disabled   s=    188??@@ 	*F33333r   c                 |    t          j        d          }t          |d          }|                     d|           d S )Na  
            import readline
            def complete(text, state):
                if state == 0 and text == "$":
                    return "$complete"
                return None
            if "libedit" in getattr(readline, "__doc__", ""):
                readline.parse_and_bind(r'bind "\t" rl_complete')
            else:
                readline.parse_and_bind(r'"\t": complete')
            readline.set_completer_delims(" \t\n")
            readline.set_completer(complete)
            print(input())
        s   $	
)inputs	   $complete)textwrapdedentr   rQ   )r%   scriptrS   s      r   test_set_complete_delimsz%TestReadline.test_set_complete_delims   sF     "   x000lF+++++r   c                 L   t          j        t           j        d           }|dv r|                     d|           	 t                              d           n<# t          $ r/}|                     dt          |          z              Y d }~nd }~ww xY wd}d}|dt          d          z  z  }|d	z  }|d
z  }|dz  }t          ||          }| 
                    d|           | 
                    d|           t          j        dk    st          s| 
                    d|           t          sAt          t          d          r,| 
                    d|           | 
                    d|           d}| 
                    d|z   dz   |           | 
                    d|z   |           d S )N)CPOSIXzthe LC_CTYPE locale is u   ëïr8   a*  import readline

is_editline = readline.__doc__ and "libedit" in readline.__doc__
inserted = "[\xEFnserted]"
macro = "|t\xEB[after]"
set_pre_input_hook = getattr(readline, "set_pre_input_hook", None)
if is_editline or not set_pre_input_hook:
    # The insert_line() call via pre_input_hook() does nothing with Editline,
    # so include the extra text that would have been inserted here
    macro = inserted + macro

if is_editline:
    readline.parse_and_bind(r'bind ^B ed-prev-char')
    readline.parse_and_bind(r'bind "\t" rl_complete')
    readline.parse_and_bind(r'bind -s ^A "{}"'.format(macro))
else:
    readline.parse_and_bind(r'Control-b: backward-char')
    readline.parse_and_bind(r'"\t": complete')
    readline.parse_and_bind(r'set disable-completion off')
    readline.parse_and_bind(r'set show-all-if-ambiguous off')
    readline.parse_and_bind(r'set show-all-if-unmodified off')
    readline.parse_and_bind(r'Control-a: "{}"'.format(macro))

def pre_input_hook():
    readline.insert_text(inserted)
    readline.redisplay()
if set_pre_input_hook:
    set_pre_input_hook(pre_input_hook)

def completer(text, state):
    if text == "t\xEB":
        if state == 0:
            print("text", ascii(text))
            print("line", ascii(readline.get_line_buffer()))
            print("indexes", readline.get_begidx(), readline.get_endidx())
            return "t\xEBnt"
        if state == 1:
            return "t\xEBxt"
    if text == "t\xEBx" and state == 0:
        return "t\xEBxt"
    return None
readline.set_completer(completer)

def display(substitution, matches, longest_match_length):
    print("substitution", ascii(substitution))
    print("matches", ascii(matches))
readline.set_completion_display_matches_hook(display)

print("result", ascii(input()))
print("history", ascii(readline.get_history_item(1)))
      z[after]s   		s   x	   s   text 't\xeb'
s#   line '[\xefnserted]|t\xeb[after]'
darwins   indexes 11 13
set_pre_input_hooks   substitution 't\xeb'
s    matches ['t\xebnt', 't\xebxt']
s   '[\xefnserted]|t\xebxt[after]'s   result s   
s   history )locale	setlocaleLC_CTYPEr;   r
   r   r:   r<   lenr   rQ   sysplatformr   r   )r%   locr>   r[   rX   rS   expecteds          r   test_nonasciizTestReadline.test_nonascii   s   v55.   MM;C;;<<<	L  ,,,,! 	L 	L 	LMM<vc{{JKKKKKKKK	L2h 3y>>))''*F333@&III<8##;#MM.777  	Kwx1EFF 	KMM6???MMA6JJJ5j8+g5v>>> 	kH,f55555s   A 
B"%BBi   z3this readline version does not support history-sizez-editline history size configuration is brokenc           	         d}t                      5 }t          j                            |d          }t	          |d          5 }|                    d|z             d d d            n# 1 swxY w Y   t          j                            |d          }t	          |d          5 }d                    d t          |dz            D                       }|                    |           d d d            n# 1 swxY w Y   d	}t          t          j                  }||d
<   ||d<   t          |d|           t	          |d          5 }|
                                }	d d d            n# 1 swxY w Y   |                     t          |	          |           |                     |	d                                         d           d d d            d S # 1 swxY w Y   d S )N
   inputrcwbs   set history-size %d
historyr   c              3       K   | ]	}d |z  V  
dS )s   item %d
Nr   ).0is     r   	<genexpr>z1TestReadline.test_history_size.<locals>.<genexpr>*  sC        B  B$% !-q 0  B  B  B  B  B  Br   r   z
import os
import readline

history_file = os.environ["HISTORY_FILE"]
readline.read_history_file(history_file)
input()
readline.write_history_file(history_file)
INPUTRCHISTORY_FILEs   last input)rX   envrbs
   last input)r   r2   pathjoinopenwriterangedictenvironr   	readlinesr    rh   strip)
r%   history_sizetest_dirrp   fhistory_filedatar[   ry   liness
             r   test_history_sizezTestReadline.test_history_size  s   
 ZZ 	?8gll8Y77Ggt$$ A0<?@@@A A A A A A A A A A A A A A A 7<<)<<LlD)) Qxx  B  B).|a/?)@)@ B  B  B B B	              F rz""C$C	N".CF/s;;;;lD)) &Q& & & & & & & & & & & & & & &SZZ666U2Y__..>>>?	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	?s   1GA'G'A+	+G.A+	/3G"AC3'G3C7	7G:C7	;A
GE&G&E*	*G-E*	.AGGGN)r@   rA   rB   rD   skipIfr
   r   r   rM   rP   rT   rV   r\   rm   r   r   r   r   rG   rG      s        X_X/&8L_<> >& &> >&4 4 44 4 4, , ,&[6 [6 [6D X_X/%7JL LX_[DF F!? !?F FL L!? !? !?r   rG   __main__)rC   re   r2   ri   r+   rY   rD   test.supportr   test.support.import_helperr   test.support.os_helperr   r   r   test.support.pty_helperr   test.support.script_helperr	   r
   r   r   r   r   rE   TestCaser   rG   r@   mainr   r   r   <module>r      s     				 



                4 4 4 4 4 4 ; ; ; ; ; ; ; ; ; ; + + + + + + 7 7 7 7 7 7 =$$
78011 G%)KKKK#E	X5E(EK	7 	7 	7 WWX77>? ?]E ]E ]E ]E ]Ex0 ]E ]E? ?]E@y? y? y? y? y?8$ y? y? y?x zHMOOOOO r   