summaryrefslogtreecommitdiffstats
path: root/Lib/idlelib/idle_test/test_parenmatch.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/idlelib/idle_test/test_parenmatch.py')
-rw-r--r--Lib/idlelib/idle_test/test_parenmatch.py113
1 files changed, 61 insertions, 52 deletions
diff --git a/Lib/idlelib/idle_test/test_parenmatch.py b/Lib/idlelib/idle_test/test_parenmatch.py
index 4a41d84..1621981 100644
--- a/Lib/idlelib/idle_test/test_parenmatch.py
+++ b/Lib/idlelib/idle_test/test_parenmatch.py
@@ -1,31 +1,39 @@
-"""Test parenmatch, coverage 91%.
-
-This must currently be a gui test because ParenMatch methods use
-several text methods not defined on idlelib.idle_test.mock_tk.Text.
-"""
-from idlelib.parenmatch import ParenMatch
-from test.support import requires
-requires('gui')
+"""Test idlelib.ParenMatch."""
+# This must currently be a gui test because ParenMatch methods use
+# several text methods not defined on idlelib.idle_test.mock_tk.Text.
import unittest
-from unittest.mock import Mock
-from tkinter import Tk, Text
+from test.test_support import requires
+from Tkinter import Tk, Text
+from idlelib.ParenMatch import ParenMatch
+
+class Mock: # 2.7 does not have unittest.mock
+ def __init__(self, *args, **kwargs):
+ self.called = False
+
+ def __call__(self, *args, **kwargs):
+ self.called = True
+ def reset_mock(self, *args, **kwargs):
+ self.called = False
+
+ def after(self, *args, **kwargs):
+ pass
class DummyEditwin:
def __init__(self, text):
self.text = text
self.indentwidth = 8
self.tabwidth = 8
- self.prompt_last_line = '>>>' # Currently not used by parenmatch.
+ self.context_use_ps1 = True
class ParenMatchTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
+ requires('gui')
cls.root = Tk()
- cls.root.withdraw()
cls.text = Text(cls.root)
cls.editwin = DummyEditwin(cls.text)
cls.editwin.text_frame = Mock()
@@ -33,51 +41,52 @@ class ParenMatchTest(unittest.TestCase):
@classmethod
def tearDownClass(cls):
del cls.text, cls.editwin
- cls.root.update_idletasks()
cls.root.destroy()
del cls.root
def tearDown(self):
self.text.delete('1.0', 'end')
- def get_parenmatch(self):
+ def test_paren_expression(self):
+ """
+ Test ParenMatch with 'expression' style.
+ """
+ text = self.text
pm = ParenMatch(self.editwin)
- pm.bell = lambda: None
- return pm
+ pm.set_style('expression')
+
+ text.insert('insert', 'def foobar(a, b')
+ pm.flash_paren_event('event')
+ self.assertIn('<<parenmatch-check-restore>>', text.event_info())
+ self.assertTupleEqual(text.tag_prevrange('paren', 'end'),
+ ('1.10', '1.15'))
+ text.insert('insert', ')')
+ pm.restore_event()
+ self.assertNotIn('<<parenmatch-check-restore>>', text.event_info())
+ self.assertEqual(text.tag_prevrange('paren', 'end'), ())
+
+ # paren_closed_event can only be tested as below
+ pm.paren_closed_event('event')
+ self.assertTupleEqual(text.tag_prevrange('paren', 'end'),
+ ('1.10', '1.16'))
- def test_paren_styles(self):
+ def test_paren_default(self):
"""
- Test ParenMatch with each style.
+ Test ParenMatch with 'default' style.
"""
text = self.text
- pm = self.get_parenmatch()
- for style, range1, range2 in (
- ('opener', ('1.10', '1.11'), ('1.10', '1.11')),
- ('default',('1.10', '1.11'),('1.10', '1.11')),
- ('parens', ('1.14', '1.15'), ('1.15', '1.16')),
- ('expression', ('1.10', '1.15'), ('1.10', '1.16'))):
- with self.subTest(style=style):
- text.delete('1.0', 'end')
- pm.STYLE = style
- text.insert('insert', 'def foobar(a, b')
-
- pm.flash_paren_event('event')
- self.assertIn('<<parenmatch-check-restore>>', text.event_info())
- if style == 'parens':
- self.assertTupleEqual(text.tag_nextrange('paren', '1.0'),
- ('1.10', '1.11'))
- self.assertTupleEqual(
- text.tag_prevrange('paren', 'end'), range1)
-
- text.insert('insert', ')')
- pm.restore_event()
- self.assertNotIn('<<parenmatch-check-restore>>',
- text.event_info())
- self.assertEqual(text.tag_prevrange('paren', 'end'), ())
-
- pm.paren_closed_event('event')
- self.assertTupleEqual(
- text.tag_prevrange('paren', 'end'), range2)
+ pm = ParenMatch(self.editwin)
+ pm.set_style('default')
+
+ text.insert('insert', 'def foobar(a, b')
+ pm.flash_paren_event('event')
+ self.assertIn('<<parenmatch-check-restore>>', text.event_info())
+ self.assertTupleEqual(text.tag_prevrange('paren', 'end'),
+ ('1.10', '1.11'))
+ text.insert('insert', ')')
+ pm.restore_event()
+ self.assertNotIn('<<parenmatch-check-restore>>', text.event_info())
+ self.assertEqual(text.tag_prevrange('paren', 'end'), ())
def test_paren_corner(self):
"""
@@ -86,20 +95,20 @@ class ParenMatchTest(unittest.TestCase):
These cases force conditional expression and alternate paths.
"""
text = self.text
- pm = self.get_parenmatch()
+ pm = ParenMatch(self.editwin)
text.insert('insert', '# this is a commen)')
- pm.paren_closed_event('event')
+ self.assertIsNone(pm.paren_closed_event('event'))
text.insert('insert', '\ndef')
- pm.flash_paren_event('event')
- pm.paren_closed_event('event')
+ self.assertIsNone(pm.flash_paren_event('event'))
+ self.assertIsNone(pm.paren_closed_event('event'))
text.insert('insert', ' a, *arg)')
- pm.paren_closed_event('event')
+ self.assertIsNone(pm.paren_closed_event('event'))
def test_handle_restore_timer(self):
- pm = self.get_parenmatch()
+ pm = ParenMatch(self.editwin)
pm.restore_event = Mock()
pm.handle_restore_timer(0)
self.assertTrue(pm.restore_event.called)