blob: 00b84659b6d67b0945b036703c1fac306b054fc6 [file] [log] [blame]
Fred Drake8ef67672000-09-27 22:45:25 +00001import ConfigParser
2import StringIO
3
4def basic(src):
5 print
6 print "Testing basic accessors..."
7 cf = ConfigParser.ConfigParser()
8 sio = StringIO.StringIO(src)
9 cf.readfp(sio)
10 L = cf.sections()
11 L.sort()
12 print L
13 for s in L:
14 print "%s: %s" % (s, cf.options(s))
15
16 # The use of spaces in the section names serves as a regression test for
17 # SourceForge bug #115357.
Fred Drake8ef67672000-09-27 22:45:25 +000018 # http://sourceforge.net/bugs/?func=detailbug&group_id=5470&bug_id=115357
Fred Drake8ef67672000-09-27 22:45:25 +000019 print `cf.get('Foo Bar', 'foo', raw=1)`
20 print `cf.get('Spacey Bar', 'foo', raw=1)`
21 print `cf.get('Commented Bar', 'foo', raw=1)`
22
23 if '__name__' in cf.options("Foo Bar"):
24 print '__name__ "option" should not be exposed by the API!'
25 else:
26 print '__name__ "option" properly hidden by the API.'
27
28def interpolation(src):
29 print
30 print "Testing value interpolation..."
31 cf = ConfigParser.ConfigParser({"getname": "%(__name__)s"})
32 sio = StringIO.StringIO(src)
33 cf.readfp(sio)
34 print `cf.get("Foo", "getname")`
35 print `cf.get("Foo", "bar")`
36 print `cf.get("Foo", "bar9")`
37 print `cf.get("Foo", "bar10")`
38 expect_get_error(cf, ConfigParser.InterpolationDepthError, "Foo", "bar11")
39
40def parse_errors():
41 print
42 print "Testing for parsing errors..."
43 expect_parse_error(ConfigParser.ParsingError,
44 """[Foo]\n extra-spaces: splat\n""")
45 expect_parse_error(ConfigParser.ParsingError,
46 """[Foo]\n extra-spaces= splat\n""")
47 expect_parse_error(ConfigParser.ParsingError,
48 """[Foo]\noption-without-value\n""")
49 expect_parse_error(ConfigParser.ParsingError,
50 """[Foo]\n:value-without-option-name\n""")
51 expect_parse_error(ConfigParser.ParsingError,
52 """[Foo]\n=value-without-option-name\n""")
53 expect_parse_error(ConfigParser.MissingSectionHeaderError,
54 """No Section!\n""")
55
56def query_errors():
57 print
58 print "Testing query interface..."
59 cf = ConfigParser.ConfigParser()
60 print cf.sections()
61 print "Has section 'Foo'?", cf.has_section("Foo")
62 try:
63 cf.options("Foo")
64 except ConfigParser.NoSectionError, e:
65 print "Caught expected NoSectionError:", e
66 else:
67 print "Failed to catch expected NoSectionError from options()"
68 try:
69 cf.set("foo", "bar", "value")
70 except ConfigParser.NoSectionError, e:
71 print "Caught expected NoSectionError:", e
72 else:
73 print "Failed to catch expected NoSectionError from set()"
74 expect_get_error(cf, ConfigParser.NoSectionError, "foo", "bar")
75 cf.add_section("foo")
76 expect_get_error(cf, ConfigParser.NoOptionError, "foo", "bar")
77
78def weird_errors():
79 print
80 print "Testing miscellaneous error conditions..."
81 cf = ConfigParser.ConfigParser()
82 cf.add_section("Foo")
83 try:
84 cf.add_section("Foo")
85 except ConfigParser.DuplicateSectionError, e:
86 print "Caught expected DuplicateSectionError:", e
87 else:
88 print "Failed to catch expected DuplicateSectionError"
89
90def expect_get_error(cf, exctype, section, option, raw=0):
91 try:
92 cf.get(section, option, raw=raw)
93 except exctype, e:
94 print "Caught expected", exctype.__name__, ":"
95 print e
96 else:
97 print "Failed to catch expected", exctype.__name__
98
99def expect_parse_error(exctype, src):
100 cf = ConfigParser.ConfigParser()
101 sio = StringIO.StringIO(src)
102 try:
103 cf.readfp(sio)
104 except exctype, e:
105 print "Caught expected exception:", e
106 else:
107 print "Failed to catch expected", exctype.__name__
108
109basic(r"""
110[Foo Bar]
111foo=bar
112[Spacey Bar]
Fred Drake004d5e62000-10-23 17:22:08 +0000113foo = bar
Fred Drake8ef67672000-09-27 22:45:25 +0000114[Commented Bar]
115foo: bar ; comment
116""")
117interpolation(r"""
118[Foo]
119bar=something %(with1)s interpolation (1 step)
120bar9=something %(with9)s lots of interpolation (9 steps)
121bar10=something %(with10)s lots of interpolation (10 steps)
122bar11=something %(with11)s lots of interpolation (11 steps)
123with11=%(with10)s
124with10=%(with9)s
125with9=%(with8)s
126with8=%(with7)s
127with7=%(with6)s
128with6=%(with5)s
129with5=%(with4)s
130with4=%(with3)s
131with3=%(with2)s
132with2=%(with1)s
133with1=with
134
135[Mutual Recursion]
136foo=%(bar)s
137bar=%(foo)s
138""")
139parse_errors()
140query_errors()
141weird_errors()