""" Standalone reproduction of the python-slugify trailing-delimiter bug. Runs the *exact* regression cases from the upstream fix (PR #200, commit 8f9a550) against both the buggy and the fixed `_modern_truncate`, and prints a before/after table. It is also a pytest module: * `TestBuggyTruncate` — asserts the *correct* output. These FAIL on the buggy module (documenting the bug) and PASS on the fixed one. * `TestFixedTruncate` — the same assertions, pinned to the fixed module. * `TestBuggyActuallyBuggy` — asserts the buggy module really does emit the wrong trailing delimiter, so the "before" state is pinned too. Run it two ways: # Human-readable before/after table python test_repro.py # As a test suite python -m pytest test_repro.py -v """ from __future__ import annotations import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).parent)) import truncate_buggy as buggy import truncate_fixed as fixed # (input_text, separator, max_length, word_boundary, save_order, expected) # Copied verbatim from the cases in PR #200's test. The input text is the # post-replacement string exactly as `_modern_slugify` passes it in. CASES = [ ("one--two", "-", 4, False, False, "one"), ("one--two", "-", 5, False, False, "one"), ("one--two", "-", 6, False, False, "one--t"), ("one--two", "::", 6, False, False, "one"), ("one--two", "::", 7, False, False, "one"), ("one--two", "::", 8, False, False, "one::::t"), ("--one", "-", 2, False, False, ""), ("--one", "-", 3, False, False, "--o"), ("---", "-", 2, False, False, ""), ("x--two", "x", 3, False, False, "x"), ("one--two", "", 4, False, False, "onet"), # The second upstream test (word_boundary=True, save_order both ways). ("--longword", "-", 2, True, False, ""), ("--longword", "-", 2, True, True, ""), ] def run_all(mod) -> list[tuple]: out = [] for text, sep, limit, wb, so, expected in CASES: got = mod.truncate(text, limit, wb, sep, so) out.append((text, sep, limit, wb, so, expected, got, got == expected)) return out def print_table() -> None: rows = run_all(buggy) fixed_rows = run_all(fixed) print("Trailing-delimiter bug — before/after (exact PR #200 cases)\n") header = f"{'input':12} {'sep':>4} {'max':>3} {'wb':>2} | {'expected':10} {'buggy':10} {'fixed':10} ok" print(header) print("-" * len(header)) for row, frow in zip(rows, fixed_rows): text, sep, limit, wb, so, expected, got, ok = row fgot = frow[6] mark = "PASS" if (not ok or fgot == expected) else "FAIL" sep_s = sep if sep else "(empty)" print(f"{text:12} {sep_s:>4} {limit:>3} {'Y' if wb else 'N':>2} | " f"{expected:10} {got:10} {fgot:10} {mark}") b_ok = sum(1 for r in rows if r[7]) f_ok = sum(1 for r in fixed_rows if r[7]) print("-" * len(header)) print(f"\nbuggy: {b_ok}/{len(rows)} correct " f"fixed: {f_ok}/{len(fixed_rows)} correct") # --------------------------------------------------------------------------- # pytest tests # --------------------------------------------------------------------------- import unittest class TestFixedTruncate(unittest.TestCase): """The fixed module must produce the correct output for every case.""" def test_all_cases_correct(self): for text, sep, limit, wb, so, expected in CASES: got = fixed.truncate(text, limit, wb, sep, so) self.assertEqual(got, expected, f"input={text!r} sep={sep!r} max={limit}") class TestBuggyTruncate(unittest.TestCase): """The buggy module is pinned to its (wrong) trailing-delimiter output. This documents *what* the bug was, and would fail if someone 'fixed' the buggy module in place — keeping the before/after repro honest. """ def test_buggy_emits_trailing_delimiter(self): # 'one--two' truncated to 5 chars must NOT end in a delimiter. self.assertEqual(buggy.truncate("one--two", 5, False, "-", False), "one-") # wrong: trailing '-' self.assertEqual(buggy.truncate("--one", 2, False, "-", False), "-") self.assertEqual(buggy.truncate("x--two", 3, False, "x", False), "xx") def test_buggy_word_boundary_leads_with_delimiter(self): self.assertEqual(buggy.truncate("--longword", 2, True, "-", False), "-") if __name__ == "__main__": print_table() print("\n(see 'python -m pytest test_repro.py -v' for the pinned assertions)")