def test_truncate_text_smaller_than_max_length(with_ellipsis):
    text = "lorem ipsum"

    # exact match as length
    truncated = truncate_text(text, len(text), with_ellipsis=with_ellipsis)
    assert len(truncated) == len(text)
    assert truncated == text

    # max_length > len(text)
    truncated = truncate_text(text, len(text) + 1, with_ellipsis=with_ellipsis)
    assert len(truncated) == len(text)
    assert truncated == text
def test_truncate_text():
    text = "lorem ipsum"
    length = 5

    truncated = truncate_text(text, length)
    # length should not cross the max length
    assert len(truncated) == length
    assert truncated[:-1] == text[:length - 1]
    # last character should be ellipsis
    assert truncated[-1] == "…"

    truncated = truncate_text(text, length, with_ellipsis=False)
    # length should not cross the max length
    assert len(truncated) == length
    assert truncated == text[:length]
示例#3
0
def prepare_description(
    stage: "Stage", max_length: int = MAX_TEXT_LENGTH
) -> str:
    desc = stage.short_description() or generate_description(stage)
    return truncate_text(desc, max_length)