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',
17 r
'Section\with$weird%characters[' '\t',
20 "unexpected list of section names")
22 # The use of spaces in the section names serves as a regression test for
23 # SourceForge bug #115357.
24 # http://sourceforge.net/bugs/?func=detailbug&group_id=5470&bug_id=115357
25 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')
29 verify('__name__' not in cf
.options("Foo Bar"),
30 '__name__ "option" should not be exposed by the API!')
32 # Make sure the right things happen for remove_option();
33 # added to include check for SourceForge bug #123324:
34 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"
42 cf
.remove_option('No Such Section', 'foo')
43 except ConfigParser
.NoSectionError
:
47 "remove_option() failed to report non-existance of option"
48 " that never existed")
51 def case_sensitivity():
52 print "Testing case sensitivity..."
53 cf
= ConfigParser
.ConfigParser()
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") == [])
74 cf
= ConfigParser
.ConfigParser()
75 sio
= StringIO
.StringIO("[MySection]\nOption: first line\n\tsecond line\n")
77 verify(cf
.options("MySection") == ["option"])
78 verify(cf
.get("MySection", "Option") == "first line\nsecond line")
81 def interpolation(src
):
82 print "Testing value interpolation..."
83 cf
= ConfigParser
.ConfigParser({"getname": "%(__name__)s"})
84 sio
= StringIO
.StringIO(src
)
86 verify(cf
.get("Foo", "getname") == "Foo")
87 verify(cf
.get("Foo", "bar") == "something with interpolation (1 step)")
88 verify(cf
.get("Foo", "bar9")
89 == "something with lots of interpolation (9 steps)")
90 verify(cf
.get("Foo", "bar10")
91 == "something with lots of interpolation (10 steps)")
92 expect_get_error(cf
, ConfigParser
.InterpolationDepthError
, "Foo", "bar11")
96 print "Testing parse errors..."
97 expect_parse_error(ConfigParser
.ParsingError
,
98 """[Foo]\n extra-spaces: splat\n""")
99 expect_parse_error(ConfigParser
.ParsingError
,
100 """[Foo]\n extra-spaces= splat\n""")
101 expect_parse_error(ConfigParser
.ParsingError
,
102 """[Foo]\noption-without-value\n""")
103 expect_parse_error(ConfigParser
.ParsingError
,
104 """[Foo]\n:value-without-option-name\n""")
105 expect_parse_error(ConfigParser
.ParsingError
,
106 """[Foo]\n=value-without-option-name\n""")
107 expect_parse_error(ConfigParser
.MissingSectionHeaderError
,
112 print "Testing query interface..."
113 cf
= ConfigParser
.ConfigParser()
114 verify(cf
.sections() == [],
115 "new ConfigParser should have no defined sections")
116 verify(not cf
.has_section("Foo"),
117 "new ConfigParser should have no acknowledged sections")
120 except ConfigParser
.NoSectionError
, e
:
124 "Failed to catch expected NoSectionError from options()")
126 cf
.set("foo", "bar", "value")
127 except ConfigParser
.NoSectionError
, e
:
130 raise TestFailed("Failed to catch expected NoSectionError from set()")
131 expect_get_error(cf
, ConfigParser
.NoSectionError
, "foo", "bar")
132 cf
.add_section("foo")
133 expect_get_error(cf
, ConfigParser
.NoOptionError
, "foo", "bar")
137 print "Testing miscellaneous error conditions..."
138 cf
= ConfigParser
.ConfigParser()
139 cf
.add_section("Foo")
141 cf
.add_section("Foo")
142 except ConfigParser
.DuplicateSectionError
, e
:
145 raise TestFailed("Failed to catch expected DuplicateSectionError")
148 def expect_get_error(cf
, exctype
, section
, option
, raw
=0):
150 cf
.get(section
, option
, raw
=raw
)
154 raise TestFailed("Failed to catch expected " + exctype
.__name
__)
157 def expect_parse_error(exctype
, src
):
158 cf
= ConfigParser
.ConfigParser()
159 sio
= StringIO
.StringIO(src
)
165 raise TestFailed("Failed to catch expected " + exctype
.__name
__)
175 [Section\with$weird%characters[""" '\t' r
"""]
176 [Internationalized Stuff]
185 bar=something %(with1)s interpolation (1 step)
186 bar9=something %(with9)s lots of interpolation (9 steps)
187 bar10=something %(with10)s lots of interpolation (10 steps)
188 bar11=something %(with11)s lots of interpolation (11 steps)