Null commit with -f option to force an uprev and put HEADs firmly on the trunk.
[python/dscho.git] / Lib / test / test_cfgparser.py
blob62395a0c2fb943cc5ae9ad21025786aee396c60f
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'Section\with$weird%characters[' '\t',
18 r'Spacey Bar',
20 "unexpected list of section names")
22 # The use of spaces in the section names serves as a regression test for
23 # SourceForge bug #115357.
24 # http://sourceforge.net/bugs/?func=detailbug&group_id=5470&bug_id=115357
25 verify(cf.get('Foo Bar', 'foo', raw=1) == 'bar')
26 verify(cf.get('Spacey Bar', 'foo', raw=1) == 'bar')
27 verify(cf.get('Commented Bar', 'foo', raw=1) == 'bar')
29 verify('__name__' not in cf.options("Foo Bar"),
30 '__name__ "option" should not be exposed by the API!')
32 # Make sure the right things happen for remove_option();
33 # added to include check for SourceForge bug #123324:
34 verify(cf.remove_option('Foo Bar', 'foo'),
35 "remove_option() failed to report existance of option")
36 verify(not cf.has_option('Foo Bar', 'foo'),
37 "remove_option() failed to remove option")
38 verify(not cf.remove_option('Foo Bar', 'foo'),
39 "remove_option() failed to report non-existance of option"
40 " that was removed")
41 try:
42 cf.remove_option('No Such Section', 'foo')
43 except ConfigParser.NoSectionError:
44 pass
45 else:
46 raise TestFailed(
47 "remove_option() failed to report non-existance of option"
48 " that never existed")
51 def case_sensitivity():
52 print "Testing case sensitivity..."
53 cf = ConfigParser.ConfigParser()
54 cf.add_section("A")
55 cf.add_section("a")
56 L = cf.sections()
57 L.sort()
58 verify(L == ["A", "a"])
59 cf.set("a", "B", "value")
60 verify(cf.options("a") == ["b"])
61 verify(cf.get("a", "b", raw=1) == "value",
62 "could not locate option, expecting case-insensitive option names")
63 verify(cf.has_option("a", "b"))
64 cf.set("A", "A-B", "A-B value")
65 for opt in ("a-b", "A-b", "a-B", "A-B"):
66 verify(cf.has_option("A", opt),
67 "has_option() returned false for option which should exist")
68 verify(cf.options("A") == ["a-b"])
69 verify(cf.options("a") == ["b"])
70 cf.remove_option("a", "B")
71 verify(cf.options("a") == [])
74 def interpolation(src):
75 print "Testing value interpolation..."
76 cf = ConfigParser.ConfigParser({"getname": "%(__name__)s"})
77 sio = StringIO.StringIO(src)
78 cf.readfp(sio)
79 verify(cf.get("Foo", "getname") == "Foo")
80 verify(cf.get("Foo", "bar") == "something with interpolation (1 step)")
81 verify(cf.get("Foo", "bar9")
82 == "something with lots of interpolation (9 steps)")
83 verify(cf.get("Foo", "bar10")
84 == "something with lots of interpolation (10 steps)")
85 expect_get_error(cf, ConfigParser.InterpolationDepthError, "Foo", "bar11")
88 def parse_errors():
89 print "Testing parse errors..."
90 expect_parse_error(ConfigParser.ParsingError,
91 """[Foo]\n extra-spaces: splat\n""")
92 expect_parse_error(ConfigParser.ParsingError,
93 """[Foo]\n extra-spaces= splat\n""")
94 expect_parse_error(ConfigParser.ParsingError,
95 """[Foo]\noption-without-value\n""")
96 expect_parse_error(ConfigParser.ParsingError,
97 """[Foo]\n:value-without-option-name\n""")
98 expect_parse_error(ConfigParser.ParsingError,
99 """[Foo]\n=value-without-option-name\n""")
100 expect_parse_error(ConfigParser.MissingSectionHeaderError,
101 """No Section!\n""")
104 def query_errors():
105 print "Testing query interface..."
106 cf = ConfigParser.ConfigParser()
107 verify(cf.sections() == [],
108 "new ConfigParser should have no defined sections")
109 verify(not cf.has_section("Foo"),
110 "new ConfigParser should have no acknowledged sections")
111 try:
112 cf.options("Foo")
113 except ConfigParser.NoSectionError, e:
114 pass
115 else:
116 raise TestFailed(
117 "Failed to catch expected NoSectionError from options()")
118 try:
119 cf.set("foo", "bar", "value")
120 except ConfigParser.NoSectionError, e:
121 pass
122 else:
123 raise TestFailed("Failed to catch expected NoSectionError from set()")
124 expect_get_error(cf, ConfigParser.NoSectionError, "foo", "bar")
125 cf.add_section("foo")
126 expect_get_error(cf, ConfigParser.NoOptionError, "foo", "bar")
129 def weird_errors():
130 print "Testing miscellaneous error conditions..."
131 cf = ConfigParser.ConfigParser()
132 cf.add_section("Foo")
133 try:
134 cf.add_section("Foo")
135 except ConfigParser.DuplicateSectionError, e:
136 pass
137 else:
138 raise TestFailed("Failed to catch expected DuplicateSectionError")
141 def expect_get_error(cf, exctype, section, option, raw=0):
142 try:
143 cf.get(section, option, raw=raw)
144 except exctype, e:
145 pass
146 else:
147 raise TestFailed("Failed to catch expected " + exctype.__name__)
150 def expect_parse_error(exctype, src):
151 cf = ConfigParser.ConfigParser()
152 sio = StringIO.StringIO(src)
153 try:
154 cf.readfp(sio)
155 except exctype, e:
156 pass
157 else:
158 raise TestFailed("Failed to catch expected " + exctype.__name__)
161 basic(r"""
162 [Foo Bar]
163 foo=bar
164 [Spacey Bar]
165 foo = bar
166 [Commented Bar]
167 foo: bar ; comment
168 [Section\with$weird%characters[""" '\t' r"""]
169 [Internationalized Stuff]
170 foo[bg]: Bulgarian
171 foo=Default
172 foo[en]=English
173 foo[de]=Deutsch
174 """)
175 case_sensitivity()
176 interpolation(r"""
177 [Foo]
178 bar=something %(with1)s interpolation (1 step)
179 bar9=something %(with9)s lots of interpolation (9 steps)
180 bar10=something %(with10)s lots of interpolation (10 steps)
181 bar11=something %(with11)s lots of interpolation (11 steps)
182 with11=%(with10)s
183 with10=%(with9)s
184 with9=%(with8)s
185 with8=%(with7)s
186 with7=%(with6)s
187 with6=%(with5)s
188 with5=%(with4)s
189 with4=%(with3)s
190 with3=%(with2)s
191 with2=%(with1)s
192 with1=with
194 [Mutual Recursion]
195 foo=%(bar)s
196 bar=%(foo)s
197 """)
198 parse_errors()
199 query_errors()
200 weird_errors()