o
    E¾±hmH  ã                   @   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   ú[/var/www/html/Persson_Maskin/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   ó@   | j tdd ttdƒƒ W d   ƒ d S 1 sw   Y  d S )NúDid not get expected exception©Úmsgz{% break %}©r0   r   r   r   r1   r   r   r   Útest_break_outside_loop¦   s   "ÿz$TemplateTest.test_break_outside_loopc                 C   r[   )Nr\   r]   z={% for i in [] %}{% apply foo %}{% break %}{% end %}{% end %}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`   ra   ÚunittestÚskiprc   rf   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   rm   )Nr7   z'one
two{%if 1/0%}
three{%end%}
        rn   ro   rp   r#   r   r   r   Ú test_error_line_number_directiveÓ   rw   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&   rn   ú# base.html:1z# sub.html:1)	r   r	   r"   r   rq   rr   rt   ru   rs   )r   r}   Ú	exc_stackr   r|   r   Útest_error_line_number_moduleâ   s   þýz,StackTraceTest.test_error_line_number_modulec                 C   sT   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&   rn   z# sub.html:1 (via base.html:1)rp   r#   r   r   r   Útest_error_line_number_includeõ   s   ÿÿz-StackTraceTest.test_error_line_number_includec                 C   sV   t dddœƒ}z| d¡ ¡  |  d¡ W n ty"   t ¡ }Y nw |  d|¡ d S )Nr~   z{% extends 'base.html' %}r   r€   rn   r‚   )r   r"   r   rq   rr   rt   ru   ÚassertIn)r   r$   rƒ   r   r   r   Ú)test_error_line_number_extends_base_errorÿ   s   ÿÿz8StackTraceTest.test_error_line_number_extends_base_errorc                 C   sR   t dddœƒ}z| d¡ ¡  |  d¡ W d S  ty(   |  dt ¡ ¡ Y d S w )Nz{% block 'block' %}{% end %}zM
{% extends 'base.html' %}
{% block 'block' %}
{{1/0}}
{% end %}
            r   r€   rn   z# sub.html:4 (via base.html:1)©r   r"   r   rq   rr   r†   rt   ru   r#   r   r   r   Ú(test_error_line_number_extends_sub_error
  s   þÿÿz7StackTraceTest.test_error_line_number_extends_sub_errorc                 C   sT   t ddddœƒ}z| d¡ ¡  |  d¡ W d S  ty)   |  dt ¡ ¡ Y d S w )Nz{% include 'b.html' %}z{% include 'c.html' %}r~   )úa.htmlzb.htmlzc.htmlrŠ   rn   z## c.html:1 (via b.html:1, a.html:1)rˆ   r#   r   r   r   Útest_multi_includes  s   ýÿÿz"StackTraceTest.test_multi_includesN)
rg   rh   ri   rv   rx   r„   r…   r‡   r‰   r‹   r   r   r   r   rl   Ã   s    
rl   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_details,  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_error4  s   z,ParseErrorDetailTest.test_custom_parse_errorN)rg   rh   ri   r”   r–   r   r   r   r   r   +  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   ÚsetUp;  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_offa  ó   ÿÿÿþ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_onu  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   ry   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   ry   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   ry   )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   rd   )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)rg   rh   ri   r¤   r¬   r¯   r³   r¹   rº   r¿   rÂ   rÆ   rÌ   rÍ   r   r   r   r   r—   :  s    &
r—   c                   @   rŒ   )ÚTemplateLoaderTestc                 C   s    t tj tj t¡d¡ƒ| _d S )Nr£   )r   Úosrz   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)rg   rh   ri   r¤   rÔ   r   r   r   r   rÎ   	  s    rÎ   )rÏ   rt   rj   Útornado.escaper   r   r   Útornado.templater   r   r   r   Útornado.utilr	   ÚtypingÚTestCaser
   rl   r   r—   rÎ   r   r   r   r   Ú<module>   s     8h P