djLint/tests/test_nunjucks.py

72 lines
1.8 KiB
Python
Raw Normal View History

"""Djlint tests specific to nunjucks.
run::
pytest tests/test_nunjucks.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
2021-10-28 10:42:51 +00:00
pytest tests/test_nunjucks.py::test_macro --cov=src/djlint --cov-branch \
2021-10-06 12:20:47 +00:00
--cov-report xml:coverage.xml --cov-report term-missing
"""
# pylint: disable=C0116
from pathlib import Path
from typing import TextIO
from click.testing import CliRunner
from src.djlint import main as djlint
2021-09-08 09:52:16 +00:00
from .conftest import reformat, write_to_file
def test_template_tags(runner: CliRunner, tmp_file: TextIO) -> None:
write_to_file(
tmp_file.name,
b"""{%- set posts = collections.docs -%}""",
)
runner.invoke(djlint, [tmp_file.name, "--reformat"])
assert (
Path(tmp_file.name).read_text() == """{%- set posts = collections.docs -%}\n"""
)
# ensure spaces are added
write_to_file(
tmp_file.name,
b"""{%-set posts = collections.docs-%}\n{%asdf%}""",
)
runner.invoke(djlint, [tmp_file.name, "--reformat"])
assert (
Path(tmp_file.name).read_text()
== """{%- set posts = collections.docs -%}\n{% asdf %}\n"""
)
2021-09-08 09:52:16 +00:00
def test_spaceless(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file,
runner,
b"""{%- if entry.children.length -%}<strong>{%- endif -%}""",
)
2021-10-06 12:20:47 +00:00
2021-11-29 11:19:07 +00:00
assert output.exit_code == 0
2021-09-08 09:52:16 +00:00
assert (
2021-11-29 11:19:07 +00:00
output.text
2021-10-06 12:20:47 +00:00
== r"""{%- if entry.children.length -%}<strong>{%- endif -%}
2021-09-08 09:52:16 +00:00
"""
)
2021-10-28 10:42:51 +00:00
def test_macro(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file, runner, b"{% macro 'cool' %}<div>some html</div>{% endmacro %}"
)
2021-11-29 11:19:07 +00:00
assert output.exit_code == 1
2021-10-28 10:42:51 +00:00
assert (
2021-11-29 11:19:07 +00:00
output.text
2021-10-28 10:42:51 +00:00
== r"""{% macro 'cool' %}
<div>some html</div>
{% endmacro %}
"""
)