blob: cf6bb891ae0999450ac9eb4f6cfa09bf8bcb9216 [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.
18 #
19 # http://sourceforge.net/bugs/?func=detailbug&group_id=5470&bug_id=115357
20 #
21 print "test for SourceForge bug #115357"
22 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
31def interpolation(src):
32 print
33 print "Testing value interpolation..."
34 cf = ConfigParser.ConfigParser({"getname": "%(__name__)s"})
35 sio = StringIO.StringIO(src)
36 cf.readfp(sio)
37 print `cf.get("Foo", "getname")`
38 print `cf.get("Foo", "bar")`
39 print `cf.get("Foo", "bar9")`
40 print `cf.get("Foo", "bar10")`
41 expect_get_error(cf, ConfigParser.InterpolationDepthError, "Foo", "bar11")
42
43def parse_errors():
44 print
45 print "Testing for parsing errors..."
46 expect_parse_error(ConfigParser.ParsingError,
47 """[Foo]\n extra-spaces: splat\n""")
48 expect_parse_error(ConfigParser.ParsingError,
49 """[Foo]\n extra-spaces= splat\n""")
50 expect_parse_error(ConfigParser.ParsingError,
51 """[Foo]\noption-without-value\n""")
52 expect_parse_error(ConfigParser.ParsingError,
53 """[Foo]\n:value-without-option-name\n""")
54 expect_parse_error(ConfigParser.ParsingError,
55 """[Foo]\n=value-without-option-name\n""")
56 expect_parse_error(ConfigParser.MissingSectionHeaderError,
57 """No Section!\n""")
58
59def query_errors():
60 print
61 print "Testing query interface..."
62 cf = ConfigParser.ConfigParser()
63 print cf.sections()
64 print "Has section 'Foo'?", cf.has_section("Foo")
65 try:
66 cf.options("Foo")
67 except ConfigParser.NoSectionError, e:
68 print "Caught expected NoSectionError:", e
69 else:
70 print "Failed to catch expected NoSectionError from options()"
71 try:
72 cf.set("foo", "bar", "value")
73 except ConfigParser.NoSectionError, e:
74 print "Caught expected NoSectionError:", e
75 else:
76 print "Failed to catch expected NoSectionError from set()"
77 expect_get_error(cf, ConfigParser.NoSectionError, "foo", "bar")
78 cf.add_section("foo")
79 expect_get_error(cf, ConfigParser.NoOptionError, "foo", "bar")
80
81def weird_errors():
82 print
83 print "Testing miscellaneous error conditions..."
84 cf = ConfigParser.ConfigParser()
85 cf.add_section("Foo")
86 try:
87 cf.add_section("Foo")
88 except ConfigParser.DuplicateSectionError, e:
89 print "Caught expected DuplicateSectionError:", e
90 else:
91 print "Failed to catch expected DuplicateSectionError"
92
93def expect_get_error(cf, exctype, section, option, raw=0):
94 try:
95 cf.get(section, option, raw=raw)
96 except exctype, e:
97 print "Caught expected", exctype.__name__, ":"
98 print e
99 else:
100 print "Failed to catch expected", exctype.__name__
101
102def expect_parse_error(exctype, src):
103 cf = ConfigParser.ConfigParser()
104 sio = StringIO.StringIO(src)
105 try:
106 cf.readfp(sio)
107 except exctype, e:
108 print "Caught expected exception:", e
109 else:
110 print "Failed to catch expected", exctype.__name__
111
112basic(r"""
113[Foo Bar]
114foo=bar
115[Spacey Bar]
116foo = bar
117[Commented Bar]
118foo: bar ; comment
119""")
120interpolation(r"""
121[Foo]
122bar=something %(with1)s interpolation (1 step)
123bar9=something %(with9)s lots of interpolation (9 steps)
124bar10=something %(with10)s lots of interpolation (10 steps)
125bar11=something %(with11)s lots of interpolation (11 steps)
126with11=%(with10)s
127with10=%(with9)s
128with9=%(with8)s
129with8=%(with7)s
130with7=%(with6)s
131with6=%(with5)s
132with5=%(with4)s
133with4=%(with3)s
134with3=%(with2)s
135with2=%(with1)s
136with1=with
137
138[Mutual Recursion]
139foo=%(bar)s
140bar=%(foo)s
141""")
142parse_errors()
143query_errors()
144weird_errors()