Rc@s0dZddlZddlmZddlmZmZmZmZm Z ddl m Z m Z m Z mZddlmZmZmZmZmZeZerdZneZdefd YZd efd YZd efd YZdefdYZdefdYZdZdS(s jinja2.testsuite.lexnparse ~~~~~~~~~~~~~~~~~~~~~~~~~~ All the unittests regarding lexing, parsing and syntax. :copyright: (c) 2010 by the Jinja Team. :license: BSD, see LICENSE for more details. iN(t JinjaTestCase(t EnvironmenttTemplatetTemplateSyntaxErrortUndefinedErrortnodes(tnextt iteritemst text_typetPY2(tTokent TokenStreamt TOKEN_EOFtTOKEN_BLOCK_BEGINtTOKEN_BLOCK_ENDcCst|dS(Ni(trepr(tstring((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pytjinja_string_reprstTokenStreamTestCasecBs>eZededededgZdZdZRS(iticCs-t|jdd}t|t|dS(Ntfootbar(R t test_tokensR(tselftts((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyt test_simple%s  cCs/gt|jddD]}|j^q}dS(NRR(R Rttype(Rttt token_types((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyt test_iter3s+(t__name__t __module__R R RRRR(((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyR s t LexerTestCasecBsYeZdZdZdZdZdZdZdZdZ dZ RS( cCstjd}dS(NsH{% raw %}foo{% endraw %}|{%raw%}{{ bar }}|{% baz %}{% endraw %}(tenvt from_string(Rttmpl((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyt test_raw1:scCstjd}dS(Ns'1 {%- raw -%} 2 {%- endraw -%} 3(R!R"(RR#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyt test_raw2?scCs(tdddd}|jd}dS(Ns{%s%}s${t}sC{% for item in seq %}${{'foo': item}|upper}{% endfor %}(RR"(RR!R#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_balancingCs cCs(tdddd}|jd}dS(Nst{R&sG(RR"(RR!R#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyt test_commentsIs cCs.x'dD]}tjdt|}qWdS( Nuu♨uäu u u s{{ %s }}(uu♨uäu u u (R!R"R(RtcharR#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_string_escapesTs cCs#ddlm}tjd}dS(Ni(tpformatu&{{ 'foo'|pprint }}|{{ 'bär'|pprint }}(tpprintR,R!R"(RR,R#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_bytefallbackZscCscddlm}xLt|D]>\}}|dkr;qntjjd|}t|qWdS(Ni(t operatorss([{}])s{{ %s }}(t jinja2.lexerR/RR!tlexerttokenizeR(RR/ttesttexpecttstream((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_operators_s  cCs?x8dD]0}td|}|jd}|j}qWdS(Ns s s tnewline_sequences 1 2 3 4 (s s s (RR"trender(RtseqR!R#tresult((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_normalizinghs   cCsxttgD]}td|}xsdifdifdidt6fdidt6fgD]9\}}|j|}|j||}|j}q[Wq WdS(Ntkeep_trailing_newlineRs no newlines with newline s with newlineswith several swith several (tTruetFalseRR"tgetR8(RtkeepR!ttemplatetexpectedR#R4R:((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_trailing_newlineos    ( RRR$R%R'R)R+R.R6R;RC(((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyR 8s      tParserTestCasecBsPeZdZdZdZdZdZdZdZdZ RS(cCs.tdddddd}|jd}dS(Nsss_ (RR"(RR!R#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_php_syntaxs cCs.tdddddd}|jd}dS(Ns<%s%>s<%=s<%#s]<%# I'm a comment, I'm not interesting %><% for item in seq -%> <%= item %> <%- endfor %>(RR"(RR!R#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_erb_syntaxs cCs.tdddddd}|jd}dS(Nss${R&s ${item} (RR"(RR!R#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_comment_syntaxs cCstjd}dS(Ns{{{'foo':'bar'}.foo}}(R!R"(RR#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyR'scCstjd}dS(NsR{# foo comment and bar comment #} {% macro blub() %}foo{% endmacro %} {{ blub() }}(R!R"(RR#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_start_comments c Csatddddddd}|jd}tdddddddd}|jd }dS( Ns<%s%>s${R&s<%#t%s><%# regular comment %> % for item in seq: ${item} % endfors##sW<%# regular comment %> % for item in seq: ${item} ## the rest of the stuff % endfor(RR"(RR!R#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_line_syntaxs ! c Csdtdddddddd}|jd }tdddddddd}|jd }dS( Ns{%s%}s${R&s/*s*/s##t#sw/* ignore me. I'm a multiline comment */ ## for item in seq: * ${item} # this is just extra stuff ## endfors/* ignore me. I'm a multiline comment */ # for item in seq: * ${item} ## this is just extra stuff ## extra stuff i just want to ignore # endfor(RR"(RR!R#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_line_syntax_prioritys! ! cCs[d}|dd|dd|dd|dd |d d |d d dS(NcSs(yt|Wntk r#}nXdS(N(RR(tcodeRBte((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyt assert_errors s#{% for item in seq %}...{% endif %}sEncountered unknown tag 'endif'. Jinja was looking for the following tags: 'endfor' or 'else'. The innermost block that needs to be closed is 'for'.s<{% if foo %}{% for item in seq %}...{% endfor %}{% endfor %}sEncountered unknown tag 'endfor'. Jinja was looking for the following tags: 'elif' or 'else' or 'endif'. The innermost block that needs to be closed is 'if'.s {% if foo %}sUnexpected end of template. Jinja was looking for the following tags: 'elif' or 'else' or 'endif'. The innermost block that needs to be closed is 'if'.s{% for item in seq %}sUnexpected end of template. Jinja was looking for the following tags: 'endfor' or 'else'. The innermost block that needs to be closed is 'for'.s{% block foo-bar-baz %}spBlock names in Jinja have to be valid Python identifiers and may not contain hyphens, use an underscore instead.s{% unknown_tag %}s&Encountered unknown tag 'unknown_tag'.((RRO((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_error_messagess ( RRRERFRGR'RHRJRLRP(((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyRD}s    tSyntaxTestCasecBs1eZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZ dZ!d Z"RS(!cCs,t}d|jd<|jd}dS(NcSs|||||S(N((tatbtcRNtg((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pytsRs2{{ foo('a', c='d', e='f', *['b'], **{'g': 'h'}) }}(RtglobalsR"(RR!R#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyt test_calls cCstjd}dS(Ns({{ [1, 2, 3][:] }}|{{ [1, 2, 3][::-1] }}(R!R"(RR#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyt test_slicingscCstjd}dS(Ns{{ foo.bar }}|{{ foo['bar'] }}(R!R"(RR#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyt test_attrscCstjd}dS(Ns{{ foo[0] }}|{{ foo[-1] }}(R!R"(RR#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_subscriptscCstjd}dS(Ns {{ () }}|{{ (1,) }}|{{ (1, 2) }}(R!R"(RR#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyt test_tuplescCstjd}dS(Ns${{ (1 + 1 * 2) - 3 / 2 }}|{{ 2**3 }}(R!R"(RR#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyt test_mathscCstjd}dS(Ns${{ 3 // 2 }}|{{ 3 / 2 }}|{{ 3 % 2 }}(R!R"(RR#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_divscCstjd}dS(Ns{{ +3 }}|{{ -3 }}(R!R"(RR#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyt test_unary scCstjd}dS(Ns{{ [1, 2] ~ 'foo' }}(R!R"(RR#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyt test_concatscCstjd}dS(Ns>{{ 1 > 0 }}|{{ 1 >= 1 }}|{{ 2 < 3 }}|{{ 2 == 2 }}|{{ 1 <= 1 }}(R!R"(RR#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyt test_comparescCstjd}dS(Ns-{{ 1 in [1, 2, 3] }}|{{ 1 not in [1, 2, 3] }}(R!R"(RR#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyt test_inopscCstjd}dS(Ns{{ [] }}|{{ {} }}|{{ () }}(R!R"(RR#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyt test_literalsscCstjd}dS(Ns8{{ true and false }}|{{ false or true }}|{{ not false }}(R!R"(RR#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyt test_bool!scCstjd}dS(Ns8{{ (true and false) or (false and true) and not false }}(R!R"(RR#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyt test_grouping&scCstjd}dS(Ns!{{ [1, 2, 3].0 }}|{{ [[1]].0.0 }}(R!R"(RR#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_django_attr*scCstjd}dS(Ns{{ 0 if true else 1 }}(R!R"(RR#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_conditional_expression.scCs5tjd}tjd}|jt|jdS(Ns<{{ 1 if false }}>s<{{ (1 if false).bar }}>(R!R"t assert_raisesRR8(RR#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyt!test_short_conditional_expression2scCstjd}dS(Ns{{ "foo"|upper + "bar"|upper }}(R!R"(RR#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_filter_priority9sc Cstdftdftdftdftdftdftdftdftd ftd fg }xH|D]@\}}|r|jttjd |qgtjd |qgWdS( Ns *foo, bars *foo, *bars *foo, bar=42s **foo, *bars **foo, barsfoo, bars foo, bar=42sfoo, bar=23, *argssa, b=c, *d, **es *foo, **bars {{ foo(%s) }}sfoo(%s)(R=R>RhRR!R"(Rttestst should_failtsig((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_function_calls=s          cCs-x&ddddddddgD]}qWdS( Ns{{ () }}s {{ (1, 2) }}s {{ (1, 2,) }}s{{ 1, }}s {{ 1, 2 }}s({% for foo, bar in seq %}...{% endfor %}s&{% for x in foo, bar %}...{% endfor %}s"{% for x in foo, %}...{% endfor %}((RR#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_tuple_exprQs cCstjd}dS(Ns){{ (1, 2,) }}|{{ [1, 2,] }}|{{ {1: 2,} }}(R!R"(RR#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_trailing_comma^scCs'tjd|jttjddS(Ns${% block foo %}...{% endblock foo %}s{% block x %}{% endblock y %}(R!R"RhR(R((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_block_end_namebs cCsXxQttdfD]@}tjdt|t|jt|jf}qWdS(Ns{{ %s }}|{{ %s }}|{{ %s }}(R=R>tNoneR!R"tstrtlowertupper(RtconstR#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_constant_casinggs 1cCs|jttjddS(Ns{{ foo is string is sequence }}(RhRR!R"(R((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_test_chainingnscCstjd}dS(Ns{{ "foo" "bar" "baz" }}(R!R"(RR#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_string_concatenationtscCstd}tjd}dS(Nids{{ not 42 in bar }}(trangeR!R"(RRR#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyt test_notinxs cCs)dtfdY}tjd}dS(NtFoocBseZdZRS(cSs|S(N((Rtx((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyt __getitem__s(RRR~(((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyR|~ss{{ foo[1, 2] }}(tobjectR!R"(RR|R((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_implicit_subscribed_tuple}scCstjd}dS(Ns,{% raw %}{{ FOO }} and {% BAR %}{% endraw %}(R!R"(RR#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyR%scCstjd}dS(NsP{{ true }}|{{ false }}|{{ none }}|{{ none is defined }}|{{ missing is defined }}(R!R"(RR#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyt test_constscCstjd}dS(Ns {{ -1|foo }}(R!tparse(Rtnode((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_neg_filter_priorityscCs=d}d}x*||fD]}|jttj|qWdS(Ns{% set true = 42 %}s!{% for none in seq %}{% endfor %}(RhRR!R"(Rt constass1t constass2R#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_const_assignscCstjd}dS(NsO{% set foo = 0 %}{% for item in [1, 2] %}{% set foo = 1 %}{% endfor %}{{ foo }}(R!R"(RR#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyt test_localsets cCs"tjd}tjd}dS(Ns{{ -foo["bar"] }}s{{ -foo["bar"]|abs }}(R!R"(RR#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_parse_unarys(#RRRXRYRZR[R\R]R^R_R`RaRbRcRdReRfRgRiRjRnRoRpRqRwRxRyR{RR%RRRRR(((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyRQsB                               tLstripBlocksTestCasecBseZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZdZdZdZdZdZdZRS(cCs(tdtdt}|jd}dS(Nt lstrip_blockst trim_blockss! {% if True %} {% endif %}(RR=R>R"(RR!R#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyt test_lstripscCs(tdtdt}|jd}dS(NRRs! {% if True %} {% endif %}(RR=R"(RR!R#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_lstrip_trimscCs(tdtdt}|jd}dS(NRRs# {%+ if True %} {%+ endif %}(RR=R>R"(RR!R#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_no_lstripscCs(tdtdt}|jd}dS(NRRs- hello{% if True %} goodbye{% endif %}(RR=R>R"(RR!R#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_lstrip_endlinescCs(tdtdt}|jd}dS(NRRs% {% if True %}hello {% endif %}(RR=R>R"(RR!R#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_lstrip_inlinescCs(tdtdt}|jd}dS(NRRs: {% if True %}a {% if True %}b {% endif %}c {% endif %}(RR=R>R"(RR!R#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_lstrip_nestedscCs(tdtdt}|jd}dS(NRRs. abc {% if True %} hello{% endif %}(RR=R>R"(RR!R#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_lstrip_left_charss cCs(tdtdt}|jd}dS(NRRs& {% set x = " {% str %} " %}{{ x }}(RR=R>R"(RR!R#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_lstrip_embeded_stringsscCs(tdtdt}|jd}dS(NRRs {% set hello = 1 %}(RR=R>R"(RR!R#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyt%test_lstrip_preserve_leading_newlinesscCs(tdtdt}|jd}dS(NRRs% {# if True #} hello {#endif#}(RR=R>R"(RR!R#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_lstrip_comments c Cs@tdddddddddtd t}|jd }dS( Ns<%s%>s${R&s<%#RIs##RRs% <% if True %>hello <% endif %>(RR=R"(RR!R#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyt test_lstrip_angle_bracket_simplesc Cs@tdddddddddtd t}|jd }dS( Ns<%s%>s${R&s<%#RIs##RRs' <%# if True %>hello <%# endif %>(RR=R"(RR!R#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyt!test_lstrip_angle_bracket_commentsc Cs@tdddddddddtd t}|jd }dS( Ns<%s%>s${R&s<%#RIs##RRse <%# regular comment %> <% for item in seq %> ${item} ## the rest of the stuff <% endfor %>(RR=R"(RR!R#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_lstrip_angle_brackets  c Cs@tdddddddddtd t}|jd }dS( Ns<%s%>s${R&s<%#RIs##RRs_ <%#regular comment%> <%for item in seq%> ${item} ## the rest of the stuff <%endfor%>(RR=R"(RR!R#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyt!test_lstrip_angle_bracket_compacts  c Cs:tdddddddtdt}|jd}dS( NssRRsp (RR=R"(RR!R#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_php_syntax_with_manuals  c Cs:tdddddddtdt}|jd}dS( NssRRsn (RR=R"(RR!R#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyREs  c Cs:tdddddddtdt}|jd}dS( NssRRsh (RR=R"(RR!R#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_php_syntax_compacts  c Cs:tdddddddtdt}|jd}dS(Ns<%s%>s<%=s<%#RRse<%# I'm a comment, I'm not interesting %> <% for item in seq %> <%= item %> <% endfor %> (RR=R"(RR!R#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyRFs  c Cs:tdddddddtdt}|jd}dS(Ns<%s%>s<%=s<%#RRsj<%# I'm a comment, I'm not interesting %> <% for item in seq -%> <%= item %> <%- endfor %>(RR=R"(RR!R#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_erb_syntax_with_manual,s  c Cs:tdddddddtdt}|jd}dS(Ns<%s%>s<%=s<%#RRsk<%# I'm a comment, I'm not interesting %> <%+ for item in seq -%> <%= item %> <%- endfor %>(RR=R"(RR!R#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyttest_erb_syntax_no_lstrip6s  c Cs:tdddddddtdt}|jd}dS( Nss${R&s ${item} (RR=R"(RR!R#((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyRG@s  (RRRRRRRRRRRRRRRRRRERRFRRRG(((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyRs*              cCs~tj}|jtjt|jtjt|jtjt|jtjt|jtjt|S(N( tunittestt TestSuitetaddTestt makeSuiteRR RDRQR(tsuite((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyRJs (t__doc__Rtjinja2.testsuiteRtjinja2RRRRRtjinja2._compatRRRR R0R R R R RR!RRRR RDRQRR(((s>/usr/lib/python2.7/site-packages/jinja2/testsuite/lexnparse.pyt s ("(  Eo