mirror of
https://github.com/release-engineering/repo-autoindex.git
synced 2025-02-23 13:42:52 +00:00
Add a test for text elision
This commit is contained in:
parent
99ccc97e29
commit
c4fd70e240
2 changed files with 26 additions and 5 deletions
|
@ -10,12 +10,11 @@ TEMPLATE_DIR = os.path.join(os.path.dirname(__file__), "templates")
|
|||
|
||||
|
||||
class TemplateContext:
|
||||
def __init__(self) -> None:
|
||||
def __init__(self, max_text_length: int = 800) -> None:
|
||||
self.env = jinja2.Environment(
|
||||
autoescape=True, loader=jinja2.FileSystemLoader(TEMPLATE_DIR)
|
||||
)
|
||||
# low number is for testing - increase it later
|
||||
self.max_text_length = 200
|
||||
self.max_text_length = max_text_length
|
||||
|
||||
def render_index(
|
||||
self,
|
||||
|
@ -39,8 +38,9 @@ class TemplateContext:
|
|||
|
||||
out = []
|
||||
for entry in entries:
|
||||
if len(entry.text) >= (self.max_text_length - 3):
|
||||
entry = replace(entry, text=entry.text[:-3] + "...")
|
||||
if len(entry.text) > self.max_text_length:
|
||||
text = entry.text[: self.max_text_length - 3] + "..."
|
||||
entry = replace(entry, text=text)
|
||||
|
||||
# pad right so they all have the same length
|
||||
padcount = max_len - len(entry.text)
|
||||
|
|
21
tests/test_render_elision.py
Normal file
21
tests/test_render_elision.py
Normal file
|
@ -0,0 +1,21 @@
|
|||
from repo_autoindex._impl.template import TemplateContext
|
||||
from repo_autoindex._impl.base import IndexEntry
|
||||
|
||||
|
||||
def test_long_string_elision():
|
||||
"""Entries with a very long name will trigger elision."""
|
||||
ctx = TemplateContext(max_text_length=6)
|
||||
|
||||
rendered = ctx.render_index(
|
||||
index_entries=[
|
||||
IndexEntry(href="href1", text="123456"),
|
||||
IndexEntry(href="some longer href", text="1234567"),
|
||||
]
|
||||
)
|
||||
|
||||
# Text which fits in the limit should be left alone
|
||||
assert '<a href="href1">123456</a>' in rendered
|
||||
|
||||
# Text which exceeds the limit should trigger elision (but href should
|
||||
# still be left alone)
|
||||
assert '<a href="some longer href">123...</a>' in rendered
|
Loading…
Add table
Reference in a new issue