Added options that use square brackets in their names; this ensures that

GNOME-style internationalized options can be parsed using ConfigParser
(SF bug #131635).

Converted the tests to use test_support.verify() instead of output
comparison to work.
This commit is contained in:
Fred Drake 2001-02-12 17:23:20 +00:00
parent d83bbbfd22
commit 95b96d3941
2 changed files with 50 additions and 89 deletions

View File

@ -1,51 +1,6 @@
test_cfgparser test_cfgparser
Testing basic accessors... Testing basic accessors...
['Commented Bar', 'Foo Bar', 'Spacey Bar']
Commented Bar: ['foo']
Foo Bar: ['foo']
Spacey Bar: ['foo']
'bar'
'bar'
'bar'
__name__ "option" properly hidden by the API.
Testing value interpolation... Testing value interpolation...
'Foo' Testing parse errors...
'something with interpolation (1 step)'
'something with lots of interpolation (9 steps)'
'something with lots of interpolation (10 steps)'
Caught expected InterpolationDepthError :
Value interpolation too deeply recursive:
section: [Foo]
option : bar11
rawval : something %(with11)s lots of interpolation (11 steps)
Testing for parsing errors...
Caught expected exception: File contains parsing errors: <???>
[line 2]: ' extra-spaces: splat\n'
Caught expected exception: File contains parsing errors: <???>
[line 2]: ' extra-spaces= splat\n'
Caught expected exception: File contains parsing errors: <???>
[line 2]: 'option-without-value\n'
Caught expected exception: File contains parsing errors: <???>
[line 2]: ':value-without-option-name\n'
Caught expected exception: File contains parsing errors: <???>
[line 2]: '=value-without-option-name\n'
Caught expected exception: File contains no section headers.
file: <???>, line: 1
'No Section!\n'
Testing query interface... Testing query interface...
[]
Has section 'Foo'? 0
Caught expected NoSectionError: No section: Foo
Caught expected NoSectionError: No section: foo
Caught expected NoSectionError :
No section: foo
Caught expected NoOptionError :
No option `bar' in section: foo
Testing miscellaneous error conditions... Testing miscellaneous error conditions...
Caught expected DuplicateSectionError: Section Foo already exists

View File

@ -1,44 +1,39 @@
import ConfigParser import ConfigParser
import StringIO import StringIO
from test_support import TestFailed from test_support import TestFailed, verify
def basic(src): def basic(src):
print
print "Testing basic accessors..." print "Testing basic accessors..."
cf = ConfigParser.ConfigParser() cf = ConfigParser.ConfigParser()
sio = StringIO.StringIO(src) sio = StringIO.StringIO(src)
cf.readfp(sio) cf.readfp(sio)
L = cf.sections() L = cf.sections()
L.sort() L.sort()
print L verify(L == ['Commented Bar', 'Foo Bar',
for s in L: 'Internationalized Stuff', 'Spacey Bar'],
print "%s: %s" % (s, cf.options(s)) "unexpected list of section names")
# The use of spaces in the section names serves as a regression test for # The use of spaces in the section names serves as a regression test for
# SourceForge bug #115357. # SourceForge bug #115357.
# http://sourceforge.net/bugs/?func=detailbug&group_id=5470&bug_id=115357 # http://sourceforge.net/bugs/?func=detailbug&group_id=5470&bug_id=115357
print `cf.get('Foo Bar', 'foo', raw=1)` verify(cf.get('Foo Bar', 'foo', raw=1) == 'bar')
print `cf.get('Spacey Bar', 'foo', raw=1)` verify(cf.get('Spacey Bar', 'foo', raw=1) == 'bar')
print `cf.get('Commented Bar', 'foo', raw=1)` verify(cf.get('Commented Bar', 'foo', raw=1) == 'bar')
if '__name__' in cf.options("Foo Bar"): verify('__name__' not in cf.options("Foo Bar"),
print '__name__ "option" should not be exposed by the API!' '__name__ "option" should not be exposed by the API!')
else:
print '__name__ "option" properly hidden by the API.'
# Make sure the right things happen for remove_option(); # Make sure the right things happen for remove_option();
# added to include check for SourceForge bug #123324: # added to include check for SourceForge bug #123324:
if not cf.remove_option('Foo Bar', 'foo'): verify(cf.remove_option('Foo Bar', 'foo'),
raise TestFailed( "remove_option() failed to report existance of option")
"remove_option() failed to report existance of option") verify(not cf.has_option('Foo Bar', 'foo'),
if cf.has_option('Foo Bar', 'foo'): "remove_option() failed to remove option")
raise TestFailed("remove_option() failed to remove option") verify(not cf.remove_option('Foo Bar', 'foo'),
if cf.remove_option('Foo Bar', 'foo'): "remove_option() failed to report non-existance of option"
raise TestFailed( " that was removed")
"remove_option() failed to report non-existance of option"
" that was removed")
try: try:
cf.remove_option('No Such Section', 'foo') cf.remove_option('No Such Section', 'foo')
except ConfigParser.NoSectionError: except ConfigParser.NoSectionError:
@ -50,20 +45,21 @@ def basic(src):
def interpolation(src): def interpolation(src):
print
print "Testing value interpolation..." print "Testing value interpolation..."
cf = ConfigParser.ConfigParser({"getname": "%(__name__)s"}) cf = ConfigParser.ConfigParser({"getname": "%(__name__)s"})
sio = StringIO.StringIO(src) sio = StringIO.StringIO(src)
cf.readfp(sio) cf.readfp(sio)
print `cf.get("Foo", "getname")` verify(cf.get("Foo", "getname") == "Foo")
print `cf.get("Foo", "bar")` verify(cf.get("Foo", "bar") == "something with interpolation (1 step)")
print `cf.get("Foo", "bar9")` verify(cf.get("Foo", "bar9")
print `cf.get("Foo", "bar10")` == "something with lots of interpolation (9 steps)")
verify(cf.get("Foo", "bar10")
== "something with lots of interpolation (10 steps)")
expect_get_error(cf, ConfigParser.InterpolationDepthError, "Foo", "bar11") expect_get_error(cf, ConfigParser.InterpolationDepthError, "Foo", "bar11")
def parse_errors(): def parse_errors():
print print "Testing parse errors..."
print "Testing for parsing errors..."
expect_parse_error(ConfigParser.ParsingError, expect_parse_error(ConfigParser.ParsingError,
"""[Foo]\n extra-spaces: splat\n""") """[Foo]\n extra-spaces: splat\n""")
expect_parse_error(ConfigParser.ParsingError, expect_parse_error(ConfigParser.ParsingError,
@ -77,48 +73,52 @@ def parse_errors():
expect_parse_error(ConfigParser.MissingSectionHeaderError, expect_parse_error(ConfigParser.MissingSectionHeaderError,
"""No Section!\n""") """No Section!\n""")
def query_errors(): def query_errors():
print
print "Testing query interface..." print "Testing query interface..."
cf = ConfigParser.ConfigParser() cf = ConfigParser.ConfigParser()
print cf.sections() verify(cf.sections() == [],
print "Has section 'Foo'?", cf.has_section("Foo") "new ConfigParser should have no defined sections")
verify(not cf.has_section("Foo"),
"new ConfigParser should have no acknowledged sections")
try: try:
cf.options("Foo") cf.options("Foo")
except ConfigParser.NoSectionError, e: except ConfigParser.NoSectionError, e:
print "Caught expected NoSectionError:", e pass
else: else:
print "Failed to catch expected NoSectionError from options()" raise TestFailed(
"Failed to catch expected NoSectionError from options()")
try: try:
cf.set("foo", "bar", "value") cf.set("foo", "bar", "value")
except ConfigParser.NoSectionError, e: except ConfigParser.NoSectionError, e:
print "Caught expected NoSectionError:", e pass
else: else:
print "Failed to catch expected NoSectionError from set()" raise TestFailed("Failed to catch expected NoSectionError from set()")
expect_get_error(cf, ConfigParser.NoSectionError, "foo", "bar") expect_get_error(cf, ConfigParser.NoSectionError, "foo", "bar")
cf.add_section("foo") cf.add_section("foo")
expect_get_error(cf, ConfigParser.NoOptionError, "foo", "bar") expect_get_error(cf, ConfigParser.NoOptionError, "foo", "bar")
def weird_errors(): def weird_errors():
print
print "Testing miscellaneous error conditions..." print "Testing miscellaneous error conditions..."
cf = ConfigParser.ConfigParser() cf = ConfigParser.ConfigParser()
cf.add_section("Foo") cf.add_section("Foo")
try: try:
cf.add_section("Foo") cf.add_section("Foo")
except ConfigParser.DuplicateSectionError, e: except ConfigParser.DuplicateSectionError, e:
print "Caught expected DuplicateSectionError:", e pass
else: else:
print "Failed to catch expected DuplicateSectionError" raise TestFailed("Failed to catch expected DuplicateSectionError")
def expect_get_error(cf, exctype, section, option, raw=0): def expect_get_error(cf, exctype, section, option, raw=0):
try: try:
cf.get(section, option, raw=raw) cf.get(section, option, raw=raw)
except exctype, e: except exctype, e:
print "Caught expected", exctype.__name__, ":" pass
print e
else: else:
print "Failed to catch expected", exctype.__name__ raise TestFailed("Failed to catch expected " + exctype.__name__)
def expect_parse_error(exctype, src): def expect_parse_error(exctype, src):
cf = ConfigParser.ConfigParser() cf = ConfigParser.ConfigParser()
@ -126,9 +126,10 @@ def expect_parse_error(exctype, src):
try: try:
cf.readfp(sio) cf.readfp(sio)
except exctype, e: except exctype, e:
print "Caught expected exception:", e pass
else: else:
print "Failed to catch expected", exctype.__name__ raise TestFailed("Failed to catch expected " + exctype.__name__)
basic(r""" basic(r"""
[Foo Bar] [Foo Bar]
@ -137,6 +138,11 @@ foo=bar
foo = bar foo = bar
[Commented Bar] [Commented Bar]
foo: bar ; comment foo: bar ; comment
[Internationalized Stuff]
foo[bg]: Bulgarian
foo=Default
foo[en]=English
foo[de]=Deutsch
""") """)
interpolation(r""" interpolation(r"""
[Foo] [Foo]