o
    Qœ_ìH  ã                   @   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   ú</usr/lib/python3/dist-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.htmlÚ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   re   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   rl   )Nr7   z'one
two{%if 1/0%}
three{%end%}
        rm   rn   ro   r#   r   r   r   Ú test_error_line_number_directiveÙ   rv   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.htmlÚ_tt_modulesr-   r<   r&   rm   ú# base.html:1z# sub.html:1)	r   r	   r"   r   rp   rq   rs   rt   rr   )r   r|   Ú	exc_stackr   r{   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&   rm   z# sub.html:1 (via base.html:1)ro   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   rm   r�   )r   r"   r   rp   rq   rs   rt   rr   )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   rm   z# sub.html:4 (via base.html:1)ro   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ˆ   rm   z## c.html:1 (via b.html:1, a.html:1)ro   r#   r   r   r   Útest_multi_includes"  s   ýÿ

ÿÿz"StackTraceTest.test_multi_includesN)
rf   rg   rh   ru   rw   rƒ   r…   r†   r‡   r‰   r   r   r   r   rk   É   s    
rk   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:0Úasdf)r   r�   r   r1   r   r   r   Útest_custom_parse_error<  s   z,ParseErrorDetailTest.test_custom_parse_errorN)rf   rg   rh   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©Ú
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 )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©   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   rx   r   r{   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   r{   r   Útest_extended_blockœ  s   
ÿÿÿz"AutoEscapeTest.test_extended_blockc                    s*   t | jƒ‰ ‡ fdd„}|  |dƒd¡ d S )Nc                    r²   )Nz<>&"r   rx   r   r{   r   r   r³   ±  r´   z2AutoEscapeTest.test_raw_expression.<locals>.renderr    s#   expr: &lt;&gt;&amp;&quot;
raw: <>&"rµ   r¶   r   r{   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   rx   )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
Ú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)rf   rg   rh   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   Úosry   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)rf   rg   rh   r¢   rÒ   r   r   r   r   rÌ     s    rÌ   )rÍ   rs   ri   Útornado.escaper   r   r   Útornado.templater   r   r   r   Útornado.utilr	   ÚtypingÚTestCaser
   rk   r‹   r•   rÌ   r   r   r   r   Ú<module>   s     >j P