1"""Test idlelib.configdialog.
2
3Half the class creates dialog, half works with user customizations.
4Coverage: 46% just by creating dialog, 56% with current tests.
5"""
6from idlelib.configdialog import ConfigDialog, idleConf  # test import
7from test.support import requires
8requires('gui')
9from tkinter import Tk
10import unittest
11import idlelib.config as config
12
13# Tests should not depend on fortuitous user configurations.
14# They must not affect actual user .cfg files.
15# Use solution from test_config: empty parsers with no filename.
16usercfg = idleConf.userCfg
17testcfg = {
18    'main': config.IdleUserConfParser(''),
19    'highlight': config.IdleUserConfParser(''),
20    'keys': config.IdleUserConfParser(''),
21    'extensions': config.IdleUserConfParser(''),
22}
23
24# ConfigDialog.changedItems is a 3-level hierarchical dictionary of
25# pending changes that mirrors the multilevel user config dict.
26# For testing, record args in a list for comparison with expected.
27changes = []
28class TestDialog(ConfigDialog):
29    def AddChangedItem(self, *args):
30        changes.append(args)
31
32def setUpModule():
33    global root, configure
34    idleConf.userCfg = testcfg
35    root = Tk()
36    root.withdraw()
37    configure = TestDialog(root, 'Test', _utest=True)
38
39
40def tearDownModule():
41    global root, configure
42    idleConf.userCfg = testcfg
43    configure.remove_var_callbacks()
44    del configure
45    root.update_idletasks()
46    root.destroy()
47    del root
48
49
50class FontTabTest(unittest.TestCase):
51
52
53    def setUp(self):
54        changes.clear()
55
56    def test_font(self):
57        # Set values guaranteed not to be defaults.
58        dfont = idleConf.GetFont(root, 'main', 'EditorWindow')
59        dsize = str(dfont[1])
60        dbold = dfont[2] == 'bold'
61        configure.fontName.set('Test Font')
62        expected = [
63            ('main', 'EditorWindow', 'font', 'Test Font'),
64            ('main', 'EditorWindow', 'font-size', dsize),
65            ('main', 'EditorWindow', 'font-bold', dbold)]
66        self.assertEqual(changes, expected)
67        changes.clear()
68        configure.fontSize.set(20)
69        expected = [
70            ('main', 'EditorWindow', 'font', 'Test Font'),
71            ('main', 'EditorWindow', 'font-size', '20'),
72            ('main', 'EditorWindow', 'font-bold', dbold)]
73        self.assertEqual(changes, expected)
74        changes.clear()
75        configure.fontBold.set(not dbold)
76        expected = [
77            ('main', 'EditorWindow', 'font', 'Test Font'),
78            ('main', 'EditorWindow', 'font-size', '20'),
79            ('main', 'EditorWindow', 'font-bold', not dbold)]
80        self.assertEqual(changes, expected)
81
82    #def test_sample(self): pass  # TODO
83
84    def test_tabspace(self):
85        configure.spaceNum.set(6)
86        self.assertEqual(changes, [('main', 'Indent', 'num-spaces', 6)])
87
88
89class HighlightTest(unittest.TestCase):
90
91    def setUp(self):
92        changes.clear()
93
94    #def test_colorchoose(self): pass  # TODO
95
96
97class KeysTest(unittest.TestCase):
98
99    def setUp(self):
100        changes.clear()
101
102
103class GeneralTest(unittest.TestCase):
104
105    def setUp(self):
106        changes.clear()
107
108    def test_startup(self):
109        configure.radioStartupEdit.invoke()
110        self.assertEqual(changes,
111                         [('main', 'General', 'editor-on-startup', 1)])
112
113    def test_autosave(self):
114        configure.radioSaveAuto.invoke()
115        self.assertEqual(changes, [('main', 'General', 'autosave', 1)])
116
117    def test_editor_size(self):
118        configure.entryWinHeight.insert(0, '1')
119        self.assertEqual(changes, [('main', 'EditorWindow', 'height', '140')])
120        changes.clear()
121        configure.entryWinWidth.insert(0, '1')
122        self.assertEqual(changes, [('main', 'EditorWindow', 'width', '180')])
123
124    #def test_help_sources(self): pass  # TODO
125
126
127if __name__ == '__main__':
128    unittest.main(verbosity=2)
129