|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING |
| 4 | + |
| 5 | +from commitizen import interactive_preview |
| 6 | + |
| 7 | +if TYPE_CHECKING: |
| 8 | + import pytest |
| 9 | + |
| 10 | + |
| 11 | +def test_wrap_display_width_empty_and_non_positive_width(): |
| 12 | + assert interactive_preview._wrap_display_width("", 10) == [] |
| 13 | + assert interactive_preview._wrap_display_width("abc", 0) == ["abc"] |
| 14 | + assert interactive_preview._wrap_display_width("abc", -1) == ["abc"] |
| 15 | + |
| 16 | + |
| 17 | +def test_wrap_display_width_ascii_simple_wrap(monkeypatch: pytest.MonkeyPatch): |
| 18 | + monkeypatch.setattr(interactive_preview, "get_cwidth", lambda _c: 1) |
| 19 | + assert interactive_preview._wrap_display_width("abcd", 2) == ["ab", "cd"] |
| 20 | + assert interactive_preview._wrap_display_width("abc", 2) == ["ab", "c"] |
| 21 | + |
| 22 | + |
| 23 | +def test_wrap_display_width_cjk_width_2(monkeypatch: pytest.MonkeyPatch): |
| 24 | + def fake_cwidth(c: str) -> int: |
| 25 | + return 2 if c == "你" else 1 |
| 26 | + |
| 27 | + monkeypatch.setattr(interactive_preview, "get_cwidth", fake_cwidth) |
| 28 | + assert interactive_preview._wrap_display_width("你a", 2) == ["你", "a"] |
| 29 | + |
| 30 | + |
| 31 | +def test_make_toolbar_content_includes_preview_and_counter_with_max( |
| 32 | + monkeypatch: pytest.MonkeyPatch, |
| 33 | + mocker, |
| 34 | +): |
| 35 | + def subject_builder(_field: str, _text: str) -> str: |
| 36 | + return "feat: abc" |
| 37 | + |
| 38 | + monkeypatch.setattr( |
| 39 | + interactive_preview, |
| 40 | + "get_terminal_size", |
| 41 | + lambda: mocker.Mock(columns=20), |
| 42 | + ) |
| 43 | + |
| 44 | + content = interactive_preview.make_toolbar_content( |
| 45 | + subject_builder, "subject", "abc", max_length=50 |
| 46 | + ) |
| 47 | + preview, counter = content.splitlines() |
| 48 | + assert preview == "feat: abc" |
| 49 | + assert counter.strip() == "9/50 chars" |
| 50 | + |
| 51 | + |
| 52 | +def test_make_toolbar_content_counter_without_max_when_zero( |
| 53 | + monkeypatch: pytest.MonkeyPatch, |
| 54 | + mocker, |
| 55 | +): |
| 56 | + def subject_builder(_field: str, _text: str) -> str: |
| 57 | + return "fix: a" |
| 58 | + |
| 59 | + monkeypatch.setattr( |
| 60 | + interactive_preview, |
| 61 | + "get_terminal_size", |
| 62 | + lambda: mocker.Mock(columns=80), |
| 63 | + ) |
| 64 | + content = interactive_preview.make_toolbar_content( |
| 65 | + subject_builder, "subject", "a", max_length=0 |
| 66 | + ) |
| 67 | + assert content.splitlines()[-1].strip() == "6 chars" |
| 68 | + |
| 69 | + |
| 70 | +def test_make_toolbar_content_terminal_size_oserror_fallback_80( |
| 71 | + monkeypatch: pytest.MonkeyPatch, |
| 72 | +): |
| 73 | + def subject_builder(_field: str, _text: str) -> str: |
| 74 | + return "feat: abc" |
| 75 | + |
| 76 | + def raise_oserror(): |
| 77 | + raise OSError("no tty") |
| 78 | + |
| 79 | + monkeypatch.setattr(interactive_preview, "get_terminal_size", raise_oserror) |
| 80 | + |
| 81 | + content = interactive_preview.make_toolbar_content( |
| 82 | + subject_builder, "subject", "abc", max_length=50 |
| 83 | + ) |
| 84 | + assert content.splitlines()[-1].startswith(" " * (80 - len("9/50 chars"))) |
| 85 | + |
| 86 | + |
| 87 | +def test_make_toolbar_content_counter_padding_not_negative( |
| 88 | + monkeypatch: pytest.MonkeyPatch, |
| 89 | + mocker, |
| 90 | +): |
| 91 | + def subject_builder(_field: str, _text: str) -> str: |
| 92 | + return "x" |
| 93 | + |
| 94 | + # Make width tiny so counter is longer than width |
| 95 | + monkeypatch.setattr( |
| 96 | + interactive_preview, |
| 97 | + "get_terminal_size", |
| 98 | + lambda: mocker.Mock(columns=3), |
| 99 | + ) |
| 100 | + content = interactive_preview.make_toolbar_content( |
| 101 | + subject_builder, "subject", "x", max_length=50 |
| 102 | + ) |
| 103 | + assert content.splitlines()[-1] == "1/50 chars" |
| 104 | + |
| 105 | + |
| 106 | +def test_make_length_validator_disabled_when_max_length_zero(): |
| 107 | + def subject_builder(_field: str, text: str) -> str: |
| 108 | + return text |
| 109 | + |
| 110 | + validate = interactive_preview.make_length_validator( |
| 111 | + subject_builder, "subject", max_length=0 |
| 112 | + ) |
| 113 | + assert validate("x" * 10) is True |
| 114 | + |
| 115 | + |
| 116 | +def test_make_length_validator_returns_error_string_when_exceeded(): |
| 117 | + def subject_builder(_field: str, text: str) -> str: |
| 118 | + return text |
| 119 | + |
| 120 | + validate = interactive_preview.make_length_validator( |
| 121 | + subject_builder, "subject", max_length=3 |
| 122 | + ) |
| 123 | + assert validate("abc") is True |
| 124 | + assert validate("abcd") == "4/3 chars (subject length exceeded)" |
0 commit comments