4 from test_support
import TestFailed
, verify
8 print "Testing basic accessors..."
9 cf
= ConfigParser
.ConfigParser()
10 sio
= StringIO
.StringIO(src
)
14 verify(L
== [r
'Commented Bar',
16 r
'Internationalized Stuff',
18 r
'Section\with$weird%characters[' '\t',
21 "unexpected list of section names")
23 # The use of spaces in the section names serves as a regression test for
24 # SourceForge bug #115357.
25 # http://sourceforge.net/bugs/?func=detailbug&group_id=5470&bug_id=115357
26 verify(cf
.get('Foo Bar', 'foo', raw
=1) == 'bar')
27 verify(cf
.get('Spacey Bar', 'foo', raw
=1) == 'bar')
28 verify(cf
.get('Commented Bar', 'foo', raw
=1) == 'bar')
30 verify('__name__' not in cf
.options("Foo Bar"),
31 '__name__ "option" should not be exposed by the API!')
33 # Make sure the right things happen for remove_option();
34 # added to include check for SourceForge bug #123324:
35 verify(cf
.remove_option('Foo Bar', 'foo'),
36 "remove_option() failed to report existance of option")
37 verify(not cf
.has_option('Foo Bar', 'foo'),
38 "remove_option() failed to remove option")
39 verify(not cf
.remove_option('Foo Bar', 'foo'),
40 "remove_option() failed to report non-existance of option"
43 cf
.remove_option('No Such Section', 'foo')
44 except ConfigParser
.NoSectionError
:
48 "remove_option() failed to report non-existance of option"
49 " that never existed")
51 verify(cf
.get('Long Line', 'foo', raw
=1) ==
52 'this line is much, much longer than my editor\nlikes it.')
56 print "Testing writing of files..."
57 cf
= ConfigParser
.ConfigParser()
58 sio
= StringIO
.StringIO(src
)
60 output
= StringIO
.StringIO()
62 verify(output
, """[DEFAULT]
67 foo = this line is much, much longer than my editor
71 def case_sensitivity():
72 print "Testing case sensitivity..."
73 cf
= ConfigParser
.ConfigParser()
78 verify(L
== ["A", "a"])
79 cf
.set("a", "B", "value")
80 verify(cf
.options("a") == ["b"])
81 verify(cf
.get("a", "b", raw
=1) == "value",
82 "could not locate option, expecting case-insensitive option names")
83 verify(cf
.has_option("a", "b"))
84 cf
.set("A", "A-B", "A-B value")
85 for opt
in ("a-b", "A-b", "a-B", "A-B"):
86 verify(cf
.has_option("A", opt
),
87 "has_option() returned false for option which should exist")
88 verify(cf
.options("A") == ["a-b"])
89 verify(cf
.options("a") == ["b"])
90 cf
.remove_option("a", "B")
91 verify(cf
.options("a") == [])
94 cf
= ConfigParser
.ConfigParser()
95 sio
= StringIO
.StringIO("[MySection]\nOption: first line\n\tsecond line\n")
97 verify(cf
.options("MySection") == ["option"])
98 verify(cf
.get("MySection", "Option") == "first line\nsecond line")
102 print "Testing interpretation of boolean Values..."
103 cf
= ConfigParser
.ConfigParser()
104 sio
= StringIO
.StringIO(src
)
106 for x
in range(1, 5):
107 verify(cf
.getboolean('BOOLTEST', 't%d' % (x
)) == 1)
108 for x
in range(1, 5):
109 verify(cf
.getboolean('BOOLTEST', 'f%d' % (x
)) == 0)
110 for x
in range(1, 5):
112 cf
.getboolean('BOOLTEST', 'e%d' % (x
))
117 "getboolean() failed to report a non boolean value")
120 def interpolation(src
):
121 print "Testing value interpolation..."
122 cf
= ConfigParser
.ConfigParser({"getname": "%(__name__)s"})
123 sio
= StringIO
.StringIO(src
)
125 verify(cf
.get("Foo", "getname") == "Foo")
126 verify(cf
.get("Foo", "bar") == "something with interpolation (1 step)")
127 verify(cf
.get("Foo", "bar9")
128 == "something with lots of interpolation (9 steps)")
129 verify(cf
.get("Foo", "bar10")
130 == "something with lots of interpolation (10 steps)")
131 expect_get_error(cf
, ConfigParser
.InterpolationDepthError
, "Foo", "bar11")
135 print "Testing parse errors..."
136 expect_parse_error(ConfigParser
.ParsingError
,
137 """[Foo]\n extra-spaces: splat\n""")
138 expect_parse_error(ConfigParser
.ParsingError
,
139 """[Foo]\n extra-spaces= splat\n""")
140 expect_parse_error(ConfigParser
.ParsingError
,
141 """[Foo]\noption-without-value\n""")
142 expect_parse_error(ConfigParser
.ParsingError
,
143 """[Foo]\n:value-without-option-name\n""")
144 expect_parse_error(ConfigParser
.ParsingError
,
145 """[Foo]\n=value-without-option-name\n""")
146 expect_parse_error(ConfigParser
.MissingSectionHeaderError
,
151 print "Testing query interface..."
152 cf
= ConfigParser
.ConfigParser()
153 verify(cf
.sections() == [],
154 "new ConfigParser should have no defined sections")
155 verify(not cf
.has_section("Foo"),
156 "new ConfigParser should have no acknowledged sections")
159 except ConfigParser
.NoSectionError
, e
:
163 "Failed to catch expected NoSectionError from options()")
165 cf
.set("foo", "bar", "value")
166 except ConfigParser
.NoSectionError
, e
:
169 raise TestFailed("Failed to catch expected NoSectionError from set()")
170 expect_get_error(cf
, ConfigParser
.NoSectionError
, "foo", "bar")
171 cf
.add_section("foo")
172 expect_get_error(cf
, ConfigParser
.NoOptionError
, "foo", "bar")
176 print "Testing miscellaneous error conditions..."
177 cf
= ConfigParser
.ConfigParser()
178 cf
.add_section("Foo")
180 cf
.add_section("Foo")
181 except ConfigParser
.DuplicateSectionError
, e
:
184 raise TestFailed("Failed to catch expected DuplicateSectionError")
187 def expect_get_error(cf
, exctype
, section
, option
, raw
=0):
189 cf
.get(section
, option
, raw
=raw
)
193 raise TestFailed("Failed to catch expected " + exctype
.__name
__)
196 def expect_parse_error(exctype
, src
):
197 cf
= ConfigParser
.ConfigParser()
198 sio
= StringIO
.StringIO(src
)
204 raise TestFailed("Failed to catch expected " + exctype
.__name
__)
215 foo: this line is much, much longer than my editor
217 [Section\with$weird%characters[""" '\t' r
"""]
218 [Internationalized Stuff]
225 foo: this line is much, much longer than my editor
251 bar=something %(with1)s interpolation (1 step)
252 bar9=something %(with9)s lots of interpolation (9 steps)
253 bar10=something %(with10)s lots of interpolation (10 steps)
254 bar11=something %(with11)s lots of interpolation (11 steps)