U
    ycH                     @   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   g/var/www/sbouman/reportbro-server-tornado/env/lib/python3.8/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   s2   zt td tdW n tk
r,   Y nX 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   s2   zt td tdW n tk
r,   Y nX 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   sT   t ddi}z|d  | d W n& tk
rN   | dt k Y nX 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   sT   t ddi}z|d  | d W n& tk
rN   | dt k Y nX 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 n8 tk
r   t }| d|k | d|k Y nX d S )Nc                    s    d k	st  | jf |S r<   )AssertionErrorr   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   sV   t ddd}z|d  | d W n& tk
rP   | dt k Y nX 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   sZ   t ddd}z|d  | d W n tk
rF   t }Y nX | d|k 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   sV   t ddd}z|d  | d W n& tk
rP   | dt k Y nX 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   sX   t dddd}z|d  | d W n& tk
rR   | dt k Y nX 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   sb   t ddi}| t}|d W 5 Q R X | 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   r   r   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   r   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   r   )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

Z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