o !b(@s ddlZddlZddlZddlZddlmZddlmZmZm Z m Z m Z Gdddej Z Gdddej ZGdd d ej ZGd d d ej ZGd d d ej ZGdddej ZGdddej ZGdddej ZGdddej ZGdddej ZddZedkrejdddSdS)N) IS_PYTHON2) directivesMarkupTemplate TextTemplateTemplateRuntimeErrorTemplateSyntaxErrorc@s(eZdZdZddZddZddZdS) AttrsDirectiveTestCasez,Tests for the `py:attrs` template directive.cCs8td}ddiddig}|d|j|djdddS) zM Verify that the directive has access to the loop variables. zt idz; itemsNencodingr assertEqualgeneraterender)selftmplr rB/usr/lib/python3/dist-packages/genshi/template/tests/directives.pytest_combined_with_loops z.AttrsDirectiveTestCase.test_combined_with_loopcC$td}|d|jdddS)} Verify that an attribute value that evaluates to `None` removes an existing attribute of that name. zu z2 Nrrrrrrrtest_update_existing_attr'z0AttrsDirectiveTestCase.test_update_existing_attrcCr)rzt z& Nrrrrrrtest_remove_existing_attr3rz0AttrsDirectiveTestCase.test_remove_existing_attrN)__name__ __module__ __qualname____doc__rrrrrrrrs  rc@sxeZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ ddZddZddZdS)ChooseDirectiveTestCasezoTests for the `py:choose` template directive and the complementary directives `py:when` and `py:otherwise`.cCr)zd Verify that, if multiple `py:when` bodies match, only the first is output. z
1 2 3
z-
1
Nrrrrrrtest_multiple_true_whensDz0ChooseDirectiveTestCase.test_multiple_true_whenscC$td}|d|jdddS)Nz
hidden hello
z1
hello
rrrrrrtest_otherwiseRz&ChooseDirectiveTestCase.test_otherwisecCr)z? Verify that `py:choose` blocks can be nested: z
2 3
zw
3
Nrrrrrr test_nesting[z$ChooseDirectiveTestCase.test_nestingcCr)z. Verify more complex nesting. z
OK FAIL
x
OK
Nrrrrrrtest_complex_nestingor*z,ChooseDirectiveTestCase.test_complex_nestingcCr)z> Verify more complex nesting using otherwise. a
FAIL OK
r+Nrrrrrrtest_complex_nesting_otherwiser*z6ChooseDirectiveTestCase.test_complex_nesting_otherwisecCr)zs Verify that a when directive with a strip directive actually strips of the outer element. z
foo
z1 foo Nrrrrrrtest_when_with_stripr%z,ChooseDirectiveTestCase.test_when_with_stripcCtd}|tt|dS)zq Verify that a `when` directive outside of a `choose` directive is reported as an error. zZ
Nr assertRaisesrstrrrrrrtest_when_outside_choosez0ChooseDirectiveTestCase.test_when_outside_choosecCr/)zw Verify that an `otherwise` directive outside of a `choose` directive is reported as an error. z]
Nr0rrrrtest_otherwise_outside_chooser4z5ChooseDirectiveTestCase.test_otherwise_outside_choosecCr/)zw Verify that an `when` directive that doesn't have a `test` attribute is reported as an error. z
foo
Nr0rrrrtest_when_without_testsz.ChooseDirectiveTestCase.test_when_without_testcC(td}|d|jddjdddS)z Verify that an `when` directive that doesn't have a `test` attribute works as expected as long as the parent `choose` directive has a test expression. z
foo
$ foo YeahfooNrrrrrr,test_when_without_test_but_with_choose_valueszDChooseDirectiveTestCase.test_when_without_test_but_with_choose_valuecCr)zf Verify that an `otherwise` directive can be used without a `test` attribute. z
foo
r8Nrrrrrrtest_otherwise_without_testr%z3ChooseDirectiveTestCase.test_otherwise_without_testcCr)K Verify that the directive can also be used as an element. z 1 2 3 z" 1 Nrrrrrrtest_as_elementsz'ChooseDirectiveTestCase.test_as_elementcCr)Q Verify that the directive works as expected in a text template. z#choose #when 1 == 1 1 #end #when 2 == 2 2 #end #when 3 == 3 3 #end #endz 1 Nrrrrrrrrrtest_in_text_templates z-ChooseDirectiveTestCase.test_in_text_templateN)rr r!r"r$r'r)r,r-r.r3r5r6r<r=r?rBrrrrr#@s     r#c@sheZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ ddZdS)DefDirectiveTestCasez*Tests for the `py:def` template directive.cCr)z| Verify that a named template function with a strip directive actually strips of the outer element. z
${what}
${echo('foo')}
+ foo Nrrrrrrtest_function_with_stripz-DefDirectiveTestCase.test_function_with_stripcCr&)Nz

${greeting}, ${name}!

z[

hello, world!

rrrrrrtest_exec_in_replacez)DefDirectiveTestCase.test_exec_in_replacecCr)r>z ${what} ${echo('foo')} rDNrrrrrrr?#z$DefDirectiveTestCase.test_as_elementcCr7)z Verify that a template function defined inside a conditional block can be called from outside that block. a* ${what} ${what} ${echo('foo')} z3 foo T)semanticNrrrrrrtest_nested_defs1s z%DefDirectiveTestCase.test_nested_defscCr)zN Verify that keyword arguments work with `py:def` directives. z ${what} ${echo('foo')} z" foo Nrrrrrrtest_function_with_default_argCsz3DefDirectiveTestCase.test_function_with_default_argcCr&)Nz ${what or 'something'}

bar

z5

bar

rrrrrrtest_invocation_in_attributeOr(z1DefDirectiveTestCase.test_invocation_in_attributecCr&)Nz ${None}

bar

z)

bar

rrrrrr!test_invocation_in_attribute_noneXr(z6DefDirectiveTestCase.test_invocation_in_attribute_nonecCs*dd}td}|tt|j|ddS)NcSst)N) TypeErrorrrrrbadfuncbszEDefDirectiveTestCase.test_function_raising_typeerror..badfuncz
${badfunc()}
)rP)rr1rOlistr)rrPrrrrtest_function_raising_typeerrorasz4DefDirectiveTestCase.test_function_raising_typeerrorcCr&)Na ${select('*')} ${maketitle(True)} ? True rrrrrrtest_def_in_matchedlz(DefDirectiveTestCase.test_def_in_matchedcCr)r@z #def echo(greeting, name='world') ${greeting}, ${name}! #end ${echo('Hi', name='you')} z) Hi, you! NrrArrrrrBxsz*DefDirectiveTestCase.test_in_text_templatecCr)zi Verify that a named template function using "star arguments" works as expected. z
${repr(args)} ${repr(sorted(kwargs.items()))}
${f(1, 2, a=3, b=4)}
zi
[1, 2] [('a', 3), ('b', 4)]
Nrrrrrrtest_function_with_star_argssz1DefDirectiveTestCase.test_function_with_star_argsN)rr r!r"rErGr?rKrLrMrNrRrTrBrVrrrrrCs      rCc@s@eZdZdZddZddZddZdd Zd d Zd d Z dS)ForDirectiveTestCasez*Tests for the `py:for` template directive.cC.td}|d|jtdddjdddS)zk Verify that the combining the `py:for` directive with `py:strip` works correctly. z
${item}
} 1 2 3 4 5 r r Nrrrrrangerrrrrtest_loop_with_stripsz)ForDirectiveTestCase.test_loop_with_stripcCrX)r>z ${item} rYr rZr Nrr[rrrrr?sz$ForDirectiveTestCase.test_as_elementcCr7)zC Verify that assignment to tuples works correctly. z

key=$k, value=$v

zX

key=a, value=1

key=b, value=2

)ar br r Nrrrrrrtest_multi_assignments  z*ForDirectiveTestCase.test_multi_assignmentcCs0td}|d|jtddgdjdddS)J Verify that assignment to nested tuples works correctly. z

$idx: key=$k, value=$v

z^

0: key=a, value=1

1: key=b, value=2

r^r`r Nr)rrr enumeraterrrrrtest_nested_assignments z+ForDirectiveTestCase.test_nested_assignmentc Cstddd}zt|jdd|dWdStyx}zVt|dks,t|dks,Jt\}}}|j}g}|jrF|j}| ||js;t rJd nd }| d ||d j j j| d|d j j j| d |d jWYd}~dSd}~ww)rcz $item test.htmlfilename r:zExpected TemplateRuntimeErrorziteration over non-sequencez'int' object is not iterablez u'iter(foo)'z 'iter(foo)'zr N)rrQrfailrOr2sysexc_infotb_nextappendrrtb_framef_codeco_name co_filename tb_lineno) rreexc_type exc_value exc_tracebackframeframesexpected_iter_strrrrtest_not_iterables4       z&ForDirectiveTestCase.test_not_iterablec Csztddd|dWdStyA}z%|d|jtjdddkr6|d|jWYd}~dSWYd}~dSd}~ww)z9 Verify an empty 'for' value is an error z empty rfrgExpectedTemplateSyntaxErrorNr )r ) rrrkrrrhrl version_infolinenorrurrrtest_for_with_empty_valuesz.ForDirectiveTestCase.test_for_with_empty_valueN) rr r!r"r]r?rbrer|rrrrrrWs rWc@ eZdZdZddZddZdS)IfDirectiveTestCasez)Tests for the `py:if` template directive.cC*td}|d|jdddjdddS)zj Verify that the combining the `py:if` directive with `py:strip` works correctly. zk ${bar} $ Hello THellor;barNrrrrrrr]sz(IfDirectiveTestCase.test_loop_with_stripcCr)r>zf ${bar} rTrrNrrrrrrr?sz#IfDirectiveTestCase.test_as_elementN)rr r!r"r]r?rrrrr s rc@seZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ ddZddZddZddZddZd d!Zd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/Zd0d1Zd2d3Zd4d5Zd6S)7MatchDirectiveTestCasez,Tests for the `py:match` template directive.cCr) Verify that a match template can produce the same kind of element that it matched without entering an infinite recursion. z
${select('text()')}
Hey Joe
@
Hey Joe
Nrrrrrrtest_with_strip*rFz&MatchDirectiveTestCase.test_with_stripcCr)rz
${select('text()')}
Hey Joe
zc
Hey Joe
Nrrrrrrtest_without_strip9sz)MatchDirectiveTestCase.test_without_stripcCr)r>z
${select('text()')}
Hey Joe
rNrrrrrrr?JrIz&MatchDirectiveTestCase.test_as_elementcCr)z Match directives are applied recursively, meaning that they are also applied to any content they may have produced themselves: a#
${select('*')}
z
Nrrrrrrtest_recursive_match_1Xs  z-MatchDirectiveTestCase.test_recursive_match_1cCr)z When two or more match templates match the same element and also themselves output the element they match, avoiding recursion is even more complex, but should work. aS