Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
a7e130a
bpo-1529353: update the Squeezer extension and its tests
taleinat Jun 11, 2018
0af6a62
bpo-1529353: use idlelib.textview for previewing squeezed output
taleinat Aug 12, 2018
71fb2c1
Merge branch 'master' into bpo-1529353
taleinat Aug 12, 2018
bda6572
Refactor Squeezer from an extension to an integral part of IDLE.
taleinat Aug 13, 2018
53e74d9
Merge branch 'master' into bpo-1529353
taleinat Aug 27, 2018
902091e
Update Squeezer's tests given the latest changes
taleinat Aug 27, 2018
6d7035b
add ability to control text wrapping in IDLE's text viewer
taleinat Aug 28, 2018
45fb1a6
ask for user confirmation before expanding huge squeezed outputs
taleinat Aug 28, 2018
18106e8
increase Squeezer's default auto-squeeze-min-lines from 30 to 50
taleinat Aug 28, 2018
2c795e3
move 'copy' and 'preview' squeezed output actions to a context menu
taleinat Aug 28, 2018
1a1288a
Squeezer: rename "preview" to "view"
taleinat Aug 28, 2018
f753b37
Squeezer: remove tooltip configuration options (delay set to 80ms)
taleinat Aug 30, 2018
6087d3f
Squeezer: remove expand/view-last-squeezed events
taleinat Aug 30, 2018
b7d27cb
Squeezer: make view window non-modal and add horizontal scrollbar
taleinat Aug 30, 2018
ce07987
Merge remote-tracking branch 'origin/master' into pr_7626.
terryjreedy Sep 25, 2018
9676991
Correct error in merge resolution.
terryjreedy Sep 25, 2018
e768e0f
Make test_squeezer runnable.
terryjreedy Sep 25, 2018
fcc9084
bpo-1529353: explicitly create root Tk() objects in tests
taleinat Sep 25, 2018
468d9ec
bpo-1529353: reformat doc-strings as PEP8 and rename test classes
taleinat Sep 25, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
bpo-1529353: use idlelib.textview for previewing squeezed output
  • Loading branch information
taleinat committed Aug 12, 2018
commit 0af6a62245db4f830e89ab8fa12a74910cceae59
2 changes: 0 additions & 2 deletions Lib/idlelib/config-extensions.def
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ enable_editor= False
auto-squeeze-min-lines= 30
show-tooltip= True
tooltip-delay= 500
preview-command-win= notepad.exe {filepath}
preview-command-posix=
[Squeezer_bindings]
expand-last-squeezed=
preview-last-squeezed=
Expand Down
159 changes: 13 additions & 146 deletions Lib/idlelib/idle_test/test_squeezer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
from tkinter import Text
import unittest
from unittest.mock import Mock, NonCallableMagicMock, patch, sentinel, ANY
from test.support import captured_stderr, requires
from test.support import requires

from idlelib.squeezer import count_lines_with_wrapping, ExpandingButton, \
Squeezer
from idlelib.textview import view_text
from idlelib.pyshell import PyShell


Expand Down Expand Up @@ -266,22 +267,6 @@ def test_preview_last_squeezed_event_no_squeezed(self):
self.assertEqual(retval, "break")
self.assertEqual(squeezer.text.bell.call_count, 1)

def test_preview_last_squeezed_event_no_preview_command(self):
"""test the preview_last_squeezed event"""
# The tested scenario: There is one squeezed text, therefore there
# is one ExpandingButton instance. However, no preview command has been
# configured. The preview_last_squeezed event is called and should fail
# (i.e. call squeezer.text.bell()).
editwin = self.make_mock_editor_window()
squeezer = self.make_squeezer_instance(editwin)
squeezer.get_preview_command = Mock(return_value='')
mock_expandingbutton = Mock()
squeezer.expandingbuttons = [mock_expandingbutton]

retval = squeezer.preview_last_squeezed_event(event=Mock())
self.assertEqual(retval, "break")
self.assertEqual(squeezer.text.bell.call_count, 1)

def test_preview_last_squeezed_event(self):
"""test the preview_last_squeezed event"""
# The tested scenario: There are two squeezed texts, therefore there
Expand All @@ -290,7 +275,6 @@ def test_preview_last_squeezed_event(self):
# second ExpandingButton.
editwin = self.make_mock_editor_window()
squeezer = self.make_squeezer_instance(editwin)
squeezer.get_preview_command = Mock(return_value='notepad.exe %(fn)s')
mock_expandingbutton1 = Mock()
mock_expandingbutton2 = Mock()
squeezer.expandingbuttons = [mock_expandingbutton1,
Expand Down Expand Up @@ -441,32 +425,6 @@ def test_get_auto_squeeze_min_lines(self):
self.assertEqual(sig.type, "int")
self.assertNotEqual(sig.default, sentinel.NOT_GIVEN)

def test_get_preview_command(self):
"""test the preview-command config getter"""
fake_cmd = 'FAKE_VIEWER_APP {filepath}'
with patch('idlelib.config.idleConf.GetOption') as MockGetOption:
MockGetOption.return_value = fake_cmd
retval = Squeezer.get_preview_command()

self.assertEqual(retval, fake_cmd)
self.assertEqual(MockGetOption.call_count, 1)
sig = self.get_GetOption_signature(MockGetOption.call_args)
self.assertSequenceEqual(
(sig.configType, sig.section, sig.option),
("extensions", "Squeezer",
Squeezer._PREVIEW_COMMAND_CONFIG_PARAM_NAME),
)
self.assertTrue(sig.raw)
self.assertNotEqual(sig.default, sentinel.NOT_GIVEN)

def test_invalid_preview_command_template(self):
"""test the preview-command config getter"""
with patch('idlelib.config.idleConf.GetOption') as MockGetOption:
MockGetOption.return_value = 'FAKE_VIEWER_APP {filepath'
with captured_stderr():
retval = Squeezer.get_preview_command()
self.assertFalse(retval)

def test_get_show_tooltip(self):
"""test the show-tooltip config getter"""
with patch('idlelib.config.idleConf.GetOption') as MockGetOption:
Expand Down Expand Up @@ -499,44 +457,6 @@ def test_get_tooltip_delay(self):
self.assertEqual(sig.type, "int")
self.assertNotEqual(sig.default, sentinel.NOT_GIVEN)

def test_conditional_add_preview_last_squeezed_text_to_edit_menu(self):
"""test conditionally adding preview-last-squeezed to the edit menu"""
import importlib
import idlelib.squeezer

# cleanup -- make sure to reload idlelib.squeezer, since ths test
# messes around with it a bit
self.addCleanup(importlib.reload, idlelib.squeezer)

preview_last_squeezed_menu_item = \
("Preview last squeezed text", "<<preview-last-squeezed>>")

# We can't override idlelib.squeezer.Squeezer.get_preview_command()
# in time, since what we want to test happens at module load time,
# and such a change can only be done once the module has been loaded.
# Instead, we'll patch idlelib.config.idleConf.GetOption which
# is used by get_preview_command().
with patch('idlelib.config.idleConf.GetOption') as MockGetOption:
# First, load the module with no preview command defined, and check
# that the preview-last-squeezed option is not added to the Edit
# menu.
MockGetOption.return_value = ''
importlib.reload(idlelib.squeezer)
edit_menu = dict(idlelib.squeezer.Squeezer.menudefs)['edit']
self.assertNotIn(preview_last_squeezed_menu_item, edit_menu)

# save the length of the edit menu spec, for comparison later
edit_menu_len_without_preview_last = len(edit_menu)

# Second, load the module with a preview command defined, and check
# that the preview-last-squeezed option is indeed added to the Edit
# menu.
MockGetOption.return_value = 'notepad.exe %(fn)s'
importlib.reload(idlelib.squeezer)
edit_menu = dict(idlelib.squeezer.Squeezer.menudefs)['edit']
self.assertEqual(edit_menu[-1], preview_last_squeezed_menu_item)
self.assertEqual(len(edit_menu), edit_menu_len_without_preview_last + 1)


class TestExpandingButton(unittest.TestCase):
"""tests for the ExpandingButton class"""
Expand All @@ -550,17 +470,15 @@ def make_mock_squeezer(self):

# Set default values for the configuration settings
squeezer.get_max_num_of_lines = Mock(return_value=30)
squeezer.get_preview_command = Mock(return_value='')
squeezer.get_show_tooltip = Mock(return_value=False)
squeezer.get_tooltip_delay = Mock(return_value=1500)
return squeezer

@patch('idlelib.squeezer.ToolTip')
def test_init_no_preview_command_nor_tooltip(self, MockToolTip):
def test_init_no_tooltip(self, MockToolTip):
"""Test the simplest creation of an ExpandingButton"""
squeezer = self.make_mock_squeezer()
squeezer.get_show_tooltip.return_value = False
squeezer.get_preview_command.return_value = ''
text_widget = squeezer.editwin.text

expandingbutton = ExpandingButton('TEXT', 'TAGS', 30, squeezer)
Expand All @@ -576,67 +494,28 @@ def test_init_no_preview_command_nor_tooltip(self, MockToolTip):
# check that no tooltip was created
self.assertEqual(MockToolTip.call_count, 0)

def test_bindings_with_preview_command(self):
"""test tooltip creation with a preview command configured"""
squeezer = self.make_mock_squeezer()
squeezer.get_preview_command.return_value = 'notepad.exe %(fn)s'
expandingbutton = ExpandingButton('TEXT', 'TAGS', 30, squeezer)

# check that when a preview command is configured, an event is bound
# on the button for middle-click
# check that the mouse events are bound
self.assertIn('<Double-Button-1>', expandingbutton.bind())
self.assertIn('<Button-2>', expandingbutton.bind())
self.assertIn('<Button-3>', expandingbutton.bind())

def test_bindings_without_preview_command(self):
"""test tooltip creation without a preview command configured"""
squeezer = self.make_mock_squeezer()
squeezer.get_preview_command.return_value = ''
expandingbutton = ExpandingButton('TEXT', 'TAGS', 30, squeezer)

# check button's event bindings: double-click, right-click, middle-click
self.assertIn('<Double-Button-1>', expandingbutton.bind())
self.assertIn('<Button-2>', expandingbutton.bind())
self.assertNotIn('<Button-3>', expandingbutton.bind())

@patch('idlelib.squeezer.ToolTip')
def test_init_tooltip_with_preview_command(self, MockToolTip):
"""test tooltip creation with a preview command configured"""
def test_init_tooltip(self, MockToolTip):
"""test tooltip creation"""
squeezer = self.make_mock_squeezer()
squeezer.get_show_tooltip.return_value = True
squeezer.get_tooltip_delay.return_value = SENTINEL_VALUE
squeezer.get_preview_command.return_value = 'notepad.exe %(fn)s'
expandingbutton = ExpandingButton('TEXT', 'TAGS', 30, squeezer)

# check that ToolTip was called once, with appropriate values
self.assertEqual(MockToolTip.call_count, 1)
MockToolTip.assert_called_with(expandingbutton, ANY,
delay=SENTINEL_VALUE)

# check that 'right-click' appears in the tooltip text, since we
# configured a non-empty preview command
# check that 'right-click' appears in the tooltip text
tooltip_text = MockToolTip.call_args[0][1]
self.assertIn('right-click', tooltip_text.lower())

@patch('idlelib.squeezer.ToolTip')
def test_init_tooltip_without_preview_command(self, MockToolTip):
"""test tooltip creation without a preview command configured"""
squeezer = self.make_mock_squeezer()
squeezer.get_show_tooltip.return_value = True
squeezer.get_tooltip_delay.return_value = SENTINEL_VALUE
squeezer.get_preview_command.return_value = ''
expandingbutton = ExpandingButton('TEXT', 'TAGS', 30, squeezer)

# check that ToolTip was called once, with appropriate values
self.assertEqual(MockToolTip.call_count, 1)
MockToolTip.assert_called_with(expandingbutton, ANY,
delay=SENTINEL_VALUE)

# check that 'right-click' doesn't appear in the tooltip text, since
# we configured an empty preview command
tooltip_text = MockToolTip.call_args[0][1]
self.assertNotIn('right-click', tooltip_text.lower())

def test_expand(self):
"""test the expand event"""
squeezer = self.make_mock_squeezer()
Expand Down Expand Up @@ -698,28 +577,16 @@ def _file_cleanup(self, filename):
def test_preview(self):
"""test the preview event"""
squeezer = self.make_mock_squeezer()
squeezer.get_preview_command.return_value = 'FAKE_VIEWER_APP {filepath}'
expandingbutton = ExpandingButton('TEXT', 'TAGS', 30, squeezer)
expandingbutton.selection_own = Mock()

with patch('subprocess.Popen') as mock_popen:
with patch('idlelib.textview.view_text', autospec=view_text)\
as mock_view_text:
# trigger the preview event
expandingbutton.preview(event=Mock())

# check that the expanding button called Popen once
self.assertEqual(mock_popen.call_count, 1)

command = mock_popen.call_args[0][0]
viewer, filename = command.split(' ', 1)

# check that the command line was created using the configured
# preview command, and that a temporary file was actually created
self.assertEqual(viewer, 'FAKE_VIEWER_APP')
self.assertTrue(os.path.isfile(filename))

# cleanup - remove the temporary file after this test
self.addCleanup(self._file_cleanup, filename)
# check that the expanding button called view_text
self.assertEqual(mock_view_text.call_count, 1)

# check that the temporary file contains the squeezed text
with open(filename, 'r') as f:
self.assertEqual(f.read(), 'TEXT')
# check that the proper text was passed
self.assertEqual(mock_view_text.call_args[0][2], 'TEXT')
Loading