Beispiel #1
0
    def test_init(self, MockHovertip):
        """Test the simplest creation of an ExpandingButton."""
        squeezer = self.make_mock_squeezer()
        text_widget = squeezer.editwin.text

        expandingbutton = ExpandingButton('TEXT', 'TAGS', 50, squeezer)
        self.assertEqual(expandingbutton.s, 'TEXT')

        # Check that the underlying tkinter.Button is properly configured.
        self.assertEqual(expandingbutton.master, text_widget)
        self.assertTrue('50 lines' in expandingbutton.cget('text'))

        # Check that the text widget still contains no text.
        self.assertEqual(text_widget.get('1.0', 'end'), '\n')

        # Check that the mouse events are bound.
        self.assertIn('<Double-Button-1>', expandingbutton.bind())
        right_button_code = '<Button-%s>' % ('2' if macosx.isAquaTk() else '3')
        self.assertIn(right_button_code, expandingbutton.bind())

        # Check that ToolTip was called once, with appropriate values.
        self.assertEqual(MockHovertip.call_count, 1)
        MockHovertip.assert_called_with(expandingbutton, ANY, hover_delay=ANY)

        # Check that 'right-click' appears in the tooltip text.
        tooltip_text = MockHovertip.call_args[0][1]
        self.assertIn('right-click', tooltip_text.lower())
Beispiel #2
0
    def test_expand(self):
        """Test the expand event."""
        squeezer = self.make_mock_squeezer()
        expandingbutton = ExpandingButton('TEXT', 'TAGS', 50, squeezer)

        # Insert the button into the text widget
        # (this is normally done by the Squeezer class).
        text_widget = expandingbutton.text
        text_widget.window_create("1.0", window=expandingbutton)

        # Set base_text to the text widget, so that changes are actually
        # made to it (by ExpandingButton) and we can inspect these
        # changes afterwards.
        expandingbutton.base_text = expandingbutton.text

        # trigger the expand event
        retval = expandingbutton.expand(event=Mock())
        self.assertEqual(retval, None)

        # Check that the text was inserted into the text widget.
        self.assertEqual(text_widget.get('1.0', 'end'), 'TEXT\n')

        # Check that the 'TAGS' tag was set on the inserted text.
        text_end_index = text_widget.index('end-1c')
        self.assertEqual(text_widget.get('1.0', text_end_index), 'TEXT')
        self.assertEqual(text_widget.tag_nextrange('TAGS', '1.0'),
                         ('1.0', text_end_index))

        # Check that the button removed itself from squeezer.expandingbuttons.
        self.assertEqual(squeezer.expandingbuttons.remove.call_count, 1)
        squeezer.expandingbuttons.remove.assert_called_with(expandingbutton)
Beispiel #3
0
    def test_expand(self):
        """Test the expand event."""
        squeezer = self.make_mock_squeezer()
        expandingbutton = ExpandingButton('TEXT', 'TAGS', 50, squeezer)

        # insert the button into the text widget
        # (this is normally done by the Squeezer class)
        text_widget = expandingbutton.text
        text_widget.window_create("1.0", window=expandingbutton)

        # set base_text to the text widget, so that changes are actually made
        # to it (by ExpandingButton) and we can inspect these changes afterwards
        expandingbutton.base_text = expandingbutton.text

        # trigger the expand event
        retval = expandingbutton.expand(event=Mock())
        self.assertEqual(retval, None)

        # check that the text was inserted into the text widget
        self.assertEqual(text_widget.get('1.0', 'end'), 'TEXT\n')

        # check that the 'TAGS' tag was set on the inserted text
        text_end_index = text_widget.index('end-1c')
        self.assertEqual(text_widget.get('1.0', text_end_index), 'TEXT')
        self.assertEqual(text_widget.tag_nextrange('TAGS', '1.0'),
                          ('1.0', text_end_index))

        # check that the button removed itself from squeezer.expandingbuttons
        self.assertEqual(squeezer.expandingbuttons.remove.call_count, 1)
        squeezer.expandingbuttons.remove.assert_called_with(expandingbutton)
Beispiel #4
0
    def test_init(self, MockHovertip):
        """Test the simplest creation of an ExpandingButton."""
        squeezer = self.make_mock_squeezer()
        text_widget = squeezer.editwin.text

        expandingbutton = ExpandingButton('TEXT', 'TAGS', 50, squeezer)
        self.assertEqual(expandingbutton.s, 'TEXT')

        # check that the underlying tkinter.Button is properly configured
        self.assertEqual(expandingbutton.master, text_widget)
        self.assertTrue('50 lines' in expandingbutton.cget('text'))

        # check that the text widget still contains no text
        self.assertEqual(text_widget.get('1.0', 'end'), '\n')

        # check that the mouse events are bound
        self.assertIn('<Double-Button-1>', expandingbutton.bind())
        right_button_code = '<Button-%s>' % ('2' if macosx.isAquaTk() else '3')
        self.assertIn(right_button_code, expandingbutton.bind())

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

        # check that 'right-click' appears in the tooltip text
        tooltip_text = MockHovertip.call_args[0][1]
        self.assertIn('right-click', tooltip_text.lower())
Beispiel #5
0
 def test_rmenu(self):
     """Test the context menu."""
     squeezer = self.make_mock_squeezer()
     expandingbutton = ExpandingButton('TEXT', 'TAGS', 50, squeezer)
     with patch('tkinter.Menu') as mock_Menu:
         mock_menu = Mock()
         mock_Menu.return_value = mock_menu
         mock_event = Mock()
         mock_event.x = 10
         mock_event.y = 10
         expandingbutton.context_menu_event(event=mock_event)
         self.assertEqual(mock_menu.add_command.call_count,
                          len(expandingbutton.rmenu_specs))
         for label, *data in expandingbutton.rmenu_specs:
             mock_menu.add_command.assert_any_call(label=label, command=ANY)
Beispiel #6
0
 def test_rmenu(self):
     """Test the context menu."""
     squeezer = self.make_mock_squeezer()
     expandingbutton = ExpandingButton('TEXT', 'TAGS', 50, squeezer)
     with patch('tkinter.Menu') as mock_Menu:
         mock_menu = Mock()
         mock_Menu.return_value = mock_menu
         mock_event = Mock()
         mock_event.x = 10
         mock_event.y = 10
         expandingbutton.context_menu_event(event=mock_event)
         self.assertEqual(mock_menu.add_command.call_count,
                          len(expandingbutton.rmenu_specs))
         for label, *data in expandingbutton.rmenu_specs:
             mock_menu.add_command.assert_any_call(label=label, command=ANY)
Beispiel #7
0
    def test_view(self):
        """Test the view event."""
        squeezer = self.make_mock_squeezer()
        expandingbutton = ExpandingButton('TEXT', 'TAGS', 50, squeezer)
        expandingbutton.selection_own = Mock()

        with patch('idlelib.squeezer.view_text', autospec=view_text)\
                as mock_view_text:
            # Trigger the view event.
            expandingbutton.view(event=Mock())

            # Check that the expanding button called view_text.
            self.assertEqual(mock_view_text.call_count, 1)

            # Check that the proper text was passed.
            self.assertEqual(mock_view_text.call_args[0][2], 'TEXT')
Beispiel #8
0
    def test_view(self):
        """Test the view event."""
        squeezer = self.make_mock_squeezer()
        expandingbutton = ExpandingButton('TEXT', 'TAGS', 50, squeezer)
        expandingbutton.selection_own = Mock()

        with patch('idlelib.squeezer.view_text', autospec=view_text)\
                as mock_view_text:
            # trigger the view event
            expandingbutton.view(event=Mock())

            # check that the expanding button called view_text
            self.assertEqual(mock_view_text.call_count, 1)

            # check that the proper text was passed
            self.assertEqual(mock_view_text.call_args[0][2], 'TEXT')
Beispiel #9
0
    def test_copy(self):
        """Test the copy event."""
        # Testing with the actual clipboard proved problematic, so this
        # test replaces the clipboard manipulation functions with mocks
        # and checks that they are called appropriately.
        squeezer = self.make_mock_squeezer()
        expandingbutton = ExpandingButton('TEXT', 'TAGS', 50, squeezer)
        expandingbutton.clipboard_clear = Mock()
        expandingbutton.clipboard_append = Mock()

        # Trigger the copy event.
        retval = expandingbutton.copy(event=Mock())
        self.assertEqual(retval, None)

        # Vheck that the expanding button called clipboard_clear() and
        # clipboard_append('TEXT') once each.
        self.assertEqual(expandingbutton.clipboard_clear.call_count, 1)
        self.assertEqual(expandingbutton.clipboard_append.call_count, 1)
        expandingbutton.clipboard_append.assert_called_with('TEXT')
Beispiel #10
0
    def test_copy(self):
        """Test the copy event."""
        # testing with the actual clipboard proved problematic, so this test
        # replaces the clipboard manipulation functions with mocks and checks
        # that they are called appropriately
        squeezer = self.make_mock_squeezer()
        expandingbutton = ExpandingButton('TEXT', 'TAGS', 50, squeezer)
        expandingbutton.clipboard_clear = Mock()
        expandingbutton.clipboard_append = Mock()

        # trigger the copy event
        retval = expandingbutton.copy(event=Mock())
        self.assertEqual(retval, None)

        # check that the expanding button called clipboard_clear() and
        # clipboard_append('TEXT') once each
        self.assertEqual(expandingbutton.clipboard_clear.call_count, 1)
        self.assertEqual(expandingbutton.clipboard_append.call_count, 1)
        expandingbutton.clipboard_append.assert_called_with('TEXT')
Beispiel #11
0
    def test_expand_dangerous_oupput(self):
        """Test that expanding very long output asks user for confirmation."""
        squeezer = self.make_mock_squeezer()
        text = 'a' * 10**5
        expandingbutton = ExpandingButton(text, 'TAGS', 50, squeezer)
        expandingbutton.set_is_dangerous()
        self.assertTrue(expandingbutton.is_dangerous)

        # Insert the button into the text widget
        # (this is normally done by the Squeezer class).
        text_widget = expandingbutton.text
        text_widget.window_create("1.0", window=expandingbutton)

        # Patch the message box module to always return False.
        with patch('idlelib.squeezer.messagebox') as mock_msgbox:
            mock_msgbox.askokcancel.return_value = False
            mock_msgbox.askyesno.return_value = False
            # Trigger the expand event.
            retval = expandingbutton.expand(event=Mock())

        # Check that the event chain was broken and no text was inserted.
        self.assertEqual(retval, 'break')
        self.assertEqual(expandingbutton.text.get('1.0', 'end-1c'), '')

        # Patch the message box module to always return True.
        with patch('idlelib.squeezer.messagebox') as mock_msgbox:
            mock_msgbox.askokcancel.return_value = True
            mock_msgbox.askyesno.return_value = True
            # Trigger the expand event.
            retval = expandingbutton.expand(event=Mock())

        # Check that the event chain wasn't broken and the text was inserted.
        self.assertEqual(retval, None)
        self.assertEqual(expandingbutton.text.get('1.0', 'end-1c'), text)
Beispiel #12
0
    def test_expand_dangerous_oupput(self):
        """Test that expanding very long output asks user for confirmation."""
        squeezer = self.make_mock_squeezer()
        text = 'a' * 10**5
        expandingbutton = ExpandingButton(text, 'TAGS', 50, squeezer)
        expandingbutton.set_is_dangerous()
        self.assertTrue(expandingbutton.is_dangerous)

        # insert the button into the text widget
        # (this is normally done by the Squeezer class)
        text_widget = expandingbutton.text
        text_widget.window_create("1.0", window=expandingbutton)

        # set base_text to the text widget, so that changes are actually made
        # to it (by ExpandingButton) and we can inspect these changes afterwards
        expandingbutton.base_text = expandingbutton.text

        # patch the message box module to always return False
        with patch('idlelib.squeezer.tkMessageBox') as mock_msgbox:
            mock_msgbox.askokcancel.return_value = False
            mock_msgbox.askyesno.return_value = False

            # trigger the expand event
            retval = expandingbutton.expand(event=Mock())

        # check that the event chain was broken and no text was inserted
        self.assertEqual(retval, 'break')
        self.assertEqual(expandingbutton.text.get('1.0', 'end-1c'), '')

        # patch the message box module to always return True
        with patch('idlelib.squeezer.tkMessageBox') as mock_msgbox:
            mock_msgbox.askokcancel.return_value = True
            mock_msgbox.askyesno.return_value = True

            # trigger the expand event
            retval = expandingbutton.expand(event=Mock())

        # check that the event chain wasn't broken and the text was inserted
        self.assertEqual(retval, None)
        self.assertEqual(expandingbutton.text.get('1.0', 'end-1c'), text)