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 15 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. 20 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('') 28 29def setUpModule(): 30 idleConf.userCfg = testcfg 31 idlelib.testing = True 32 33def tearDownModule(): 34 idleConf.userCfg = usercfg 35 idlelib.testing = False 36 37 38class IdleConfParserTest(unittest.TestCase): 39 """Test that IdleConfParser works""" 40 41 config = """ 42 [one] 43 one = false 44 two = true 45 three = 10 46 47 [two] 48 one = a string 49 two = true 50 three = false 51 """ 52 53 def test_get(self): 54 parser = config.IdleConfParser('') 55 parser.read_string(self.config) 56 eq = self.assertEqual 57 58 # Test with type argument. 59 self.assertIs(parser.Get('one', 'one', type='bool'), False) 60 self.assertIs(parser.Get('one', 'two', type='bool'), True) 61 eq(parser.Get('one', 'three', type='int'), 10) 62 eq(parser.Get('two', 'one'), 'a string') 63 self.assertIs(parser.Get('two', 'two', type='bool'), True) 64 self.assertIs(parser.Get('two', 'three', type='bool'), False) 65 66 # Test without type should fallback to string. 67 eq(parser.Get('two', 'two'), 'true') 68 eq(parser.Get('two', 'three'), 'false') 69 70 # If option not exist, should return None, or default. 71 self.assertIsNone(parser.Get('not', 'exist')) 72 eq(parser.Get('not', 'exist', default='DEFAULT'), 'DEFAULT') 73 74 def test_get_option_list(self): 75 parser = config.IdleConfParser('') 76 parser.read_string(self.config) 77 get_list = parser.GetOptionList 78 self.assertCountEqual(get_list('one'), ['one', 'two', 'three']) 79 self.assertCountEqual(get_list('two'), ['one', 'two', 'three']) 80 self.assertEqual(get_list('not exist'), []) 81 82 def test_load_nothing(self): 83 parser = config.IdleConfParser('') 84 parser.Load() 85 self.assertEqual(parser.sections(), []) 86 87 def test_load_file(self): 88 # Borrow test/cfgparser.1 from test_configparser. 89 config_path = findfile('cfgparser.1') 90 parser = config.IdleConfParser(config_path) 91 parser.Load() 92 93 self.assertEqual(parser.Get('Foo Bar', 'foo'), 'newbar') 94 self.assertEqual(parser.GetOptionList('Foo Bar'), ['foo']) 95 96 97class IdleUserConfParserTest(unittest.TestCase): 98 """Test that IdleUserConfParser works""" 99 100 def new_parser(self, path=''): 101 return config.IdleUserConfParser(path) 102 103 def test_set_option(self): 104 parser = self.new_parser() 105 parser.add_section('Foo') 106 # Setting new option in existing section should return True. 107 self.assertTrue(parser.SetOption('Foo', 'bar', 'true')) 108 # Setting existing option with same value should return False. 109 self.assertFalse(parser.SetOption('Foo', 'bar', 'true')) 110 # Setting exiting option with new value should return True. 111 self.assertTrue(parser.SetOption('Foo', 'bar', 'false')) 112 self.assertEqual(parser.Get('Foo', 'bar'), 'false') 113 114 # Setting option in new section should create section and return True. 115 self.assertTrue(parser.SetOption('Bar', 'bar', 'true')) 116 self.assertCountEqual(parser.sections(), ['Bar', 'Foo']) 117 self.assertEqual(parser.Get('Bar', 'bar'), 'true') 118 119 def test_remove_option(self): 120 parser = self.new_parser() 121 parser.AddSection('Foo') 122 parser.SetOption('Foo', 'bar', 'true') 123 124 self.assertTrue(parser.RemoveOption('Foo', 'bar')) 125 self.assertFalse(parser.RemoveOption('Foo', 'bar')) 126 self.assertFalse(parser.RemoveOption('Not', 'Exist')) 127 128 def test_add_section(self): 129 parser = self.new_parser() 130 self.assertEqual(parser.sections(), []) 131 132 # Should not add duplicate section. 133 # Configparser raises DuplicateError, IdleParser not. 134 parser.AddSection('Foo') 135 parser.AddSection('Foo') 136 parser.AddSection('Bar') 137 self.assertCountEqual(parser.sections(), ['Bar', 'Foo']) 138 139 def test_remove_empty_sections(self): 140 parser = self.new_parser() 141 142 parser.AddSection('Foo') 143 parser.AddSection('Bar') 144 parser.SetOption('Idle', 'name', 'val') 145 self.assertCountEqual(parser.sections(), ['Bar', 'Foo', 'Idle']) 146 parser.RemoveEmptySections() 147 self.assertEqual(parser.sections(), ['Idle']) 148 149 def test_is_empty(self): 150 parser = self.new_parser() 151 152 parser.AddSection('Foo') 153 parser.AddSection('Bar') 154 self.assertTrue(parser.IsEmpty()) 155 self.assertEqual(parser.sections(), []) 156 157 parser.SetOption('Foo', 'bar', 'false') 158 parser.AddSection('Bar') 159 self.assertFalse(parser.IsEmpty()) 160 self.assertCountEqual(parser.sections(), ['Foo']) 161 162 def test_save(self): 163 with tempfile.TemporaryDirectory() as tdir: 164 path = os.path.join(tdir, 'test.cfg') 165 parser = self.new_parser(path) 166 parser.AddSection('Foo') 167 parser.SetOption('Foo', 'bar', 'true') 168 169 # Should save to path when config is not empty. 170 self.assertFalse(os.path.exists(path)) 171 parser.Save() 172 self.assertTrue(os.path.exists(path)) 173 174 # Should remove the file from disk when config is empty. 175 parser.remove_section('Foo') 176 parser.Save() 177 self.assertFalse(os.path.exists(path)) 178 179 180class IdleConfTest(unittest.TestCase): 181 """Test for idleConf""" 182 183 @classmethod 184 def setUpClass(cls): 185 cls.config_string = {} 186 187 conf = config.IdleConf(_utest=True) 188 if __name__ != '__main__': 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() 196 197 cls.orig_warn = config._warn 198 config._warn = Func() 199 200 @classmethod 201 def tearDownClass(cls): 202 config._warn = cls.orig_warn 203 204 def new_config(self, _utest=False): 205 return config.IdleConf(_utest=_utest) 206 207 def mock_config(self): 208 """Return a mocked idleConf 209 210 Both default and user config used the same config-*.def 211 """ 212 conf = config.IdleConf(_utest=True) 213 for ctype in conf.config_types: 214 conf.defaultCfg[ctype] = config.IdleConfParser('') 215 conf.defaultCfg[ctype].read_string(self.config_string[ctype]) 216 conf.userCfg[ctype] = config.IdleUserConfParser('') 217 conf.userCfg[ctype].read_string(self.config_string[ctype]) 218 219 return conf 220 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) 225 226 # Check normal way should success 227 with mock.patch('os.path.expanduser', return_value='/home/foo'): 228 with mock.patch('os.path.exists', return_value=True): 229 self.assertEqual(conf.GetUserCfgDir(), '/home/foo/.idlerc') 230 231 # Check os.getcwd should success 232 with mock.patch('os.path.expanduser', return_value='~'): 233 with mock.patch('os.getcwd', return_value='/home/foo/cpython'): 234 with mock.patch('os.mkdir'): 235 self.assertEqual(conf.GetUserCfgDir(), 236 '/home/foo/cpython/.idlerc') 237 238 # Check user dir not exists and created failed should raise SystemExit 239 with mock.patch('os.path.join', return_value='/path/not/exists'): 240 with self.assertRaises(SystemExit): 241 with self.assertRaises(FileNotFoundError): 242 conf.GetUserCfgDir() 243 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) 248 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') 253 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') 260 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() 266 267 def test_create_config_handlers(self): 268 conf = self.new_config(_utest=True) 269 270 # Mock out idle_dir 271 idle_dir = '/home/foo' 272 with mock.patch.dict({'__name__': '__foo__'}): 273 with mock.patch('os.path.dirname', return_value=idle_dir): 274 conf.CreateConfigHandlers() 275 276 # Check keys are equal 277 self.assertCountEqual(conf.defaultCfg.keys(), conf.config_types) 278 self.assertCountEqual(conf.userCfg.keys(), conf.config_types) 279 280 # Check conf parser are correct type 281 for default_parser in conf.defaultCfg.values(): 282 self.assertIsInstance(default_parser, config.IdleConfParser) 283 for user_parser in conf.userCfg.values(): 284 self.assertIsInstance(user_parser, config.IdleUserConfParser) 285 286 # Check config path are correct 287 for cfg_type, parser in conf.defaultCfg.items(): 288 self.assertEqual(parser.file, 289 os.path.join(idle_dir, f'config-{cfg_type}.def')) 290 for cfg_type, parser in conf.userCfg.items(): 291 self.assertEqual(parser.file, 292 os.path.join(conf.userdir or '#', f'config-{cfg_type}.cfg')) 293 294 def test_load_cfg_files(self): 295 conf = self.new_config(_utest=True) 296 297 # Borrow test/cfgparser.1 from test_configparser. 298 config_path = findfile('cfgparser.1') 299 conf.defaultCfg['foo'] = config.IdleConfParser(config_path) 300 conf.userCfg['foo'] = config.IdleUserConfParser(config_path) 301 302 # Load all config from path 303 conf.LoadCfgFiles() 304 305 eq = self.assertEqual 306 307 # Check defaultCfg is loaded 308 eq(conf.defaultCfg['foo'].Get('Foo Bar', 'foo'), 'newbar') 309 eq(conf.defaultCfg['foo'].GetOptionList('Foo Bar'), ['foo']) 310 311 # Check userCfg is loaded 312 eq(conf.userCfg['foo'].Get('Foo Bar', 'foo'), 'newbar') 313 eq(conf.userCfg['foo'].GetOptionList('Foo Bar'), ['foo']) 314 315 def test_save_user_cfg_files(self): 316 conf = self.mock_config() 317 318 with mock.patch('idlelib.config.IdleUserConfParser.Save') as m: 319 conf.SaveUserCfgFiles() 320 self.assertEqual(m.call_count, len(conf.userCfg)) 321 322 def test_get_option(self): 323 conf = self.mock_config() 324 325 eq = self.assertEqual 326 eq(conf.GetOption('main', 'EditorWindow', 'width'), '80') 327 eq(conf.GetOption('main', 'EditorWindow', 'width', type='int'), 80) 328 with mock.patch('idlelib.config._warn') as _warn: 329 eq(conf.GetOption('main', 'EditorWindow', 'font', type='int'), None) 330 eq(conf.GetOption('main', 'EditorWindow', 'NotExists'), None) 331 eq(conf.GetOption('main', 'EditorWindow', 'NotExists', default='NE'), 'NE') 332 eq(_warn.call_count, 4) 333 334 def test_set_option(self): 335 conf = self.mock_config() 336 337 conf.SetOption('main', 'Foo', 'bar', 'newbar') 338 self.assertEqual(conf.GetOption('main', 'Foo', 'bar'), 'newbar') 339 340 def test_get_section_list(self): 341 conf = self.mock_config() 342 343 self.assertCountEqual( 344 conf.GetSectionList('default', 'main'), 345 ['General', 'EditorWindow', 'PyShell', 'Indent', 'Theme', 346 'Keys', 'History', 'HelpFiles']) 347 self.assertCountEqual( 348 conf.GetSectionList('user', 'main'), 349 ['General', 'EditorWindow', 'PyShell', 'Indent', 'Theme', 350 'Keys', 'History', 'HelpFiles']) 351 352 with self.assertRaises(config.InvalidConfigSet): 353 conf.GetSectionList('foobar', 'main') 354 with self.assertRaises(config.InvalidConfigType): 355 conf.GetSectionList('default', 'notexists') 356 357 def test_get_highlight(self): 358 conf = self.mock_config() 359 360 eq = self.assertEqual 361 eq(conf.GetHighlight('IDLE Classic', 'normal'), {'foreground': '#000000', 362 'background': '#ffffff'}) 363 364 # Test cursor (this background should be normal-background) 365 eq(conf.GetHighlight('IDLE Classic', 'cursor'), {'foreground': 'black', 366 'background': '#ffffff'}) 367 368 # Test get user themes 369 conf.SetOption('highlight', 'Foobar', 'normal-foreground', '#747474') 370 conf.SetOption('highlight', 'Foobar', 'normal-background', '#171717') 371 with mock.patch('idlelib.config._warn'): 372 eq(conf.GetHighlight('Foobar', 'normal'), {'foreground': '#747474', 373 'background': '#171717'}) 374 375 def test_get_theme_dict(self): 376 # TODO: finish. 377 conf = self.mock_config() 378 379 # These two should be the same 380 self.assertEqual( 381 conf.GetThemeDict('default', 'IDLE Classic'), 382 conf.GetThemeDict('user', 'IDLE Classic')) 383 384 with self.assertRaises(config.InvalidTheme): 385 conf.GetThemeDict('bad', 'IDLE Classic') 386 387 def test_get_current_theme_and_keys(self): 388 conf = self.mock_config() 389 390 self.assertEqual(conf.CurrentTheme(), conf.current_colors_and_keys('Theme')) 391 self.assertEqual(conf.CurrentKeys(), conf.current_colors_and_keys('Keys')) 392 393 def test_current_colors_and_keys(self): 394 conf = self.mock_config() 395 396 self.assertEqual(conf.current_colors_and_keys('Theme'), 'IDLE Classic') 397 398 def test_default_keys(self): 399 current_platform = sys.platform 400 conf = self.new_config(_utest=True) 401 402 sys.platform = 'win32' 403 self.assertEqual(conf.default_keys(), 'IDLE Classic Windows') 404 405 sys.platform = 'darwin' 406 self.assertEqual(conf.default_keys(), 'IDLE Classic OSX') 407 408 sys.platform = 'some-linux' 409 self.assertEqual(conf.default_keys(), 'IDLE Modern Unix') 410 411 # Restore platform 412 sys.platform = current_platform 413 414 def test_get_extensions(self): 415 userextn.read_string(''' 416 [ZzDummy] 417 enable = True 418 [DISABLE] 419 enable = False 420 ''') 421 eq = self.assertEqual 422 iGE = idleConf.GetExtensions 423 eq(iGE(shell_only=True), []) 424 eq(iGE(), ['ZzDummy']) 425 eq(iGE(editor_only=True), ['ZzDummy']) 426 eq(iGE(active_only=False), ['ZzDummy', 'DISABLE']) 427 eq(iGE(active_only=False, editor_only=True), ['ZzDummy', 'DISABLE']) 428 userextn.remove_section('ZzDummy') 429 userextn.remove_section('DISABLE') 430 431 432 def test_remove_key_bind_names(self): 433 conf = self.mock_config() 434 435 self.assertCountEqual( 436 conf.RemoveKeyBindNames(conf.GetSectionList('default', 'extensions')), 437 ['AutoComplete', 'CodeContext', 'FormatParagraph', 'ParenMatch', 'ZzDummy']) 438 439 def test_get_extn_name_for_event(self): 440 userextn.read_string(''' 441 [ZzDummy] 442 enable = True 443 ''') 444 eq = self.assertEqual 445 eq(idleConf.GetExtnNameForEvent('z-in'), 'ZzDummy') 446 eq(idleConf.GetExtnNameForEvent('z-out'), None) 447 userextn.remove_section('ZzDummy') 448 449 def test_get_extension_keys(self): 450 userextn.read_string(''' 451 [ZzDummy] 452 enable = True 453 ''') 454 self.assertEqual(idleConf.GetExtensionKeys('ZzDummy'), 455 {'<<z-in>>': ['<Control-Shift-KeyRelease-Insert>']}) 456 userextn.remove_section('ZzDummy') 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}) 460 461 def test_get_extension_bindings(self): 462 userextn.read_string(''' 463 [ZzDummy] 464 enable = True 465 ''') 466 eq = self.assertEqual 467 iGEB = idleConf.GetExtensionBindings 468 eq(iGEB('NotExists'), {}) 469 expect = {'<<z-in>>': ['<Control-Shift-KeyRelease-Insert>'], 470 '<<z-out>>': ['<Control-Shift-KeyRelease-Delete>']} 471 eq(iGEB('ZzDummy'), expect) 472 userextn.remove_section('ZzDummy') 473 474 def test_get_keybinding(self): 475 conf = self.mock_config() 476 477 eq = self.assertEqual 478 eq(conf.GetKeyBinding('IDLE Modern Unix', '<<copy>>'), 479 ['<Control-Shift-Key-C>', '<Control-Key-Insert>']) 480 eq(conf.GetKeyBinding('IDLE Classic Unix', '<<copy>>'), 481 ['<Alt-Key-w>', '<Meta-Key-w>']) 482 eq(conf.GetKeyBinding('IDLE Classic Windows', '<<copy>>'), 483 ['<Control-Key-c>', '<Control-Key-C>']) 484 eq(conf.GetKeyBinding('IDLE Classic Mac', '<<copy>>'), ['<Command-Key-c>']) 485 eq(conf.GetKeyBinding('IDLE Classic OSX', '<<copy>>'), ['<Command-Key-c>']) 486 487 # Test keybinding not exists 488 eq(conf.GetKeyBinding('NOT EXISTS', '<<copy>>'), []) 489 eq(conf.GetKeyBinding('IDLE Modern Unix', 'NOT EXISTS'), []) 490 491 def test_get_current_keyset(self): 492 current_platform = sys.platform 493 conf = self.mock_config() 494 495 # Ensure that platform isn't darwin 496 sys.platform = 'some-linux' 497 self.assertEqual(conf.GetCurrentKeySet(), conf.GetKeySet(conf.CurrentKeys())) 498 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())) 504 505 # Restore platform 506 sys.platform = current_platform 507 508 def test_get_keyset(self): 509 conf = self.mock_config() 510 511 # Conflict with key set, should be disable to '' 512 conf.defaultCfg['extensions'].add_section('Foobar') 513 conf.defaultCfg['extensions'].add_section('Foobar_cfgBindings') 514 conf.defaultCfg['extensions'].set('Foobar', 'enable', 'True') 515 conf.defaultCfg['extensions'].set('Foobar_cfgBindings', 'newfoo', '<Key-F3>') 516 self.assertEqual(conf.GetKeySet('IDLE Modern Unix')['<<newfoo>>'], '') 517 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() 521 522 self.assertTrue(conf.IsCoreBinding('copy')) 523 self.assertTrue(conf.IsCoreBinding('cut')) 524 self.assertTrue(conf.IsCoreBinding('del-word-right')) 525 self.assertFalse(conf.IsCoreBinding('not-exists')) 526 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() 531 532 # Test default with no extra help source 533 self.assertEqual(conf.GetExtraHelpSourceList('default'), []) 534 self.assertEqual(conf.GetExtraHelpSourceList('user'), []) 535 with self.assertRaises(config.InvalidConfigSet): 536 self.assertEqual(conf.GetExtraHelpSourceList('bad'), []) 537 self.assertCountEqual( 538 conf.GetAllExtraHelpSourcesList(), 539 conf.GetExtraHelpSourceList('default') + conf.GetExtraHelpSourceList('user')) 540 541 # Add help source to user config 542 conf.userCfg['main'].SetOption('HelpFiles', '4', 'Python;https://python.org') # This is bad input 543 conf.userCfg['main'].SetOption('HelpFiles', '3', 'Python:https://python.org') # This is bad input 544 conf.userCfg['main'].SetOption('HelpFiles', '2', 'Pillow;https://pillow.readthedocs.io/en/latest/') 545 conf.userCfg['main'].SetOption('HelpFiles', '1', 'IDLE;C:/Programs/Python36/Lib/idlelib/help.html') 546 self.assertEqual(conf.GetExtraHelpSourceList('user'), 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( 551 conf.GetAllExtraHelpSourcesList(), 552 conf.GetExtraHelpSourceList('default') + conf.GetExtraHelpSourceList('user')) 553 554 def test_get_font(self): 555 from test.support import requires 556 from tkinter import Tk 557 from tkinter.font import Font 558 conf = self.mock_config() 559 560 requires('gui') 561 root = Tk() 562 root.withdraw() 563 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'])) 568 569 # Cleanup root 570 root.destroy() 571 del root 572 573 def test_get_core_keys(self): 574 conf = self.mock_config() 575 576 eq = self.assertEqual 577 eq(conf.GetCoreKeys()['<<center-insert>>'], ['<Control-l>']) 578 eq(conf.GetCoreKeys()['<<copy>>'], ['<Control-c>', '<Control-C>']) 579 eq(conf.GetCoreKeys()['<<history-next>>'], ['<Alt-n>']) 580 eq(conf.GetCoreKeys('IDLE Classic Windows')['<<center-insert>>'], 581 ['<Control-Key-l>', '<Control-Key-L>']) 582 eq(conf.GetCoreKeys('IDLE Classic OSX')['<<copy>>'], ['<Command-Key-c>']) 583 eq(conf.GetCoreKeys('IDLE Classic Unix')['<<history-next>>'], 584 ['<Alt-Key-n>', '<Meta-Key-n>']) 585 eq(conf.GetCoreKeys('IDLE Modern Unix')['<<history-next>>'], 586 ['<Alt-Key-n>', '<Meta-Key-n>']) 587 588 589class CurrentColorKeysTest(unittest.TestCase): 590 """ Test colorkeys function with user config [Theme] and [Keys] patterns. 591 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() 599 600 def test_old_builtin_theme(self): 601 # On initial installation, user main is blank. 602 self.assertEqual(self.colorkeys('Theme'), self.default_theme) 603 # For old default, name2 must be blank. 604 usermain.read_string(''' 605 [Theme] 606 default = True 607 ''') 608 # IDLE omits 'name' for default old builtin theme. 609 self.assertEqual(self.colorkeys('Theme'), self.default_theme) 610 # IDLE adds 'name' for non-default old builtin theme. 611 usermain['Theme']['name'] = 'IDLE New' 612 self.assertEqual(self.colorkeys('Theme'), 'IDLE New') 613 # Erroneous non-default old builtin reverts to default. 614 usermain['Theme']['name'] = 'non-existent' 615 self.assertEqual(self.colorkeys('Theme'), self.default_theme) 616 usermain.remove_section('Theme') 617 618 def test_new_builtin_theme(self): 619 # IDLE writes name2 for new builtins. 620 usermain.read_string(''' 621 [Theme] 622 default = True 623 name2 = IDLE Dark 624 ''') 625 self.assertEqual(self.colorkeys('Theme'), 'IDLE Dark') 626 # Leftover 'name', not removed, is ignored. 627 usermain['Theme']['name'] = 'IDLE New' 628 self.assertEqual(self.colorkeys('Theme'), 'IDLE Dark') 629 # Erroneous non-default new builtin reverts to default. 630 usermain['Theme']['name2'] = 'non-existent' 631 self.assertEqual(self.colorkeys('Theme'), self.default_theme) 632 usermain.remove_section('Theme') 633 634 def test_user_override_theme(self): 635 # Erroneous custom name (no definition) reverts to default. 636 usermain.read_string(''' 637 [Theme] 638 default = False 639 name = Custom Dark 640 ''') 641 self.assertEqual(self.colorkeys('Theme'), self.default_theme) 642 # Custom name is valid with matching Section name. 643 userhigh.read_string('[Custom Dark]\na=b') 644 self.assertEqual(self.colorkeys('Theme'), 'Custom Dark') 645 # Name2 is ignored. 646 usermain['Theme']['name2'] = 'non-existent' 647 self.assertEqual(self.colorkeys('Theme'), 'Custom Dark') 648 usermain.remove_section('Theme') 649 userhigh.remove_section('Custom Dark') 650 651 def test_old_builtin_keys(self): 652 # On initial installation, user main is blank. 653 self.assertEqual(self.colorkeys('Keys'), self.default_keys) 654 # For old default, name2 must be blank, name is always used. 655 usermain.read_string(''' 656 [Keys] 657 default = True 658 name = IDLE Classic Unix 659 ''') 660 self.assertEqual(self.colorkeys('Keys'), 'IDLE Classic Unix') 661 # Erroneous non-default old builtin reverts to default. 662 usermain['Keys']['name'] = 'non-existent' 663 self.assertEqual(self.colorkeys('Keys'), self.default_keys) 664 usermain.remove_section('Keys') 665 666 def test_new_builtin_keys(self): 667 # IDLE writes name2 for new builtins. 668 usermain.read_string(''' 669 [Keys] 670 default = True 671 name2 = IDLE Modern Unix 672 ''') 673 self.assertEqual(self.colorkeys('Keys'), 'IDLE Modern Unix') 674 # Leftover 'name', not removed, is ignored. 675 usermain['Keys']['name'] = 'IDLE Classic Unix' 676 self.assertEqual(self.colorkeys('Keys'), 'IDLE Modern Unix') 677 # Erroneous non-default new builtin reverts to default. 678 usermain['Keys']['name2'] = 'non-existent' 679 self.assertEqual(self.colorkeys('Keys'), self.default_keys) 680 usermain.remove_section('Keys') 681 682 def test_user_override_keys(self): 683 # Erroneous custom name (no definition) reverts to default. 684 usermain.read_string(''' 685 [Keys] 686 default = False 687 name = Custom Keys 688 ''') 689 self.assertEqual(self.colorkeys('Keys'), self.default_keys) 690 # Custom name is valid with matching Section name. 691 userkeys.read_string('[Custom Keys]\na=b') 692 self.assertEqual(self.colorkeys('Keys'), 'Custom Keys') 693 # Name2 is ignored. 694 usermain['Keys']['name2'] = 'non-existent' 695 self.assertEqual(self.colorkeys('Keys'), 'Custom Keys') 696 usermain.remove_section('Keys') 697 userkeys.remove_section('Custom Keys') 698 699 700class ChangesTest(unittest.TestCase): 701 702 empty = {'main':{}, 'highlight':{}, 'keys':{}, 'extensions':{}} 703 704 def load(self): # Test_add_option verifies that this works. 705 changes = self.changes 706 changes.add_option('main', 'Msec', 'mitem', 'mval') 707 changes.add_option('highlight', 'Hsec', 'hitem', 'hval') 708 changes.add_option('keys', 'Ksec', 'kitem', 'kval') 709 return changes 710 711 loaded = {'main': {'Msec': {'mitem': 'mval'}}, 712 'highlight': {'Hsec': {'hitem': 'hval'}}, 713 'keys': {'Ksec': {'kitem':'kval'}}, 714 'extensions': {}} 715 716 def setUp(self): 717 self.changes = config.ConfigChanges() 718 719 def test_init(self): 720 self.assertEqual(self.changes, self.empty) 721 722 def test_add_option(self): 723 changes = self.load() 724 self.assertEqual(changes, self.loaded) 725 changes.add_option('main', 'Msec', 'mitem', 'mval') 726 self.assertEqual(changes, self.loaded) 727 728 def test_save_option(self): # Static function does not touch changes. 729 save_option = self.changes.save_option 730 self.assertTrue(save_option('main', 'Indent', 'what', '0')) 731 self.assertFalse(save_option('main', 'Indent', 'what', '0')) 732 self.assertEqual(usermain['Indent']['what'], '0') 733 734 self.assertTrue(save_option('main', 'Indent', 'use-spaces', '0')) 735 self.assertEqual(usermain['Indent']['use-spaces'], '0') 736 self.assertTrue(save_option('main', 'Indent', 'use-spaces', '1')) 737 self.assertFalse(usermain.has_option('Indent', 'use-spaces')) 738 usermain.remove_section('Indent') 739 740 def test_save_added(self): 741 changes = self.load() 742 self.assertTrue(changes.save_all()) 743 self.assertEqual(usermain['Msec']['mitem'], 'mval') 744 self.assertEqual(userhigh['Hsec']['hitem'], 'hval') 745 self.assertEqual(userkeys['Ksec']['kitem'], 'kval') 746 changes.add_option('main', 'Msec', 'mitem', 'mval') 747 self.assertFalse(changes.save_all()) 748 usermain.remove_section('Msec') 749 userhigh.remove_section('Hsec') 750 userkeys.remove_section('Ksec') 751 752 def test_save_help(self): 753 # Any change to HelpFiles overwrites entire section. 754 changes = self.changes 755 changes.save_option('main', 'HelpFiles', 'IDLE', 'idledoc') 756 changes.add_option('main', 'HelpFiles', 'ELDI', 'codeldi') 757 changes.save_all() 758 self.assertFalse(usermain.has_option('HelpFiles', 'IDLE')) 759 self.assertTrue(usermain.has_option('HelpFiles', 'ELDI')) 760 761 def test_save_default(self): # Cover 2nd and 3rd false branches. 762 changes = self.changes 763 changes.add_option('main', 'Indent', 'use-spaces', '1') 764 # save_option returns False; cfg_type_changed remains False. 765 766 # TODO: test that save_all calls usercfg Saves. 767 768 def test_delete_section(self): 769 changes = self.load() 770 changes.delete_section('main', 'fake') # Test no exception. 771 self.assertEqual(changes, self.loaded) # Test nothing deleted. 772 for cfgtype, section in (('main', 'Msec'), ('keys', 'Ksec')): 773 testcfg[cfgtype].SetOption(section, 'name', 'value') 774 changes.delete_section(cfgtype, section) 775 with self.assertRaises(KeyError): 776 changes[cfgtype][section] # Test section gone from changes 777 testcfg[cfgtype][section] # and from mock userCfg. 778 # TODO test for save call. 779 780 def test_clear(self): 781 changes = self.load() 782 changes.clear() 783 self.assertEqual(changes, self.empty) 784 785 786class WarningTest(unittest.TestCase): 787 788 def test_warn(self): 789 Equal = self.assertEqual 790 config._warned = set() 791 with captured_stderr() as stderr: 792 config._warn('warning', 'key') 793 Equal(config._warned, {('warning','key')}) 794 Equal(stderr.getvalue(), 'warning'+'\n') 795 with captured_stderr() as stderr: 796 config._warn('warning', 'key') 797 Equal(stderr.getvalue(), '') 798 with captured_stderr() as stderr: 799 config._warn('warn2', 'yek') 800 Equal(config._warned, {('warning','key'), ('warn2','yek')}) 801 Equal(stderr.getvalue(), 'warn2'+'\n') 802 803 804if __name__ == '__main__': 805 unittest.main(verbosity=2) 806