- Got rid of newmodule.c
[python/dscho.git] / Lib / test / test_cfgparser.py
blob6a6fecbfbb3a697a10b0da58e291c102b149809f
1 import ConfigParser
2 import StringIO
4 from test_support import TestFailed, verify
7 def basic(src):
8 print "Testing basic accessors..."
9 cf = ConfigParser.ConfigParser()
10 sio = StringIO.StringIO(src)
11 cf.readfp(sio)
12 L = cf.sections()
13 L.sort()
14 verify(L == [r'Commented Bar',
15 r'Foo Bar',
16 r'Internationalized Stuff',
17 r'Long Line',
18 r'Section\with$weird%characters[' '\t',
19 r'Spacey Bar',
21 "unexpected list of section names")
23 # The use of spaces in the section names serves as a regression test for
24 # SourceForge bug #115357.
25 # http://sourceforge.net/bugs/?func=detailbug&group_id=5470&bug_id=115357
26 verify(cf.get('Foo Bar', 'foo', raw=1) == 'bar')
27 verify(cf.get('Spacey Bar', 'foo', raw=1) == 'bar')
28 verify(cf.get('Commented Bar', 'foo', raw=1) == 'bar')
30 verify('__name__' not in cf.options("Foo Bar"),
31 '__name__ "option" should not be exposed by the API!')
33 # Make sure the right things happen for remove_option();
34 # added to include check for SourceForge bug #123324:
35 verify(cf.remove_option('Foo Bar', 'foo'),
36 "remove_option() failed to report existance of option")
37 verify(not cf.has_option('Foo Bar', 'foo'),
38 "remove_option() failed to remove option")
39 verify(not cf.remove_option('Foo Bar', 'foo'),
40 "remove_option() failed to report non-existance of option"
41 " that was removed")
42 try:
43 cf.remove_option('No Such Section', 'foo')
44 except ConfigParser.NoSectionError:
45 pass
46 else:
47 raise TestFailed(
48 "remove_option() failed to report non-existance of option"
49 " that never existed")
51 verify(cf.get('Long Line', 'foo', raw=1) ==
52 'this line is much, much longer than my editor\nlikes it.')
55 def write(src):
56 print "Testing writing of files..."
57 cf = ConfigParser.ConfigParser()
58 sio = StringIO.StringIO(src)
59 cf.readfp(sio)
60 output = StringIO.StringIO()
61 cf.write(output)
62 verify(output, """[DEFAULT]
63 foo = another very
64 long line
66 [Long Line]
67 foo = this line is much, much longer than my editor
68 likes it.
69 """)
71 def case_sensitivity():
72 print "Testing case sensitivity..."
73 cf = ConfigParser.ConfigParser()
74 cf.add_section("A")
75 cf.add_section("a")
76 L = cf.sections()
77 L.sort()
78 verify(L == ["A", "a"])
79 cf.set("a", "B", "value")
80 verify(cf.options("a") == ["b"])
81 verify(cf.get("a", "b", raw=1) == "value",
82 "could not locate option, expecting case-insensitive option names")
83 verify(cf.has_option("a", "b"))
84 cf.set("A", "A-B", "A-B value")
85 for opt in ("a-b", "A-b", "a-B", "A-B"):
86 verify(cf.has_option("A", opt),
87 "has_option() returned false for option which should exist")
88 verify(cf.options("A") == ["a-b"])
89 verify(cf.options("a") == ["b"])
90 cf.remove_option("a", "B")
91 verify(cf.options("a") == [])
93 # SF bug #432369:
94 cf = ConfigParser.ConfigParser()
95 sio = StringIO.StringIO("[MySection]\nOption: first line\n\tsecond line\n")
96 cf.readfp(sio)
97 verify(cf.options("MySection") == ["option"])
98 verify(cf.get("MySection", "Option") == "first line\nsecond line")
101 def boolean(src):
102 print "Testing interpretation of boolean Values..."
103 cf = ConfigParser.ConfigParser()
104 sio = StringIO.StringIO(src)
105 cf.readfp(sio)
106 for x in range(1, 5):
107 verify(cf.getboolean('BOOLTEST', 't%d' % (x)) == 1)
108 for x in range(1, 5):
109 verify(cf.getboolean('BOOLTEST', 'f%d' % (x)) == 0)
110 for x in range(1, 5):
111 try:
112 cf.getboolean('BOOLTEST', 'e%d' % (x))
113 except ValueError:
114 pass
115 else:
116 raise TestFailed(
117 "getboolean() failed to report a non boolean value")
120 def interpolation(src):
121 print "Testing value interpolation..."
122 cf = ConfigParser.ConfigParser({"getname": "%(__name__)s"})
123 sio = StringIO.StringIO(src)
124 cf.readfp(sio)
125 verify(cf.get("Foo", "getname") == "Foo")
126 verify(cf.get("Foo", "bar") == "something with interpolation (1 step)")
127 verify(cf.get("Foo", "bar9")
128 == "something with lots of interpolation (9 steps)")
129 verify(cf.get("Foo", "bar10")
130 == "something with lots of interpolation (10 steps)")
131 expect_get_error(cf, ConfigParser.InterpolationDepthError, "Foo", "bar11")
134 def parse_errors():
135 print "Testing parse errors..."
136 expect_parse_error(ConfigParser.ParsingError,
137 """[Foo]\n extra-spaces: splat\n""")
138 expect_parse_error(ConfigParser.ParsingError,
139 """[Foo]\n extra-spaces= splat\n""")
140 expect_parse_error(ConfigParser.ParsingError,
141 """[Foo]\noption-without-value\n""")
142 expect_parse_error(ConfigParser.ParsingError,
143 """[Foo]\n:value-without-option-name\n""")
144 expect_parse_error(ConfigParser.ParsingError,
145 """[Foo]\n=value-without-option-name\n""")
146 expect_parse_error(ConfigParser.MissingSectionHeaderError,
147 """No Section!\n""")
150 def query_errors():
151 print "Testing query interface..."
152 cf = ConfigParser.ConfigParser()
153 verify(cf.sections() == [],
154 "new ConfigParser should have no defined sections")
155 verify(not cf.has_section("Foo"),
156 "new ConfigParser should have no acknowledged sections")
157 try:
158 cf.options("Foo")
159 except ConfigParser.NoSectionError, e:
160 pass
161 else:
162 raise TestFailed(
163 "Failed to catch expected NoSectionError from options()")
164 try:
165 cf.set("foo", "bar", "value")
166 except ConfigParser.NoSectionError, e:
167 pass
168 else:
169 raise TestFailed("Failed to catch expected NoSectionError from set()")
170 expect_get_error(cf, ConfigParser.NoSectionError, "foo", "bar")
171 cf.add_section("foo")
172 expect_get_error(cf, ConfigParser.NoOptionError, "foo", "bar")
175 def weird_errors():
176 print "Testing miscellaneous error conditions..."
177 cf = ConfigParser.ConfigParser()
178 cf.add_section("Foo")
179 try:
180 cf.add_section("Foo")
181 except ConfigParser.DuplicateSectionError, e:
182 pass
183 else:
184 raise TestFailed("Failed to catch expected DuplicateSectionError")
187 def expect_get_error(cf, exctype, section, option, raw=0):
188 try:
189 cf.get(section, option, raw=raw)
190 except exctype, e:
191 pass
192 else:
193 raise TestFailed("Failed to catch expected " + exctype.__name__)
196 def expect_parse_error(exctype, src):
197 cf = ConfigParser.ConfigParser()
198 sio = StringIO.StringIO(src)
199 try:
200 cf.readfp(sio)
201 except exctype, e:
202 pass
203 else:
204 raise TestFailed("Failed to catch expected " + exctype.__name__)
207 basic(r"""
208 [Foo Bar]
209 foo=bar
210 [Spacey Bar]
211 foo = bar
212 [Commented Bar]
213 foo: bar ; comment
214 [Long Line]
215 foo: this line is much, much longer than my editor
216 likes it.
217 [Section\with$weird%characters[""" '\t' r"""]
218 [Internationalized Stuff]
219 foo[bg]: Bulgarian
220 foo=Default
221 foo[en]=English
222 foo[de]=Deutsch
223 """)
224 write("""[Long Line]
225 foo: this line is much, much longer than my editor
226 likes it.
227 [DEFAULT]
228 foo: another very
229 long line""")
230 case_sensitivity()
231 boolean(r"""
232 [BOOLTEST]
233 T1=1
234 T2=TRUE
235 T3=True
236 T4=oN
237 T5=yes
238 F1=0
239 F2=FALSE
240 F3=False
241 F4=oFF
242 F5=nO
243 E1=2
244 E2=foo
245 E3=-1
246 E4=0.1
247 E5=FALSE AND MORE
248 """)
249 interpolation(r"""
250 [Foo]
251 bar=something %(with1)s interpolation (1 step)
252 bar9=something %(with9)s lots of interpolation (9 steps)
253 bar10=something %(with10)s lots of interpolation (10 steps)
254 bar11=something %(with11)s lots of interpolation (11 steps)
255 with11=%(with10)s
256 with10=%(with9)s
257 with9=%(with8)s
258 with8=%(with7)s
259 with7=%(with6)s
260 with6=%(with5)s
261 with5=%(with4)s
262 with4=%(with3)s
263 with3=%(with2)s
264 with2=%(with1)s
265 with1=with
267 [Mutual Recursion]
268 foo=%(bar)s
269 bar=%(foo)s
270 """)
271 parse_errors()
272 query_errors()
273 weird_errors()