o
    )ZhH                     @   s   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 dejZG dd dejZG d	d
 d
ejZG dd dejZG dd dejZdS )    N)utf8
native_str
to_unicode)Template
DictLoader
ParseErrorLoader)
ObjectDictc                   @   s   e 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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#d$ Zd%d& Zd'd( Zd)d* Zed+d,d- Zd.d/ Zd0S )1TemplateTestc                 C   s    t d}| |jddd d S NzHello {{ name }}!Benname
   Hello Ben!r   assertEqualgenerateselftemplate r   Q/var/www/html/lang_env/lib/python3.10/site-packages/tornado/test/template_test.pytest_simple   s   zTemplateTest.test_simplec                 C   $   t d}| |jtddd d S r   r   r   r   r   r   r   r   r   
test_bytes      zTemplateTest.test_bytesc                 C   s   t d}| | d d S )Nz2 + 2 = {{ 2 + 2 }}s	   2 + 2 = 4r   r   r   r   r   test_expressions   s   zTemplateTest.test_expressionsc                 C   r   )Nz Hello{# TODO i18n #} {{ name }}!r   r   r   r   r   r   r   r   test_comment   r   zTemplateTest.test_commentc                 C   (   t ddd}| |d d d S )Nz%{% include "header.html" %}
body textzheader text)
index.htmlzheader.htmlr    s   header text
body textr   r   loadr   r   loaderr   r   r   test_include   s   zTemplateTest.test_includec                 C   r   )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#   r   r   r   test_extends(   s   zTemplateTest.test_extendsc                 C   s*   t dddd}| |d d d S )Nz{% include '2.html' %}z{% include '../b/3.html' %}ok)a/1.htmlza/2.htmlzb/3.htmlr*   s   okr!   r#   r   r   r   test_relative_load;   s   zTemplateTest.test_relative_loadc                 C   st   |  tdd  |  tdd  | td d | td d | td d	 | td
 d d S )Nc                   S      t dS )Nz{{r   r   r   r   r   <lambda>F       z,TemplateTest.test_escaping.<locals>.<lambda>c                   S   r,   )Nz{%r-   r   r   r   r   r.   G   r/   z{{!s   {{z{%!s   {%z{#!s   {#z{{ 'expr' }} {{!jquery expr}}s   expr {{jquery expr}})assertRaisesr   r   r   r   r   r   r   r   test_escapingE   s   
zTemplateTest.test_escapingc                 C   s$   t td}| | td d S )N   ér   r   r   r   r   r   r   r   test_unicode_templateP   s   z"TemplateTest.test_unicode_templatec                 C   s$   t td}| | td d S )Nu
   {{ "é" }}r3   r4   r   r   r   r   test_unicode_literal_expressionT   s   z,TemplateTest.test_unicode_literal_expressionc                 C   s2   t ddiddd id}| |d d d S )N	test.htmlz{{ inc(5) }}incc                 S   s   | d S )N   r   xr   r   r   r.   ^   r/   z4TemplateTest.test_custom_namespace.<locals>.<lambda>	namespace   6r!   r#   r   r   r   test_custom_namespace\   s   z"TemplateTest.test_custom_namespacec                 C   s,   dd }t td}| |j|dd d S )Nc                 S   s   |   S Nuppersr   r   r   rB   c   s   z&TemplateTest.test_apply.<locals>.upperz{% apply upper %}foo{% end %}rA   s   FOOr4   r   rB   r   r   r   r   
test_applyb   s   zTemplateTest.test_applyc                 C   0   dd }t td}| |j|dtd d S )Nc                 S   s   t |  S r@   )r   rB   rC   r   r   r   rB   j   s   z.TemplateTest.test_unicode_apply.<locals>.upper    {% apply upper %}foo é{% end %}rA      FOO Ér4   rE   r   r   r   test_unicode_applyi      zTemplateTest.test_unicode_applyc                 C   rG   )Nc                 S   s   t t|  S r@   )r   r   rB   rC   r   r   r   rB   q   s   z,TemplateTest.test_bytes_apply.<locals>.upperrH   rA   rI   r4   rE   r   r   r   test_bytes_applyp   rK   zTemplateTest.test_bytes_applyc                 C   8   t td}| |jddd | |jddd d S )Nz&{% if x > 4 %}yes{% else %}no{% end %}   r:   s   yes   s   nor4   r   r   r   r   test_ifw   s   zTemplateTest.test_ifc                 C       t td}| | d d S )Nz {% if True %}{% else %}{% end %}    r4   r   r   r   r   test_if_empty_body|      zTemplateTest.test_if_empty_bodyc                 C   rM   )Nzb{% try %}
try{% set y = 1/x %}
{% except %}-except
{% else %}-else
{% finally %}-finally
{% end %}r9   r:   s   
try
-else
-finally
r   s   
try-except
-finally
r4   r   r   r   r   test_try   s   
zTemplateTest.test_tryc                 C   rQ   )Nz{% comment blah blah %}foos   foor4   r   r   r   r   test_comment_directive   rT   z#TemplateTest.test_comment_directivec                 C   s2   t td}| }d| }| |d d S )Nz{% for i in range(10) %}
    {% if i == 2 %}
        {% continue %}
    {% end %}
    {{ i }}
    {% if i == 6 %}
        {% break %}
    {% end %}
{% end %}rR   s   013456)r   r   r   joinsplitr   )r   r   resultr   r   r   test_break_continue   s   z TemplateTest.test_break_continuec                 C   *   z
t td td ty   Y d S w )Nz{% break %}Did not get expected exceptionr   r   	Exceptionr   r1   r   r   r   test_break_outside_loop   s   z$TemplateTest.test_break_outside_loopc                 C   r[   )Nz={% for i in [] %}{% apply foo %}{% break %}{% end %}{% end %}r\   r]   r1   r   r   r   test_break_in_apply   s   z TemplateTest.test_break_in_applyzno testable future importsc                 C   s(   |  dd td}|  | d d S )Ng      ?z{{ 1 / 2 }}0)r   r   r   r   r   r   r   test_no_inherit_future   s   z#TemplateTest.test_no_inherit_futurec                 C   &   t ddi}| |d d d S )Nu
   tést.htmlZhellos   hellor!   r#   r   r   r   test_non_ascii_name   s   z TemplateTest.test_non_ascii_nameN)__name__
__module____qualname__r   r   r   r   r%   r(   r+   r2   r5   r6   r?   rF   rJ   rL   rP   rS   rU   rV   rZ   r_   r`   unittestskiprb   rd   r   r   r   r   r
      s2    

r
   c                   @   sD   e 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S )StackTraceTestc                 C   R   t ddi}z|d  | d W d S  ty(   | dt v  Y d S w )Nr7   zone
two{{1/0}}
three
        did not get expected exception# test.html:2r   r"   r   failZeroDivisionError
assertTrue	traceback
format_excr#   r   r   r   !test_error_line_number_expression      z0StackTraceTest.test_error_line_number_expressionc                 C   rk   )Nr7   z'one
two{%if 1/0%}
three{%end%}
        rl   rm   rn   r#   r   r   r    test_error_line_number_directive   ru   z/StackTraceTest.test_error_line_number_directivec                    s   d   fdd}t ddddt|did z d	  | d
 W d S  tyA   t }| d|v  | d|v  Y d S w )Nc                    s"    d usJ   | jdi |S )Nr   r"   r   )pathkwargsr$   r   r   load_generate   s   zCStackTraceTest.test_error_line_number_module.<locals>.load_generatez!{% module Template('sub.html') %}{{1/0}}r&   sub.htmlZ_tt_modulesr-   r<   r&   rl   # base.html:1z# sub.html:1)	r   r	   r"   r   ro   rp   rr   rs   rq   )r   r{   	exc_stackr   rz   r   test_error_line_number_module   s   z,StackTraceTest.test_error_line_number_modulec                 C   T   t ddd}z|d  | d W d S  ty)   | dt v  Y d S w )Nz{% include 'sub.html' %}r|   r}   r&   rl   z# sub.html:1 (via base.html:1)rn   r#   r   r   r   test_error_line_number_include   s   z-StackTraceTest.test_error_line_number_includec                 C   sX   t ddd}z|d  | d W n ty"   t }Y nw | d|v  d S )Nr|   z{% extends 'base.html' %}r}   r~   rl   r   )r   r"   r   ro   rp   rr   rs   rq   )r   r$   r   r   r   r   )test_error_line_number_extends_base_error  s   z8StackTraceTest.test_error_line_number_extends_base_errorc                 C   r   )Nz{% block 'block' %}{% end %}zM
{% extends 'base.html' %}
{% block 'block' %}
{{1/0}}
{% end %}
            r}   r~   rl   z# sub.html:4 (via base.html:1)rn   r#   r   r   r   (test_error_line_number_extends_sub_error  s   z7StackTraceTest.test_error_line_number_extends_sub_errorc                 C   sV   t dddd}z|d  | d W d S  ty*   | dt v  Y d S w )Nz{% include 'b.html' %}z{% include 'c.html' %}r|   )a.htmlzb.htmlzc.htmlr   rl   z## c.html:1 (via b.html:1, a.html:1)rn   r#   r   r   r   test_multi_includes"  s   

z"StackTraceTest.test_multi_includesN)
re   rf   rg   rt   rv   r   r   r   r   r   r   r   r   r   rj      s    
rj   c                   @      e Zd Zdd Zdd ZdS )ParseErrorDetailTestc                 C   sv   t ddi}| t}|d W d    n1 sw   Y  | dt|j | d|jj | d|jj d S )Nfoo.htmlz

{{z'Missing end expression }} at foo.html:3rO   )	r   r0   r   r"   r   str	exceptionfilenamelineno)r   r$   cmr   r   r   test_details4  s   z!ParseErrorDetailTest.test_detailsc                 C   s   |  dttd d S )Nzasdf at None:0Zasdf)r   r   r   r1   r   r   r   test_custom_parse_error<  s   z,ParseErrorDetailTest.test_custom_parse_errorN)re   rf   rg   r   r   r   r   r   r   r   3  s    r   c                   @   sd   e 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d Zdd Zdd ZdS )AutoEscapeTestc                 C   s"   ddddddddd	d
dd| _ d S )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)	templatesr1   r   r   r   setUpC  s   zAutoEscapeTest.setUpc                 C   s~   t | jd d}d}| |dj|dd | |dj|dd | |dj|dd | |d	j|dd
 d S )NZ
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   r   r   r   test_default_offi     zAutoEscapeTest.test_default_offc                 C   s~   t | jdd}d}| |dj|dd | |dj|dd | |d	j|dd | |d
j|dd d S )NZ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   r   r   r   test_default_on}  r   zAutoEscapeTest.test_default_onc                 C   sF   t | j}d}| |dj|dd | |dj|dd d S )N<script>r   r      base: &lt;script&gt;r      base: <script>r   r   r   r   r   test_unextended_block  s   
z$AutoEscapeTest.test_unextended_blockc                    sZ   t | j  fdd}| |dd | |dd | |dd | |d	d
 d S )Nc                         | jddS )Nr   r   rw   r   rz   r   r   render     z2AutoEscapeTest.test_extended_block.<locals>.renderr   r   r   s   extended: &lt;script&gt;r   r   r   s   extended: <script>r   r   r   r   r   r   rz   r   test_extended_block  s   
z"AutoEscapeTest.test_extended_blockc                    s*   t | j  fdd}| |dd d S )Nc                    r   )Nz<>&"r   rw   r   rz   r   r   r     r   z2AutoEscapeTest.test_raw_expression.<locals>.renderr   s#   expr: &lt;&gt;&amp;&quot;
raw: <>&"r   r   r   rz   r   test_raw_expression  s
   
z"AutoEscapeTest.test_raw_expressionc                    sb   t ddi fdd fdd}|ddd |dd	d
 |ddgd d S )Nzfoo.pyz){% autoescape py_escape %}s = {{ name }}
c                    s     t| t tt| S r@   )r   typebytesreprr   rC   r1   r   r   	py_escape  s   z4AutoEscapeTest.test_custom_escape.<locals>.py_escapec                    s     | j|dS )N)r   r   rw   )r   r   )r$   r   r   r   r     s   z1AutoEscapeTest.test_custom_escape.<locals>.renderz<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   r   r   test_custom_escape  s   z!AutoEscapeTest.test_custom_escapec                 C   s.   t ddi}| |djtddd d S )Nzfoo.txtzG{% for i in items
  %}{% if i > 0 %}, {% end %}{#
  #}{{i
  }}{% end
%}rN   )itemss   0, 1, 2, 3, 4)r   r   r"   r   ranger#   r   r   r   test_manual_minimize_whitespace  s   
z.AutoEscapeTest.test_manual_minimize_whitespacec                 C   s   t dddddd}| |d d | |d	 d
 | |d d | |d d | |d d d S )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#   r   r   r   test_whitespace_by_filename  s   z*AutoEscapeTest.test_whitespace_by_filenamec                 C   s   ddd}t |dd}| |d d | |d d	 t |d
d}| |d d | |d d t |dd}| |d d | |d d d S )Nz		foo

z		bar

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

r   s   		bar

singles    foo
s    bar
Zonelines    foo s    bar r!   )r   r   r$   r   r   r   test_whitespace_by_loader  s   
z(AutoEscapeTest.test_whitespace_by_loaderc                 C   rc   )Nr   zz{% whitespace oneline %}
    {% for i in range(3) %}
        {{ i }}
    {% end %}
{% whitespace all %}
    pre	formatted
s     0  1  2  
    pre	formatted
r!   r#   r   r   r   test_whitespace_directive  s   z(AutoEscapeTest.test_whitespace_directiveN)re   rf   rg   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   B  s    &
r   c                   @   r   )TemplateLoaderTestc                 C   s    t tjtjtd| _d S )Nr   )r   osrx   rW   dirname__file__r$   r1   r   r   r   r     s    zTemplateLoaderTest.setUpc                 C   s,   | j d}| }| t| d d S )Nz	utf8.htmlu   Héllo)r$   r"   r   r   r   strip)r   tmplrY   r   r   r   test_utf8_in_file  s   z$TemplateLoaderTest.test_utf8_in_fileN)re   rf   rg   r   r   r   r   r   r   r     s    r   )r   rr   rh   Ztornado.escaper   r   r   Ztornado.templater   r   r   r   Ztornado.utilr	   typingZTestCaser
   rj   r   r   r   r   r   r   r   <module>   s     >j P