cpython: 22ce68d98345 (original) (raw)
Mercurial > cpython
changeset 84613:22ce68d98345 3.3
Issue #18279: Add tests for idlelib/RstripExtension.py. Original patch by Phil Webster. With that available, modify RstripExtension.py to stop deleting null slices, which caused a file to be marked as changed when it was not. [#18279]
Terry Jan Reedy tjreedy@udel.edu | |
---|---|
date | Sat, 13 Jul 2013 02:34:43 -0400 |
parents | f91e6d0eb0f7 |
children | ffd923b388d8 bc3a34e47923 |
files | Lib/idlelib/RstripExtension.py Lib/idlelib/idle_test/mock_idle.py Lib/idlelib/idle_test/test_rstrip.py Misc/ACKS Misc/NEWS |
diffstat | 5 files changed, 94 insertions(+), 8 deletions(-)[+] [-] Lib/idlelib/RstripExtension.py 20 Lib/idlelib/idle_test/mock_idle.py 27 Lib/idlelib/idle_test/test_rstrip.py 49 Misc/ACKS 1 Misc/NEWS 5 |
line wrap: on
line diff
--- a/Lib/idlelib/RstripExtension.py +++ b/Lib/idlelib/RstripExtension.py @@ -1,13 +1,9 @@ 'Provides "Strip trailing whitespace" under the "Format" menu.' -author = "Roger D. Serwy <roger.serwy at gmail.com>" - class RstripExtension: menudefs = [
('format', [None,[](#l1.11)
('Strip trailing whitespace', '<<do-rstrip>>'),[](#l1.12)
]),][](#l1.13)
('format', [None, ('Strip trailing whitespace', '<<do-rstrip>>'), ] ), ][](#l1.14)
def init(self, editwin): self.editwin = editwin @@ -20,10 +16,18 @@ class RstripExtension: undo.undo_block_start()
end_line = int(float(text.index('end'))) + 1[](#l1.22)
end_line = int(float(text.index('end')))[](#l1.23) for cur in range(1, end_line):[](#l1.24)
txt = text.get('%i.0' % cur, '%i.0 lineend' % cur)[](#l1.25)
txt = text.get('%i.0' % cur, '%i.end' % cur)[](#l1.26)
raw = len(txt)[](#l1.27) cut = len(txt.rstrip())[](#l1.28)
text.delete('%i.%i' % (cur, cut), '%i.0 lineend' % cur)[](#l1.29)
# Since text.delete() marks file as changed, even if not,[](#l1.30)
# only call it when needed to actually delete something.[](#l1.31)
if cut < raw:[](#l1.32)
text.delete('%i.%i' % (cur, cut), '%i.end' % cur)[](#l1.33)
undo.undo_block_stop() + +if name == "main":
new file mode 100644 --- /dev/null +++ b/Lib/idlelib/idle_test/mock_idle.py @@ -0,0 +1,27 @@ +'''Mock classes that imitate idlelib modules or classes. + +Attributes and methods will be added as needed for tests. +''' + +from idlelib.idle_test.mock_tk import Text + +class Editor:
- '''Minimally imitate EditorWindow.EditorWindow class.
- '''
- def init(self, flist=None, filename=None, key=None, root=None):
self.text = Text()[](#l2.16)
self.undo = UndoDelegator()[](#l2.17)
- def get_selection_indices(self):
first = self.text.index('1.0')[](#l2.20)
last = self.text.index('end')[](#l2.21)
return first, last[](#l2.22)
- '''Minimally imitate UndoDelegator,UndoDelegator class.
- '''
A real undo block is only needed for user interaction.
- def undo_block_start(*args):
pass[](#l2.29)
- def undo_block_stop(*args):
pass[](#l2.31)
new file mode 100644 --- /dev/null +++ b/Lib/idlelib/idle_test/test_rstrip.py @@ -0,0 +1,49 @@ +import unittest +import idlelib.RstripExtension as rs +from idlelib.idle_test.mock_idle import Editor + +class rstripTest(unittest.TestCase): +
- def test_rstrip_line(self):
editor = Editor()[](#l3.12)
text = editor.text[](#l3.13)
do_rstrip = rs.RstripExtension(editor).do_rstrip[](#l3.14)
do_rstrip()[](#l3.16)
self.assertEqual(text.get('1.0', 'insert'), '')[](#l3.17)
text.insert('1.0', ' ')[](#l3.18)
do_rstrip()[](#l3.19)
self.assertEqual(text.get('1.0', 'insert'), '')[](#l3.20)
text.insert('1.0', ' \n')[](#l3.21)
do_rstrip()[](#l3.22)
self.assertEqual(text.get('1.0', 'insert'), '\n')[](#l3.23)
- def test_rstrip_multiple(self):
editor = Editor()[](#l3.26)
# Uncomment following to verify that test passes with real widgets.[](#l3.27)
+## from idlelib.EditorWindow import EditorWindow as Editor +## from tkinter import Tk +## editor = Editor(root=Tk())
text = editor.text[](#l3.31)
do_rstrip = rs.RstripExtension(editor).do_rstrip[](#l3.32)
original = ([](#l3.34)
"Line with an ending tab \n"[](#l3.35)
"Line ending in 5 spaces \n"[](#l3.36)
"Linewithnospaces\n"[](#l3.37)
" indented line\n"[](#l3.38)
" indented line with trailing space \n"[](#l3.39)
" ")[](#l3.40)
stripped = ([](#l3.41)
"Line with an ending tab\n"[](#l3.42)
"Line ending in 5 spaces\n"[](#l3.43)
"Linewithnospaces\n"[](#l3.44)
" indented line\n"[](#l3.45)
" indented line with trailing space\n")[](#l3.46)
text.insert('1.0', original)[](#l3.48)
do_rstrip()[](#l3.49)
self.assertEqual(text.get('1.0', 'insert'), stripped)[](#l3.50)
--- a/Misc/ACKS +++ b/Misc/ACKS @@ -1303,6 +1303,7 @@ Aaron Watters Henrik Weber Corran Webster Glyn Webster +Phil Webster Stefan Wehr Zack Weinberg Bob Weiner
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -160,6 +160,10 @@ C API IDLE ---- +- Issue #18279: Format - Strip trailing whitespace no longer marks a file as
- changed when it has not been changed. This fix followed the addition of a
- test file originally written by Phil Webster (the issue's main goal). +