-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Fix sphinx.writers.text.TextWrapper
#13762
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
picnixz
wants to merge
6
commits into
sphinx-doc:master
Choose a base branch
from
picnixz:fix/writer/text-table
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f7b5e2a
fix `sphinx.writers.text.Table.wrap`
picnixz d01a3b5
Update CHANGES.rst
picnixz de24ced
Merge branch 'master' into fix/writer/text-table
picnixz b3e6f2f
Merge branch 'master' into fix/writer/text-table
picnixz 37f1983
Merge branch 'master' into fix/writer/text-table
picnixz ecdb434
Update CHANGES.rst
picnixz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| """Test the LaTeX writer""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import pytest | ||
| from docutils.utils import column_width | ||
|
|
||
| from sphinx.writers.text import TextWrapper | ||
|
|
||
| find_break_end = TextWrapper._find_break_end | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| # glyph of column width 0 | ||
| 'glyph', | ||
| ['', '\N{COMBINING TILDE}'], | ||
| ) | ||
| def test_text_wrapper_break_phantom_symbol(glyph: str) -> None: | ||
| assert column_width(glyph) == 0 | ||
| glyph_length = len(glyph) | ||
|
|
||
| for n in range(1, 5): | ||
| # Since the glyph has length 0 and column width 0, | ||
| # we can always take the entire glpyh. | ||
| assert find_break_end(glyph, n) == glyph_length | ||
| for m in range(1, 5): | ||
| # The multiplied glyph may have non-zero length | ||
| # but its column width will always be 0, so we | ||
| # take the entire glyph again. | ||
| assert find_break_end(m * glyph, n) == m * glyph_length | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ('text', 'colwidth'), | ||
| [ | ||
| # Glyph of length 1 and column width 1 | ||
| ('X', 1), | ||
| # Glyph of length 1 and column width 2 | ||
| ('\N{CJK UNIFIED IDEOGRAPH-65E5}', 2), | ||
| # Glyph of length 2 and column width 1 | ||
| ('\N{COMBINING TILDE}X', 1), | ||
| # Glyph of length 2 and column width 2 | ||
| ('\N{COMBINING TILDE}\N{CJK UNIFIED IDEOGRAPH-65E5}', 2), | ||
| # Glyph of length 3 and column width 1 | ||
| ('\N{COMBINING TILDE}\N{COMBINING BREVE}X', 1), | ||
| ], | ||
| ) | ||
| def test_text_wrapper_break_visible_symbol(text: str, colwidth: int) -> None: | ||
| assert column_width(text) == colwidth | ||
| for n in range(1, 5): | ||
| end = find_break_end(text, n) | ||
| assert column_width(text[:end]) <= n | ||
| for m in range(2, 5): | ||
| m_text = m * text | ||
| end = find_break_end(m_text, n) | ||
| assert column_width(m_text[:end]) <= n | ||
| assert end == m * len(text) or column_width(m_text[: end + 1]) > n | ||
|
|
||
|
|
||
| def test_text_wrapper_break_stop_after_combining_symbols() -> None: | ||
| tilde = '\N{COMBINING TILDE}' | ||
| multi = '\N{CJK UNIFIED IDEOGRAPH-65E5}' | ||
|
|
||
| head = tilde + tilde + '....' | ||
| tail = multi + tilde + tilde | ||
| text = head + tail | ||
| assert find_break_end(head + tail, column_width(head)) == len(head) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ('text', 'results'), | ||
| [ | ||
| ('Hello', {1: list('Hello'), 2: ['He', 'll', 'o']}), | ||
| ( | ||
| 'Hello a\N{CJK UNIFIED IDEOGRAPH-65E5}ab!', | ||
| { | ||
| 1: list('Helloa\N{CJK UNIFIED IDEOGRAPH-65E5}ab!'), | ||
| 2: ['He', 'll', 'o', 'a', '\N{CJK UNIFIED IDEOGRAPH-65E5}', 'ab', '!'], | ||
| 3: ['Hel', 'lo', 'a\N{CJK UNIFIED IDEOGRAPH-65E5}', 'ab!'], | ||
| }, | ||
| ), | ||
| ( | ||
| 'ab c\N{COMBINING TILDE}def', | ||
| { | ||
| 1: ['a', 'b', 'c\N{COMBINING TILDE}', 'd', 'e', 'f'], | ||
| 2: ['ab', 'c\N{COMBINING TILDE}d', 'ef'], | ||
| 3: ['ab ', 'c\N{COMBINING TILDE}de', 'f'], | ||
| }, | ||
| ), | ||
| ( | ||
| 'abc\N{COMBINING TILDE}\N{CJK UNIFIED IDEOGRAPH-65E5}def', | ||
| { | ||
| 1: [ | ||
| 'a', | ||
| 'b', | ||
| 'c\N{COMBINING TILDE}', | ||
| '\N{CJK UNIFIED IDEOGRAPH-65E5}', | ||
| 'd', | ||
| 'e', | ||
| 'f', | ||
| ], | ||
| 2: [ | ||
| 'ab', | ||
| 'c\N{COMBINING TILDE}', | ||
| '\N{CJK UNIFIED IDEOGRAPH-65E5}', | ||
| 'de', | ||
| 'f', | ||
| ], | ||
| 3: ['abc\N{COMBINING TILDE}', '\N{CJK UNIFIED IDEOGRAPH-65E5}', 'def'], | ||
| }, | ||
| ), | ||
| ( | ||
| 'abc\N{COMBINING TILDE}\N{COMBINING BREVE}def', | ||
| { | ||
| 1: ['a', 'b', 'c\N{COMBINING TILDE}\N{COMBINING BREVE}', 'd', 'e', 'f'], | ||
| 2: ['ab', 'c\N{COMBINING TILDE}\N{COMBINING BREVE}d', 'ef'], | ||
| 3: ['abc\N{COMBINING TILDE}\N{COMBINING BREVE}', 'def'], | ||
| }, | ||
| ), | ||
| ], | ||
| ) | ||
| def test_text_wrapper(text: str, results: dict[int, list[str]]) -> None: | ||
| for width, expected in results.items(): | ||
| w = TextWrapper(width=width, drop_whitespace=True) | ||
| assert w.wrap(text) == expected | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ('text', 'results'), | ||
| [ | ||
| ('Hello', {1: list('Hello'), 2: ['He', 'll', 'o']}), | ||
| ( | ||
| 'Hello a\N{CJK UNIFIED IDEOGRAPH-65E5}ab!', | ||
| { | ||
| 1: list('Hello a\N{CJK UNIFIED IDEOGRAPH-65E5}ab!'), | ||
| 2: ['He', 'll', 'o ', 'a', '\N{CJK UNIFIED IDEOGRAPH-65E5}', 'ab', '!'], | ||
| 3: ['Hel', 'lo ', 'a\N{CJK UNIFIED IDEOGRAPH-65E5}', 'ab!'], | ||
| }, | ||
| ), | ||
| ( | ||
| 'ab c\N{COMBINING TILDE}def', | ||
| { | ||
| 1: ['a', 'b', ' ', 'c\N{COMBINING TILDE}', 'd', 'e', 'f'], | ||
| 2: ['ab', ' c\N{COMBINING TILDE}', 'de', 'f'], | ||
| 3: ['ab ', 'c\N{COMBINING TILDE}de', 'f'], | ||
| }, | ||
| ), | ||
| ( | ||
| 'abc\N{COMBINING TILDE}\N{CJK UNIFIED IDEOGRAPH-65E5}def', | ||
| { | ||
| 1: [ | ||
| 'a', | ||
| 'b', | ||
| 'c\N{COMBINING TILDE}', | ||
| '\N{CJK UNIFIED IDEOGRAPH-65E5}', | ||
| 'd', | ||
| 'e', | ||
| 'f', | ||
| ], | ||
| 2: [ | ||
| 'ab', | ||
| 'c\N{COMBINING TILDE}', | ||
| '\N{CJK UNIFIED IDEOGRAPH-65E5}', | ||
| 'de', | ||
| 'f', | ||
| ], | ||
| 3: ['abc\N{COMBINING TILDE}', '\N{CJK UNIFIED IDEOGRAPH-65E5}', 'def'], | ||
| }, | ||
| ), | ||
| ( | ||
| 'abc\N{COMBINING TILDE}\N{COMBINING BREVE}def', | ||
| { | ||
| 1: ['a', 'b', 'c\N{COMBINING TILDE}\N{COMBINING BREVE}', 'd', 'e', 'f'], | ||
| 2: ['ab', 'c\N{COMBINING TILDE}\N{COMBINING BREVE}d', 'ef'], | ||
| 3: ['abc\N{COMBINING TILDE}\N{COMBINING BREVE}', 'def'], | ||
| }, | ||
| ), | ||
| ], | ||
| ) | ||
| def test_text_wrapper_drop_ws(text: str, results: dict[int, list[str]]) -> None: | ||
| for width, expected in results.items(): | ||
| w = TextWrapper(width=width, drop_whitespace=False) | ||
| assert w.wrap(text) == expected |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.