3
3
Much is tested by opening config dialog live or in test_configdialog.
4
4
Coverage: 27%
5
5
'''
6
+ import os
7
+ import tempfile
8
+ from test import support
6
9
from test .support import captured_stderr
7
10
import unittest
8
11
from idlelib import config
@@ -26,6 +29,154 @@ def tearDownModule():
26
29
idleConf .userCfg = usercfg
27
30
28
31
32
+ class IdleConfParserTest (unittest .TestCase ):
33
+ """Test IdleConfParser works"""
34
+
35
+ config = """
36
+ [one]
37
+ one = false
38
+ two = true
39
+ three = 10
40
+
41
+ [two]
42
+ one = a string
43
+ two = true
44
+ three = false
45
+ """
46
+
47
+ def test_get (self ):
48
+ parser = config .IdleConfParser ('' )
49
+ parser .read_string (self .config )
50
+
51
+ self .assertEqual (parser .Get ('one' , 'one' , type = 'bool' ), False )
52
+ self .assertEqual (parser .Get ('one' , 'two' , type = 'bool' ), True )
53
+ self .assertEqual (parser .Get ('one' , 'three' , type = 'int' ), 10 )
54
+ self .assertEqual (parser .Get ('two' , 'one' ), 'a string' )
55
+ self .assertEqual (parser .Get ('two' , 'two' , type = 'bool' ), True )
56
+ self .assertEqual (parser .Get ('two' , 'three' , type = 'bool' ), False )
57
+ self .assertEqual (parser .Get ('two' , 'two' ), 'true' )
58
+ self .assertEqual (parser .Get ('two' , 'three' ), 'false' )
59
+ self .assertEqual (parser .Get ('not' , 'exist' ), None )
60
+ self .assertEqual (parser .Get ('not' , 'exist' , default = 'DEFAULT' ), 'DEFAULT' )
61
+
62
+ def test_get_option_list (self ):
63
+ parser = config .IdleConfParser ('' )
64
+ parser .read_string (self .config )
65
+
66
+ self .assertEqual (parser .GetOptionList ('one' ), ['one' , 'two' , 'three' ])
67
+ self .assertEqual (parser .GetOptionList ('two' ), ['one' , 'two' , 'three' ])
68
+ self .assertEqual (parser .GetOptionList ('not exist' ), [])
69
+
70
+ def test_load_file (self ):
71
+ config_path = support .findfile ('cfgparser.1' )
72
+ parser = config .IdleConfParser (config_path )
73
+ parser .Load ()
74
+
75
+ self .assertEqual (parser .Get ('Foo Bar' , 'foo' ), 'newbar' )
76
+ self .assertEqual (parser .GetOptionList ('Foo Bar' ), ['foo' ])
77
+
78
+
79
+ class IdleUserConfParserTest (unittest .TestCase ):
80
+ """Test IdleUserConfParser works"""
81
+
82
+ def new_parser (self , path = '' ):
83
+ return config .IdleUserConfParser (path )
84
+
85
+ def test_add_section (self ):
86
+ parser = self .new_parser ()
87
+ self .assertEqual (parser .sections (), [])
88
+
89
+ # Duplicate section should only add one time.
90
+ # In normal configparser, it will raise DuplicateError,
91
+ # IdleParser won't raise it
92
+ parser .AddSection ('Foo' )
93
+ parser .AddSection ('Foo' )
94
+ parser .AddSection ('Bar' )
95
+ s = parser .sections ()
96
+ s .sort ()
97
+ self .assertEqual (s , ['Bar' , 'Foo' ])
98
+
99
+ def test_remove_empty_sections (self ):
100
+ parser = self .new_parser ()
101
+
102
+ parser .AddSection ('Foo' )
103
+ parser .AddSection ('Bar' )
104
+ self .assertEqual (sorted (parser .sections ()), ['Bar' , 'Foo' ])
105
+ parser .RemoveEmptySections ()
106
+ self .assertEqual (parser .sections (), [])
107
+
108
+ def test_is_empty (self ):
109
+ parser = self .new_parser ()
110
+
111
+ parser .AddSection ('Foo' )
112
+ parser .AddSection ('Bar' )
113
+ self .assertEqual (parser .IsEmpty (), True )
114
+ self .assertEqual (parser .sections (), [])
115
+
116
+ parser .AddSection ('Foo' )
117
+ parser .AddSection ('Bar' )
118
+ parser .SetOption ('Foo' , 'bar' , 'false' )
119
+ self .assertEqual (parser .IsEmpty (), False )
120
+ self .assertEqual (parser .sections (), ['Foo' ])
121
+
122
+ def test_set_options (self ):
123
+ parser = self .new_parser ()
124
+
125
+ parser .AddSection ('Foo' )
126
+
127
+ # Set option success should return True
128
+ self .assertEqual (parser .SetOption ('Foo' , 'bar' , 'true' ), True )
129
+
130
+ # Set duplicate option (with same value) should return False
131
+ self .assertEqual (parser .SetOption ('Foo' , 'bar' , 'true' ), False )
132
+
133
+ # Set option and change value should return True
134
+ self .assertEqual (parser .SetOption ('Foo' , 'bar' , 'false' ), True )
135
+
136
+ # Set option to not exist section should create section and return True
137
+ self .assertEqual (parser .SetOption ('Bar' , 'bar' , 'true' ), True )
138
+ self .assertEqual (sorted (parser .sections ()), ['Bar' , 'Foo' ])
139
+
140
+ def test_remove_options (self ):
141
+ parser = self .new_parser ()
142
+
143
+ parser .AddSection ('Foo' )
144
+ parser .SetOption ('Foo' , 'bar' , 'true' )
145
+
146
+ self .assertEqual (parser .RemoveOption ('Foo' , 'bar' ), True )
147
+ self .assertEqual (parser .RemoveOption ('Foo' , 'bar' ), False )
148
+ self .assertEqual (parser .RemoveOption ('Not' , 'Exist' ), False )
149
+
150
+ def test_remove_file (self ):
151
+ with tempfile .TemporaryDirectory () as tdir :
152
+ path = os .path .join (tdir , 'test.cfg' )
153
+ parser = self .new_parser (path )
154
+ parser .AddSection ('Foo' )
155
+ parser .SetOption ('Foo' , 'bar' , 'true' )
156
+
157
+ parser .Save ()
158
+ self .assertTrue (os .path .exists (path ))
159
+ parser .RemoveFile ()
160
+ self .assertFalse (os .path .exists (path ))
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 when config is empty
175
+ parser .remove_section ('Foo' )
176
+ parser .Save ()
177
+ self .assertFalse (os .path .exists (path ))
178
+
179
+
29
180
class CurrentColorKeysTest (unittest .TestCase ):
30
181
""" Test colorkeys function with user config [Theme] and [Keys] patterns.
31
182
0 commit comments