
    gH                     .   d dl Z d dlZd dlZd dlmZmZmZ d dlmZm	Z	m
Z
mZ d dlmZ d dlZ G d dej                        Z G d dej                        Z G d	 d
ej                        Z G d dej                        Z G d dej                        Zy)    N)utf8
native_str
to_unicode)Template
DictLoader
ParseErrorLoader)
ObjectDictc                       e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z ej2                  d      d        Zd Zy)TemplateTestc                 ^    t        d      }| j                  |j                  d      d       y NzHello {{ name }}!Benname
   Hello Ben!r   assertEqualgenerateselftemplates     O/var/www/openai/venv/lib/python3.12/site-packages/tornado/test/template_test.pytest_simplezTemplateTest.test_simple   s+    /0***6F    c                 p    t        d      }| j                  |j                  t        d            d       y r   r   r   r   r   r   s     r   
test_byteszTemplateTest.test_bytes   s/    /0**U*<mLr   c                 Z    t        d      }| j                  |j                         d       y )Nz2 + 2 = {{ 2 + 2 }}s	   2 + 2 = 4r   r   s     r   test_expressionszTemplateTest.test_expressions   s&    12**,l;r   c                 p    t        d      }| j                  |j                  t        d            d       y )Nz Hello{# TODO i18n #} {{ name }}!r   r   r   r   r   s     r   test_commentzTemplateTest.test_comment   s/    >?**U*<mLr   c                 ~    t        ddd      }| j                  |j                  d      j                         d       y )Nz%{% include "header.html" %}
body textzheader text)
index.htmlzheader.htmlr$   s   header text
body textr   r   loadr   r   loaders     r   test_includezTemplateTest.test_include   s?    F,
 	KK%..02K	
r   c                 ~    t        ddd      }| j                  |j                  d      j                         d       y )Nzj<title>{% block title %}default title{% end %}</title>
<body>{% block body %}default body{% end %}</body>
zb{% extends "base.html" %}
{% block title %}page title{% end %}
{% block body %}page body{% end %}
)	base.html	page.htmlr,   s1   <title>page title</title>
<body>page body</body>
r%   r'   s     r   test_extendszTemplateTest.test_extends(   sC    

 	KK$--/B	
r   c                     t        dddd      }| j                  |j                  d      j                         d       y )Nz{% include '2.html' %}z{% include '../b/3.html' %}ok)a/1.htmlza/2.htmlzb/3.htmlr0   s   okr%   r'   s     r   test_relative_loadzTemplateTest.test_relative_load;   s>    49 
 	Z099;UCr   c                    | j                  t        d        | j                  t        d        | j                  t        d      j	                         d       | j                  t        d      j	                         d       | j                  t        d      j	                         d       | j                  t        d	      j	                         d
       y )Nc                      t        d      S )Nz{{r    r   r   <lambda>z,TemplateTest.test_escaping.<locals>.<lambda>F       htnr   c                      t        d      S )Nz{%r4   r5   r   r   r6   z,TemplateTest.test_escaping.<locals>.<lambda>G   r7   r   z{{!s   {{z{%!s   {%z{#!s   {#z{{ 'expr' }} {{!jquery expr}}s   expr {{jquery expr}})assertRaisesr   r   r   r   r   s    r   test_escapingzTemplateTest.test_escapingE   s    *&<=*&<=%113U;%113U;%113U;45>>@#	
r   c                 ~    t        t        d            }| j                  |j                         t        d             y )N   ér   r   r   r   r   s     r   test_unicode_templatez"TemplateTest.test_unicode_templateP   s-    DN+**,d8n=r   c                 ~    t        t        d            }| j                  |j                         t        d             y )Nu
   {{ "é" }}r=   r>   r   s     r   test_unicode_literal_expressionz,TemplateTest.test_unicode_literal_expressionT   s1    
 D!123**,d8n=r   c                     t        ddidd i      }| j                  |j                  d      j                         d       y )N	test.htmlz{{ inc(5) }}incc                     | dz   S )N   r5   xs    r   r6   z4TemplateTest.test_custom_namespace.<locals>.<lambda>^   s    q1ur   	namespace   6r%   r'   s     r   test_custom_namespacez"TemplateTest.test_custom_namespace\   s?    .)e_5M
 	[1::<dCr   c                 v    d }t        t        d            }| j                  |j                  |      d       y )Nc                 "    | j                         S Nupperss    r   rQ   z&TemplateTest.test_apply.<locals>.upperc   s    779r   z{% apply upper %}foo{% end %}rP   s   FOOr>   r   rQ   r   s      r   
test_applyzTemplateTest.test_applyb   s7    	 D!@AB***7@r   c                     d }t        t        d            }| j                  |j                  |      t        d             y )Nc                 4    t        |       j                         S rO   )r   rQ   rR   s    r   rQ   z.TemplateTest.test_unicode_apply.<locals>.upperj   s    a=&&((r       {% apply upper %}foo é{% end %}rP      FOO Ér>   rT   s      r   test_unicode_applyzTemplateTest.test_unicode_applyi   s<    	) D!GHI***7l9KLr   c                     d }t        t        d            }| j                  |j                  |      t        d             y )Nc                 F    t        t        |       j                               S rO   )r   r   rQ   rR   s    r   rQ   z,TemplateTest.test_bytes_apply.<locals>.upperq   s    
1++-..r   rX   rP   rY   r>   rT   s      r   test_bytes_applyzTemplateTest.test_bytes_applyp   s<    	/ D!GHI***7l9KLr   c                     t        t        d            }| j                  |j                  d      d       | j                  |j                  d      d       y )Nz&{% if x > 4 %}yes{% else %}no{% end %}   rG   s   yes   s   nor>   r   s     r   test_ifzTemplateTest.test_ifw   sN    D!IJK**Q*/8**Q*/7r   c                 l    t        t        d            }| j                  |j                         d       y )Nz {% if True %}{% else %}{% end %}r   r>   r   s     r   test_if_empty_bodyzTemplateTest.test_if_empty_body|   s+    D!CDE**,c2r   c                     t        t        d            }| j                  |j                  d      d       | j                  |j                  d      d       y )Nzb{% try %}
try{% set y = 1/x %}
{% except %}-except
{% else %}-else
{% finally %}-finally
{% end %}rF   rG   s   
try
-else
-finally
r   s   
try-except
-finally
r>   r   s     r   test_tryzTemplateTest.test_try   sY    	
 	**Q*/1LM**Q*/1LMr   c                 l    t        t        d            }| j                  |j                         d       y )Nz{% comment blah blah %}foos   foor>   r   s     r   test_comment_directivez#TemplateTest.test_comment_directive   s+    D!=>?**,f5r   c                     t        t        d            }|j                         }dj                  |j	                               }| j                  |d       y )Nz{% for i in range(10) %}
    {% if i == 2 %}
        {% continue %}
    {% end %}
    {{ i }}
    {% if i == 6 %}
        {% break %}
    {% end %}
{% end %}r   s   013456)r   r   r   joinsplitr   )r   r   results      r   test_break_continuez TemplateTest.test_break_continue   sM    	
 ""$&,,.)+r   c                 `    	 t        t        d             t        d      # t        $ r Y y w xY w)Nz{% break %}Did not get expected exceptionr   r   	Exceptionr   r:   s    r   test_break_outside_loopz$TemplateTest.test_break_outside_loop   s2    	T-()<== 		   ! 	--c                 `    	 t        t        d             t        d      # t        $ r Y y w xY w)Nz={% for i in [] %}{% apply foo %}{% break %}{% end %}{% end %}rn   ro   r:   s    r   test_break_in_applyz TemplateTest.test_break_in_apply   s8    	TU <== 		rr   zno testable future importsc                 ~    | j                  dd       t        d      }| j                  |j                         d       y )Ng      ?z{{ 1 / 2 }}0)r   r   r   r   s     r   test_no_inherit_futurez#TemplateTest.test_no_inherit_future   s7     	$M***,c2r   c                 |    t        ddi      }| j                  |j                  d      j                         d       y )Nu
   tést.htmlhellos   hellor%   r'   s     r   test_non_ascii_namez TemplateTest.test_non_ascii_name   s5    -w78%56??A8Lr   N)__name__
__module____qualname__r   r   r    r"   r)   r-   r1   r;   r?   rA   rL   rU   rZ   r]   ra   rc   re   rg   rl   rq   rt   unittestskiprw   rz   r5   r   r   r   r      s    GM<M	

&D	
>>DAMM8
3N6,(	 X]]/0	3 1	3Mr   r   c                   6    e Zd Zd Zd Zd Zd Zd Zd Zd Z	y)	StackTraceTestc                     t        ddi      }	 |j                  d      j                          | j                  d       y # t        $ r( | j                  dt        j                         v        Y y w xY w)NrC   zone
two{{1/0}}
three
        did not get expected exception# test.html:2r   r&   r   failZeroDivisionError
assertTrue	traceback
format_excr'   s     r   !test_error_line_number_expressionz0StackTraceTest.test_error_line_number_expression   l     
	GKK$--/II67  	GOOOy/C/C/EEF	G   0A   .A10A1c                     t        ddi      }	 |j                  d      j                          | j                  d       y # t        $ r( | j                  dt        j                         v        Y y w xY w)NrC   z'one
two{%if 1/0%}
three{%end%}
        r   r   r   r'   s     r    test_error_line_number_directivez/StackTraceTest.test_error_line_number_directive   r   r   c                 @   d fd}t        ddddt        |      i      	 j                  d      j                          | j	                  d	       y # t
        $ r= t        j                         }| j                  d
|v        | j                  d|v        Y y w xY w)Nc                 N    J  j                  |       j                  di |S )Nr5   r&   r   )pathkwargsr(   s     r   load_generatezCStackTraceTest.test_error_line_number_module.<locals>.load_generate   s.    %%%-6;;t$--777r   z!{% module Template('sub.html') %}{{1/0}}r+   sub.html_tt_modulesr4   rI   r+   r   # base.html:1z# sub.html:1)	r   r
   r&   r   r   r   r   r   r   )r   r   	exc_stackr(   s      @r   test_error_line_number_modulez,StackTraceTest.test_error_line_number_module   s    	8 =9U$j-&HI
	9KK$--/II67  	9!,,.IOOOy89OONi78	9s   0A ABBc                     t        ddd      }	 |j                  d      j                          | j                  d       y # t        $ r( | j                  dt        j                         v        Y y w xY w)Nz{% include 'sub.html' %}r   r   r+   r   z# sub.html:1 (via base.html:1)r   r'   s     r   test_error_line_number_includez-StackTraceTest.test_error_line_number_include   sh    4)L
	XKK$--/II67  	XOO<	@T@T@VVW	X   0A .A21A2c                     t        ddd      }	 |j                  d      j                          | j                  d       | j                  dv        y # t        $ r t        j                         }Y 3w xY w)Nr   z{% extends 'base.html' %}r   r   r   r   )r   r&   r   r   r   r   r   r   )r   r(   r   s      r   )test_error_line_number_extends_base_errorz8StackTraceTest.test_error_line_number_extends_base_error  sn    #1LM
	/KK
#,,.II67 	945 ! 	/!,,.I	/s   0A A43A4c                     t        ddd      }	 |j                  d      j                          | j                  d       y # t        $ r( | j                  dt        j                         v        Y y w xY w)Nz{% block 'block' %}{% end %}zM
{% extends 'base.html' %}
{% block 'block' %}
{{1/0}}
{% end %}
            r   r   r   z# sub.html:4 (via base.html:1)r   r'   s     r   (test_error_line_number_extends_sub_errorz7StackTraceTest.test_error_line_number_extends_sub_error  sm    ;

	XKK
#,,.II67  	XOO<	@T@T@VVW	Xr   c                     t        dddd      }	 |j                  d      j                          | j                  d       y # t        $ r( | j                  dt        j                         v        Y y w xY w)Nz{% include 'b.html' %}z{% include 'c.html' %}r   )a.htmlzb.htmlzc.htmlr   r   z## c.html:1 (via b.html:1, a.html:1)r   r'   s     r   test_multi_includesz"StackTraceTest.test_multi_includes"  sm    22#
	KK!**,II67  	OO59M9M9OO	s   0A .A32A3N)
r{   r|   r}   r   r   r   r   r   r   r   r5   r   r   r   r      s)    GG9&X	6X$r   r   c                       e Zd Zd Zd Zy)ParseErrorDetailTestc                 v   t        ddi      }| j                  t              5 }|j                  d       d d d        | j	                  dt        j                               | j	                  d|j                  j                         | j	                  d|j                  j                         y # 1 sw Y   {xY w)Nfoo.htmlz

{{z'Missing end expression }} at foo.html:3r`   )	r   r9   r   r&   r   str	exceptionfilenamelineno)r   r(   cms      r   test_detailsz!ParseErrorDetailTest.test_details4  s    Z23z*bKK
# +BCDUVR\\%:%:;BLL//0	 +*s   B//B8c                 L    | j                  dt        t        d                   y )Nzasdf at None:0asdf)r   r   r   r:   s    r   test_custom_parse_errorz,ParseErrorDetailTest.test_custom_parse_error<  s     	)3z&/A+BCr   N)r{   r|   r}   r   r   r5   r   r   r   r   3  s    1Dr   r   c                   N    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zy)AutoEscapeTestc                 *    ddddddddd	d
dd| _         y )Nz'{% autoescape xhtml_escape %}{{ name }}z{% autoescape None %}{{ name }}z
{{ name }}zvescaped: {% include 'escaped.html' %}
unescaped: {% include 'unescaped.html' %}
default: {% include 'default.html' %}
zF{% autoescape xhtml_escape %}{% block name %}base: {{ name }}{% end %}z>{% autoescape None %}{% block name %}base: {{ name }}{% end %}zA{% autoescape xhtml_escape %}{% extends "unescaped_block.html" %}zn{% autoescape xhtml_escape %}{% extends "unescaped_block.html" %}{% block name %}extended: {{ name }}{% end %}z7{% autoescape None %}{% extends "escaped_block.html" %}zd{% autoescape None %}{% extends "escaped_block.html" %}{% block name %}extended: {{ name }}{% end %}zA{% autoescape xhtml_escape %}expr: {{ name }}
raw: {% raw name %})escaped.htmlunescaped.htmldefault.htmlinclude.htmlescaped_block.htmlunescaped_block.htmlescaped_extends_unescaped.html escaped_overrides_unescaped.htmlunescaped_extends_escaped.html unescaped_overrides_escaped.htmlraw_expression.html)	templatesr:   s    r   setUpzAutoEscapeTest.setUpC  s<    E?(
#-%-
/(11/&11$?#
r   c                    t        | j                  d       }d}| j                  |j                  d      j	                  |      d       | j                  |j                  d      j	                  |      d       | j                  |j                  d      j	                  |      d       | j                  |j                  d	      j	                  |      d
       y )N
autoescapeBobby <table>sr   r      Bobby &lt;table&gt;sr      Bobby <table>sr   r   sP   escaped: Bobby &lt;table&gt;s
unescaped: Bobby <table>s
default: Bobby <table>s
r   r   r   r&   r   r   r(   r   s      r   test_default_offzAutoEscapeTest.test_default_offi  s    DNNt<KK'00d0;=T	
 	KK()222=?P	
 	KK'00d0;=N	
 	KK'00d0;)	
r   c                    t        | j                  d      }d}| j                  |j                  d      j	                  |      d       | j                  |j                  d      j	                  |      d       | j                  |j                  d	      j	                  |      d       | j                  |j                  d
      j	                  |      d       y )Nxhtml_escaper   r   r   r   r   r   r   r   r   sV   escaped: Bobby &lt;table&gt;s
unescaped: Bobby <table>s
default: Bobby &lt;table&gt;s
r   r   s      r   test_default_onzAutoEscapeTest.test_default_on}  s    DNN~FKK'00d0;=T	
 	KK()222=?P	
 	KK'00d0;=T	
 	KK'00d0;/	
r   c                     t        | j                        }d}| j                  |j                  d      j	                  |      d       | j                  |j                  d      j	                  |      d       y )N<script>r   r      base: &lt;script&gt;r      base: <script>r   r   s      r   test_unextended_blockz$AutoEscapeTest.test_unextended_block  so    DNN+KK,-66D6A#	
 	KK./88d8CEV	
r   c                     t        | j                        fd}| j                   |d      d       | j                   |d      d       | j                   |d      d       | j                   |d      d	       y )
Nc                 F    j                  |       j                  d      S )Nr   r   r   r   r(   s    r   renderz2AutoEscapeTest.test_extended_block.<locals>.render  s     ;;t$--:->>r   r   r   r   s   extended: &lt;script&gt;r   r   r   s   extended: <script>r   r   r   r   r   r(   s     @r   test_extended_blockz"AutoEscapeTest.test_extended_block  s    DNN+	? 	 @ACTU568S	
 	346M	
 	568M	
r   c                 j    t        | j                        fd}| j                   |d      d       y )Nc                 F    j                  |       j                  d      S )Nz<>&"r   r   r   s    r   r   z2AutoEscapeTest.test_raw_expression.<locals>.render  s     ;;t$--6-::r   r   s#   expr: &lt;&gt;&amp;&quot;
raw: <>&"r   r   s     @r   test_raw_expressionz"AutoEscapeTest.test_raw_expression  s2    DNN+	; 	()+V	
r   c                      t        ddi       fdfd} j                   |dd      d        j                   |dd      d        j                   |dd	g      d
       y )Nzfoo.pyz){% autoescape py_escape %}s = {{ name }}
c                 j    j                  t        |       t               t        t	        |             S rO   )r   typebytesreprr   )rS   r   s    r   	py_escapez4AutoEscapeTest.test_custom_escape.<locals>.py_escape  s&    T!We,
1&&r   c                 H    j                  |       j                  |      S )N)r   r   r   )r   r   r(   r   s     r   r   z1AutoEscapeTest.test_custom_escape.<locals>.render  s"    ;;x(11ID1QQr   z<html>s   s = '<html>'
z';sys.exit()s   s = "';sys.exit()"
znot a strings   s = "['not a string']"
)r   r   )r   r   r(   r   s   ` @@r   test_custom_escapez!AutoEscapeTest.test_custom_escape  sn    X'STU	'	R 	(35FG.9;VW8n-.0O	
r   c                     t        ddi      }| j                  |j                  d      j                  t	        d            d       y )Nzfoo.txtzG{% for i in items
  %}{% if i > 0 %}, {% end %}{#
  #}{{i
  }}{% end
%}r_   )itemss   0, 1, 2, 3, 4)r   r   r&   r   ranger'   s     r   test_manual_minimize_whitespacez.AutoEscapeTest.test_manual_minimize_whitespace  sL      	
 	KK	"++%(+;=M	
r   c                    t        dddddd      }| j                  |j                  d      j                         d       | j                  |j                  d	      j                         d
       | j                  |j                  d      j                         d       | j                  |j                  d      j                         d       | j                  |j                  d      j                         d       y )Nz   
	
 asdf	   z 


	 qwer     z	    zxcv

z  {% include baz.txt %} 
 z		{% include foo.html %}    )r   bar.jsbaz.txtr   include.txtr   s   
asdf r   s   
qwer r   s   	    zxcv

r   s    	    zxcv


r   s   		
asdf     r%   r'   s     r   test_whitespace_by_filenamez*AutoEscapeTest.test_whitespace_by_filename  s    1/+ =?
 	Z099;ZHX.779:FY/88:<MN 	^4==?AUV]3<<>@RSr   c                    ddd}t        |d      }| j                  |j                  d      j                         d       | j                  |j                  d      j                         d	       t        |d
      }| j                  |j                  d      j                         d       | j                  |j                  d      j                         d       t        |d      }| j                  |j                  d      j                         d       | j                  |j                  d      j                         d       y )Nz		foo

z		bar

)r   bar.txtall)
whitespacer   s   		foo

r   s   		bar

singles    foo
s    bar
onelines    foo s    bar r%   )r   r   r(   s      r   test_whitespace_by_loaderz(AutoEscapeTest.test_whitespace_by_loader  s    !.=I	I%8Z099;^LY/88:NKI(;Z099;YGY/88:IFI)<Z099;XFY/88:HEr   c                 |    t        ddi      }| j                  |j                  d      j                         d       y )Nr   zz{% whitespace oneline %}
    {% for i in range(3) %}
        {{ i }}
    {% end %}
{% whitespace all %}
    pre	formatted
s     0  1  2  
    pre	formatted
r%   r'   s     r   test_whitespace_directivez(AutoEscapeTest.test_whitespace_directive  sA     	
 	KK
#,,.0T	
r   N)r{   r|   r}   r   r   r   r   r   r   r   r   r   r   r   r5   r   r   r   r   B  s<    $
L
(
(	

$

 
$T.F
r   r   c                       e Zd Zd Zd Zy)TemplateLoaderTestc                     t        t        j                  j                  t        j                  j	                  t
              d            | _        y )Nr   )r	   osr   ri   dirname__file__r(   r:   s    r   r   zTemplateLoaderTest.setUp  s*    RWW\\"''//(*C[QRr   c                     | j                   j                  d      }|j                         }| j                  t	        |      j                         d       y )Nz	utf8.htmlu   Héllo)r(   r&   r   r   r   strip)r   tmplrk   s      r   test_utf8_in_filez$TemplateLoaderTest.test_utf8_in_file  s?    {{,F+113\Br   N)r{   r|   r}   r   r  r5   r   r   r   r     s    SCr   r   )r   r   r~   tornado.escaper   r   r   tornado.templater   r   r   r	   tornado.utilr
   typingTestCaser   r   r   r   r   r5   r   r   <module>r     s    	   7 7 E E # zM8$$ zMzgX&& gTD8,, DL
X&& L
^C** Cr   