blob: 62395a0c2fb943cc5ae9ad21025786aee396c60f [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 Drakecc1f9512001-02-14 15:30:31 +000014 verify(L == [r'Commented Bar',
15 r'Foo Bar',
16 r'Internationalized Stuff',
17 r'Section\with$weird%characters[' '\t',
18 r'Spacey Bar',
19 ],
Fred Drake95b96d32001-02-12 17:23:20 +000020 "unexpected list of section names")
Fred Drake8ef67672000-09-27 22:45:25 +000021
22 # The use of spaces in the section names serves as a regression test for
23 # SourceForge bug #115357.
Fred Drake8ef67672000-09-27 22:45:25 +000024 # http://sourceforge.net/bugs/?func=detailbug&group_id=5470&bug_id=115357
Fred Drake95b96d32001-02-12 17:23:20 +000025 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')
Fred Drake8ef67672000-09-27 22:45:25 +000028
Fred Drake95b96d32001-02-12 17:23:20 +000029 verify('__name__' not in cf.options("Foo Bar"),
30 '__name__ "option" should not be exposed by the API!')
Fred Drake8ef67672000-09-27 22:45:25 +000031
Fred Drake3d5f7e82000-12-04 16:30:40 +000032 # Make sure the right things happen for remove_option();
33 # added to include check for SourceForge bug #123324:
Fred Drake95b96d32001-02-12 17:23:20 +000034 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")
Fred Drake3d5f7e82000-12-04 16:30:40 +000041 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")
49
50
Fred Drake3c823aa2001-02-26 21:55:34 +000051def 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") == [])
72
73
Fred Drake8ef67672000-09-27 22:45:25 +000074def interpolation(src):
Fred Drake8ef67672000-09-27 22:45:25 +000075 print "Testing value interpolation..."
76 cf = ConfigParser.ConfigParser({"getname": "%(__name__)s"})
77 sio = StringIO.StringIO(src)
78 cf.readfp(sio)
Fred Drake95b96d32001-02-12 17:23:20 +000079 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)")
Fred Drake8ef67672000-09-27 22:45:25 +000085 expect_get_error(cf, ConfigParser.InterpolationDepthError, "Foo", "bar11")
86
Fred Drake95b96d32001-02-12 17:23:20 +000087
Fred Drake8ef67672000-09-27 22:45:25 +000088def parse_errors():
Fred Drake95b96d32001-02-12 17:23:20 +000089 print "Testing parse errors..."
Fred Drake8ef67672000-09-27 22:45:25 +000090 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""")
102
Fred Drake95b96d32001-02-12 17:23:20 +0000103
Fred Drake8ef67672000-09-27 22:45:25 +0000104def query_errors():
Fred Drake8ef67672000-09-27 22:45:25 +0000105 print "Testing query interface..."
106 cf = ConfigParser.ConfigParser()
Fred Drake95b96d32001-02-12 17:23:20 +0000107 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")
Fred Drake8ef67672000-09-27 22:45:25 +0000111 try:
112 cf.options("Foo")
113 except ConfigParser.NoSectionError, e:
Fred Drake95b96d32001-02-12 17:23:20 +0000114 pass
Fred Drake8ef67672000-09-27 22:45:25 +0000115 else:
Fred Drake95b96d32001-02-12 17:23:20 +0000116 raise TestFailed(
117 "Failed to catch expected NoSectionError from options()")
Fred Drake8ef67672000-09-27 22:45:25 +0000118 try:
119 cf.set("foo", "bar", "value")
120 except ConfigParser.NoSectionError, e:
Fred Drake95b96d32001-02-12 17:23:20 +0000121 pass
Fred Drake8ef67672000-09-27 22:45:25 +0000122 else:
Fred Drake95b96d32001-02-12 17:23:20 +0000123 raise TestFailed("Failed to catch expected NoSectionError from set()")
Fred Drake8ef67672000-09-27 22:45:25 +0000124 expect_get_error(cf, ConfigParser.NoSectionError, "foo", "bar")
125 cf.add_section("foo")
126 expect_get_error(cf, ConfigParser.NoOptionError, "foo", "bar")
127
Fred Drake95b96d32001-02-12 17:23:20 +0000128
Fred Drake8ef67672000-09-27 22:45:25 +0000129def weird_errors():
Fred Drake8ef67672000-09-27 22:45:25 +0000130 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:
Fred Drake95b96d32001-02-12 17:23:20 +0000136 pass
Fred Drake8ef67672000-09-27 22:45:25 +0000137 else:
Fred Drake95b96d32001-02-12 17:23:20 +0000138 raise TestFailed("Failed to catch expected DuplicateSectionError")
139
Fred Drake8ef67672000-09-27 22:45:25 +0000140
141def expect_get_error(cf, exctype, section, option, raw=0):
142 try:
143 cf.get(section, option, raw=raw)
144 except exctype, e:
Fred Drake95b96d32001-02-12 17:23:20 +0000145 pass
Fred Drake8ef67672000-09-27 22:45:25 +0000146 else:
Fred Drake95b96d32001-02-12 17:23:20 +0000147 raise TestFailed("Failed to catch expected " + exctype.__name__)
148
Fred Drake8ef67672000-09-27 22:45:25 +0000149
150def expect_parse_error(exctype, src):
151 cf = ConfigParser.ConfigParser()
152 sio = StringIO.StringIO(src)
153 try:
154 cf.readfp(sio)
155 except exctype, e:
Fred Drake95b96d32001-02-12 17:23:20 +0000156 pass
Fred Drake8ef67672000-09-27 22:45:25 +0000157 else:
Fred Drake95b96d32001-02-12 17:23:20 +0000158 raise TestFailed("Failed to catch expected " + exctype.__name__)
159
Fred Drake8ef67672000-09-27 22:45:25 +0000160
161basic(r"""
162[Foo Bar]
163foo=bar
164[Spacey Bar]
Fred Drake004d5e62000-10-23 17:22:08 +0000165foo = bar
Fred Drake8ef67672000-09-27 22:45:25 +0000166[Commented Bar]
167foo: bar ; comment
Fred Drakecc1f9512001-02-14 15:30:31 +0000168[Section\with$weird%characters[""" '\t' r"""]
Fred Drake95b96d32001-02-12 17:23:20 +0000169[Internationalized Stuff]
170foo[bg]: Bulgarian
171foo=Default
172foo[en]=English
173foo[de]=Deutsch
Fred Drake8ef67672000-09-27 22:45:25 +0000174""")
Fred Drake3c823aa2001-02-26 21:55:34 +0000175case_sensitivity()
Fred Drake8ef67672000-09-27 22:45:25 +0000176interpolation(r"""
177[Foo]
178bar=something %(with1)s interpolation (1 step)
179bar9=something %(with9)s lots of interpolation (9 steps)
180bar10=something %(with10)s lots of interpolation (10 steps)
181bar11=something %(with11)s lots of interpolation (11 steps)
182with11=%(with10)s
183with10=%(with9)s
184with9=%(with8)s
185with8=%(with7)s
186with7=%(with6)s
187with6=%(with5)s
188with5=%(with4)s
189with4=%(with3)s
190with3=%(with2)s
191with2=%(with1)s
192with1=with
193
194[Mutual Recursion]
195foo=%(bar)s
196bar=%(foo)s
197""")
198parse_errors()
199query_errors()
200weird_errors()