added tests for all formatters; stylecheck workflow now also checks in tests now
This commit is contained in:
1 parent
2f3f45151d
commit
c3f7fd88bb
18 files changed
+364
-14
No files matched your search
@@ -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))
|
||||
@@ -4,17 +4,20 @@ from html_to_django.formatters import block_formatter
|
||||
|
||||
|
||||
class BlockTests(TestCase):
|
||||
def test_basic(self):
|
||||
def test_basic(self) -> None:
|
||||
soup = BeautifulSoup('<div dj-block="content"></div>', "html.parser")
|
||||
block_formatter.format(soup)
|
||||
self.assertEqual("{% block content %}{% endblock %}", str(soup))
|
||||
|
||||
def test_with_content(self):
|
||||
expected_result = "{% block content %}{% endblock %}"
|
||||
self.assertEqual(expected_result, str(soup))
|
||||
|
||||
def test_with_content(self) -> None:
|
||||
soup = BeautifulSoup('<div dj-block="content">Test</div>', "html.parser")
|
||||
block_formatter.format(soup)
|
||||
self.assertEqual("{% block content %}Test{% endblock %}", str(soup))
|
||||
|
||||
def test_with_block_inside(self):
|
||||
expected_result = "{% block content %}Test{% endblock %}"
|
||||
self.assertEqual(expected_result, str(soup))
|
||||
|
||||
def test_with_block_inside(self) -> None:
|
||||
soup = BeautifulSoup('<div dj-block="content"><div dj-block="inner">Test</div></div>', "html.parser")
|
||||
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))
|
||||
@@ -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))
|
||||
@@ -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))
|
||||
@@ -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))
|
||||
@@ -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))
|
||||
Reference in new issue
Block a user