a
    õ£Beá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   ú]/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/tornado/test/template_test.pyÚtest_simple   s    zTemplateTest.test_simplec                 C   s$   t dƒ}|  |jtdƒd�d¡ d S r   ©r   r   r   r   r   r   r   r   Ú
test_bytes   s    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   s$   t dƒ}|  |jtdƒd�d¡ d S )Nz Hello{# TODO i18n #} {{ name }}!r   r   r   r   r   r   r   r   Útest_comment   s    zTemplateTest.test_commentc                 C   s(   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   s(   t dddœƒ}|  | d¡ ¡ d¡ d S )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   s   t dƒS )Nz{{©r   r   r   r   r   Ú<lambda>F   ó    z,TemplateTest.test_escaping.<locals>.<lambda>c                   S   s   t dƒS )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
   {{ "Ã©" }}r/   r0   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   r>   c   s    z&TemplateTest.test_apply.<locals>.upperz{% apply upper %}foo{% end %}r=   s   FOOr0   ©r   r>   r   r   r   r   Ú
test_applyb   s    zTemplateTest.test_applyc                 C   s0   dd„ }t tdƒƒ}|  |j|d�tdƒ¡ d S )Nc                 S   s   t | ƒ ¡ S r<   )r   r>   r?   r   r   r   r>   j   s    z.TemplateTest.test_unicode_apply.<locals>.upperõ    {% apply upper %}foo Ã©{% end %}r=   õ   FOO Ã‰r0   rA   r   r   r   Útest_unicode_applyi   s    zTemplateTest.test_unicode_applyc                 C   s0   dd„ }t tdƒƒ}|  |j|d�tdƒ¡ d S )Nc                 S   s   t t| ƒ ¡ ƒS r<   )r   r   r>   r?   r   r   r   r>   q   s    z,TemplateTest.test_bytes_apply.<locals>.upperrC   r=   rD   r0   rA   r   r   r   Útest_bytes_applyp   s    zTemplateTest.test_bytes_applyc                 C   s8   t tdƒƒ}|  |jdd�d¡ |  |jdd�d¡ d S )Nz&{% if x > 4 %}yes{% else %}no{% end %}é   r6   s   yesé   s   nor0   r   r   r   r   Útest_ifw   s    zTemplateTest.test_ifc                 C   s    t tdƒƒ}|  | ¡ d¡ d S )Nz {% if True %}{% else %}{% end %}r+   r0   r   r   r   r   Útest_if_empty_body|   s    zTemplateTest.test_if_empty_bodyc                 C   s8   t tdƒƒ}|  |jdd�d¡ |  |jdd�d¡ d S )Nzb{% try %}
try{% set y = 1/x %}
{% except %}-except
{% else %}-else
{% finally %}-finally
{% end %}r5   r6   s   
try
-else
-finally
r   s   
try-except
-finally
r0   r   r   r   r   Útest_try€   s    ÿÿ
zTemplateTest.test_tryc                 C   s    t tdƒƒ}|  | ¡ d¡ d S )Nz{% comment blah blah %}foos   foor0   r   r   r   r   Útest_comment_directiveŽ   s    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 %}r+   s   013456)r   r   r   ÚjoinÚsplitr   )r   r   Úresultr   r   r   Útest_break_continue’   s    ÿÿz TemplateTest.test_break_continuec                 C   s0   zt tdƒƒ tdƒ‚W n ty*   Y n0 d S )Nz{% break %}úDid not get expected exception©r   r   Ú	Exceptionr   r-   r   r   r   Útest_break_outside_loop¦   s
    z$TemplateTest.test_break_outside_loopc                 C   s0   zt tdƒƒ tdƒ‚W n ty*   Y n0 d S )Nz={% for i in [] %}{% apply foo %}{% break %}{% end %}{% end %}rQ   rR   r-   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   s&   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(   r.   r1   r2   r;   rB   rE   rF   rI   rJ   rK   rL   rP   rT   rU   ÚunittestÚskiprW   rX   r   r   r   r   r
      s0   

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   sR   t ddiƒ}z| d¡ ¡  |  d¡ W n$ tyL   |  dt ¡ v ¡ Y n0 d S )Nr3   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Ê   s    ÿÿz0StackTraceTest.test_error_line_number_expressionc                 C   sR   t ddiƒ}z| d¡ ¡  |  d¡ W n$ tyL   |  dt ¡ v ¡ Y n0 d S )Nr3   z'one
two{%if 1/0%}
three{%end%}
        r_   r`   ra   r    r   r   r   Ú test_error_line_number_directiveÙ   s    ÿÿ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 n6 ty~   t ¡ }|  d|v ¡ |  d|v ¡ Y n0 d S )Nc                    s"   ˆ d usJ ‚ˆ   | ¡jf i |¤ŽS r<   ©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)   r8   r#   r_   ú# base.html:1z# sub.html:1)	r   r	   r   r   rb   rc   re   rf   rd   )r   rm   Ú	exc_stackr   rl   r   Útest_error_line_number_moduleè   s    þz,StackTraceTest.test_error_line_number_modulec                 C   sT   t dddœƒ}z| d¡ ¡  |  d¡ W n$ tyN   |  dt ¡ v ¡ Y n0 d S )Nz{% include 'sub.html' %}rn   ro   r#   r_   z# sub.html:1 (via base.html:1)ra   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yD   t ¡ }Y n0 |  d|v ¡ d S )Nrn   z{% extends 'base.html' %}ro   rp   r_   rq   )r   r   r   rb   rc   re   rf   rd   )r   r!   rr   r   r   r   Ú)test_error_line_number_extends_base_error  s    ÿz8StackTraceTest.test_error_line_number_extends_base_errorc                 C   sT   t dddœƒ}z| d¡ ¡  |  d¡ W n$ tyN   |  dt ¡ v ¡ Y n0 d S )Nz{% block 'block' %}{% end %}zM
{% extends 'base.html' %}
{% block 'block' %}
{{1/0}}
{% end %}
            ro   rp   r_   z# sub.html:4 (via base.html:1)ra   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 n$ tyP   |  dt ¡ v ¡ Y n0 d S )Nz{% include 'b.html' %}z{% include 'c.html' %}rn   )úa.htmlzb.htmlzc.htmlrw   r_   z## c.html:1 (via b.html:1, a.html:1)ra   r    r   r   r   Útest_multi_includes"  s    ýÿ
ÿz"StackTraceTest.test_multi_includesN)
rY   rZ   r[   rg   rh   rs   rt   ru   rv   rx   r   r   r   r   r^   É   s   
r^   c                   @   s   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 s60    Y  |  dt|jƒ¡ |  d|jj¡ |  d|jj¡ d S )Núfoo.htmlz

{{z'Missing end expression }} at foo.html:3rH   )	r   r,   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   r-   r   r   r   Útest_custom_parse_error<  s    z,ParseErrorDetailTest.test_custom_parse_errorN)rY   rZ   r[   r€   r�   r   r   r   r   ry   3  s   ry   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)Ú	templatesr-   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  s    ÿÿÿþ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}  s    ÿÿÿþ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                    s   ˆ   | ¡jdd�S )Nr˜   r   ri   r   rl   r   r   ÚrenderŸ  s    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   rl   r   Útest_extended_blockœ  s    
ÿÿÿz"AutoEscapeTest.test_extended_blockc                    s*   t | jƒ‰ ‡ fdd„}|  |dƒd¡ d S )Nc                    s   ˆ   | ¡jdd�S )Nz<>&"r   ri   r   rl   r   r   rœ   ±  s    z2AutoEscapeTest.test_raw_expression.<locals>.renderr�   s#   expr: &lt;&gt;&amp;&quot;
raw: <>&"r�   rž   r   rl   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   r?   r-   r   r   Ú	py_escape»  s    z4AutoEscapeTest.test_custom_escape.<locals>.py_escapec                    s   ˆ   | ¡jˆ|d�S )N)r¤   r   ri   )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
%}rG   )Ú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 %}    )rz   úbar.jsúbaz.txtr†   úinclude.txtrz   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

)rz   úbar.txtÚall)Ú
whitespacerz   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   s&   t ddiƒ}|  | d¡ ¡ d¡ d S )Nrz   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)rY   rZ   r[   r�   r–   r—   r›   rŸ   r    r¥   r¨   r¬   r±   r²   r   r   r   r   r‚   B  s   &
r‚   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚTemplateLoaderTestc                 C   s    t tj tj t¡d¡ƒ| _d S )NrŽ   )r   Úosrj   rM   ÚdirnameÚ__file__r!   r-   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   ÚtmplrO   r   r   r   Útest_utf8_in_file  s    z$TemplateLoaderTest.test_utf8_in_fileN)rY   rZ   r[   r�   r¹   r   r   r   r   r³     s   r³   )r´   re   r\   Ztornado.escaper   r   r   Ztornado.templater   r   r   r   Ztornado.utilr	   ÚtypingZTestCaser
   r^   ry   r‚   r³   r   r   r   r   Ú<module>   s    >j P