2021-09-08 08:46:40 +00:00
""" Djlint tests specific to django.
run : :
pytest tests / test_django . py - - cov = src / djlint - - cov - branch \
- - cov - report xml : coverage . xml - - cov - report term - missing
for a single test , run : :
2022-01-24 18:03:44 +00:00
pytest tests / test_django . py : : test_comment - - cov = src / djlint \
2021-09-08 08:46:40 +00:00
- - cov - branch - - cov - report xml : coverage . xml - - cov - report term - missing
"""
# pylint: disable=C0116
from typing import TextIO
from click . testing import CliRunner
from . conftest import reformat
2021-10-06 13:41:21 +00:00
def test_empty_tags_on_one_line ( runner : CliRunner , tmp_file : TextIO ) - > None :
output = reformat ( tmp_file , runner , b " { % i f stuff % } \n { % e ndif % } " )
2021-11-29 11:19:07 +00:00
assert output . text == """ { % i f stuff % } { % e ndif % } \n """
assert output . exit_code == 1
2021-10-06 13:41:21 +00:00
2021-09-08 08:46:40 +00:00
def test_dj_comments_tag ( runner : CliRunner , tmp_file : TextIO ) - > None :
output = reformat (
tmp_file , runner , b " { # comment #} \n { % i f this % }<div></div> { % e ndif % } "
)
2021-11-29 11:19:07 +00:00
assert output . text == """ { # comment #} \n { % i f this % }<div></div> { % e ndif % } \n """
2021-09-08 08:46:40 +00:00
# no change was required
2021-11-29 11:19:07 +00:00
assert output . exit_code == 0
2021-09-08 08:46:40 +00:00
def test_reformat_asset_tag ( runner : CliRunner , tmp_file : TextIO ) - > None :
# pylint: disable=C0301
output = reformat (
tmp_file ,
runner ,
b """ { % block css % } { % a ssets " css_error " % }<link type= " text/css " rel= " stylesheet " href= " {{ ASSET_URL }} " /> { % e ndassets % } { % e ndblock css % } """ ,
) # noqa: E501
assert (
2021-11-29 11:19:07 +00:00
output . text
2021-09-08 08:46:40 +00:00
== """ { % block css % }
{ % assets " css_error " % }
< link type = " text/css " rel = " stylesheet " href = " {{ ASSET_URL }} " / >
{ % endassets % }
{ % endblock css % }
"""
)
2021-11-29 11:19:07 +00:00
assert output . exit_code == 1
2021-09-08 08:46:40 +00:00
def test_autoescape ( runner : CliRunner , tmp_file : TextIO ) - > None :
output = reformat (
tmp_file , runner , b " { % a utoescape on % } {{ body }} { % e ndautoescape % } "
)
2021-11-29 11:19:07 +00:00
assert output . exit_code == 1
2021-09-08 08:46:40 +00:00
assert (
2021-11-29 11:19:07 +00:00
output . text
2021-09-08 08:46:40 +00:00
== r """ { % a utoescape on % }
{ { body } }
{ % endautoescape % }
"""
)
def test_comment ( runner : CliRunner , tmp_file : TextIO ) - > None :
output = reformat (
tmp_file , runner , b """ { % c omment " Optional note " % } {{ body }} { % e ndcomment % } """
)
2021-11-29 11:19:07 +00:00
assert output . exit_code == 0
2021-09-08 08:46:40 +00:00
# too short to put on multiple lines
assert (
2021-11-29 11:19:07 +00:00
output . text
2021-09-08 08:46:40 +00:00
== r """ { % c omment " Optional note " % } {{ body }} { % e ndcomment % }
"""
)
2022-01-07 14:49:00 +00:00
output = reformat (
tmp_file ,
runner ,
b """ <div class= " hi " >
< div class = " poor " >
< p class = " format " >
Lorem ipsum dolor
< span class = " bold " > sit < / span >
amet
< / p >
< img src = " ./pic.jpg " >
< / div >
< script src = " file1.js " > < / script >
{ % comment % } < script src = " file2.js " > < / script >
< script src = " file3.js " > < / script > { % endcomment % }
< script src = " file4.js " > < / script >
< / div > """ ,
)
assert output . exit_code == 0
output = reformat (
tmp_file ,
runner ,
b """ <div class= " hi " >
< div class = " poor " >
{ # djlint:off #}
< p class = " format " >
Lorem ipsum dolor < span class = " bold " > sit < / span > amet
< / p >
{ # djlint:on #}
< img src = " ./pic.jpg " >
< / div >
< ul >
{ % for i in items % }
< li > item { { i } } < / li >
{ % if i > 10 % } { % endif % }
< li > item { { i } } < / li >
{ % endfor % }
< / ul >
< / div >
""" ,
)
assert output . exit_code == 0
2022-01-24 18:03:44 +00:00
output = reformat (
tmp_file ,
runner ,
b """ <html>
< head >
< script src = " file1.js " > < / script >
{ % comment % }
< script src = " file2.js " > < / script >
< script src = " file3.js " > < / script >
< script src = " file4.js " > < / script >
{ % endcomment % }
< script src = " file5.js " > < / script >
< / head >
< body >
< / body >
< / html >
""" ,
)
assert output . exit_code == 0
output = reformat (
tmp_file ,
runner ,
b """ <html>
< head >
< script src = " file1.js " > < / script >
{ # djlint:off #}
{ % comment % }
< script src = " file2.js " > < / script >
< script src = " file3.js " > < / script >
< script src = " file4.js " > < / script >
{ % endcomment % }
{ # djlint:on #}
< script src = " file5.js " > < / script >
< / head >
< body >
< / body >
< / html >
""" ,
)
assert output . exit_code == 0
2021-09-08 08:46:40 +00:00
2021-09-21 10:50:35 +00:00
def test_inline_comment ( runner : CliRunner , tmp_file : TextIO ) - > None :
output = reformat (
tmp_file , runner , b " { # <div></div> #} \n { % i f this % }<div></div> { % e ndif % } "
)
2021-11-29 11:19:07 +00:00
assert output . text == """ { # <div></div> #} \n { % i f this % }<div></div> { % e ndif % } \n """
assert output . exit_code == 0
2021-09-21 10:50:35 +00:00
2021-09-08 08:46:40 +00:00
def test_for_loop ( runner : CliRunner , tmp_file : TextIO ) - > None :
output = reformat (
tmp_file ,
runner ,
b """ <ul> { % f or athlete in athlete_list % }<li> {{ athlete.name }}</li> { % e mpty % }<li>Sorry, no athletes in this list.</li> { % e ndfor % }</ul> """ ,
)
2021-11-29 11:19:07 +00:00
assert output . exit_code == 1
2021-09-08 08:46:40 +00:00
assert (
2021-11-29 11:19:07 +00:00
output . text
2021-09-08 08:46:40 +00:00
== r """ <ul>
{ % for athlete in athlete_list % }
< li > { { athlete . name } } < / li >
{ % empty % }
< li > Sorry , no athletes in this list . < / li >
{ % endfor % }
< / ul >
"""
)
def test_filter ( runner : CliRunner , tmp_file : TextIO ) - > None :
output = reformat (
tmp_file ,
runner ,
b """ { % f ilter force_escape|lower % }This text will be HTML-escaped, and will appear in all lowercase. { % e ndfilter % } """ ,
)
2021-11-29 11:19:07 +00:00
assert output . exit_code == 1
2021-09-08 08:46:40 +00:00
assert (
2021-11-29 11:19:07 +00:00
output . text
2021-09-08 08:46:40 +00:00
== r """ { % f ilter force_escape|lower % }
This text will be HTML - escaped , and will appear in all lowercase .
{ % endfilter % }
"""
)
def test_if ( runner : CliRunner , tmp_file : TextIO ) - > None :
output = reformat (
tmp_file ,
runner ,
b """ { % i f athlete_list % }Number of athletes: {{ athlete_list|length }} { % e lif athlete_in_locker_room_list % }Athletes should be out of the locker room soon! { % e lse % }No athletes. { % e ndif % } """ ,
)
2021-11-29 11:19:07 +00:00
assert output . exit_code == 1
2021-09-08 08:46:40 +00:00
assert (
2021-11-29 11:19:07 +00:00
output . text
2021-09-08 08:46:40 +00:00
== r """ { % i f athlete_list % }
Number of athletes : { { athlete_list | length } }
{ % elif athlete_in_locker_room_list % }
Athletes should be out of the locker room soon !
{ % else % }
No athletes .
{ % endif % }
"""
)
def test_ifchanged ( runner : CliRunner , tmp_file : TextIO ) - > None :
output = reformat (
tmp_file ,
runner ,
b """ { % f or match in matches % }<div style= " background-color: " pink " > { % i fchanged match.ballot_id % } { % c ycle " red " " blue " % } { % e lse % }gray { % e ndifchanged % } {{ match }}</div> { % e ndfor % } """ ,
)
2021-11-29 11:19:07 +00:00
assert output . exit_code == 1
2021-09-08 08:46:40 +00:00
assert (
2021-11-29 11:19:07 +00:00
output . text
2021-09-08 08:46:40 +00:00
== r """ { % f or match in matches % }
< div style = " background-color: " pink " >
{ % ifchanged match . ballot_id % }
{ % cycle " red " " blue " % }
{ % else % }
gray
{ % endifchanged % }
{ { match } }
< / div >
{ % endfor % }
"""
)
def test_include ( runner : CliRunner , tmp_file : TextIO ) - > None :
output = reformat ( tmp_file , runner , b """ { % i nclude " this " % } { % i nclude " that " % } """ )
2021-11-29 11:19:07 +00:00
assert output . exit_code == 1
2021-09-08 08:46:40 +00:00
assert (
2021-11-29 11:19:07 +00:00
output . text
2021-09-08 08:46:40 +00:00
== r """ { % i nclude " this " % }
{ % include " that " % }
"""
)
def test_spaceless ( runner : CliRunner , tmp_file : TextIO ) - > None :
output = reformat (
tmp_file ,
runner ,
b """ { % s paceless % }<p><a href= " foo/ " >Foo</a></p> { % e ndspaceless % } """ ,
)
2021-11-29 11:19:07 +00:00
assert output . exit_code == 1
2021-09-08 08:46:40 +00:00
assert (
2021-11-29 11:19:07 +00:00
output . text
2021-09-08 08:46:40 +00:00
== r """ { % s paceless % }
< p >
< a href = " foo/ " > Foo < / a >
< / p >
{ % endspaceless % }
"""
)
def test_templatetag ( runner : CliRunner , tmp_file : TextIO ) - > None :
output = reformat (
tmp_file ,
runner ,
b """ { % templatetag openblock % } url ' entry_list ' { % templatetag closeblock % } """ ,
)
2021-11-29 11:19:07 +00:00
assert output . exit_code == 0
2021-09-08 08:46:40 +00:00
assert (
2021-11-29 11:19:07 +00:00
output . text
2021-09-08 08:46:40 +00:00
== r """ { % templatetag openblock % } url ' entry_list ' { % templatetag closeblock % }
"""
)
def test_verbatim ( runner : CliRunner , tmp_file : TextIO ) - > None :
output = reformat (
tmp_file , runner , b """ { % verbatim % }Still alive. { % e ndverbatim % } """
)
2021-11-29 11:19:07 +00:00
assert output . exit_code == 1
2021-09-08 08:46:40 +00:00
assert (
2021-11-29 11:19:07 +00:00
output . text
2021-09-08 08:46:40 +00:00
== r """ { % verbatim % }
Still alive .
{ % endverbatim % }
"""
)
def test_blocktranslate ( runner : CliRunner , tmp_file : TextIO ) - > None :
output = reformat (
tmp_file ,
runner ,
b """ { % blocktranslate % }The width is: {{ width }} { % e ndblocktranslate % } """ ,
)
2021-11-29 11:19:07 +00:00
assert output . exit_code == 0
2021-11-16 10:40:29 +00:00
assert (
2021-11-29 11:19:07 +00:00
output . text
2021-11-16 10:40:29 +00:00
== r """ { % blocktranslate % }The width is: {{ width }} { % e ndblocktranslate % }
"""
)
output = reformat (
tmp_file ,
runner ,
b """ { % blocktranslate trimmed % }The width is: {{ width }} { % e ndblocktranslate % } """ ,
)
2022-01-07 14:49:00 +00:00
assert output . exit_code == 0
2021-09-08 08:46:40 +00:00
2021-11-24 08:18:58 +00:00
output = reformat (
tmp_file ,
runner ,
b """ { % blocktrans % }The width is: {{ width }} { % e ndblocktrans % } """ ,
)
2021-11-29 11:19:07 +00:00
assert output . exit_code == 0
2021-11-24 08:18:58 +00:00
assert (
2021-11-29 11:19:07 +00:00
output . text
2021-11-24 08:18:58 +00:00
== r """ { % blocktrans % }The width is: {{ width }} { % e ndblocktrans % }
"""
)
output = reformat (
tmp_file ,
runner ,
b """ { % blocktrans trimmed % }The width is: {{ width }} { % e ndblocktrans % } """ ,
)
2022-01-07 14:49:00 +00:00
assert output . exit_code == 0
2021-11-24 08:18:58 +00:00
2022-01-06 19:41:15 +00:00
output = reformat (
tmp_file ,
runner ,
b """ <p>
{ % blocktrans % } If you have not created an account yet , then please
< a href = " {{ signup_url }} " > sign up < / a > first . { % endblocktrans % }
< / p > \n """ ,
)
assert output . exit_code == 0
2021-09-08 08:46:40 +00:00
2021-10-06 06:54:43 +00:00
def test_trans ( runner : CliRunner , tmp_file : TextIO ) - > None :
output = reformat (
tmp_file , runner , b """ <p> { % trans ' Please do <b>Blah</b>. ' % }</p> """
)
2021-11-29 11:19:07 +00:00
assert output . exit_code == 1
2021-10-06 06:54:43 +00:00
assert (
""" <p>
{ % trans ' Please do <b>Blah</b>. ' % }
< / p >
"""
2021-11-29 11:19:07 +00:00
in output . text
2021-10-06 06:54:43 +00:00
)
2021-09-08 08:46:40 +00:00
def test_with ( runner : CliRunner , tmp_file : TextIO ) - > None :
output = reformat (
tmp_file ,
runner ,
2021-10-14 10:19:48 +00:00
b """ { % with total=business.employees.count % } {{ total }}<div>employee</div> {{ total|pluralize }} { % e ndwith % } """ ,
2021-09-08 08:46:40 +00:00
)
2021-11-29 11:19:07 +00:00
assert output . exit_code == 1
2021-09-08 08:46:40 +00:00
assert (
2021-11-29 11:19:07 +00:00
output . text
2021-09-08 08:46:40 +00:00
== r """ { % with total=business.employees.count % }
2021-10-14 10:19:48 +00:00
{ { total } }
< div > employee < / div >
{ { total | pluralize } }
2021-09-08 08:46:40 +00:00
{ % endwith % }
"""
)
2021-10-05 08:19:28 +00:00
def test_load_tag ( runner : CliRunner , tmp_file : TextIO ) - > None :
output = reformat (
tmp_file ,
runner ,
b """ { % block content % } { % lo ad i18n % } { % e ndblock % } """ ,
)
2021-11-29 11:19:07 +00:00
assert output . exit_code == 1
2021-10-05 08:19:28 +00:00
assert (
2021-11-29 11:19:07 +00:00
output . text
2021-10-05 08:19:28 +00:00
== r """ { % block content % }
{ % load i18n % }
{ % endblock % }
"""
)
2021-10-06 07:42:08 +00:00
2021-11-29 11:19:07 +00:00
def test_single_line_tag ( runner : CliRunner , tmp_file : TextIO ) - > None :
2021-10-29 07:02:48 +00:00
output = reformat (
tmp_file ,
runner ,
2021-11-29 11:19:07 +00:00
b """ { % i f messages|length % } { % f or message in messages % } {{ message }} { % e ndfor % } { % e ndif % } """ ,
2021-10-29 07:02:48 +00:00
)
2021-11-29 11:19:07 +00:00
assert output . exit_code == 1
2021-10-29 07:02:48 +00:00
assert (
2021-11-29 11:19:07 +00:00
output . text
== r """ { % i f messages|length % }
{ % for message in messages % } { { message } } { % endfor % }
{ % endif % }
2021-10-29 07:02:48 +00:00
"""
)
2021-10-06 07:42:08 +00:00
def test_multiple_endblocks ( runner : CliRunner , tmp_file : TextIO ) - > None :
output = reformat (
tmp_file ,
runner ,
b """ { % block content % } { % block scripts % } { % e ndblock % } { % e ndblock % } """ ,
)
2021-11-29 11:19:07 +00:00
assert output . exit_code == 1
2021-10-06 07:42:08 +00:00
assert (
""" { % block content % } \n { % block scripts % } { % e ndblock % } \n { % e ndblock % }
"""
2021-11-29 11:19:07 +00:00
== output . text
2021-10-06 07:42:08 +00:00
)