Commit 5d0f30aa authored by Terry Jan Reedy's avatar Terry Jan Reedy Committed by GitHub

bpo-30853: IDLE - touch-up configdialog.VarTrace and tests. (#2936)

Add clear method for tests.  Adjust tests to use global instance.
Remove unneeded ConfigDialog method.
parent 5b59154c
...@@ -99,7 +99,6 @@ class ConfigDialog(Toplevel): ...@@ -99,7 +99,6 @@ class ConfigDialog(Toplevel):
create_page_extensions create_page_extensions
create_action_buttons create_action_buttons
load_configs: Load pages except for extensions. load_configs: Load pages except for extensions.
remove_var_callbacks
activate_config_changes: Tell editors to reload. activate_config_changes: Tell editors to reload.
""" """
self.tab_pages = TabbedPageSet(self, self.tab_pages = TabbedPageSet(self,
...@@ -133,10 +132,6 @@ class ConfigDialog(Toplevel): ...@@ -133,10 +132,6 @@ class ConfigDialog(Toplevel):
self.load_general_cfg() self.load_general_cfg()
# note: extension page handled separately # note: extension page handled separately
def remove_var_callbacks(self):
"Remove callbacks to prevent memory leaks."
tracers.detach()
def create_action_buttons(self): def create_action_buttons(self):
"""Return frame of action buttons for dialog. """Return frame of action buttons for dialog.
...@@ -1846,6 +1841,11 @@ class VarTrace: ...@@ -1846,6 +1841,11 @@ class VarTrace:
self.untraced = [] self.untraced = []
self.traced = [] self.traced = []
def clear(self):
"Clear lists (for tests)."
self.untraced.clear()
self.traced.clear()
def add(self, var, callback): def add(self, var, callback):
"""Add (var, callback) tuple to untraced list. """Add (var, callback) tuple to untraced list.
......
...@@ -11,7 +11,7 @@ from unittest import mock ...@@ -11,7 +11,7 @@ from unittest import mock
from idlelib.idle_test.mock_idle import Func from idlelib.idle_test.mock_idle import Func
from tkinter import Tk, IntVar, BooleanVar, DISABLED, NORMAL from tkinter import Tk, IntVar, BooleanVar, DISABLED, NORMAL
from idlelib import config from idlelib import config
from idlelib.configdialog import ConfigDialog, idleConf, changes, VarTrace from idlelib.configdialog import idleConf, changes, tracers
# Tests should not depend on fortuitous user configurations. # Tests should not depend on fortuitous user configurations.
# They must not affect actual user .cfg files. # They must not affect actual user .cfg files.
...@@ -35,12 +35,12 @@ def setUpModule(): ...@@ -35,12 +35,12 @@ def setUpModule():
idleConf.userCfg = testcfg idleConf.userCfg = testcfg
root = Tk() root = Tk()
# root.withdraw() # Comment out, see issue 30870 # root.withdraw() # Comment out, see issue 30870
dialog = ConfigDialog(root, 'Test', _utest=True) dialog = configdialog.ConfigDialog(root, 'Test', _utest=True)
def tearDownModule(): def tearDownModule():
global root, dialog global root, dialog
idleConf.userCfg = usercfg idleConf.userCfg = usercfg
dialog.remove_var_callbacks() tracers.detach()
del dialog del dialog
root.update_idletasks() root.update_idletasks()
root.destroy() root.destroy()
...@@ -423,14 +423,14 @@ class GeneralTest(unittest.TestCase): ...@@ -423,14 +423,14 @@ class GeneralTest(unittest.TestCase):
d.update_help_changes = Func() d.update_help_changes = Func()
class TestVarTrace(unittest.TestCase): class VarTraceTest(unittest.TestCase):
def setUp(self): def setUp(self):
changes.clear() changes.clear()
tracers.clear()
self.v1 = IntVar(root) self.v1 = IntVar(root)
self.v2 = BooleanVar(root) self.v2 = BooleanVar(root)
self.called = 0 self.called = 0
self.tracers = VarTrace()
def tearDown(self): def tearDown(self):
del self.v1, self.v2 del self.v1, self.v2
...@@ -442,11 +442,19 @@ class TestVarTrace(unittest.TestCase): ...@@ -442,11 +442,19 @@ class TestVarTrace(unittest.TestCase):
pass pass
def test_init(self): def test_init(self):
self.assertEqual(self.tracers.untraced, []) tracers.__init__()
self.assertEqual(self.tracers.traced, []) self.assertEqual(tracers.untraced, [])
self.assertEqual(tracers.traced, [])
def test_clear(self):
tracers.untraced.append(0)
tracers.traced.append(1)
tracers.clear()
self.assertEqual(tracers.untraced, [])
self.assertEqual(tracers.traced, [])
def test_add(self): def test_add(self):
tr = self.tracers tr = tracers
func = Func() func = Func()
cb = tr.make_callback = mock.Mock(return_value=func) cb = tr.make_callback = mock.Mock(return_value=func)
...@@ -469,8 +477,7 @@ class TestVarTrace(unittest.TestCase): ...@@ -469,8 +477,7 @@ class TestVarTrace(unittest.TestCase):
del tr.make_callback del tr.make_callback
def test_make_callback(self): def test_make_callback(self):
tr = self.tracers cb = tracers.make_callback(self.v1, ('main', 'section', 'option'))
cb = tr.make_callback(self.v1, ('main', 'section', 'option'))
self.assertTrue(callable(cb)) self.assertTrue(callable(cb))
self.v1.set(42) self.v1.set(42)
# Not attached, so set didn't invoke the callback. # Not attached, so set didn't invoke the callback.
...@@ -481,7 +488,7 @@ class TestVarTrace(unittest.TestCase): ...@@ -481,7 +488,7 @@ class TestVarTrace(unittest.TestCase):
self.assertEqual(changes['main']['section']['option'], '42') self.assertEqual(changes['main']['section']['option'], '42')
def test_attach_detach(self): def test_attach_detach(self):
tr = self.tracers tr = tracers
v1 = tr.add(self.v1, self.var_changed_increment) v1 = tr.add(self.v1, self.var_changed_increment)
v2 = tr.add(self.v2, self.var_changed_boolean) v2 = tr.add(self.v2, self.var_changed_boolean)
expected = [(v1, self.var_changed_increment), expected = [(v1, self.var_changed_increment),
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment