blob: bd06d3bb857bafd1bc79e9401d3aedb0b29105a1 [file] [log] [blame]
Fred Drake8ef67672000-09-27 22:45:25 +00001import ConfigParser
2import StringIO
3
Fred Drake3d5f7e82000-12-04 16:30:40 +00004from test_support import TestFailed
5
6
Fred Drake8ef67672000-09-27 22:45:25 +00007def basic(src):
8 print
9 print "Testing basic accessors..."
10 cf = ConfigParser.ConfigParser()
11 sio = StringIO.StringIO(src)
12 cf.readfp(sio)
13 L = cf.sections()
14 L.sort()
15 print L
16 for s in L:
17 print "%s: %s" % (s, cf.options(s))
18
19 # The use of spaces in the section names serves as a regression test for
20 # SourceForge bug #115357.
Fred Drake8ef67672000-09-27 22:45:25 +000021 # http://sourceforge.net/bugs/?func=detailbug&group_id=5470&bug_id=115357
Fred Drake8ef67672000-09-27 22:45:25 +000022 print `cf.get('Foo Bar', 'foo', raw=1)`
23 print `cf.get('Spacey Bar', 'foo', raw=1)`
24 print `cf.get('Commented Bar', 'foo', raw=1)`
25
26 if '__name__' in cf.options("Foo Bar"):
27 print '__name__ "option" should not be exposed by the API!'
28 else:
29 print '__name__ "option" properly hidden by the API.'
30
Fred Drake3d5f7e82000-12-04 16:30:40 +000031 # Make sure the right things happen for remove_option();
32 # added to include check for SourceForge bug #123324:
33 if not cf.remove_option('Foo Bar', 'foo'):
34 raise TestFailed(
35 "remove_option() failed to report existance of option")
36 if cf.has_option('Foo Bar', 'foo'):
37 raise TestFailed("remove_option() failed to remove option")
38 if cf.remove_option('Foo Bar', 'foo'):
39 raise TestFailed(
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")
50
51
Fred Drake8ef67672000-09-27 22:45:25 +000052def interpolation(src):
53 print
54 print "Testing value interpolation..."
55 cf = ConfigParser.ConfigParser({"getname": "%(__name__)s"})
56 sio = StringIO.StringIO(src)
57 cf.readfp(sio)
58 print `cf.get("Foo", "getname")`
59 print `cf.get("Foo", "bar")`
60 print `cf.get("Foo", "bar9")`
61 print `cf.get("Foo", "bar10")`
62 expect_get_error(cf, ConfigParser.InterpolationDepthError, "Foo", "bar11")
63
64def parse_errors():
65 print
66 print "Testing for parsing errors..."
67 expect_parse_error(ConfigParser.ParsingError,
68 """[Foo]\n extra-spaces: splat\n""")
69 expect_parse_error(ConfigParser.ParsingError,
70 """[Foo]\n extra-spaces= splat\n""")
71 expect_parse_error(ConfigParser.ParsingError,
72 """[Foo]\noption-without-value\n""")
73 expect_parse_error(ConfigParser.ParsingError,
74 """[Foo]\n:value-without-option-name\n""")
75 expect_parse_error(ConfigParser.ParsingError,
76 """[Foo]\n=value-without-option-name\n""")
77 expect_parse_error(ConfigParser.MissingSectionHeaderError,
78 """No Section!\n""")
79
80def query_errors():
81 print
82 print "Testing query interface..."
83 cf = ConfigParser.ConfigParser()
84 print cf.sections()
85 print "Has section 'Foo'?", cf.has_section("Foo")
86 try:
87 cf.options("Foo")
88 except ConfigParser.NoSectionError, e:
89 print "Caught expected NoSectionError:", e
90 else:
91 print "Failed to catch expected NoSectionError from options()"
92 try:
93 cf.set("foo", "bar", "value")
94 except ConfigParser.NoSectionError, e:
95 print "Caught expected NoSectionError:", e
96 else:
97 print "Failed to catch expected NoSectionError from set()"
98 expect_get_error(cf, ConfigParser.NoSectionError, "foo", "bar")
99 cf.add_section("foo")
100 expect_get_error(cf, ConfigParser.NoOptionError, "foo", "bar")
101
102def weird_errors():
103 print
104 print "Testing miscellaneous error conditions..."
105 cf = ConfigParser.ConfigParser()
106 cf.add_section("Foo")
107 try:
108 cf.add_section("Foo")
109 except ConfigParser.DuplicateSectionError, e:
110 print "Caught expected DuplicateSectionError:", e
111 else:
112 print "Failed to catch expected DuplicateSectionError"
113
114def expect_get_error(cf, exctype, section, option, raw=0):
115 try:
116 cf.get(section, option, raw=raw)
117 except exctype, e:
118 print "Caught expected", exctype.__name__, ":"
119 print e
120 else:
121 print "Failed to catch expected", exctype.__name__
122
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:
129 print "Caught expected exception:", e
130 else:
131 print "Failed to catch expected", exctype.__name__
132
133basic(r"""
134[Foo Bar]
135foo=bar
136[Spacey Bar]
Fred Drake004d5e62000-10-23 17:22:08 +0000137foo = bar
Fred Drake8ef67672000-09-27 22:45:25 +0000138[Commented Bar]
139foo: bar ; comment
140""")
141interpolation(r"""
142[Foo]
143bar=something %(with1)s interpolation (1 step)
144bar9=something %(with9)s lots of interpolation (9 steps)
145bar10=something %(with10)s lots of interpolation (10 steps)
146bar11=something %(with11)s lots of interpolation (11 steps)
147with11=%(with10)s
148with10=%(with9)s
149with9=%(with8)s
150with8=%(with7)s
151with7=%(with6)s
152with6=%(with5)s
153with5=%(with4)s
154with4=%(with3)s
155with3=%(with2)s
156with2=%(with1)s
157with1=with
158
159[Mutual Recursion]
160foo=%(bar)s
161bar=%(foo)s
162""")
163parse_errors()
164query_errors()
165weird_errors()