Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 1 addition & 28 deletions python/tests/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,6 @@ def test_open_as_type(txt_path):
assert file.file_type() == pyodr.FileType.text_file


def test_open_with_options(txt_path):
options = pyodr.DecodeOptions(as_file_type=pyodr.FileType.text_file)
file = pyodr.open(str(txt_path), options)
assert file.file_type() == pyodr.FileType.text_file


def test_open_carries_csv_options(tmp_path):
path = tmp_path / "semicolons.csv"
path.write_text("a;b\n1;2\n", encoding="utf-8")
Expand Down Expand Up @@ -170,35 +164,14 @@ def test_open_from_memory_as_type(odt_path):
assert pyodr.open(file, options).is_archive_file()


def test_decoded_file_from_file(odt_path):
file = pyodr.File.from_memory(odt_path.read_bytes())

assert pyodr.open(file).file_type() == pyodr.FileType.opendocument_text
assert pyodr.open(
file, pyodr.DecodeOptions(as_file_type=pyodr.FileType.zip)
).is_archive_file()

options = pyodr.DecodeOptions(as_file_type=pyodr.FileType.zip)
assert pyodr.open(file, options).is_archive_file()


def test_document_file_from_file(odt_path):
file = pyodr.File.from_memory(odt_path.read_bytes())

document_file = pyodr.open(file).as_document_file()
assert document_file.file_type() == pyodr.FileType.opendocument_text
assert document_file.file_meta().type == pyodr.FileType.opendocument_text

assert document_file.document_type() == pyodr.DocumentType.text


def test_document_file_from_disk_and_from_memory(odt_path):
from_disk = pyodr.open(str(odt_path)).as_document_file()
from_memory = pyodr.open(
pyodr.File.from_memory(odt_path.read_bytes())
).as_document_file()

assert from_disk.file_type() == pyodr.FileType.opendocument_text
assert from_memory.file_meta().type == from_disk.file_type()
assert from_memory.file_type() == from_disk.file_type()
assert from_memory.document_type() == from_disk.document_type()
assert (
Expand Down
40 changes: 14 additions & 26 deletions python/tests/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ def translate_offline(path, tmp_path):
return service.bring_offline(str(output))


def render(path, config):
service = pyodr.html.translate(pyodr.open(str(path)), config)
content, _ = service.list_views()[0].write_html()
return content


def test_html_config_defaults():
config = pyodr.HtmlConfig()
assert config.embed_images
Expand Down Expand Up @@ -87,70 +93,52 @@ def test_viewport_mode_reaches_the_html(odt_path):
# The C++ suite covers the mode matrix; this only proves the config crosses
# the binding. A text document without margins is reflowing content, so
# `automatic` resolves to `actual_size`.
def render(config):
file = pyodr.open(str(odt_path))
service = pyodr.html.translate(file, config)
content, _ = service.list_views()[0].write_html()
return content

assert (
'<meta name="viewport" '
'content="width=device-width,initial-scale=1.0,user-scalable=yes"/>'
in render(pyodr.HtmlConfig())
in render(odt_path, pyodr.HtmlConfig())
)

fit_width = pyodr.HtmlConfig()
fit_width.viewport_mode = pyodr.HtmlViewportMode.fit_width
assert (
'<meta name="viewport" content="width=device-width,user-scalable=yes"/>'
in render(fit_width)
in render(odt_path, fit_width)
)

# only paged content has a width to fit, hence the margins
by_view = pyodr.HtmlConfig()
by_view.viewport_mode = pyodr.HtmlViewportMode.fit_width_by_view
by_view.text_document_margin = True
assert "--odr-fit:view" in render(by_view)
assert "--odr-fit:view" in render(odt_path, by_view)

raw = pyodr.HtmlConfig()
raw.viewport_content = "width=420"
assert '<meta name="viewport" content="width=420"/>' in render(raw)
assert '<meta name="viewport" content="width=420"/>' in render(odt_path, raw)


def test_editing_scope_reaches_the_html(odt_path):
# proves the scope crosses the binding; the C++ suite covers the rest
def render(config):
file = pyodr.open(str(odt_path))
service = pyodr.html.translate(file, config)
content, _ = service.list_views()[0].write_html()
return content

# the attribute, not the name: the script names it too
assert 'data-odr-editing-scope="' not in render(pyodr.HtmlConfig())
assert 'data-odr-editing-scope="' not in render(odt_path, pyodr.HtmlConfig())

config = pyodr.HtmlConfig()
config.editable = True
config.editing_scope = pyodr.HtmlEditingScope.paragraph
assert 'data-odr-editing-scope="paragraph"' in render(config)
assert 'data-odr-editing-scope="paragraph"' in render(odt_path, config)


def test_min_content_margin_reaches_the_html(odt_path):
# The C++ suite covers where the floor lands; this only proves it crosses
# the binding, unset sides and all.
def render(config):
file = pyodr.open(str(odt_path))
service = pyodr.html.translate(file, config)
content, _ = service.list_views()[0].write_html()
return content

default = pyodr.HtmlConfig()
assert default.min_content_margin.top is None
assert ":root{--odr-min-margin" not in render(default)
assert ":root{--odr-min-margin" not in render(odt_path, default)

config = pyodr.HtmlConfig()
config.min_content_margin.top = pyodr.Measure("12px")
config.min_content_margin.left = pyodr.Measure("1cm")
html = render(config)
html = render(odt_path, config)
assert ":root{--odr-min-margin-top:12px;--odr-min-margin-left:1cm;}" in html
assert "--odr-min-margin-right:" not in html

Expand Down
11 changes: 7 additions & 4 deletions python/tests/test_http_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,13 @@ def test_stop_waits_for_listen():

thread = threading.Thread(target=server.listen, daemon=True)
thread.start()
while not server.is_running():
time.sleep(0.001)

server.stop()
deadline = time.monotonic() + 5
try:
while not server.is_running():
assert thread.is_alive() and time.monotonic() < deadline
time.sleep(0.001)
finally:
server.stop()
# the accept loop is gone for good by now, which is what makes dropping the
# server right after safe
assert not server.is_running()
Expand Down
6 changes: 3 additions & 3 deletions python/tests/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ def __init__(self, level=pyodr.LogLevel.verbose):
super().__init__()
self.level = level
self.messages = []
self.consulted = []
self.flushes = 0

def will_log(self, level):
self.consulted.append(level)
return level >= self.level

def log(self, time, level, message, location):
Expand Down Expand Up @@ -56,9 +58,7 @@ def test_custom_sink_receives_library_diagnostics(odt_path):
"""A logger passed to `open` is actually used by the library."""
sink = CollectingLogger()
pyodr.open(str(odt_path), logger=pyodr.Logger(sink))
# The odt path logs at least one diagnostic; at minimum it must not crash
# and the sink must have been consulted.
assert isinstance(sink.messages, list)
assert sink.consulted


def test_logger_accepted_by_entry_points(odt_path):
Expand Down
Loading