Skip to content

Commit 546f19b

Browse files
committed
write some tests
1 parent 6a59e50 commit 546f19b

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

Lib/idlelib/editor.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,6 +1338,23 @@ def set_indentation_params(self, is_py_src, guess=True):
13381338
self.usetabs = False
13391339
self.set_tk_tabwidth(self.tabwidth)
13401340

1341+
@staticmethod
1342+
def delete_trail_whitespace(want, chars, tabwidth):
1343+
ncharsdeleted = 0
1344+
have = len(chars.expandtabs(tabwidth))
1345+
for i in range(len(chars) - 1, -1, -1):
1346+
# ``Delete'' chars[i], and subtract count
1347+
# (since redoing expandtabs is O(n))
1348+
ncharsdeleted += 1
1349+
if chars[i] == '\t':
1350+
have -= tabwidth
1351+
else:
1352+
have -= 1
1353+
if have <= want or chars[i-1] not in " \t":
1354+
break
1355+
chars = chars[:len(chars) - ncharsdeleted]
1356+
return ncharsdeleted, chars
1357+
13411358
def smart_backspace_event(self, event):
13421359
text = self.text
13431360
first, last = self.get_selection_indices()
@@ -1366,19 +1383,7 @@ def smart_backspace_event(self, event):
13661383
assert have > 0
13671384
want = ((have - 1) // self.indentwidth) * self.indentwidth
13681385
# Debug prompt is multilined....
1369-
ncharsdeleted = 0
1370-
have = len(chars.expandtabs(tabwidth))
1371-
for i in range(len(chars) - 1, -1, -1):
1372-
# ``Delete'' chars[i], and subtract count
1373-
# (since redoing expandtabs is O(n))
1374-
ncharsdeleted += 1
1375-
if chars[i] == '\t':
1376-
have -= tabwidth
1377-
else:
1378-
have -= 1
1379-
if have <= want or chars[i-1] not in " \t":
1380-
break
1381-
chars = chars[:len(chars) - ncharsdeleted]
1386+
ncharsdeleted, chars = TestWindow.delete_trail_whitespace(want, chars, tabwidth)
13821387
text.undo_block_start()
13831388
text.delete("insert-%dc" % ncharsdeleted, "insert")
13841389
if have < want:

Lib/idlelib/idle_test/test_editor.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,5 +237,16 @@ def test_rclick(self):
237237
pass
238238

239239

240+
class DeleteWantTest(unittest.TestCase):
241+
242+
def test_delete_trail_whitespace(self):
243+
test_str = "abcde" + 10000 * "\t" + 10000 * " "
244+
res_str = Editor.delete_trail_whitespace(30000, test_str, 4)[1]
245+
self.assertEqual(res_str, "abcde" + 7498 * "\t")
246+
res_str = Editor.delete_trail_whitespace(41005, test_str, 4)[1]
247+
self.assertEqual(res_str, "abcde" + 10000 * "\t" + 1000 * " ")
248+
res_str = Editor.delete_trail_whitespace(4, test_str, 4)[1]
249+
self.assertEqual(res_str, "abcd")
250+
240251
if __name__ == '__main__':
241252
unittest.main(verbosity=2)

0 commit comments

Comments
 (0)