ยปCore Development>Code coverage>Lib/idlelib/idle_test/test_config.py

Python code coverage for Lib/idlelib/idle_test/test_config.py

#countcontent
1n/a'''Test idlelib.config.
2n/a
3n/aMuch is tested by opening config dialog live or in test_configdialog.
4n/aCoverage: 27%
5n/a'''
6n/afrom sys import modules
7n/afrom test.support import captured_stderr
8n/afrom tkinter import Tk
9n/aimport unittest
10n/afrom idlelib import config
11n/a
12n/a# Tests should not depend on fortuitous user configurations.
13n/a# They must not affect actual user .cfg files.
14n/a# Replace user parsers with empty parsers that cannot be saved.
15n/a
16n/aidleConf = config.idleConf
17n/ausercfg = idleConf.userCfg
18n/atestcfg = {}
19n/ausermain = testcfg['main'] = config.IdleUserConfParser('') # filename
20n/auserhigh = testcfg['highlight'] = config.IdleUserConfParser('')
21n/auserkeys = testcfg['keys'] = config.IdleUserConfParser('')
22n/a
23n/adef setUpModule():
24n/a idleConf.userCfg = testcfg
25n/a
26n/adef tearDownModule():
27n/a idleConf.userCfg = usercfg
28n/a
29n/a
30n/aclass CurrentColorKeysTest(unittest.TestCase):
31n/a """ Test colorkeys function with user config [Theme] and [Keys] patterns.
32n/a
33n/a colorkeys = config.IdleConf.current_colors_and_keys
34n/a Test all patterns written by IDLE and some errors
35n/a Item 'default' should really be 'builtin' (versus 'custom).
36n/a """
37n/a colorkeys = idleConf.current_colors_and_keys
38n/a default_theme = 'IDLE Classic'
39n/a default_keys = idleConf.default_keys()
40n/a
41n/a def test_old_builtin_theme(self):
42n/a # On initial installation, user main is blank.
43n/a self.assertEqual(self.colorkeys('Theme'), self.default_theme)
44n/a # For old default, name2 must be blank.
45n/a usermain.read_string('''
46n/a [Theme]
47n/a default = True
48n/a ''')
49n/a # IDLE omits 'name' for default old builtin theme.
50n/a self.assertEqual(self.colorkeys('Theme'), self.default_theme)
51n/a # IDLE adds 'name' for non-default old builtin theme.
52n/a usermain['Theme']['name'] = 'IDLE New'
53n/a self.assertEqual(self.colorkeys('Theme'), 'IDLE New')
54n/a # Erroneous non-default old builtin reverts to default.
55n/a usermain['Theme']['name'] = 'non-existent'
56n/a self.assertEqual(self.colorkeys('Theme'), self.default_theme)
57n/a usermain.remove_section('Theme')
58n/a
59n/a def test_new_builtin_theme(self):
60n/a # IDLE writes name2 for new builtins.
61n/a usermain.read_string('''
62n/a [Theme]
63n/a default = True
64n/a name2 = IDLE Dark
65n/a ''')
66n/a self.assertEqual(self.colorkeys('Theme'), 'IDLE Dark')
67n/a # Leftover 'name', not removed, is ignored.
68n/a usermain['Theme']['name'] = 'IDLE New'
69n/a self.assertEqual(self.colorkeys('Theme'), 'IDLE Dark')
70n/a # Erroneous non-default new builtin reverts to default.
71n/a usermain['Theme']['name2'] = 'non-existent'
72n/a self.assertEqual(self.colorkeys('Theme'), self.default_theme)
73n/a usermain.remove_section('Theme')
74n/a
75n/a def test_user_override_theme(self):
76n/a # Erroneous custom name (no definition) reverts to default.
77n/a usermain.read_string('''
78n/a [Theme]
79n/a default = False
80n/a name = Custom Dark
81n/a ''')
82n/a self.assertEqual(self.colorkeys('Theme'), self.default_theme)
83n/a # Custom name is valid with matching Section name.
84n/a userhigh.read_string('[Custom Dark]\na=b')
85n/a self.assertEqual(self.colorkeys('Theme'), 'Custom Dark')
86n/a # Name2 is ignored.
87n/a usermain['Theme']['name2'] = 'non-existent'
88n/a self.assertEqual(self.colorkeys('Theme'), 'Custom Dark')
89n/a usermain.remove_section('Theme')
90n/a userhigh.remove_section('Custom Dark')
91n/a
92n/a def test_old_builtin_keys(self):
93n/a # On initial installation, user main is blank.
94n/a self.assertEqual(self.colorkeys('Keys'), self.default_keys)
95n/a # For old default, name2 must be blank, name is always used.
96n/a usermain.read_string('''
97n/a [Keys]
98n/a default = True
99n/a name = IDLE Classic Unix
100n/a ''')
101n/a self.assertEqual(self.colorkeys('Keys'), 'IDLE Classic Unix')
102n/a # Erroneous non-default old builtin reverts to default.
103n/a usermain['Keys']['name'] = 'non-existent'
104n/a self.assertEqual(self.colorkeys('Keys'), self.default_keys)
105n/a usermain.remove_section('Keys')
106n/a
107n/a def test_new_builtin_keys(self):
108n/a # IDLE writes name2 for new builtins.
109n/a usermain.read_string('''
110n/a [Keys]
111n/a default = True
112n/a name2 = IDLE Modern Unix
113n/a ''')
114n/a self.assertEqual(self.colorkeys('Keys'), 'IDLE Modern Unix')
115n/a # Leftover 'name', not removed, is ignored.
116n/a usermain['Keys']['name'] = 'IDLE Classic Unix'
117n/a self.assertEqual(self.colorkeys('Keys'), 'IDLE Modern Unix')
118n/a # Erroneous non-default new builtin reverts to default.
119n/a usermain['Keys']['name2'] = 'non-existent'
120n/a self.assertEqual(self.colorkeys('Keys'), self.default_keys)
121n/a usermain.remove_section('Keys')
122n/a
123n/a def test_user_override_keys(self):
124n/a # Erroneous custom name (no definition) reverts to default.
125n/a usermain.read_string('''
126n/a [Keys]
127n/a default = False
128n/a name = Custom Keys
129n/a ''')
130n/a self.assertEqual(self.colorkeys('Keys'), self.default_keys)
131n/a # Custom name is valid with matching Section name.
132n/a userkeys.read_string('[Custom Keys]\na=b')
133n/a self.assertEqual(self.colorkeys('Keys'), 'Custom Keys')
134n/a # Name2 is ignored.
135n/a usermain['Keys']['name2'] = 'non-existent'
136n/a self.assertEqual(self.colorkeys('Keys'), 'Custom Keys')
137n/a usermain.remove_section('Keys')
138n/a userkeys.remove_section('Custom Keys')
139n/a
140n/a
141n/aclass WarningTest(unittest.TestCase):
142n/a
143n/a def test_warn(self):
144n/a Equal = self.assertEqual
145n/a config._warned = set()
146n/a with captured_stderr() as stderr:
147n/a config._warn('warning', 'key')
148n/a Equal(config._warned, {('warning','key')})
149n/a Equal(stderr.getvalue(), 'warning'+'\n')
150n/a with captured_stderr() as stderr:
151n/a config._warn('warning', 'key')
152n/a Equal(stderr.getvalue(), '')
153n/a with captured_stderr() as stderr:
154n/a config._warn('warn2', 'yek')
155n/a Equal(config._warned, {('warning','key'), ('warn2','yek')})
156n/a Equal(stderr.getvalue(), 'warn2'+'\n')
157n/a
158n/a
159n/aif __name__ == '__main__':
160n/a unittest.main(verbosity=2)