summaryrefslogtreecommitdiff
path: root/Lib/idlelib/idle_test/test_config.py
blob: 53665cd761af7592ddba6803d73d34835ae4e1ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
'''Test idlelib.config.

Much is tested by opening config dialog live or in test_configdialog.
Coverage: 27%
'''
from sys import modules
from test.support import captured_stderr
from tkinter import Tk
import unittest
from idlelib import config

# Tests should not depend on fortuitous user configurations.
# They must not affect actual user .cfg files.
# Replace user parsers with empty parsers that cannot be saved.

idleConf = config.idleConf
usercfg = idleConf.userCfg
testcfg = {}
usermain = testcfg['main'] = config.IdleUserConfParser('')  # filename
userhigh = testcfg['highlight'] = config.IdleUserConfParser('')
userkeys = testcfg['keys'] = config.IdleUserConfParser('')

def setUpModule():
    idleConf.userCfg = testcfg

def tearDownModule():
    idleConf.userCfg = usercfg


class CurrentColorKeysTest(unittest.TestCase):
    """Test correct scenarios for colorkeys and wrap functions.

        The 5 correct patterns are possible results of config dialog.
    """
    colorkeys = idleConf.current_colors_and_keys

    def test_old_default(self):
        # name2 must be blank
        usermain.read_string('''
            [Theme]
            default= 1
            ''')
        self.assertEqual(self.colorkeys('Theme'), 'IDLE Classic')
        usermain['Theme']['name'] = 'IDLE New'
        self.assertEqual(self.colorkeys('Theme'), 'IDLE New')
        usermain['Theme']['name'] = 'non-default'  # error
        self.assertEqual(self.colorkeys('Theme'), 'IDLE Classic')
        usermain.remove_section('Theme')

    def test_new_default(self):
        # name2 overrides name
        usermain.read_string('''
            [Theme]
            default= 1
            name= IDLE New
            name2= IDLE Dark
            ''')
        self.assertEqual(self.colorkeys('Theme'), 'IDLE Dark')
        usermain['Theme']['name2'] = 'non-default'  # error
        self.assertEqual(self.colorkeys('Theme'), 'IDLE Classic')
        usermain.remove_section('Theme')

    def test_user_override(self):
        # name2 does not matter
        usermain.read_string('''
            [Theme]
            default= 0
            name= Custom Dark
            ''')  # error until set userhigh
        self.assertEqual(self.colorkeys('Theme'), 'IDLE Classic')
        userhigh.read_string('[Custom Dark]\na=b')
        self.assertEqual(self.colorkeys('Theme'), 'Custom Dark')
        usermain['Theme']['name2'] = 'IDLE Dark'
        self.assertEqual(self.colorkeys('Theme'), 'Custom Dark')
        usermain.remove_section('Theme')
        userhigh.remove_section('Custom Dark')


class WarningTest(unittest.TestCase):

    def test_warn(self):
        Equal = self.assertEqual
        config._warned = set()
        with captured_stderr() as stderr:
            config._warn('warning', 'key')
        Equal(config._warned, {('warning','key')})
        Equal(stderr.getvalue(), 'warning'+'\n')
        with captured_stderr() as stderr:
            config._warn('warning', 'key')
        Equal(stderr.getvalue(), '')
        with captured_stderr() as stderr:
            config._warn('warn2', 'yek')
        Equal(config._warned, {('warning','key'), ('warn2','yek')})
        Equal(stderr.getvalue(), 'warn2'+'\n')


if __name__ == '__main__':
    unittest.main(verbosity=2)