Files
wehub-resource-sync 4b6817381b
CI (OpenClaw E2E) / openclaw test (push) Has been cancelled
CI / coverage-report (push) Has been cancelled
CI / test-kubernetes (push) Has been cancelled
CI / should-run-thorough (push) Has been cancelled
CI / test-thorough (cloudwatch-demo) (push) Has been cancelled
CI / test-thorough (flink-ecs) (push) Has been cancelled
CI / test-thorough (upstream-lambda) (push) Has been cancelled
CI / test-thorough (prefect-ecs-fargate) (push) Has been cancelled
Release / build-binaries (zip, opensre.exe, onefile, windows-latest, windows-x64) (push) Has been cancelled
Benchmark image — build + push to ECR (any adapter) / build + push (push) Has been cancelled
CI / quality (ubuntu-latest) (push) Has been cancelled
CI / test (tools-runtime) (push) Has been cancelled
CI / test (e2e-general) (push) Has been cancelled
CI / test (cli-runtime) (push) Has been cancelled
CI / test (e2e-provider-and-openclaw) (push) Has been cancelled
CI / test (integrations-and-misc) (push) Has been cancelled
Release / verify (push) Has been cancelled
Release / build-python-dist (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, macos-15-intel, darwin-x64) (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, macos-latest, darwin-arm64) (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04, linux-x64) (push) Has been cancelled
Release / publish-release (push) Has been cancelled
Release / publish-main-release (push) Has been cancelled
Interactive Shell Live (PR + post-merge) / turn-checks (no-LLM) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Interactive Shell Live (PR + post-merge) / turn-live shard ${{ matrix.shard_index }} (push) Has been cancelled
Release / prepare (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04-arm, linux-arm64) (push) Has been cancelled
Synthetic Deterministic Tests / Synthetic offline (deterministic) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:10:45 +08:00

85 lines
2.5 KiB
Python

"""Tests for the shared truncation utility."""
import pytest
from platform.common.truncation import truncate
@pytest.mark.parametrize(
"text,limit,suffix,expected",
[
# text shorter than limit — returned unchanged
("short", 10, "...", "short"),
# text exactly at limit — returned unchanged
("exactly10c", 10, "...", "exactly10c"),
# text longer than limit — truncated with suffix
("this is a long string", 10, "...", "this is..."),
# limit equals suffix length — suffix returned as-is
("long", 3, "...", "..."),
# unicode suffix
("unicode text", 5, "…", "unic…"),
# no change needed
("no change needed", 100, "...", "no change needed"),
],
)
def test_truncate(text: str, limit: int, suffix: str, expected: str) -> None:
assert truncate(text, limit, suffix=suffix) == expected
def test_truncate_result_length_equals_limit() -> None:
result = truncate("hello world", 8)
assert result == "hello..."
assert len(result) == 8
def test_truncate_unicode_suffix() -> None:
result = truncate("hello world", 8, suffix="…")
assert result.endswith("…")
assert len(result) == 8
def test_truncate_text_at_exact_limit_not_truncated() -> None:
text = "a" * 10
assert truncate(text, 10) == text
def test_truncate_empty_string() -> None:
assert truncate("", 5) == ""
def test_truncate_limit_smaller_than_suffix_returns_clipped_suffix() -> None:
# limit=2, suffix="..." (len 3) — cannot fit suffix, return suffix[:limit]
result = truncate("hello", 2, suffix="...")
assert result == ".."
assert len(result) == 2
def test_truncate_limit_zero_returns_empty_string() -> None:
result = truncate("hello", 0, suffix="...")
assert result == ""
assert len(result) == 0
def test_truncate_limit_one_with_long_suffix() -> None:
result = truncate("hello", 1, suffix="...")
assert result == "."
assert len(result) == 1
def test_truncate_suffix_longer_than_text_but_within_limit() -> None:
# text shorter than limit — no truncation even if suffix is long
result = truncate("hi", 10, suffix=".....")
assert result == "hi"
def test_truncate_very_long_text() -> None:
result = truncate("x" * 10_000, 100)
assert result.endswith("...")
assert len(result) == 100
def test_truncate_empty_suffix() -> None:
result = truncate("hello world", 5, suffix="")
assert result == "hello"
assert len(result) == 5