added tests for all formatters; stylecheck workflow now also checks in tests now

This commit is contained in:
truePhilipp committed 2024-05-06 22:26:45 +02:00
1 parent 2f3f45151d
commit c3f7fd88bb
18 files changed
+364 -14

No files matched your search

+17
View File
@@ -27,3 +27,20 @@ jobs:
run: mypy --strict html_to_django run: mypy --strict html_to_django
- name: Pycodestyle - name: Pycodestyle
run: pycodestyle --max-line-length 120 html_to_django run: pycodestyle --max-line-length 120 html_to_django
stylecheck-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Python 3.12
uses: actions/setup-python@v5
with:
python-version: 3.12
- name: Install Dependencies
run: |
pip install ".[test]"
mypy tests || true
mypy --install-types --non-interactive
- name: MyPy
run: mypy --strict tests
- name: Pycodestyle
run: pycodestyle --max-line-length 120 tests
+2 -2
View File
@@ -3,6 +3,6 @@ from bs4 import BeautifulSoup
def format(soup: BeautifulSoup) -> None: def format(soup: BeautifulSoup) -> None:
for element in soup.find_all(attrs={"dj-attr": True}): for element in soup.find_all(attrs={"dj-attr": True}):
tag, value = element.get("dj-attr").split(";") for tag, value in [attr.split(";") for attr in element["dj-attr"].split("~")]:
element[tag] = f"{{{{ {value} }}}}" element[tag] = f"{{{{ {value} }}}}"
del element.attrs["dj-attr"] del element.attrs["dj-attr"]
+8 -2
View File
@@ -21,12 +21,18 @@ def parse_style(style: str) -> dict[str, str]:
def style_to_string(style: dict[str, str]) -> str: def style_to_string(style: dict[str, str]) -> str:
return ";".join([f"{name}:{value}" for name, value in style.items()]) result = ";".join([f"{name}:{value}" for name, value in style.items()])
if len(style) != 0:
result += ";"
return result
def format(soup: BeautifulSoup) -> None: def format(soup: BeautifulSoup) -> None:
for element in soup.find_all(attrs={"dj-style": True}): for element in soup.find_all(attrs={"dj-style": True}):
style = parse_style(element["style"]) if element.has_attr("style"):
style = parse_style(element["style"])
else:
style = {}
dj_style = element.get("dj-style") dj_style = element.get("dj-style")
for name, value, value_type in [declaration.split(";") for declaration in dj_style.split("~") if declaration]: for name, value, value_type in [declaration.split(";") for declaration in dj_style.split("~") if declaration]:
style[name] = f"{{{{ {value} }}}}{value_type}" style[name] = f"{{{{ {value} }}}}{value_type}"
@@ -4,7 +4,10 @@ from .style_formatter import parse_style, style_to_string
def format(soup: BeautifulSoup) -> None: def format(soup: BeautifulSoup) -> None:
for element in soup.find_all(attrs={"dj-style-raw": True}): for element in soup.find_all(attrs={"dj-style-raw": True}):
style = parse_style(element["style"]) if element.has_attr("style"):
style = parse_style(element["style"])
else:
style = {}
dj_style = element.get("dj-style-raw") dj_style = element.get("dj-style-raw")
for name, value in [declaration.split(";") for declaration in dj_style.split("~") if declaration]: for name, value in [declaration.split(";") for declaration in dj_style.split("~") if declaration]:
style[name] = value style[name] = value
+23
View File
@@ -0,0 +1,23 @@
import unittest
from bs4 import BeautifulSoup
from html_to_django.formatters.attr_formatter import format
class TestAttrFormatter(unittest.TestCase):
def test_format(self) -> None:
soup = BeautifulSoup('<div dj-attr="class;my_class"></div>', "html.parser")
format(soup)
expected_result = '<div class="{{ my_class }}"></div>'
self.assertEqual(expected_result, str(soup))
def test_format_multiple(self) -> None:
soup = BeautifulSoup('<div dj-attr="class;my_class~id;my_id"></div>', "html.parser")
format(soup)
expected_result = '<div class="{{ my_class }}" id="{{ my_id }}"></div>'
self.assertEqual(expected_result, str(soup))
def test_format_overwrite_existing(self) -> None:
soup = BeautifulSoup('<div class="original_class" dj-attr="class;new_class"></div>', "html.parser")
format(soup)
expected_result = '<div class="{{ new_class }}"></div>'
self.assertEqual(expected_result, str(soup))
+11 -8
View File
@@ -4,17 +4,20 @@ from html_to_django.formatters import block_formatter
class BlockTests(TestCase): class BlockTests(TestCase):
def test_basic(self): def test_basic(self) -> None:
soup = BeautifulSoup('<div dj-block="content"></div>', "html.parser") soup = BeautifulSoup('<div dj-block="content"></div>', "html.parser")
block_formatter.format(soup) block_formatter.format(soup)
self.assertEqual("{% block content %}{% endblock %}", str(soup)) expected_result = "{% block content %}{% endblock %}"
self.assertEqual(expected_result, str(soup))
def test_with_content(self):
def test_with_content(self) -> None:
soup = BeautifulSoup('<div dj-block="content">Test</div>', "html.parser") soup = BeautifulSoup('<div dj-block="content">Test</div>', "html.parser")
block_formatter.format(soup) block_formatter.format(soup)
self.assertEqual("{% block content %}Test{% endblock %}", str(soup)) expected_result = "{% block content %}Test{% endblock %}"
self.assertEqual(expected_result, str(soup))
def test_with_block_inside(self):
def test_with_block_inside(self) -> None:
soup = BeautifulSoup('<div dj-block="content"><div dj-block="inner">Test</div></div>', "html.parser") soup = BeautifulSoup('<div dj-block="content"><div dj-block="inner">Test</div></div>', "html.parser")
block_formatter.format(soup) block_formatter.format(soup)
self.assertEqual("{% block content %}{% block inner %}Test{% endblock %}{% endblock %}", str(soup)) expected_result = "{% block content %}{% block inner %}Test{% endblock %}{% endblock %}"
self.assertEqual(expected_result, str(soup))
@@ -0,0 +1,17 @@
from unittest import TestCase
from bs4 import BeautifulSoup
from html_to_django.formatters import command_formatter
class CommandTests(TestCase):
def test_basic(self) -> None:
soup = BeautifulSoup('<div dj-command="command_name"></div>', "html.parser")
command_formatter.format(soup)
expected_result = "{% command_name %}"
self.assertEqual(expected_result, str(soup))
def test_with_content(self) -> None:
soup = BeautifulSoup('<div dj-command="command_name">Test</div>', "html.parser")
command_formatter.format(soup)
expected_result = "{% command_name %}"
self.assertEqual(expected_result, str(soup))
+24
View File
@@ -0,0 +1,24 @@
from unittest import TestCase
from bs4 import BeautifulSoup
from html_to_django.formatters import for_formatter
class ForTests(TestCase):
def test_basic(self) -> None:
soup = BeautifulSoup('<div dj-for="item in list"></div>', "html.parser")
for_formatter.format(soup)
expected_result = "{% for item in list %}{% endfor %}"
self.assertEqual(expected_result, str(soup))
def test_with_content(self) -> None:
soup = BeautifulSoup('<div dj-for="item in list">Test</div>', "html.parser")
for_formatter.format(soup)
expected_result = "{% for item in list %}Test{% endfor %}"
self.assertEqual(expected_result, str(soup))
def test_with_for_inside(self) -> None:
soup = BeautifulSoup('<div dj-for="item in list"><div dj-for="subitem in sublist">Test</div></div>',
"html.parser")
for_formatter.format(soup)
expected_result = "{% for item in list %}{% for subitem in sublist %}Test{% endfor %}{% endfor %}"
self.assertEqual(expected_result, str(soup))
@@ -0,0 +1,25 @@
from unittest import TestCase
from bs4 import BeautifulSoup
from html_to_django.formatters import for_wrap_formatter
class ForWrapTests(TestCase):
def test_basic(self) -> None:
soup = BeautifulSoup('<div dj-for-wrap="item in list"></div>', "html.parser")
for_wrap_formatter.format(soup)
expected_result = "{% for item in list %}<div></div>{% endfor %}"
self.assertEqual(expected_result, str(soup))
def test_with_content(self) -> None:
soup = BeautifulSoup('<div dj-for-wrap="item in list">Test</div>', "html.parser")
for_wrap_formatter.format(soup)
expected_result = "{% for item in list %}<div>Test</div>{% endfor %}"
self.assertEqual(expected_result, str(soup))
def test_with_for_wrap_inside(self) -> None:
soup = BeautifulSoup('<div dj-for-wrap="item in list"><div dj-for-wrap="subitem in sublist">Test</div></div>',
"html.parser")
for_wrap_formatter.format(soup)
expected_result = ("{% for item in list %}<div>{% for subitem in sublist %}<div>Test</div>{% endfor %}"
"</div>{% endfor %}")
self.assertEqual(expected_result, str(soup))
+30
View File
@@ -0,0 +1,30 @@
import unittest
from bs4 import BeautifulSoup
from html_to_django.formatters.if_formatter import format
class TestIfFormatter(unittest.TestCase):
def test_format(self) -> None:
soup = BeautifulSoup('<div dj-if="condition">Test</div>', "html.parser")
format(soup)
expected_result = "{% if condition %}Test{% endif %}"
self.assertEqual(expected_result, str(soup))
def test_format_with_elif(self) -> None:
soup = BeautifulSoup('<div dj-if="condition1">Test</div><div dj-elif="condition2">Test2</div>', "html.parser")
format(soup)
expected_result = "{% if condition1 %}Test{% elif condition2 %}Test2{% endif %}"
self.assertEqual(expected_result, str(soup))
def test_format_with_else(self) -> None:
soup = BeautifulSoup('<div dj-if="condition1">Test</div><div dj-else>Test2</div>', "html.parser")
format(soup)
expected_result = "{% if condition1 %}Test{% else %}Test2{% endif %}"
self.assertEqual(expected_result, str(soup))
def test_format_with_elif_and_else(self) -> None:
soup = BeautifulSoup('<div dj-if="condition1">Test</div><div dj-elif="condition2">Test2</div>'
'<div dj-else>Test3</div>', "html.parser")
format(soup)
expected_result = "{% if condition1 %}Test{% elif condition2 %}Test2{% else %}Test3{% endif %}"
self.assertEqual(expected_result, str(soup))
@@ -0,0 +1,17 @@
from unittest import TestCase
from bs4 import BeautifulSoup
from html_to_django.formatters import include_formatter
class IncludeTests(TestCase):
def test_basic(self) -> None:
soup = BeautifulSoup('<div dj-include="template_name"></div>', "html.parser")
include_formatter.format(soup)
expected_result = '<div>{% include "template_name" %}</div>'
self.assertEqual(expected_result, str(soup))
def test_with_content(self) -> None:
soup = BeautifulSoup('<div dj-include="template_name">Test</div>', "html.parser")
include_formatter.format(soup)
expected_result = '<div>{% include "template_name" %}</div>'
self.assertEqual(expected_result, str(soup))
@@ -0,0 +1,29 @@
import unittest
from bs4 import BeautifulSoup
from bs4.formatter import HTMLFormatter
from html_to_django.formatters.remove_default_formatter import format
class TestFormatFunction(unittest.TestCase):
# The strings here look weird because of the indentation and line breaks, but it must be this way
def test_basic(self) -> None:
html = """<!DOCTYPE html>
<html lang="en" dj-remove-default>
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>
Test
</h1>
<script src="test.js">
</script>
</body>
</html>
"""
soup = BeautifulSoup(html, "html.parser")
format(soup)
expected_result = "<h1>\n Test\n</h1>\n"
self.assertEqual(expected_result, soup.prettify(formatter=HTMLFormatter(indent=4)))
@@ -0,0 +1,17 @@
from unittest import TestCase
from bs4 import BeautifulSoup
from html_to_django.formatters import static_formatter
class StaticTests(TestCase):
def test_basic(self) -> None:
soup = BeautifulSoup('<img dj-static="src;image.png"/>', "html.parser")
static_formatter.format(soup)
expected_result = '<img src="{% static \'image.png\' %}"/>'
self.assertEqual(expected_result, str(soup))
def test_with_content(self) -> None:
soup = BeautifulSoup('<div dj-static="background;image.png">Test</div>', "html.parser")
static_formatter.format(soup)
expected_result = '<div background="{% static \'image.png\' %}">Test</div>'
self.assertEqual(expected_result, str(soup))
+76
View File
@@ -0,0 +1,76 @@
from unittest import TestCase
from bs4 import BeautifulSoup
from html_to_django.formatters.style_formatter import parse_style, style_to_string, format
class TestParseStyle(TestCase):
def test_parse_style(self) -> None:
style = "color:red;background-color:blue;width:100px"
expected_result = {
"color": "red",
"background-color": "blue",
"width": "100px"
}
self.assertEqual(expected_result, parse_style(style))
def test_parse_style_ending_semicolon(self) -> None:
style = "color:red;background-color:blue;width:100px;"
expected_result = {
"color": "red",
"background-color": "blue",
"width": "100px"
}
self.assertEqual(expected_result, parse_style(style))
def test_parse_style_with_spaces(self) -> None:
style = "color: red ; background-color: blue ; width: 100px"
expected_result = {
"color": "red",
"background-color": "blue",
"width": "100px"
}
self.assertEqual(expected_result, parse_style(style))
def test_parse_style_empty(self) -> None:
style = ""
expected_result: dict[str, str] = {}
self.assertEqual(expected_result, parse_style(style))
class TestStyleToString(TestCase):
def test_style_to_string(self) -> None:
style = {
"color": "red",
"background-color": "blue",
"width": "100px"
}
expected_result = "color:red;background-color:blue;width:100px;"
self.assertEqual(expected_result, style_to_string(style))
def test_style_to_string_with_empty_dict(self) -> None:
style: dict[str, str] = {}
expected_result = ""
self.assertEqual(expected_result, style_to_string(style))
class TestFormat(TestCase):
def test_format(self) -> None:
soup = BeautifulSoup('<div dj-style="color;color;~background-color;background_color;~width;100;px"'
'style="font-size:16px"></div>', "html.parser")
format(soup)
expected_result = ('<div style="font-size:16px;color:{{ color }};background-color:{{ background_color }};'
'width:{{ 100 }}px;"></div>')
self.assertEqual(expected_result, str(soup))
def test_format_overwriting_existing_style(self) -> None:
soup = BeautifulSoup('<div dj-style="color;color;~width;100;px" style="color:blue;font-size:16px"></div>',
"html.parser")
format(soup)
expected_result = '<div style="color:{{ color }};font-size:16px;width:{{ 100 }}px;"></div>'
self.assertEqual(expected_result, str(soup))
def test_format_with_no_style(self) -> None:
soup = BeautifulSoup('<div dj-style="color;color;~width;100;px"></div>', "html.parser")
format(soup)
expected_result = '<div style="color:{{ color }};width:{{ 100 }}px;"></div>'
self.assertEqual(expected_result, str(soup))
@@ -0,0 +1,23 @@
from unittest import TestCase
from bs4 import BeautifulSoup
from html_to_django.formatters.style_raw_formatter import format
class TestStyleRawFormat(TestCase):
def test_format(self) -> None:
soup = BeautifulSoup('<div dj-style-raw="color;red~width;100px" style="font-size:16px"></div>', "html.parser")
format(soup)
expected_result = '<div style="font-size:16px;color:red;width:100px;"></div>'
self.assertEqual(expected_result, str(soup))
def test_format_with_existing_style(self) -> None:
soup = BeautifulSoup('<div dj-style-raw="color;red" style="color:blue;font-size:16px"></div>', "html.parser")
format(soup)
expected_result = '<div style="color:red;font-size:16px;"></div>'
self.assertEqual(expected_result, str(soup))
def test_format_with_no_style(self) -> None:
soup = BeautifulSoup('<div dj-style-raw="color;red~width;100px"></div>', "html.parser")
format(soup)
expected_result = '<div style="color:red;width:100px;"></div>'
self.assertEqual(expected_result, str(soup))
@@ -0,0 +1,23 @@
from unittest import TestCase
from bs4 import BeautifulSoup
from html_to_django.formatters import unwrap_formatter
class UnwrapTests(TestCase):
def test_basic(self) -> None:
soup = BeautifulSoup('<div dj-unwrap="true"><div>Test</div></div>', "html.parser")
unwrap_formatter.format(soup)
expected_result = "<div>Test</div>"
self.assertEqual(expected_result, str(soup))
def test_empty(self) -> None:
soup = BeautifulSoup('<div dj-unwrap="true"></div>', "html.parser")
unwrap_formatter.format(soup)
expected_result = ""
self.assertEqual(expected_result, str(soup))
def test_with_unwrap_inside(self) -> None:
soup = BeautifulSoup('<div dj-unwrap="true"><div dj-unwrap="true">Inner Test</div></div>', "html.parser")
unwrap_formatter.format(soup)
expected_result = "Inner Test"
self.assertEqual(expected_result, str(soup))
+17
View File
@@ -0,0 +1,17 @@
from unittest import TestCase
from bs4 import BeautifulSoup
from html_to_django.formatters import var_formatter
class VarTests(TestCase):
def test_basic(self) -> None:
soup = BeautifulSoup('<div dj-var="item"></div>', "html.parser")
var_formatter.format(soup)
expected_result = "<div>{{ item }}</div>"
self.assertEqual(expected_result, str(soup))
def test_with_content(self) -> None:
soup = BeautifulSoup('<div dj-var="item">Test</div>', "html.parser")
var_formatter.format(soup)
expected_result = "<div>{{ item }}</div>"
self.assertEqual(expected_result, str(soup))
+1 -1
View File
@@ -6,7 +6,7 @@ from html_to_django import converter
class ConverterTests(TestCase): class ConverterTests(TestCase):
def test_convert_file(self): def test_convert_file(self) -> None:
shutil.copytree( shutil.copytree(
os.path.abspath("tests/test_project"), os.path.abspath("tests/test_project"),
os.path.abspath("tests/test_project_test_result"), os.path.abspath("tests/test_project_test_result"),