"""Djlint tests for html attributes.
run::
pytest tests/test_html/test_attributes.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
pytest tests/test_html/test_attributes.py::test_long_attributes
pytest tests/test_html/test_attributes.py::test_ignored_attributes
pytest tests/test_html/test_attributes.py::test_boolean_attributes
Some tests may be from prettier.io's html test suite.
Where applicable this notice may be needed:
#### Prettier.io license ####
Copyright © James Long and contributors
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
# pylint: disable=C0116,C0302
from typing import TextIO
from click.testing import CliRunner
from tests.conftest import reformat
def test_srcset(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file,
runner,
b"""
""",
)
assert output.exit_code == 0
def test_long_attributes(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file,
runner,
b"""""",
)
assert output.exit_code == 1
assert (
output.text
== """
"""
)
# check styles
output = reformat(
tmp_file,
runner,
b"""
""",
)
assert output.exit_code == 0
# check styles when tag is first
output = reformat(
tmp_file,
runner,
b"""
""",
)
assert output.exit_code == 0
# attributes with space around = are not broken
# https://github.com/Riverside-Healthcare/djLint/issues/317
# https://github.com/Riverside-Healthcare/djLint/issues/330
output = reformat(
tmp_file,
runner,
b"""
Test\n""",
)
assert (
output.text
== """
Test\n"""
)
def test_ignored_attributes(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file,
runner,
b"""
""",
)
assert output.exit_code == 1
print(output.text)
assert (
output.text
== """
\n
"""
)
def test_boolean_attributes(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file,
runner,
b"""
""",
)
# boolean attributes after tag must be reformatted correctly
assert output.exit_code == 1
print(output.text)
assert (
output.text
== """
"""
)
# boolean attributes after tag with long attributes must be reformatted correctly
output = reformat(
tmp_file,
runner,
b"""
""",
)
assert output.exit_code == 1
assert (
output.text
== """
"""
)
# boolean attributes after tag are accepted
output = reformat(
tmp_file,
runner,
b"""
""",
)
assert output.exit_code == 0
# def test_attributes(runner: CliRunner, tmp_file: TextIO) -> None:
# html_in = (
# b"""
#
#
#
#
#
#
#
#
#
#
#
#
String
#
String
#
String
#
String
#
String
#
String
#
String
#
String
#
String
#
#
String
#
String
#
String
#
#
#
...
#
# ...
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
# """
# ).strip()
# html_out = (
# """
#
#
#
#
#
#
#
#
#
#
#
#
String
#
String
#
String
#
String
#
String
#
String
#
String
#
String
#
String
#
#
String
#
String
#
String
#
#
# ...
#
#
# ...
#
#
#
#
#
#
#
#
# """
# ).strip()
# output = reformat(tmp_file, runner, html_in)
# assert output.text == html_out
# def test_boolean(runner: CliRunner, tmp_file: TextIO) -> None:
# html_in = (
# b"""
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
# """
# ).strip()
# html_out = (
# """
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
# """
# ).strip()
# output = reformat(tmp_file, runner, html_in)
# assert output.text == html_out
# def test_case_sensitive(runner: CliRunner, tmp_file: TextIO) -> None:
# html_in = (
# b"""
#
# """
# ).strip()
# html_out = (
# """
#
# """
# ).strip()
# output = reformat(tmp_file, runner, html_in)
# assert output.text == html_out
# def test_class_bem1(runner: CliRunner, tmp_file: TextIO) -> None:
# html_in = (
# b"""
#
Foo
#
# """
# ).strip()
# html_out = (
# """
#
# Foo
#
#
# """
# ).strip()
# output = reformat(tmp_file, runner, html_in)
# assert output.text == html_out
# def test_class_bem2(runner: CliRunner, tmp_file: TextIO) -> None:
# html_in = (
# b"""
#
# """
# ).strip()
# html_out = (
# """
#
# """
# ).strip()
# output = reformat(tmp_file, runner, html_in)
# assert output.text == html_out
# def test_class_colon(runner: CliRunner, tmp_file: TextIO) -> None:
# html_in = (
# b"""
#
# """
# ).strip()
# html_out = (
# """
#
# """
# ).strip()
# output = reformat(tmp_file, runner, html_in)
# assert output.text == html_out
# def test_class_leading_dashes(runner: CliRunner, tmp_file: TextIO) -> None:
# html_in = (
# b"""
#
#
# """
# ).strip()
# html_out = (
# """
#
#
# """
# ).strip()
# output = reformat(tmp_file, runner, html_in)
# assert output.text == html_out
# def test_class_many_short_names(runner: CliRunner, tmp_file: TextIO) -> None:
# html_in = (
# b"""
#
# Jump to
# x
#
# """
# ).strip()
# html_out = (
# """
#
# Jump to
# x
#
# """
# ).strip()
# output = reformat(tmp_file, runner, html_in)
# assert output.text == html_out
# def test_class_names(runner: CliRunner, tmp_file: TextIO) -> None:
# html_in = (
# b"""
#
![]()
#
![]()
#
![]()
#
![]()
#
![]()
#
![]()
#
#
#
![]()
#
#
# """
# ).strip()
# html_out = (
# """
#
![]()
#
![]()
#
![]()
#
![]()
#
![]()
#
![]()
#
#
#
![]()
#
#
# """
# ).strip()
# output = reformat(tmp_file, runner, html_in)
# assert output.text == html_out
# def test_class_print_width_edge(runner: CliRunner, tmp_file: TextIO) -> None:
# html_in = (
# b"""
#
#
# """
# ).strip()
# html_out = (
# """
#
# """
# ).strip()
# output = reformat(tmp_file, runner, html_in)
# assert output.text == html_out
# def test_dobule_quotes(runner: CliRunner, tmp_file: TextIO) -> None:
# html_in = (
# b"""
#

# """
# ).strip()
# html_out = (
# """
#

# """
# ).strip()
# output = reformat(tmp_file, runner, html_in)
# assert output.text == html_out
# def test_duplicate(runner: CliRunner, tmp_file: TextIO) -> None:
# html_in = (
# b"""
#
123
# """
# ).strip()
# html_out = (
# """
#
123
# """
# ).strip()
# output = reformat(tmp_file, runner, html_in)
# assert output.text == html_out
# def test_single_quotes(runner: CliRunner, tmp_file: TextIO) -> None:
# html_in = (
# b"""
#

# """
# ).strip()
# html_out = (
# """
#

# """
# ).strip()
# output = reformat(tmp_file, runner, html_in)
# assert output.text == html_out
# def test_smart_quotes(runner: CliRunner, tmp_file: TextIO) -> None:
# html_in = (
# b"""
#
# """
# ).strip()
# html_out = (
# """
#
# """
# ).strip()
# output = reformat(tmp_file, runner, html_in)
# assert output.text == html_out
# def test_srcset(runner: CliRunner, tmp_file: TextIO) -> None:
# html_in = (
# b"""
#

#

#

#
![]()
#
#

# """
# ).strip()
# html_out = (
# """
#

#

#

#
![]()
#
#

# """
# ).strip()
# output = reformat(tmp_file, runner, html_in)
# assert output.text == html_out
# def test_style(runner: CliRunner, tmp_file: TextIO) -> None:
# html_in = (
# b"""
#
#
#
#
#
#
#
#
#
#
#
#
#
# """
# ).strip()
# html_out = (
# """
#
#
#
#
#
#
#
#
#
#
# """
# ).strip()
# output = reformat(tmp_file, runner, html_in)
# assert output.text == html_out
# def test_without_quotes(runner: CliRunner, tmp_file: TextIO) -> None:
# html_in = (
# b"""
#
String
# """
# ).strip()
# html_out = (
# """
#
String
# """
# ).strip()
# output = reformat(tmp_file, runner, html_in)
# assert output.text == html_out