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