Skip to content
Next Next commit
bpo-35208: Fix Squeezer line counting logic
  • Loading branch information
taleinat committed Nov 10, 2018
commit a803865b00b8196ceb251b563502a68bad770c47
23 changes: 23 additions & 0 deletions Lib/idlelib/idle_test/test_squeezer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections import namedtuple
from textwrap import dedent
from tkinter import Text, Tk
import unittest
from unittest.mock import Mock, NonCallableMagicMock, patch, sentinel, ANY
Expand Down Expand Up @@ -77,6 +78,28 @@ def test_tab_width(self):
self.check(expected=3, text='\t' * 6, linewidth=11, tabwidth=4)
self.check(expected=2, text='\t' * 6, linewidth=13, tabwidth=4)

def test_empty_lines(self):
self.check(expected=1, text='\n', linewidth=80, tabwidth=8)
self.check(expected=2, text='\n\n', linewidth=80, tabwidth=8)
self.check(expected=10, text='\n' * 10, linewidth=80, tabwidth=8)

def test_long_line(self):
self.check(expected=3, text='a' * 200, linewidth=80, tabwidth=8)
self.check(expected=3, text='a' * 200 + '\n', linewidth=80, tabwidth=8)

def test_several_lines_different_lengths(self):
text = dedent("""\
13 characters
43 is the number of characters on this line

7 chars
13 characters""")
self.check(expected=5, text=text, linewidth=80, tabwidth=8)
self.check(expected=5, text=text + '\n', linewidth=80, tabwidth=8)
self.check(expected=6, text=text, linewidth=40, tabwidth=8)
self.check(expected=7, text=text, linewidth=20, tabwidth=8)
self.check(expected=11, text=text, linewidth=10, tabwidth=8)


class SqueezerTest(unittest.TestCase):
"""Tests for the Squeezer class."""
Expand Down
8 changes: 7 additions & 1 deletion Lib/idlelib/squeezer.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@ def count_lines_with_wrapping(s, linewidth=80, tabwidth=8):

# deal with tab or newline
if s[pos] == '\n':
linecount += 1
# If the current column was exactly linewidth, divmod would give
# (1,0), even though a new line hadn't yet been started. The same
# is true if length is any exact multiple of linewidth. Therefore,
# subtract 1 before dividing, except if the current column is 0.
linecount += 1 + (
((current_column - 1) // linewidth) if current_column > 0 else 0
)
current_column = 0
else:
assert s[pos] == '\t'
Expand Down