Coverage for idle_test/test_config.py: 93%
482 statements
« prev ^ index » next coverage.py v7.2.5, created at 2023-05-11 13:22 -0700
« prev ^ index » next coverage.py v7.2.5, created at 2023-05-11 13:22 -0700
1"""Test config, coverage 93%.
2(100% for IdleConfParser, IdleUserConfParser*, ConfigChanges).
3* Exception is OSError clause in Save method.
4Much of IdleConf is also exercised by ConfigDialog and test_configdialog.
5"""
6from idlelib import config
7import sys
8import os
9import tempfile
10from test.support import captured_stderr, findfile
11import unittest
12from unittest import mock
13import idlelib
14from idlelib.idle_test.mock_idle import Func
16# Tests should not depend on fortuitous user configurations.
17# They must not affect actual user .cfg files.
18# Replace user parsers with empty parsers that cannot be saved
19# due to getting '' as the filename when created.
21idleConf = config.idleConf
22usercfg = idleConf.userCfg
23testcfg = {}
24usermain = testcfg['main'] = config.IdleUserConfParser('')
25userhigh = testcfg['highlight'] = config.IdleUserConfParser('')
26userkeys = testcfg['keys'] = config.IdleUserConfParser('')
27userextn = testcfg['extensions'] = config.IdleUserConfParser('')
29def setUpModule():
30 idleConf.userCfg = testcfg
31 idlelib.testing = True
33def tearDownModule():
34 idleConf.userCfg = usercfg
35 idlelib.testing = False
38class IdleConfParserTest(unittest.TestCase):
39 """Test that IdleConfParser works"""
41 config = """
42 [one]
43 one = false
44 two = true
45 three = 10
47 [two]
48 one = a string
49 two = true
50 three = false
51 """
53 def test_get(self):
54 parser = config.IdleConfParser('') 1j
55 parser.read_string(self.config) 1j
56 eq = self.assertEqual 1j
58 # Test with type argument.
59 self.assertIs(parser.Get('one', 'one', type='bool'), False) 1j
60 self.assertIs(parser.Get('one', 'two', type='bool'), True) 1j
61 eq(parser.Get('one', 'three', type='int'), 10) 1j
62 eq(parser.Get('two', 'one'), 'a string') 1j
63 self.assertIs(parser.Get('two', 'two', type='bool'), True) 1j
64 self.assertIs(parser.Get('two', 'three', type='bool'), False) 1j
66 # Test without type should fallback to string.
67 eq(parser.Get('two', 'two'), 'true') 1j
68 eq(parser.Get('two', 'three'), 'false') 1j
70 # If option not exist, should return None, or default.
71 self.assertIsNone(parser.Get('not', 'exist')) 1j
72 eq(parser.Get('not', 'exist', default='DEFAULT'), 'DEFAULT') 1j
74 def test_get_option_list(self):
75 parser = config.IdleConfParser('') 1P
76 parser.read_string(self.config) 1P
77 get_list = parser.GetOptionList 1P
78 self.assertCountEqual(get_list('one'), ['one', 'two', 'three']) 1P
79 self.assertCountEqual(get_list('two'), ['one', 'two', 'three']) 1P
80 self.assertEqual(get_list('not exist'), []) 1P
82 def test_load_nothing(self):
83 parser = config.IdleConfParser('') 1U
84 parser.Load() 1U
85 self.assertEqual(parser.sections(), []) 1U
87 def test_load_file(self):
88 # Borrow test/cfgparser.1 from test_configparser.
89 config_path = findfile('cfgparser.1') 1S
90 parser = config.IdleConfParser(config_path) 1S
91 parser.Load() 1S
93 self.assertEqual(parser.Get('Foo Bar', 'foo'), 'newbar') 1S
94 self.assertEqual(parser.GetOptionList('Foo Bar'), ['foo']) 1S
97class IdleUserConfParserTest(unittest.TestCase):
98 """Test that IdleUserConfParser works"""
100 def new_parser(self, path=''):
101 return config.IdleUserConfParser(path) 1KvFLow
103 def test_set_option(self):
104 parser = self.new_parser() 1w
105 parser.add_section('Foo') 1w
106 # Setting new option in existing section should return True.
107 self.assertTrue(parser.SetOption('Foo', 'bar', 'true')) 1w
108 # Setting existing option with same value should return False.
109 self.assertFalse(parser.SetOption('Foo', 'bar', 'true')) 1w
110 # Setting exiting option with new value should return True.
111 self.assertTrue(parser.SetOption('Foo', 'bar', 'false')) 1w
112 self.assertEqual(parser.Get('Foo', 'bar'), 'false') 1w
114 # Setting option in new section should create section and return True.
115 self.assertTrue(parser.SetOption('Bar', 'bar', 'true')) 1w
116 self.assertCountEqual(parser.sections(), ['Bar', 'Foo']) 1w
117 self.assertEqual(parser.Get('Bar', 'bar'), 'true') 1w
119 def test_remove_option(self):
120 parser = self.new_parser() 1L
121 parser.AddSection('Foo') 1L
122 parser.SetOption('Foo', 'bar', 'true') 1L
124 self.assertTrue(parser.RemoveOption('Foo', 'bar')) 1L
125 self.assertFalse(parser.RemoveOption('Foo', 'bar')) 1L
126 self.assertFalse(parser.RemoveOption('Not', 'Exist')) 1L
128 def test_add_section(self):
129 parser = self.new_parser() 1K
130 self.assertEqual(parser.sections(), []) 1K
132 # Should not add duplicate section.
133 # Configparser raises DuplicateError, IdleParser not.
134 parser.AddSection('Foo') 1K
135 parser.AddSection('Foo') 1K
136 parser.AddSection('Bar') 1K
137 self.assertCountEqual(parser.sections(), ['Bar', 'Foo']) 1K
139 def test_remove_empty_sections(self):
140 parser = self.new_parser() 1F
142 parser.AddSection('Foo') 1F
143 parser.AddSection('Bar') 1F
144 parser.SetOption('Idle', 'name', 'val') 1F
145 self.assertCountEqual(parser.sections(), ['Bar', 'Foo', 'Idle']) 1F
146 parser.RemoveEmptySections() 1F
147 self.assertEqual(parser.sections(), ['Idle']) 1F
149 def test_is_empty(self):
150 parser = self.new_parser() 1v
152 parser.AddSection('Foo') 1v
153 parser.AddSection('Bar') 1v
154 self.assertTrue(parser.IsEmpty()) 1v
155 self.assertEqual(parser.sections(), []) 1v
157 parser.SetOption('Foo', 'bar', 'false') 1v
158 parser.AddSection('Bar') 1v
159 self.assertFalse(parser.IsEmpty()) 1v
160 self.assertCountEqual(parser.sections(), ['Foo']) 1v
162 def test_save(self):
163 with tempfile.TemporaryDirectory() as tdir: 1o
164 path = os.path.join(tdir, 'test.cfg') 1o
165 parser = self.new_parser(path) 1o
166 parser.AddSection('Foo') 1o
167 parser.SetOption('Foo', 'bar', 'true') 1o
169 # Should save to path when config is not empty.
170 self.assertFalse(os.path.exists(path)) 1o
171 parser.Save() 1o
172 self.assertTrue(os.path.exists(path)) 1o
174 # Should remove the file from disk when config is empty.
175 parser.remove_section('Foo') 1o
176 parser.Save() 1o
177 self.assertFalse(os.path.exists(path)) 1o
180class IdleConfTest(unittest.TestCase):
181 """Test for idleConf"""
183 @classmethod
184 def setUpClass(cls):
185 cls.config_string = {}
187 conf = config.IdleConf(_utest=True)
188 if __name__ != '__main__': 188 ↛ 191line 188 didn't jump to line 191, because the condition on line 188 was never false
189 idle_dir = os.path.dirname(__file__)
190 else:
191 idle_dir = os.path.abspath(sys.path[0])
192 for ctype in conf.config_types:
193 config_path = os.path.join(idle_dir, '../config-%s.def' % ctype)
194 with open(config_path, 'r') as f:
195 cls.config_string[ctype] = f.read()
197 cls.orig_warn = config._warn
198 config._warn = Func()
200 @classmethod
201 def tearDownClass(cls):
202 config._warn = cls.orig_warn
204 def new_config(self, _utest=False):
205 return config.IdleConf(_utest=_utest) 1cxks
207 def mock_config(self):
208 """Return a mocked idleConf
210 Both default and user config used the same config-*.def
211 """
212 conf = config.IdleConf(_utest=True) 1BbdpyqgelfitrCuz
213 for ctype in conf.config_types: 1BbdpyqgelfitrCuz
214 conf.defaultCfg[ctype] = config.IdleConfParser('') 1BbdpyqgelfitrCuz
215 conf.defaultCfg[ctype].read_string(self.config_string[ctype]) 1BbdpyqgelfitrCuz
216 conf.userCfg[ctype] = config.IdleUserConfParser('') 1BbdpyqgelfitrCuz
217 conf.userCfg[ctype].read_string(self.config_string[ctype]) 1BbdpyqgelfitrCuz
219 return conf 1BbdpyqgelfitrCuz
221 @unittest.skipIf(sys.platform.startswith('win'), 'this is test for unix system')
222 def test_get_user_cfg_dir_unix(self):
223 # Test to get user config directory under unix.
224 conf = self.new_config(_utest=True) 1k
226 # Check normal way should success
227 with mock.patch('os.path.expanduser', return_value='/home/foo'): 1k
228 with mock.patch('os.path.exists', return_value=True): 1k
229 self.assertEqual(conf.GetUserCfgDir(), '/home/foo/.idlerc') 1k
231 # Check os.getcwd should success
232 with mock.patch('os.path.expanduser', return_value='~'): 1k
233 with mock.patch('os.getcwd', return_value='/home/foo/cpython'): 1k
234 with mock.patch('os.mkdir'): 1k
235 self.assertEqual(conf.GetUserCfgDir(), 1k
236 '/home/foo/cpython/.idlerc')
238 # Check user dir not exists and created failed should raise SystemExit
239 with mock.patch('os.path.join', return_value='/path/not/exists'): 1k
240 with self.assertRaises(SystemExit): 1k
241 with self.assertRaises(FileNotFoundError): 1k
242 conf.GetUserCfgDir() 1k
244 @unittest.skipIf(not sys.platform.startswith('win'), 'this is test for Windows system')
245 def test_get_user_cfg_dir_windows(self):
246 # Test to get user config directory under Windows.
247 conf = self.new_config(_utest=True)
249 # Check normal way should success
250 with mock.patch('os.path.expanduser', return_value='C:\\foo'):
251 with mock.patch('os.path.exists', return_value=True):
252 self.assertEqual(conf.GetUserCfgDir(), 'C:\\foo\\.idlerc')
254 # Check os.getcwd should success
255 with mock.patch('os.path.expanduser', return_value='~'):
256 with mock.patch('os.getcwd', return_value='C:\\foo\\cpython'):
257 with mock.patch('os.mkdir'):
258 self.assertEqual(conf.GetUserCfgDir(),
259 'C:\\foo\\cpython\\.idlerc')
261 # Check user dir not exists and created failed should raise SystemExit
262 with mock.patch('os.path.join', return_value='/path/not/exists'):
263 with self.assertRaises(SystemExit):
264 with self.assertRaises(FileNotFoundError):
265 conf.GetUserCfgDir()
267 def test_create_config_handlers(self):
268 conf = self.new_config(_utest=True) 1c
270 # Mock out idle_dir
271 idle_dir = '/home/foo' 1c
272 with mock.patch.dict({'__name__': '__foo__'}): 1c
273 with mock.patch('os.path.dirname', return_value=idle_dir): 1c
274 conf.CreateConfigHandlers() 1c
276 # Check keys are equal
277 self.assertCountEqual(conf.defaultCfg.keys(), conf.config_types) 1c
278 self.assertCountEqual(conf.userCfg.keys(), conf.config_types) 1c
280 # Check conf parser are correct type
281 for default_parser in conf.defaultCfg.values(): 1c
282 self.assertIsInstance(default_parser, config.IdleConfParser) 1c
283 for user_parser in conf.userCfg.values(): 1c
284 self.assertIsInstance(user_parser, config.IdleUserConfParser) 1c
286 # Check config path are correct
287 for cfg_type, parser in conf.defaultCfg.items(): 1c
288 self.assertEqual(parser.file, 1c
289 os.path.join(idle_dir, f'config-{cfg_type}.def'))
290 for cfg_type, parser in conf.userCfg.items(): 1c
291 self.assertEqual(parser.file, 1c
292 os.path.join(conf.userdir or '#', f'config-{cfg_type}.cfg'))
294 def test_load_cfg_files(self):
295 conf = self.new_config(_utest=True) 1s
297 # Borrow test/cfgparser.1 from test_configparser.
298 config_path = findfile('cfgparser.1') 1s
299 conf.defaultCfg['foo'] = config.IdleConfParser(config_path) 1s
300 conf.userCfg['foo'] = config.IdleUserConfParser(config_path) 1s
302 # Load all config from path
303 conf.LoadCfgFiles() 1s
305 eq = self.assertEqual 1s
307 # Check defaultCfg is loaded
308 eq(conf.defaultCfg['foo'].Get('Foo Bar', 'foo'), 'newbar') 1s
309 eq(conf.defaultCfg['foo'].GetOptionList('Foo Bar'), ['foo']) 1s
311 # Check userCfg is loaded
312 eq(conf.userCfg['foo'].Get('Foo Bar', 'foo'), 'newbar') 1s
313 eq(conf.userCfg['foo'].GetOptionList('Foo Bar'), ['foo']) 1s
315 def test_save_user_cfg_files(self):
316 conf = self.mock_config() 1u
318 with mock.patch('idlelib.config.IdleUserConfParser.Save') as m: 1u
319 conf.SaveUserCfgFiles() 1u
320 self.assertEqual(m.call_count, len(conf.userCfg)) 1u
322 def test_get_option(self):
323 conf = self.mock_config() 1f
325 eq = self.assertEqual 1f
326 eq(conf.GetOption('main', 'EditorWindow', 'width'), '80') 1f
327 eq(conf.GetOption('main', 'EditorWindow', 'width', type='int'), 80) 1f
328 with mock.patch('idlelib.config._warn') as _warn: 1f
329 eq(conf.GetOption('main', 'EditorWindow', 'font', type='int'), None) 1f
330 eq(conf.GetOption('main', 'EditorWindow', 'NotExists'), None) 1f
331 eq(conf.GetOption('main', 'EditorWindow', 'NotExists', default='NE'), 'NE') 1f
332 eq(_warn.call_count, 4) 1f
334 def test_set_option(self):
335 conf = self.mock_config() 1z
337 conf.SetOption('main', 'Foo', 'bar', 'newbar') 1z
338 self.assertEqual(conf.GetOption('main', 'Foo', 'bar'), 'newbar') 1z
340 def test_get_section_list(self):
341 conf = self.mock_config() 1i
343 self.assertCountEqual( 1i
344 conf.GetSectionList('default', 'main'),
345 ['General', 'EditorWindow', 'PyShell', 'Indent', 'Theme',
346 'Keys', 'History', 'HelpFiles'])
347 self.assertCountEqual( 1i
348 conf.GetSectionList('user', 'main'),
349 ['General', 'EditorWindow', 'PyShell', 'Indent', 'Theme',
350 'Keys', 'History', 'HelpFiles'])
352 with self.assertRaises(config.InvalidConfigSet): 1i
353 conf.GetSectionList('foobar', 'main') 1i
354 with self.assertRaises(config.InvalidConfigType): 1i
355 conf.GetSectionList('default', 'notexists') 1i
357 def test_get_highlight(self):
358 conf = self.mock_config() 1g
360 eq = self.assertEqual 1g
361 eq(conf.GetHighlight('IDLE Classic', 'normal'), {'foreground': '#000000', 1g
362 'background': '#ffffff'})
364 # Test cursor (this background should be normal-background)
365 eq(conf.GetHighlight('IDLE Classic', 'cursor'), {'foreground': 'black', 1g
366 'background': '#ffffff'})
368 # Test get user themes
369 conf.SetOption('highlight', 'Foobar', 'normal-foreground', '#747474') 1g
370 conf.SetOption('highlight', 'Foobar', 'normal-background', '#171717') 1g
371 with mock.patch('idlelib.config._warn'): 1g
372 eq(conf.GetHighlight('Foobar', 'normal'), {'foreground': '#747474', 1g
373 'background': '#171717'})
375 def test_get_theme_dict(self):
376 # TODO: finish.
377 conf = self.mock_config() 1t
379 # These two should be the same
380 self.assertEqual( 1t
381 conf.GetThemeDict('default', 'IDLE Classic'),
382 conf.GetThemeDict('user', 'IDLE Classic'))
384 with self.assertRaises(config.InvalidTheme): 1t
385 conf.GetThemeDict('bad', 'IDLE Classic') 1t
387 def test_get_current_theme_and_keys(self):
388 conf = self.mock_config() 1y
390 self.assertEqual(conf.CurrentTheme(), conf.current_colors_and_keys('Theme')) 1y
391 self.assertEqual(conf.CurrentKeys(), conf.current_colors_and_keys('Keys')) 1y
393 def test_current_colors_and_keys(self):
394 conf = self.mock_config() 1B
396 self.assertEqual(conf.current_colors_and_keys('Theme'), 'IDLE Classic') 1B
398 def test_default_keys(self):
399 current_platform = sys.platform 1x
400 conf = self.new_config(_utest=True) 1x
402 sys.platform = 'win32' 1x
403 self.assertEqual(conf.default_keys(), 'IDLE Classic Windows') 1x
405 sys.platform = 'darwin' 1x
406 self.assertEqual(conf.default_keys(), 'IDLE Classic OSX') 1x
408 sys.platform = 'some-linux' 1x
409 self.assertEqual(conf.default_keys(), 'IDLE Modern Unix') 1x
411 # Restore platform
412 sys.platform = current_platform 1x
414 def test_get_extensions(self):
415 userextn.read_string(''' 1A
416 [ZzDummy]
417 enable = True
418 [DISABLE]
419 enable = False
420 ''')
421 eq = self.assertEqual 1A
422 iGE = idleConf.GetExtensions 1A
423 eq(iGE(shell_only=True), []) 1A
424 eq(iGE(), ['ZzDummy']) 1A
425 eq(iGE(editor_only=True), ['ZzDummy']) 1A
426 eq(iGE(active_only=False), ['ZzDummy', 'DISABLE']) 1A
427 eq(iGE(active_only=False, editor_only=True), ['ZzDummy', 'DISABLE']) 1A
428 userextn.remove_section('ZzDummy') 1A
429 userextn.remove_section('DISABLE') 1A
432 def test_remove_key_bind_names(self):
433 conf = self.mock_config() 1C
435 self.assertCountEqual( 1C
436 conf.RemoveKeyBindNames(conf.GetSectionList('default', 'extensions')),
437 ['AutoComplete', 'CodeContext', 'FormatParagraph', 'ParenMatch', 'ZzDummy'])
439 def test_get_extn_name_for_event(self):
440 userextn.read_string(''' 1T
441 [ZzDummy]
442 enable = True
443 ''')
444 eq = self.assertEqual 1T
445 eq(idleConf.GetExtnNameForEvent('z-in'), 'ZzDummy') 1T
446 eq(idleConf.GetExtnNameForEvent('z-out'), None) 1T
447 userextn.remove_section('ZzDummy') 1T
449 def test_get_extension_keys(self):
450 userextn.read_string(''' 1V
451 [ZzDummy]
452 enable = True
453 ''')
454 self.assertEqual(idleConf.GetExtensionKeys('ZzDummy'), 1V
455 {'<<z-in>>': ['<Control-Shift-KeyRelease-Insert>']})
456 userextn.remove_section('ZzDummy') 1V
457# need option key test
458## key = ['<Option-Key-2>'] if sys.platform == 'darwin' else ['<Alt-Key-2>']
459## eq(conf.GetExtensionKeys('ZoomHeight'), {'<<zoom-height>>': key})
461 def test_get_extension_bindings(self):
462 userextn.read_string(''' 1M
463 [ZzDummy]
464 enable = True
465 ''')
466 eq = self.assertEqual 1M
467 iGEB = idleConf.GetExtensionBindings 1M
468 eq(iGEB('NotExists'), {}) 1M
469 expect = {'<<z-in>>': ['<Control-Shift-KeyRelease-Insert>'], 1M
470 '<<z-out>>': ['<Control-Shift-KeyRelease-Delete>']}
471 eq(iGEB('ZzDummy'), expect) 1M
472 userextn.remove_section('ZzDummy') 1M
474 def test_get_keybinding(self):
475 conf = self.mock_config() 1e
477 eq = self.assertEqual 1e
478 eq(conf.GetKeyBinding('IDLE Modern Unix', '<<copy>>'), 1e
479 ['<Control-Shift-Key-C>', '<Control-Key-Insert>'])
480 eq(conf.GetKeyBinding('IDLE Classic Unix', '<<copy>>'), 1e
481 ['<Alt-Key-w>', '<Meta-Key-w>'])
482 eq(conf.GetKeyBinding('IDLE Classic Windows', '<<copy>>'), 1e
483 ['<Control-Key-c>', '<Control-Key-C>'])
484 eq(conf.GetKeyBinding('IDLE Classic Mac', '<<copy>>'), ['<Command-Key-c>']) 1e
485 eq(conf.GetKeyBinding('IDLE Classic OSX', '<<copy>>'), ['<Command-Key-c>']) 1e
487 # Test keybinding not exists
488 eq(conf.GetKeyBinding('NOT EXISTS', '<<copy>>'), []) 1e
489 eq(conf.GetKeyBinding('IDLE Modern Unix', 'NOT EXISTS'), []) 1e
491 def test_get_current_keyset(self):
492 current_platform = sys.platform 1p
493 conf = self.mock_config() 1p
495 # Ensure that platform isn't darwin
496 sys.platform = 'some-linux' 1p
497 self.assertEqual(conf.GetCurrentKeySet(), conf.GetKeySet(conf.CurrentKeys())) 1p
499 # This should not be the same, since replace <Alt- to <Option-.
500 # Above depended on config-extensions.def having Alt keys,
501 # which is no longer true.
502 # sys.platform = 'darwin'
503 # self.assertNotEqual(conf.GetCurrentKeySet(), conf.GetKeySet(conf.CurrentKeys()))
505 # Restore platform
506 sys.platform = current_platform 1p
508 def test_get_keyset(self):
509 conf = self.mock_config() 1l
511 # Conflict with key set, should be disable to ''
512 conf.defaultCfg['extensions'].add_section('Foobar') 1l
513 conf.defaultCfg['extensions'].add_section('Foobar_cfgBindings') 1l
514 conf.defaultCfg['extensions'].set('Foobar', 'enable', 'True') 1l
515 conf.defaultCfg['extensions'].set('Foobar_cfgBindings', 'newfoo', '<Key-F3>') 1l
516 self.assertEqual(conf.GetKeySet('IDLE Modern Unix')['<<newfoo>>'], '') 1l
518 def test_is_core_binding(self):
519 # XXX: Should move out the core keys to config file or other place
520 conf = self.mock_config() 1r
522 self.assertTrue(conf.IsCoreBinding('copy')) 1r
523 self.assertTrue(conf.IsCoreBinding('cut')) 1r
524 self.assertTrue(conf.IsCoreBinding('del-word-right')) 1r
525 self.assertFalse(conf.IsCoreBinding('not-exists')) 1r
527 def test_extra_help_source_list(self):
528 # Test GetExtraHelpSourceList and GetAllExtraHelpSourcesList in same
529 # place to prevent prepare input data twice.
530 conf = self.mock_config() 1b
532 # Test default with no extra help source
533 self.assertEqual(conf.GetExtraHelpSourceList('default'), []) 1b
534 self.assertEqual(conf.GetExtraHelpSourceList('user'), []) 1b
535 with self.assertRaises(config.InvalidConfigSet): 1b
536 self.assertEqual(conf.GetExtraHelpSourceList('bad'), []) 1b
537 self.assertCountEqual( 1b
538 conf.GetAllExtraHelpSourcesList(),
539 conf.GetExtraHelpSourceList('default') + conf.GetExtraHelpSourceList('user'))
541 # Add help source to user config
542 conf.userCfg['main'].SetOption('HelpFiles', '4', 'Python;https://python.org') # This is bad input 1b
543 conf.userCfg['main'].SetOption('HelpFiles', '3', 'Python:https://python.org') # This is bad input 1b
544 conf.userCfg['main'].SetOption('HelpFiles', '2', 'Pillow;https://pillow.readthedocs.io/en/latest/') 1b
545 conf.userCfg['main'].SetOption('HelpFiles', '1', 'IDLE;C:/Programs/Python36/Lib/idlelib/help.html') 1b
546 self.assertEqual(conf.GetExtraHelpSourceList('user'), 1b
547 [('IDLE', 'C:/Programs/Python36/Lib/idlelib/help.html', '1'),
548 ('Pillow', 'https://pillow.readthedocs.io/en/latest/', '2'),
549 ('Python', 'https://python.org', '4')])
550 self.assertCountEqual( 1b
551 conf.GetAllExtraHelpSourcesList(),
552 conf.GetExtraHelpSourceList('default') + conf.GetExtraHelpSourceList('user'))
554 def test_get_font(self):
555 from test.support import requires 1q
556 from tkinter import Tk 1q
557 from tkinter.font import Font 1q
558 conf = self.mock_config() 1q
560 requires('gui') 1q
561 root = Tk()
562 root.withdraw()
564 f = Font.actual(Font(name='TkFixedFont', exists=True, root=root))
565 self.assertEqual(
566 conf.GetFont(root, 'main', 'EditorWindow'),
567 (f['family'], 10 if f['size'] <= 0 else f['size'], f['weight']))
569 # Cleanup root
570 root.destroy()
571 del root
573 def test_get_core_keys(self):
574 conf = self.mock_config() 1d
576 eq = self.assertEqual 1d
577 eq(conf.GetCoreKeys()['<<center-insert>>'], ['<Control-l>']) 1d
578 eq(conf.GetCoreKeys()['<<copy>>'], ['<Control-c>', '<Control-C>']) 1d
579 eq(conf.GetCoreKeys()['<<history-next>>'], ['<Alt-n>']) 1d
580 eq(conf.GetCoreKeys('IDLE Classic Windows')['<<center-insert>>'], 1d
581 ['<Control-Key-l>', '<Control-Key-L>'])
582 eq(conf.GetCoreKeys('IDLE Classic OSX')['<<copy>>'], ['<Command-Key-c>']) 1d
583 eq(conf.GetCoreKeys('IDLE Classic Unix')['<<history-next>>'], 1d
584 ['<Alt-Key-n>', '<Meta-Key-n>'])
585 eq(conf.GetCoreKeys('IDLE Modern Unix')['<<history-next>>'], 1d
586 ['<Alt-Key-n>', '<Meta-Key-n>'])
589class CurrentColorKeysTest(unittest.TestCase):
590 """ Test colorkeys function with user config [Theme] and [Keys] patterns.
592 colorkeys = config.IdleConf.current_colors_and_keys
593 Test all patterns written by IDLE and some errors
594 Item 'default' should really be 'builtin' (versus 'custom).
595 """
596 colorkeys = idleConf.current_colors_and_keys
597 default_theme = 'IDLE Classic'
598 default_keys = idleConf.default_keys()
600 def test_old_builtin_theme(self):
601 # On initial installation, user main is blank.
602 self.assertEqual(self.colorkeys('Theme'), self.default_theme) 1G
603 # For old default, name2 must be blank.
604 usermain.read_string(''' 1G
605 [Theme]
606 default = True
607 ''')
608 # IDLE omits 'name' for default old builtin theme.
609 self.assertEqual(self.colorkeys('Theme'), self.default_theme) 1G
610 # IDLE adds 'name' for non-default old builtin theme.
611 usermain['Theme']['name'] = 'IDLE New' 1G
612 self.assertEqual(self.colorkeys('Theme'), 'IDLE New') 1G
613 # Erroneous non-default old builtin reverts to default.
614 usermain['Theme']['name'] = 'non-existent' 1G
615 self.assertEqual(self.colorkeys('Theme'), self.default_theme) 1G
616 usermain.remove_section('Theme') 1G
618 def test_new_builtin_theme(self):
619 # IDLE writes name2 for new builtins.
620 usermain.read_string(''' 1N
621 [Theme]
622 default = True
623 name2 = IDLE Dark
624 ''')
625 self.assertEqual(self.colorkeys('Theme'), 'IDLE Dark') 1N
626 # Leftover 'name', not removed, is ignored.
627 usermain['Theme']['name'] = 'IDLE New' 1N
628 self.assertEqual(self.colorkeys('Theme'), 'IDLE Dark') 1N
629 # Erroneous non-default new builtin reverts to default.
630 usermain['Theme']['name2'] = 'non-existent' 1N
631 self.assertEqual(self.colorkeys('Theme'), self.default_theme) 1N
632 usermain.remove_section('Theme') 1N
634 def test_user_override_theme(self):
635 # Erroneous custom name (no definition) reverts to default.
636 usermain.read_string(''' 1H
637 [Theme]
638 default = False
639 name = Custom Dark
640 ''')
641 self.assertEqual(self.colorkeys('Theme'), self.default_theme) 1H
642 # Custom name is valid with matching Section name.
643 userhigh.read_string('[Custom Dark]\na=b') 1H
644 self.assertEqual(self.colorkeys('Theme'), 'Custom Dark') 1H
645 # Name2 is ignored.
646 usermain['Theme']['name2'] = 'non-existent' 1H
647 self.assertEqual(self.colorkeys('Theme'), 'Custom Dark') 1H
648 usermain.remove_section('Theme') 1H
649 userhigh.remove_section('Custom Dark') 1H
651 def test_old_builtin_keys(self):
652 # On initial installation, user main is blank.
653 self.assertEqual(self.colorkeys('Keys'), self.default_keys) 1Q
654 # For old default, name2 must be blank, name is always used.
655 usermain.read_string(''' 1Q
656 [Keys]
657 default = True
658 name = IDLE Classic Unix
659 ''')
660 self.assertEqual(self.colorkeys('Keys'), 'IDLE Classic Unix') 1Q
661 # Erroneous non-default old builtin reverts to default.
662 usermain['Keys']['name'] = 'non-existent' 1Q
663 self.assertEqual(self.colorkeys('Keys'), self.default_keys) 1Q
664 usermain.remove_section('Keys') 1Q
666 def test_new_builtin_keys(self):
667 # IDLE writes name2 for new builtins.
668 usermain.read_string(''' 1O
669 [Keys]
670 default = True
671 name2 = IDLE Modern Unix
672 ''')
673 self.assertEqual(self.colorkeys('Keys'), 'IDLE Modern Unix') 1O
674 # Leftover 'name', not removed, is ignored.
675 usermain['Keys']['name'] = 'IDLE Classic Unix' 1O
676 self.assertEqual(self.colorkeys('Keys'), 'IDLE Modern Unix') 1O
677 # Erroneous non-default new builtin reverts to default.
678 usermain['Keys']['name2'] = 'non-existent' 1O
679 self.assertEqual(self.colorkeys('Keys'), self.default_keys) 1O
680 usermain.remove_section('Keys') 1O
682 def test_user_override_keys(self):
683 # Erroneous custom name (no definition) reverts to default.
684 usermain.read_string(''' 1I
685 [Keys]
686 default = False
687 name = Custom Keys
688 ''')
689 self.assertEqual(self.colorkeys('Keys'), self.default_keys) 1I
690 # Custom name is valid with matching Section name.
691 userkeys.read_string('[Custom Keys]\na=b') 1I
692 self.assertEqual(self.colorkeys('Keys'), 'Custom Keys') 1I
693 # Name2 is ignored.
694 usermain['Keys']['name2'] = 'non-existent' 1I
695 self.assertEqual(self.colorkeys('Keys'), 'Custom Keys') 1I
696 usermain.remove_section('Keys') 1I
697 userkeys.remove_section('Custom Keys') 1I
700class ChangesTest(unittest.TestCase):
702 empty = {'main':{}, 'highlight':{}, 'keys':{}, 'extensions':{}}
704 def load(self): # Test_add_option verifies that this works.
705 changes = self.changes 1DJmh
706 changes.add_option('main', 'Msec', 'mitem', 'mval') 1DJmh
707 changes.add_option('highlight', 'Hsec', 'hitem', 'hval') 1DJmh
708 changes.add_option('keys', 'Ksec', 'kitem', 'kval') 1DJmh
709 return changes 1DJmh
711 loaded = {'main': {'Msec': {'mitem': 'mval'}},
712 'highlight': {'Hsec': {'hitem': 'hval'}},
713 'keys': {'Ksec': {'kitem':'kval'}},
714 'extensions': {}}
716 def setUp(self):
717 self.changes = config.ConfigChanges()
719 def test_init(self):
720 self.assertEqual(self.changes, self.empty) 1X
722 def test_add_option(self):
723 changes = self.load() 1D
724 self.assertEqual(changes, self.loaded) 1D
725 changes.add_option('main', 'Msec', 'mitem', 'mval') 1D
726 self.assertEqual(changes, self.loaded) 1D
728 def test_save_option(self): # Static function does not touch changes.
729 save_option = self.changes.save_option 1E
730 self.assertTrue(save_option('main', 'Indent', 'what', '0')) 1E
731 self.assertFalse(save_option('main', 'Indent', 'what', '0')) 1E
732 self.assertEqual(usermain['Indent']['what'], '0') 1E
734 self.assertTrue(save_option('main', 'Indent', 'use-spaces', '0')) 1E
735 self.assertEqual(usermain['Indent']['use-spaces'], '0') 1E
736 self.assertTrue(save_option('main', 'Indent', 'use-spaces', '1')) 1E
737 self.assertFalse(usermain.has_option('Indent', 'use-spaces')) 1E
738 usermain.remove_section('Indent') 1E
740 def test_save_added(self):
741 changes = self.load() 1h
742 self.assertTrue(changes.save_all()) 1h
743 self.assertEqual(usermain['Msec']['mitem'], 'mval') 1h
744 self.assertEqual(userhigh['Hsec']['hitem'], 'hval') 1h
745 self.assertEqual(userkeys['Ksec']['kitem'], 'kval') 1h
746 changes.add_option('main', 'Msec', 'mitem', 'mval') 1h
747 self.assertFalse(changes.save_all()) 1h
748 usermain.remove_section('Msec') 1h
749 userhigh.remove_section('Hsec') 1h
750 userkeys.remove_section('Ksec') 1h
752 def test_save_help(self):
753 # Any change to HelpFiles overwrites entire section.
754 changes = self.changes 1R
755 changes.save_option('main', 'HelpFiles', 'IDLE', 'idledoc') 1R
756 changes.add_option('main', 'HelpFiles', 'ELDI', 'codeldi') 1R
757 changes.save_all() 1R
758 self.assertFalse(usermain.has_option('HelpFiles', 'IDLE')) 1R
759 self.assertTrue(usermain.has_option('HelpFiles', 'ELDI')) 1R
761 def test_save_default(self): # Cover 2nd and 3rd false branches.
762 changes = self.changes 1W
763 changes.add_option('main', 'Indent', 'use-spaces', '1') 1W
764 # save_option returns False; cfg_type_changed remains False.
766 # TODO: test that save_all calls usercfg Saves.
768 def test_delete_section(self):
769 changes = self.load() 1m
770 changes.delete_section('main', 'fake') # Test no exception. 1m
771 self.assertEqual(changes, self.loaded) # Test nothing deleted. 1m
772 for cfgtype, section in (('main', 'Msec'), ('keys', 'Ksec')): 1m
773 testcfg[cfgtype].SetOption(section, 'name', 'value') 1m
774 changes.delete_section(cfgtype, section) 1m
775 with self.assertRaises(KeyError): 1m
776 changes[cfgtype][section] # Test section gone from changes 1m
777 testcfg[cfgtype][section] # and from mock userCfg.
778 # TODO test for save call.
780 def test_clear(self):
781 changes = self.load() 1J
782 changes.clear() 1J
783 self.assertEqual(changes, self.empty) 1J
786class WarningTest(unittest.TestCase):
788 def test_warn(self):
789 Equal = self.assertEqual 1n
790 config._warned = set() 1n
791 with captured_stderr() as stderr: 1n
792 config._warn('warning', 'key') 1n
793 Equal(config._warned, {('warning','key')}) 1n
794 Equal(stderr.getvalue(), 'warning'+'\n') 1n
795 with captured_stderr() as stderr: 1n
796 config._warn('warning', 'key') 1n
797 Equal(stderr.getvalue(), '') 1n
798 with captured_stderr() as stderr: 1n
799 config._warn('warn2', 'yek') 1n
800 Equal(config._warned, {('warning','key'), ('warn2','yek')}) 1n
801 Equal(stderr.getvalue(), 'warn2'+'\n') 1n
804if __name__ == '__main__': 804 ↛ 805line 804 didn't jump to line 805, because the condition on line 804 was never true
805 unittest.main(verbosity=2)