Skip to content

Commit fa6ebca

Browse files
committed
Fix after tests and add coverage
1 parent 1a50bad commit fa6ebca

2 files changed

Lines changed: 31 additions & 22 deletions

File tree

Lib/idlelib/codecontext.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ def reload(cls):
7979
## "fgcolor", type="str", default="Black")
8080

8181
def __del__(self):
82+
"Cancel scheduled events."
8283
try:
8384
self.text.after_cancel(self.t1)
8485
self.text.after_cancel(self.t2)
@@ -217,3 +218,8 @@ def font_timer_event(self):
217218

218219

219220
CodeContext.reload()
221+
222+
223+
if __name__ == "__main__": # pragma: no cover
224+
import unittest
225+
unittest.main('idlelib.idle_test.test_codecontext', verbosity=2, exit=False)

Lib/idlelib/idle_test/test_codecontext.py

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
"""Test idlelib.codecontext.
2+
3+
Coverage: 100%
24
"""
35

46
import re
57

68
import unittest
79
from unittest import mock
810
from test.support import requires
9-
from tkinter import Tk, Frame, Text
11+
from tkinter import Tk, Frame, Text, TclError
1012

1113
import idlelib.codecontext as codecontext
1214
from idlelib import config
1315

1416

15-
root = None
1617
usercfg = codecontext.idleConf.userCfg
1718
testcfg = {
1819
'main': config.IdleUserConfParser(''),
@@ -36,21 +37,6 @@ def compare(self):
3637
"""
3738

3839

39-
def setUpModule():
40-
requires('gui')
41-
global root
42-
codecontext.idleConf.userCfg = testcfg
43-
root = Tk()
44-
45-
46-
def tearDownModule():
47-
global root
48-
codecontext.idleConf.userCfg = usercfg
49-
root.update_idletasks()
50-
root.destroy()
51-
del root
52-
53-
5440
class DummyEditwin:
5541
def __init__(self, root, frame, text):
5642
self.root = root
@@ -63,29 +49,34 @@ class CodeContextTest(unittest.TestCase):
6349

6450
@classmethod
6551
def setUpClass(cls):
52+
requires('gui')
53+
root = cls.root = Tk()
6654
frame = cls.frame = Frame(root)
6755
text = cls.text = Text(frame)
68-
# Mock 'after' to prevent timer events from being added. They
69-
# can impact other tests, even if they are deleted before this
70-
# test exits.
71-
text.after = mock.Mock()
7256
text.insert('1.0', code_sample)
7357
# Need to pack for creation of code context label widget.
7458
frame.pack(side='left', fill='both', expand=1)
7559
text.pack(side='top', fill='both', expand=1)
7660
cls.editor = DummyEditwin(root, frame, text)
61+
codecontext.idleConf.userCfg = testcfg
7762

7863
@classmethod
7964
def tearDownClass(cls):
65+
codecontext.idleConf.userCfg = usercfg
8066
cls.editor.text.delete('1.0', 'end')
8167
del cls.editor, cls.frame, cls.text
68+
cls.root.update_idletasks()
69+
cls.root.destroy()
70+
del cls.root
8271

8372
def setUp(self):
8473
self.cc = codecontext.CodeContext(self.editor)
8574

8675
def tearDown(self):
8776
if self.cc.label:
8877
self.cc.label.destroy()
78+
# Explicitly call __del__ to remove scheduled scripts.
79+
self.cc.__del__()
8980
del self.cc.label, self.cc
9081

9182
def test_init(self):
@@ -99,7 +90,19 @@ def test_init(self):
9990
self.assertIsNone(cc.label)
10091
eq(cc.info, [(0, -1, '', False)])
10192
eq(cc.topvisible, 1)
102-
self.text.after.call_count == 2
93+
self.root.tk.call('after', 'info', self.cc.t1)
94+
self.root.tk.call('after', 'info', self.cc.t2)
95+
96+
def test_del(self):
97+
self.root.tk.call('after', 'info', self.cc.t1)
98+
self.root.tk.call('after', 'info', self.cc.t2)
99+
self.cc.__del__()
100+
with self.assertRaises(TclError) as msg:
101+
self.root.tk.call('after', 'info', self.cc.t1)
102+
self.assertIn("doesn't exist", msg)
103+
with self.assertRaises(TclError) as msg:
104+
self.root.tk.call('after', 'info', self.cc.t2)
105+
self.assertIn("doesn't exist", msg)
103106

104107
def test_reload(self):
105108
codecontext.CodeContext.reload()

0 commit comments

Comments
 (0)