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 : :
2021-10-11 13:08:32 +00:00
pytest tests / test_django . py : : test_complex_attributes - - 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 % } " )
assert output [ " text " ] == """ { % i f stuff % } { % e ndif % } \n """
assert output [ " exit_code " ] == 1
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 % } "
)
assert output [ " text " ] == """ { # comment #} \n { % i f this % }<div></div> { % e ndif % } \n """
# no change was required
assert output [ " exit_code " ] == 0
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 (
output [ " text " ]
== """ { % block css % }
{ % assets " css_error " % }
< link type = " text/css " rel = " stylesheet " href = " {{ ASSET_URL }} " / >
{ % endassets % }
{ % endblock css % }
"""
)
assert output [ " exit_code " ] == 1
def test_autoescape ( runner : CliRunner , tmp_file : TextIO ) - > None :
output = reformat (
tmp_file , runner , b " { % a utoescape on % } {{ body }} { % e ndautoescape % } "
)
assert output [ " exit_code " ] == 1
assert (
output [ " text " ]
== 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 % } """
)
assert output [ " exit_code " ] == 0
# too short to put on multiple lines
assert (
output [ " text " ]
== r """ { % c omment " Optional note " % } {{ body }} { % e ndcomment % }
"""
)
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 % } "
)
assert (
output [ " text " ] == """ { # <div></div> #} \n { % i f this % }<div></div> { % e ndif % } \n """
)
assert output [ " exit_code " ] == 0
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> """ ,
)
assert output [ " exit_code " ] == 1
assert (
output [ " text " ]
== 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 % } """ ,
)
assert output [ " exit_code " ] == 1
assert (
output [ " text " ]
== 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 % } """ ,
)
assert output [ " exit_code " ] == 1
assert (
output [ " text " ]
== 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 % } """ ,
)
assert output [ " exit_code " ] == 1
assert (
output [ " text " ]
== 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 " % } """ )
assert output [ " exit_code " ] == 1
assert (
output [ " text " ]
== 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 % } """ ,
)
assert output [ " exit_code " ] == 1
assert (
output [ " text " ]
== 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 % } """ ,
)
assert output [ " exit_code " ] == 0
assert (
output [ " text " ]
== 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 % } """
)
assert output [ " exit_code " ] == 1
assert (
output [ " text " ]
== 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 % } """ ,
)
assert output [ " exit_code " ] == 1
assert (
output [ " text " ]
== r """ { % blocktranslate % }
The width is : { { width } }
{ % endblocktranslate % }
"""
)
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> """
)
assert output [ " exit_code " ] == 1
assert (
""" <p>
{ % trans ' Please do <b>Blah</b>. ' % }
< / p >
"""
in output [ " text " ]
)
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
)
assert output [ " exit_code " ] == 1
assert (
output [ " text " ]
== 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 % }
"""
)
def test_single_line_tag ( runner : CliRunner , tmp_file : TextIO ) - > None :
output = reformat (
tmp_file ,
runner ,
b """ { % i f messages|length % } { % f or message in messages % } {{ message }} { % e ndfor % } { % e ndif % } """ ,
)
assert output [ " exit_code " ] == 1
assert (
output [ " text " ]
== r """ { % i f messages|length % }
{ % for message in messages % } { { message } } { % endfor % }
{ % endif % }
"""
)
2021-09-20 09:33:23 +00:00
def test_complex_attributes ( runner : CliRunner , tmp_file : TextIO ) - > None :
output = reformat (
tmp_file ,
runner ,
b """ <img data-src= " { % i f report.imgs.exists % } {{ report.imgs.first.get_absolute_url|size: " 96x96 " }} { % e lse % } { % s tatic ' /img/report_thumb_placeholder_400x300.png ' % } { % e ndif % } " src= " { % s tatic ' /img/loader.gif ' % } " alt= " report image " /> """ ,
)
assert output [ " exit_code " ] == 1
2021-10-06 11:17:04 +00:00
2021-09-20 09:33:23 +00:00
assert (
output [ " text " ]
2021-10-06 11:17:04 +00:00
== r """ <img data-src= " { % i f report.imgs.exists % }
{ { report . imgs . first . get_absolute_url | size : " 96x96 " } }
{ % else % }
{ % static ' /img/report_thumb_placeholder_400x300.png ' % }
{ % endif % } "
2021-09-20 09:33:23 +00:00
src = " { % s tatic ' /img/loader.gif ' % } "
alt = " report image " / >
"""
)
output = reformat (
tmp_file ,
runner ,
2021-10-07 06:22:29 +00:00
b """ <a class= " asdf { % i f favorite == " yes " % }favorite { % e ndif % } has-tooltip-arrow has-tooltip-right " data-tooltip= " { % i f favorite == " yes " % }Remove from Favorites { % e lse % }Add to Favorites { % e ndif % } " fav-type= " report " object-id= " {{ report.report_id }} " ><span class= " icon has-text-grey is-large " ><i class= " fas fa-lg fa-star " ></i></span></a> """ ,
2021-09-20 09:33:23 +00:00
)
assert output [ " exit_code " ] == 1
2021-10-06 11:17:04 +00:00
2021-09-20 09:33:23 +00:00
assert (
output [ " text " ]
2021-10-07 06:22:29 +00:00
== r """ <a class= " asdf
{ % if favorite == " yes " % }
2021-10-06 11:17:04 +00:00
favorite
{ % endif % }
has - tooltip - arrow has - tooltip - right "
2021-10-06 11:40:19 +00:00
data - tooltip = " { % i f favorite == " yes " % }
2021-10-06 11:17:04 +00:00
Remove from Favorites
{ % else % }
Add to Favorites
{ % endif % } "
2021-09-20 09:33:23 +00:00
fav - type = " report "
object - id = " {{ report.report_id }} " >
< span class = " icon has-text-grey is-large " >
< i class = " fas fa-lg fa-star " > < / i >
< / span >
< / a >
"""
)
output = reformat (
tmp_file ,
runner ,
b """ <div class= " media-content " { % i fchanged comment.stream_id % } comments-msg { % e lse % } comments-newMsgReply { % e ndifchanged % }> """ ,
)
assert output [ " exit_code " ] == 1
2021-10-06 11:17:04 +00:00
2021-09-20 09:33:23 +00:00
assert (
output [ " text " ]
== r """ <div class= " media-content "
2021-10-06 11:17:04 +00:00
{ % ifchanged comment . stream_id % }
comments - msg
{ % else % }
comments - newMsgReply
{ % endifchanged % } >
2021-09-20 09:33:23 +00:00
"""
)
2021-10-06 07:19:37 +00:00
output = reformat (
tmp_file ,
runner ,
b """ <a class= " piwik_download " href= " { % s tatic activity_version.get_win_document_with_images_file_path % }? { % now " jSFYHi " % } " > """ ,
)
assert (
output [ " text " ]
== """ <a class= " piwik_download "
href = " { % s tatic activity_version.get_win_document_with_images_file_path % }? { % now " jSFYHi " % } " >
"""
)
assert output [ " exit_code " ] == 1
2021-10-05 08:19:28 +00:00
2021-10-06 11:17:04 +00:00
output = reformat (
tmp_file ,
runner ,
2021-10-06 11:40:19 +00:00
b """ <span { %i f a % }required { %e ndif % }title= " { % i f eev.status == eev.STATUS_CURRENT % } { % trans ' A ' % } { % e lif eev.status == eev.STATUS_APPROVED % } { % trans ' B ' % } { % e lif eev.status == eev.STATUS_EXPIRED % } { % trans ' C ' % } { % e ndif % } " class= " asdf { %i f a % }b { %e ndif % } asdf " { %i f a % }checked { %e ndif % }> """ ,
2021-10-06 11:17:04 +00:00
)
2021-10-06 11:40:19 +00:00
2021-10-06 11:17:04 +00:00
assert (
output [ " text " ]
== """ <span { % i f a % }
required
{ % endif % }
title = " { % i f eev.status == eev.STATUS_CURRENT % }
{ % trans ' A ' % }
{ % elif eev . status == eev . STATUS_APPROVED % }
{ % trans ' B ' % }
{ % elif eev . status == eev . STATUS_EXPIRED % }
{ % trans ' C ' % }
{ % endif % } "
class = " asdf
{ % if a % }
b
{ % endif % }
asdf "
{ % if a % }
checked
{ % endif % } >
"""
)
assert output [ " exit_code " ] == 1
2021-10-11 13:08:32 +00:00
output = reformat (
tmp_file ,
runner ,
2021-10-14 10:19:48 +00:00
b """ <div class= " bg-level { % i f value >= 70 % }1 { % e lif value >= 60 % }2 { % e lif value >= 50 % }3 { % e lse % }4 { % e ndif % }> \n </div> """ ,
2021-10-11 13:08:32 +00:00
)
assert output [ " exit_code " ] == 0
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 % } """ ,
)
assert output [ " exit_code " ] == 1
assert (
output [ " text " ]
== r """ { % block content % }
{ % load i18n % }
{ % endblock % }
"""
)
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 % } """ ,
)
assert output [ " exit_code " ] == 1
assert (
""" { % block content % } \n { % block scripts % } { % e ndblock % } \n { % e ndblock % }
"""
== output [ " text " ]
)