Skip to content

Commit

Permalink
[IMP] add some comparison testing for flex box
Browse files Browse the repository at this point in the history
  • Loading branch information
Gorash committed Sep 19, 2024
1 parent 97b9d1f commit 6411289
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 2 deletions.
2 changes: 1 addition & 1 deletion ck
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ if [ ! -f .cutekit/tools-ready ]; then
mkdir -p .cutekit
if [ ! -d .cutekit/venv ]; then
echo "Setting up Python virtual environment..."
python3 -m venv .cutekit/venv
python3.11 -m venv .cutekit/venv
fi
source .cutekit/venv/bin/activate

Expand Down
60 changes: 59 additions & 1 deletion meta/plugins/reftest.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from cutekit import shell, vt100, cli, builder, model
from pathlib import Path
from pathlib import Path, PurePath
import dataclasses as dt
from dataclasses_json import DataClassJsonMixin
import tempfile
import re
import textwrap
import difflib


def buildPaperMuncher(args: model.TargetArgs) -> builder.ProductScope:
Expand Down Expand Up @@ -38,3 +41,58 @@ def _(args: model.TargetArgs):
print(f"{vt100.GREEN}Passed{vt100.RESET}")
else:
print(f"{vt100.RED}Failed{vt100.RESET}")

for file in shell.find("tests", ["*.xml"]):
print(f"Running comparison test {file}...")

temp = Path(file).parent / '.tmp.xhtml'

with Path(file).open() as ref:
content = ref.read()

for name, test in re.findall(r"""<test\s*(?:name=['"]([^'"]+)['"])?\s*>([\w\W]+?)</test>""", content):
search = re.search(r"""<container>([\w\W]+?)</container>""", content)
container = search and search.group(1)

ref = None
ref_xhtml = None
for rendering in re.findall(r"""<rendering>([\w\W]+?)</rendering>""", test):
if container:
xhtml = container.replace("<slot/>", rendering)
else:
xhtml = rendering

with temp.open("w") as f:
f.write(f"<!DOCTYPE html>\n{textwrap.dedent(xhtml)}")

output = paperMuncher.popen("html2pdf", "-sdlpo", "/dev/null", temp)

if not ref_xhtml:
ref_xhtml = rendering
ref = output
continue

ref_result = ref.split('---')[-3]
output_result = output.split('---')[-3]

if ref_result == output_result:
print(f"{vt100.GREEN}Passed{vt100.RESET}")
else:
print(f"{vt100.RED}Failed {name!r}{vt100.RESET}")
print(textwrap.dedent(ref_xhtml).strip())
print(f"{vt100.RED}{textwrap.dedent(rendering).strip()}{vt100.RESET}")

diff_html = []
theDiffs = difflib.ndiff(ref_result.splitlines(), output_result.splitlines())
for eachDiff in theDiffs:
if eachDiff[0] == "-":
diff_html.append(f"{vt100.RED}{eachDiff}{vt100.RESET}")
elif eachDiff[0] == "+":
diff_html.append(f"{vt100.GREEN}{eachDiff}{vt100.RESET}")
elif eachDiff[0] != "?":
diff_html.append(eachDiff)
print('\n'.join(diff_html))

break

temp.unlink()
94 changes: 94 additions & 0 deletions tests/css/flex.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<test name="flex box">
<container>
<html lang="en">
<head>
<style>
div {
width: 100px;
height: 100px;
}
div.flex {
display: flex;
}
.red {
background-color: red;
}
.green {
background-color: green;
}
div.ref div {
width: 50%;
float: left;
}
</style>
</head>
<body>
<slot/>
</body>
</html>
</container>

<rendering>
<div class="ref">
<div class="red"></div>
<div class="green"></div>
</div>
</rendering>

<rendering>
<div>
<div style="width: 50%; float: left;" class="red"></div>
<div style="width: 50%; float: left;" class="green"></div>
</div>
</rendering>

<rendering>
<div class="flex">
<div class="red"></div>
<div class="green"></div>
</div>
</rendering>

<rendering>
<div style="display: flex;">
<div class="red"></div>
<div class="green"></div>
</div>
</rendering>

<rendering>
<div style="display: flex; flex-flow: nowrap;">
<div class="red"></div>
<div class="green"></div>
</div>
</rendering>

<rendering>
<div style="display: flex; flex-flow: inherit;">
<div class="red"></div>
<div class="green"></div>
</div>
</rendering>

<rendering>
<div style="display: flex; flex-flow: initial;">
<div class="red"></div>
<div class="green"></div>
</div>
</rendering>

<rendering>
<div style="display: flex; flex-flow: row;">
<div class="red"></div>
<div class="green"></div>
</div>
</rendering>

<rendering>
<div style="display: flex; flex-flow: row-reverse;">
<div class="green"></div>
<div class="red"></div>
</div>
</rendering>
</test>

0 comments on commit 6411289

Please sign in to comment.