blob: f0228b26d860ac474f5ad4bf5721a973bffacacf [file] [log] [blame]
Fred Drake8ef67672000-09-27 22:45:25 +00001import ConfigParser
2import StringIO
3
Fred Drake95b96d32001-02-12 17:23:20 +00004from test_support import TestFailed, verify
Fred Drake3d5f7e82000-12-04 16:30:40 +00005
6
Fred Drake8ef67672000-09-27 22:45:25 +00007def basic(src):
Fred Drake8ef67672000-09-27 22:45:25 +00008 print "Testing basic accessors..."
9 cf = ConfigParser.ConfigParser()
10 sio = StringIO.StringIO(src)
11 cf.readfp(sio)
12 L = cf.sections()
13 L.sort()
Fred Drake95b96d32001-02-12 17:23:20 +000014 verify(L == ['Commented Bar', 'Foo Bar',
15 'Internationalized Stuff', 'Spacey Bar'],
16 "unexpected list of section names")
Fred Drake8ef67672000-09-27 22:45:25 +000017
18 # The use of spaces in the section names serves as a regression test for
19 # SourceForge bug #115357.
Fred Drake8ef67672000-09-27 22:45:25 +000020 # http://sourceforge.net/bugs/?func=detailbug&group_id=5470&bug_id=115357
Fred Drake95b96d32001-02-12 17:23:20 +000021 verify(cf.get('Foo Bar', 'foo', raw=1) == 'bar')
22 verify(cf.get('Spacey Bar', 'foo', raw=1) == 'bar')
23 verify(cf.get('Commented Bar', 'foo', raw=1) == 'bar')
Fred Drake8ef67672000-09-27 22:45:25 +000024
Fred Drake95b96d32001-02-12 17:23:20 +000025 verify('__name__' not in cf.options("Foo Bar"),
26 '__name__ "option" should not be exposed by the API!')
Fred Drake8ef67672000-09-27 22:45:25 +000027
Fred Drake3d5f7e82000-12-04 16:30:40 +000028 # Make sure the right things happen for remove_option();
29 # added to include check for SourceForge bug #123324:
Fred Drake95b96d32001-02-12 17:23:20 +000030 verify(cf.remove_option('Foo Bar', 'foo'),
31 "remove_option() failed to report existance of option")
32 verify(not cf.has_option('Foo Bar', 'foo'),
33 "remove_option() failed to remove option")
34 verify(not cf.remove_option('Foo Bar', 'foo'),
35 "remove_option() failed to report non-existance of option"
36 " that was removed")
Fred Drake3d5f7e82000-12-04 16:30:40 +000037 try:
38 cf.remove_option('No Such Section', 'foo')
39 except ConfigParser.NoSectionError:
40 pass
41 else:
42 raise TestFailed(
43 "remove_option() failed to report non-existance of option"
44 " that never existed")
45
46
Fred Drake8ef67672000-09-27 22:45:25 +000047def interpolation(src):
Fred Drake8ef67672000-09-27 22:45:25 +000048 print "Testing value interpolation..."
49 cf = ConfigParser.ConfigParser({"getname": "%(__name__)s"})
50 sio = StringIO.StringIO(src)
51 cf.readfp(sio)
Fred Drake95b96d32001-02-12 17:23:20 +000052 verify(cf.get("Foo", "getname") == "Foo")
53 verify(cf.get("Foo", "bar") == "something with interpolation (1 step)")
54 verify(cf.get("Foo", "bar9")
55 == "something with lots of interpolation (9 steps)")
56 verify(cf.get("Foo", "bar10")
57 == "something with lots of interpolation (10 steps)")
Fred Drake8ef67672000-09-27 22:45:25 +000058 expect_get_error(cf, ConfigParser.InterpolationDepthError, "Foo", "bar11")
59
Fred Drake95b96d32001-02-12 17:23:20 +000060
Fred Drake8ef67672000-09-27 22:45:25 +000061def parse_errors():
Fred Drake95b96d32001-02-12 17:23:20 +000062 print "Testing parse errors..."
Fred Drake8ef67672000-09-27 22:45:25 +000063 expect_parse_error(ConfigParser.ParsingError,
64 """[Foo]\n extra-spaces: splat\n""")
65 expect_parse_error(ConfigParser.ParsingError,
66 """[Foo]\n extra-spaces= splat\n""")
67 expect_parse_error(ConfigParser.ParsingError,
68 """[Foo]\noption-without-value\n""")
69 expect_parse_error(ConfigParser.ParsingError,
70 """[Foo]\n:value-without-option-name\n""")
71 expect_parse_error(ConfigParser.ParsingError,
72 """[Foo]\n=value-without-option-name\n""")
73 expect_parse_error(ConfigParser.MissingSectionHeaderError,
74 """No Section!\n""")
75
Fred Drake95b96d32001-02-12 17:23:20 +000076
Fred Drake8ef67672000-09-27 22:45:25 +000077def query_errors():
Fred Drake8ef67672000-09-27 22:45:25 +000078 print "Testing query interface..."
79 cf = ConfigParser.ConfigParser()
Fred Drake95b96d32001-02-12 17:23:20 +000080 verify(cf.sections() == [],
81 "new ConfigParser should have no defined sections")
82 verify(not cf.has_section("Foo"),
83 "new ConfigParser should have no acknowledged sections")
Fred Drake8ef67672000-09-27 22:45:25 +000084 try:
85 cf.options("Foo")
86 except ConfigParser.NoSectionError, e:
Fred Drake95b96d32001-02-12 17:23:20 +000087 pass
Fred Drake8ef67672000-09-27 22:45:25 +000088 else:
Fred Drake95b96d32001-02-12 17:23:20 +000089 raise TestFailed(
90 "Failed to catch expected NoSectionError from options()")
Fred Drake8ef67672000-09-27 22:45:25 +000091 try:
92 cf.set("foo", "bar", "value")
93 except ConfigParser.NoSectionError, e:
Fred Drake95b96d32001-02-12 17:23:20 +000094 pass
Fred Drake8ef67672000-09-27 22:45:25 +000095 else:
Fred Drake95b96d32001-02-12 17:23:20 +000096 raise TestFailed("Failed to catch expected NoSectionError from set()")
Fred Drake8ef67672000-09-27 22:45:25 +000097 expect_get_error(cf, ConfigParser.NoSectionError, "foo", "bar")
98 cf.add_section("foo")
99 expect_get_error(cf, ConfigParser.NoOptionError, "foo", "bar")
100
Fred Drake95b96d32001-02-12 17:23:20 +0000101
Fred Drake8ef67672000-09-27 22:45:25 +0000102def weird_errors():
Fred Drake8ef67672000-09-27 22:45:25 +0000103 print "Testing miscellaneous error conditions..."
104 cf = ConfigParser.ConfigParser()
105 cf.add_section("Foo")
106 try:
107 cf.add_section("Foo")
108 except ConfigParser.DuplicateSectionError, e:
Fred Drake95b96d32001-02-12 17:23:20 +0000109 pass
Fred Drake8ef67672000-09-27 22:45:25 +0000110 else:
Fred Drake95b96d32001-02-12 17:23:20 +0000111 raise TestFailed("Failed to catch expected DuplicateSectionError")
112
Fred Drake8ef67672000-09-27 22:45:25 +0000113
114def expect_get_error(cf, exctype, section, option, raw=0):
115 try:
116 cf.get(section, option, raw=raw)
117 except exctype, e:
Fred Drake95b96d32001-02-12 17:23:20 +0000118 pass
Fred Drake8ef67672000-09-27 22:45:25 +0000119 else:
Fred Drake95b96d32001-02-12 17:23:20 +0000120 raise TestFailed("Failed to catch expected " + exctype.__name__)
121
Fred Drake8ef67672000-09-27 22:45:25 +0000122
123def expect_parse_error(exctype, src):
124 cf = ConfigParser.ConfigParser()
125 sio = StringIO.StringIO(src)
126 try:
127 cf.readfp(sio)
128 except exctype, e:
Fred Drake95b96d32001-02-12 17:23:20 +0000129 pass
Fred Drake8ef67672000-09-27 22:45:25 +0000130 else:
Fred Drake95b96d32001-02-12 17:23:20 +0000131 raise TestFailed("Failed to catch expected " + exctype.__name__)
132
Fred Drake8ef67672000-09-27 22:45:25 +0000133
134basic(r"""
135[Foo Bar]
136foo=bar
137[Spacey Bar]
Fred Drake004d5e62000-10-23 17:22:08 +0000138foo = bar
Fred Drake8ef67672000-09-27 22:45:25 +0000139[Commented Bar]
140foo: bar ; comment
Fred Drake95b96d32001-02-12 17:23:20 +0000141[Internationalized Stuff]
142foo[bg]: Bulgarian
143foo=Default
144foo[en]=English
145foo[de]=Deutsch
Fred Drake8ef67672000-09-27 22:45:25 +0000146""")
147interpolation(r"""
148[Foo]
149bar=something %(with1)s interpolation (1 step)
150bar9=something %(with9)s lots of interpolation (9 steps)
151bar10=something %(with10)s lots of interpolation (10 steps)
152bar11=something %(with11)s lots of interpolation (11 steps)
153with11=%(with10)s
154with10=%(with9)s
155with9=%(with8)s
156with8=%(with7)s
157with7=%(with6)s
158with6=%(with5)s
159with5=%(with4)s
160with4=%(with3)s
161with3=%(with2)s
162with2=%(with1)s
163with1=with
164
165[Mutual Recursion]
166foo=%(bar)s
167bar=%(foo)s
168""")
169parse_errors()
170query_errors()
171weird_errors()