Skip to content
Merged
Prev Previous commit
Next Next commit
Apply review comments
Test the right click in a selection and not in one, as suggested.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
  • Loading branch information
serhiy-storchaka and claude committed Sep 8, 2026
commit 3b3f69d7386dc164981ed0cf3518ada7bc52ecf7
57 changes: 27 additions & 30 deletions Lib/idlelib/idle_test/test_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,55 +238,52 @@ def tearDownClass(cls):
class DummyRMenu:
def tk_popup(x, y): pass

def click(self, x=0, y=0):
"""Simulate a right click at the (x, y) pixel of the text.

Return the index of the clicked character, as computed by the
widget itself. It cannot be computed here, because the geometry
of the text is unknown while its window is not mapped.
def click(self):
"""Simulate a right click at(text pixel 0,0).
"""
index = self.text.index(f'@{x},{y}')
Event = namedtuple('Event', ['x', 'y', 'x_root', 'y_root'])
event = Event(x, y, x_root=0, y_root=0)
event = Event(0, 0, 0, 0)
self.assertEqual(self.window.right_menu_event(event), 'break')
return index

def test_rclick_no_selection(self):
text = self.text
insert(text, 'one two three')
index = self.click()
self.assertEqual(text.tag_ranges('sel'), ())
self.assertEqual(text.index('insert'), index)
# This assertion should be moved to a new method that also
# text that dummy rmenu.tk_popup is called.

def test_rclick_outside_selection(self):
def test_rclick_not_in_a_selection(self):
# Like left click, 'insert' moves to click and any selection is deleted.
eq = self.assertEqual
text = self.text
insert(text, 'one two three')
# The selection does not contain the clicked character.
text.tag_add('sel', '1.5', '1.8')
# Selection exists but not clicked.
text.tag_add('sel', '1.4', '1.7') # 'two' selected.
text.mark_set('insert', '1.7') # Outside of selection.
self.click()
eq(text.tag_ranges('sel'), ())
eq(text.index('insert'), '1.0')
# No selection to click, same result.
text.mark_set('insert', '1.8')
index = self.click()
self.assertEqual(text.tag_ranges('sel'), ())
self.assertEqual(text.index('insert'), index)
eq(text.tag_ranges('sel'), ())
eq(text.index('insert'), '1.0')

def test_rclick_inside_selection(self):
# Unlike left click, selection is not deleted.
eq = self.assertEqual
text = self.text
insert(text, 'one two three')
insert(text, 'one two three') # 'insert' at 1.13.
# The selection contains the clicked character.
index = text.index('@0,0')
text.tag_add('sel', index, f'{index}+3c')
text.mark_set('insert', 'end-1c')
text.tag_add('sel', 1.0, 1.3) # Select 'one'.
Comment thread
serhiy-storchaka marked this conversation as resolved.
Outdated
text.mark_set('insert', '1.3') # If select rightward, 'insert' at 1.3.
self.click()
self.assertEqual(text.index('sel.first'), index)
self.assertEqual(text.index('sel.last'), text.index(f'{index}+3c'))
self.assertEqual(text.index('insert'), text.index('end-1c'))
eq((text.index('sel.first'), text.index('sel.last')), ('1.0', '1.3'))
eq(text.index('insert'), '1.3')

def test_rmenu_check_copy(self):
# copy and cut only valid for click inside selection.
eq = self.assertEqual
text = self.text
insert(text, 'one two three')
eq = self.assertEqual
eq(self.window.rmenu_check_copy(), 'disabled')
eq(self.window.rmenu_check_cut(), 'disabled')
text.tag_add('sel', '1.0', '1.3')
text.tag_add('sel', '1.0', '1.3') # Includes '1.0' click.
eq(self.window.rmenu_check_copy(), 'normal')
eq(self.window.rmenu_check_cut(), 'normal')

Expand Down
Loading