11import ConfigParser
22import StringIO
33
4- from test_support import TestFailed
4+ from test_support import TestFailed , verify
55
66
77def basic (src ):
8- print
98 print "Testing basic accessors..."
109 cf = ConfigParser .ConfigParser ()
1110 sio = StringIO .StringIO (src )
1211 cf .readfp (sio )
1312 L = cf .sections ()
1413 L .sort ()
15- print L
16- for s in L :
17- print "%s: %s" % ( s , cf . options ( s ) )
14+ verify ( L == [ 'Commented Bar' , 'Foo Bar' ,
15+ 'Internationalized Stuff' , 'Spacey Bar' ],
16+ "unexpected list of section names" )
1817
1918 # The use of spaces in the section names serves as a regression test for
2019 # SourceForge bug #115357.
2120 # http://sourceforge.net/bugs/?func=detailbug&group_id=5470&bug_id=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)`
21+ verify ( cf .get ('Foo Bar' , 'foo' , raw = 1 ) == 'bar' )
22+ verify ( cf .get ('Spacey Bar' , 'foo' , raw = 1 ) == 'bar' )
23+ verify ( cf .get ('Commented Bar' , 'foo' , raw = 1 ) == 'bar' )
2524
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.'
25+ verify ('__name__' not in cf .options ("Foo Bar" ),
26+ '__name__ "option" should not be exposed by the API!' )
3027
3128 # Make sure the right things happen for remove_option();
3229 # added to include check for SourceForge bug #123324:
33- if not cf .remove_option ('Foo Bar' , 'foo' ):
34- raise TestFailed (
35- "remove_option() failed to report existance of option" )
36- if cf .has_option ('Foo Bar' , 'foo' ):
37- raise TestFailed ("remove_option() failed to remove option" )
38- if cf .remove_option ('Foo Bar' , 'foo' ):
39- raise TestFailed (
40- "remove_option() failed to report non-existance of option"
41- " that was removed" )
30+ verify (cf .remove_option ('Foo Bar' , 'foo' ),
31+ "remove_option() failed to report existance of option" )
32+ verify (not cf .has_option ('Foo Bar' , 'foo' ),
33+ "remove_option() failed to remove option" )
34+ verify (not cf .remove_option ('Foo Bar' , 'foo' ),
35+ "remove_option() failed to report non-existance of option"
36+ " that was removed" )
4237 try :
4338 cf .remove_option ('No Such Section' , 'foo' )
4439 except ConfigParser .NoSectionError :
@@ -50,20 +45,21 @@ def basic(src):
5045
5146
5247def interpolation (src ):
53- print
5448 print "Testing value interpolation..."
5549 cf = ConfigParser .ConfigParser ({"getname" : "%(__name__)s" })
5650 sio = StringIO .StringIO (src )
5751 cf .readfp (sio )
58- print `cf.get("Foo", "getname")`
59- print `cf.get("Foo", "bar")`
60- print `cf.get("Foo", "bar9")`
61- print `cf.get("Foo", "bar10")`
52+ verify (cf .get ("Foo" , "getname" ) == "Foo" )
53+ verify (cf .get ("Foo" , "bar" ) == "something with interpolation (1 step)" )
54+ verify (cf .get ("Foo" , "bar9" )
55+ == "something with lots of interpolation (9 steps)" )
56+ verify (cf .get ("Foo" , "bar10" )
57+ == "something with lots of interpolation (10 steps)" )
6258 expect_get_error (cf , ConfigParser .InterpolationDepthError , "Foo" , "bar11" )
6359
60+
6461def parse_errors ():
65- print
66- print "Testing for parsing errors..."
62+ print "Testing parse errors..."
6763 expect_parse_error (ConfigParser .ParsingError ,
6864 """[Foo]\n extra-spaces: splat\n """ )
6965 expect_parse_error (ConfigParser .ParsingError ,
@@ -77,58 +73,63 @@ def parse_errors():
7773 expect_parse_error (ConfigParser .MissingSectionHeaderError ,
7874 """No Section!\n """ )
7975
76+
8077def query_errors ():
81- print
8278 print "Testing query interface..."
8379 cf = ConfigParser .ConfigParser ()
84- print cf .sections ()
85- print "Has section 'Foo'?" , cf .has_section ("Foo" )
80+ verify (cf .sections () == [],
81+ "new ConfigParser should have no defined sections" )
82+ verify (not cf .has_section ("Foo" ),
83+ "new ConfigParser should have no acknowledged sections" )
8684 try :
8785 cf .options ("Foo" )
8886 except ConfigParser .NoSectionError , e :
89- print "Caught expected NoSectionError:" , e
87+ pass
9088 else :
91- print "Failed to catch expected NoSectionError from options()"
89+ raise TestFailed (
90+ "Failed to catch expected NoSectionError from options()" )
9291 try :
9392 cf .set ("foo" , "bar" , "value" )
9493 except ConfigParser .NoSectionError , e :
95- print "Caught expected NoSectionError:" , e
94+ pass
9695 else :
97- print "Failed to catch expected NoSectionError from set()"
96+ raise TestFailed ( "Failed to catch expected NoSectionError from set()" )
9897 expect_get_error (cf , ConfigParser .NoSectionError , "foo" , "bar" )
9998 cf .add_section ("foo" )
10099 expect_get_error (cf , ConfigParser .NoOptionError , "foo" , "bar" )
101100
101+
102102def weird_errors ():
103- print
104103 print "Testing miscellaneous error conditions..."
105104 cf = ConfigParser .ConfigParser ()
106105 cf .add_section ("Foo" )
107106 try :
108107 cf .add_section ("Foo" )
109108 except ConfigParser .DuplicateSectionError , e :
110- print "Caught expected DuplicateSectionError:" , e
109+ pass
111110 else :
112- print "Failed to catch expected DuplicateSectionError"
111+ raise TestFailed ("Failed to catch expected DuplicateSectionError" )
112+
113113
114114def expect_get_error (cf , exctype , section , option , raw = 0 ):
115115 try :
116116 cf .get (section , option , raw = raw )
117117 except exctype , e :
118- print "Caught expected" , exctype .__name__ , ":"
119- print e
118+ pass
120119 else :
121- print "Failed to catch expected" , exctype .__name__
120+ raise TestFailed ("Failed to catch expected " + exctype .__name__ )
121+
122122
123123def expect_parse_error (exctype , src ):
124124 cf = ConfigParser .ConfigParser ()
125125 sio = StringIO .StringIO (src )
126126 try :
127127 cf .readfp (sio )
128128 except exctype , e :
129- print "Caught expected exception:" , e
129+ pass
130130 else :
131- print "Failed to catch expected" , exctype .__name__
131+ raise TestFailed ("Failed to catch expected " + exctype .__name__ )
132+
132133
133134basic (r"""
134135[Foo Bar]
@@ -137,6 +138,11 @@ def expect_parse_error(exctype, src):
137138foo = bar
138139[Commented Bar]
139140foo: bar ; comment
141+ [Internationalized Stuff]
142+ foo[bg]: Bulgarian
143+ foo=Default
144+ foo[en]=English
145+ foo[de]=Deutsch
140146""" )
141147interpolation (r"""
142148[Foo]
0 commit comments