<s> import ConfigParser <EOL> import StringIO <EOL> import unittest <EOL> from test import test_support <EOL> class TestCaseBase ( unittest . TestCase ) : <EOL> def newconfig ( self , defaults = None ) : <EOL> if defaults is None : <EOL> self . cf = self . config_class ( ) <EOL> else : <EOL> self . cf = self . config_class ( defaults ) <EOL> return self . cf <EOL> def fromstring ( self , string , defaults = None ) : <EOL> cf = self . newconfig ( defaults ) <EOL> sio = StringIO . StringIO ( string ) <EOL> cf . readfp ( sio ) <EOL> return cf <EOL> def test_basic ( self ) : <EOL> cf = self . fromstring ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> L = cf . sections ( ) <EOL> L . sort ( ) <EOL> eq = self . assertEqual <EOL> eq ( L , [ r'<STR_LIT>' , <EOL> r'<STR_LIT>' , <EOL> r'<STR_LIT>' , <EOL> r'<STR_LIT>' , <EOL> r'<STR_LIT>' '<STR_LIT:\t>' , <EOL> r'<STR_LIT>' , <EOL> r'<STR_LIT>' , <EOL> ] ) <EOL> eq ( cf . get ( '<STR_LIT>' , '<STR_LIT:foo>' ) , '<STR_LIT:bar>' ) <EOL> eq ( cf . get ( '<STR_LIT>' , '<STR_LIT:foo>' ) , '<STR_LIT:bar>' ) <EOL> eq ( cf . get ( '<STR_LIT>' , '<STR_LIT:foo>' ) , '<STR_LIT:bar>' ) <EOL> eq ( cf . get ( '<STR_LIT>' , '<STR_LIT>' ) , '<STR_LIT:value>' ) <EOL> eq ( cf . get ( '<STR_LIT>' , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . failIf ( '<STR_LIT>' in cf . options ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' ) <EOL> self . failUnless ( cf . remove_option ( '<STR_LIT>' , '<STR_LIT:foo>' ) , <EOL> "<STR_LIT>" ) <EOL> self . failIf ( cf . has_option ( '<STR_LIT>' , '<STR_LIT:foo>' ) , <EOL> "<STR_LIT>" ) <EOL> self . failIf ( cf . remove_option ( '<STR_LIT>' , '<STR_LIT:foo>' ) , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . assertRaises ( ConfigParser . NoSectionError , <EOL> cf . remove_option , '<STR_LIT>' , '<STR_LIT:foo>' ) <EOL> eq ( cf . get ( '<STR_LIT>' , '<STR_LIT:foo>' ) , <EOL> '<STR_LIT>' ) <EOL> def test_case_sensitivity ( self ) : <EOL> cf = self . newconfig ( ) <EOL> cf . add_section ( "<STR_LIT:A>" ) <EOL> cf . add_section ( "<STR_LIT:a>" ) <EOL> L = cf . sections ( ) <EOL> L . sort ( ) <EOL> eq = self . assertEqual <EOL> eq ( L , [ "<STR_LIT:A>" , "<STR_LIT:a>" ] ) <EOL> cf . set ( "<STR_LIT:a>" , "<STR_LIT:B>" , "<STR_LIT:value>" ) <EOL> eq ( cf . options ( "<STR_LIT:a>" ) , [ "<STR_LIT:b>" ] ) <EOL> eq ( cf . get ( "<STR_LIT:a>" , "<STR_LIT:b>" ) , "<STR_LIT:value>" , <EOL> "<STR_LIT>" ) <EOL> self . failUnless ( cf . has_option ( "<STR_LIT:a>" , "<STR_LIT:b>" ) ) <EOL> cf . set ( "<STR_LIT:A>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> for opt in ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) : <EOL> self . failUnless ( <EOL> cf . has_option ( "<STR_LIT:A>" , opt ) , <EOL> "<STR_LIT>" ) <EOL> eq ( cf . options ( "<STR_LIT:A>" ) , [ "<STR_LIT>" ] ) <EOL> eq ( cf . options ( "<STR_LIT:a>" ) , [ "<STR_LIT:b>" ] ) <EOL> cf . remove_option ( "<STR_LIT:a>" , "<STR_LIT:B>" ) <EOL> eq ( cf . options ( "<STR_LIT:a>" ) , [ ] ) <EOL> cf = self . fromstring ( <EOL> "<STR_LIT>" ) <EOL> eq ( cf . options ( "<STR_LIT>" ) , [ "<STR_LIT>" ] ) <EOL> eq ( cf . get ( "<STR_LIT>" , "<STR_LIT>" ) , "<STR_LIT>" ) <EOL> cf = self . fromstring ( "<STR_LIT>" , <EOL> defaults = { "<STR_LIT:key>" : "<STR_LIT:value>" } ) <EOL> self . failUnless ( cf . has_option ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> def test_default_case_sensitivity ( self ) : <EOL> cf = self . newconfig ( { "<STR_LIT:foo>" : "<STR_LIT>" } ) <EOL> self . assertEqual ( <EOL> cf . get ( "<STR_LIT>" , "<STR_LIT>" ) , "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> cf = self . newconfig ( { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> self . assertEqual ( <EOL> cf . get ( "<STR_LIT>" , "<STR_LIT>" ) , "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> def test_parse_errors ( self ) : <EOL> self . newconfig ( ) <EOL> self . parse_error ( ConfigParser . ParsingError , <EOL> "<STR_LIT>" ) <EOL> self . parse_error ( ConfigParser . ParsingError , <EOL> "<STR_LIT>" ) <EOL> self . parse_error ( ConfigParser . ParsingError , <EOL> "<STR_LIT>" ) <EOL> self . parse_error ( ConfigParser . ParsingError , <EOL> "<STR_LIT>" ) <EOL> self . parse_error ( ConfigParser . ParsingError , <EOL> "<STR_LIT>" ) <EOL> self . parse_error ( ConfigParser . MissingSectionHeaderError , <EOL> "<STR_LIT>" ) <EOL> def parse_error ( self , exc , src ) : <EOL> sio = StringIO . StringIO ( src ) <EOL> self . assertRaises ( exc , self . cf . readfp , sio ) <EOL> def test_query_errors ( self ) : <EOL> cf = self . newconfig ( ) <EOL> self . assertEqual ( cf . sections ( ) , [ ] , <EOL> "<STR_LIT>" ) <EOL> self . failIf ( cf . has_section ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" ) <EOL> self . assertRaises ( ConfigParser . NoSectionError , <EOL> cf . options , "<STR_LIT>" ) <EOL> self . assertRaises ( ConfigParser . NoSectionError , <EOL> cf . set , "<STR_LIT:foo>" , "<STR_LIT:bar>" , "<STR_LIT:value>" ) <EOL> self . get_error ( ConfigParser . NoSectionError , "<STR_LIT:foo>" , "<STR_LIT:bar>" ) <EOL> cf . add_section ( "<STR_LIT:foo>" ) <EOL> self . get_error ( ConfigParser . NoOptionError , "<STR_LIT:foo>" , "<STR_LIT:bar>" ) <EOL> def get_error ( self , exc , section , option ) : <EOL> try : <EOL> self . cf . get ( section , option ) <EOL> except exc , e : <EOL> return e <EOL> else : <EOL> self . fail ( "<STR_LIT>" <EOL> % ( exc . __module__ , exc . __name__ ) ) <EOL> def test_boolean ( self ) : <EOL> cf = self . fromstring ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> for x in range ( <NUM_LIT:1> , <NUM_LIT:5> ) : <EOL> self . failUnless ( cf . getboolean ( '<STR_LIT>' , '<STR_LIT>' % x ) ) <EOL> self . failIf ( cf . getboolean ( '<STR_LIT>' , '<STR_LIT>' % x ) ) <EOL> self . assertRaises ( ValueError , <EOL> cf . getboolean , '<STR_LIT>' , '<STR_LIT>' % x ) <EOL> def test_weird_errors ( self ) : <EOL> cf = self . newconfig ( ) <EOL> cf . add_section ( "<STR_LIT>" ) <EOL> self . assertRaises ( ConfigParser . DuplicateSectionError , <EOL> cf . add_section , "<STR_LIT>" ) <EOL> def test_write ( self ) : <EOL> cf = self . fromstring ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> output = StringIO . StringIO ( ) <EOL> cf . write ( output ) <EOL> self . assertEqual ( <EOL> output . getvalue ( ) , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT:\n>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT:\n>" <EOL> ) <EOL> def test_set_string_types ( self ) : <EOL> cf = self . fromstring ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class mystr ( str ) : <EOL> pass <EOL> cf . set ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> cf . set ( "<STR_LIT>" , "<STR_LIT>" , mystr ( "<STR_LIT>" ) ) <EOL> cf . set ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> cf . set ( "<STR_LIT>" , "<STR_LIT>" , mystr ( "<STR_LIT>" ) ) <EOL> try : <EOL> unicode <EOL> except NameError : <EOL> pass <EOL> else : <EOL> cf . set ( "<STR_LIT>" , "<STR_LIT>" , unicode ( "<STR_LIT>" ) ) <EOL> cf . set ( "<STR_LIT>" , "<STR_LIT>" , unicode ( "<STR_LIT>" ) ) <EOL> def test_read_returns_file_list ( self ) : <EOL> file1 = test_support . findfile ( "<STR_LIT>" ) <EOL> cf = self . newconfig ( ) <EOL> parsed_files = cf . read ( [ file1 , "<STR_LIT>" ] ) <EOL> self . assertEqual ( parsed_files , [ file1 ] ) <EOL> self . assertEqual ( cf . get ( "<STR_LIT>" , "<STR_LIT:foo>" ) , "<STR_LIT>" ) <EOL> cf = self . newconfig ( ) <EOL> parsed_files = cf . read ( file1 ) <EOL> self . assertEqual ( parsed_files , [ file1 ] ) <EOL> self . assertEqual ( cf . get ( "<STR_LIT>" , "<STR_LIT:foo>" ) , "<STR_LIT>" ) <EOL> cf = self . newconfig ( ) <EOL> parsed_files = cf . read ( [ "<STR_LIT>" ] ) <EOL> self . assertEqual ( parsed_files , [ ] ) <EOL> cf = self . newconfig ( ) <EOL> parsed_files = cf . read ( [ ] ) <EOL> self . assertEqual ( parsed_files , [ ] ) <EOL> def get_interpolation_config ( self ) : <EOL> return self . fromstring ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT:\n>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT:\n>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> defaults = { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> def check_items_config ( self , expected ) : <EOL> cf = self . fromstring ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> defaults = { "<STR_LIT:default>" : "<STR_LIT>" } ) <EOL> L = list ( cf . items ( "<STR_LIT>" ) ) <EOL> L . sort ( ) <EOL> self . assertEqual ( L , expected ) <EOL> class ConfigParserTestCase ( TestCaseBase ) : <EOL> config_class = ConfigParser . ConfigParser <EOL> def test_interpolation ( self ) : <EOL> cf = self . get_interpolation_config ( ) <EOL> eq = self . assertEqual <EOL> eq ( cf . get ( "<STR_LIT>" , "<STR_LIT>" ) , "<STR_LIT>" ) <EOL> eq ( cf . get ( "<STR_LIT>" , "<STR_LIT:bar>" ) , "<STR_LIT>" ) <EOL> eq ( cf . get ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" ) <EOL> eq ( cf . get ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" ) <EOL> self . get_error ( ConfigParser . InterpolationDepthError , "<STR_LIT>" , "<STR_LIT>" ) <EOL> def test_interpolation_missing_value ( self ) : <EOL> cf = self . get_interpolation_config ( ) <EOL> e = self . get_error ( ConfigParser . InterpolationError , <EOL> "<STR_LIT>" , "<STR_LIT:name>" ) <EOL> self . assertEqual ( e . reference , "<STR_LIT>" ) <EOL> self . assertEqual ( e . section , "<STR_LIT>" ) <EOL> self . assertEqual ( e . option , "<STR_LIT:name>" ) <EOL> def test_items ( self ) : <EOL> self . check_items_config ( [ ( '<STR_LIT:default>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:key>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:name>' , '<STR_LIT:value>' ) ] ) <EOL> def test_set_nonstring_types ( self ) : <EOL> cf = self . newconfig ( ) <EOL> cf . add_section ( '<STR_LIT>' ) <EOL> cf . set ( '<STR_LIT>' , '<STR_LIT:int>' , <NUM_LIT:1> ) <EOL> cf . set ( '<STR_LIT>' , '<STR_LIT:list>' , [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:5> , <NUM_LIT:8> , <NUM_LIT> , '<STR_LIT>' ] ) <EOL> cf . set ( '<STR_LIT>' , '<STR_LIT>' , { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) <EOL> cf . set ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertEqual ( cf . get ( '<STR_LIT>' , '<STR_LIT:int>' , raw = True ) , <NUM_LIT:1> ) <EOL> self . assertRaises ( TypeError , cf . get , '<STR_LIT>' , '<STR_LIT:int>' ) <EOL> self . assertEqual ( cf . get ( '<STR_LIT>' , '<STR_LIT:list>' , raw = True ) , <EOL> [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:5> , <NUM_LIT:8> , <NUM_LIT> , '<STR_LIT>' ] ) <EOL> self . assertRaises ( TypeError , cf . get , '<STR_LIT>' , '<STR_LIT:list>' ) <EOL> self . assertEqual ( cf . get ( '<STR_LIT>' , '<STR_LIT>' , raw = True ) , <EOL> { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertRaises ( TypeError , cf . get , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertEqual ( cf . get ( '<STR_LIT>' , '<STR_LIT>' , <EOL> raw = True ) , '<STR_LIT>' ) <EOL> self . assertRaises ( ValueError , cf . get , '<STR_LIT>' , <EOL> '<STR_LIT>' , raw = False ) <EOL> class RawConfigParserTestCase ( TestCaseBase ) : <EOL> config_class = ConfigParser . RawConfigParser <EOL> def test_interpolation ( self ) : <EOL> cf = self . get_interpolation_config ( ) <EOL> eq = self . assertEqual <EOL> eq ( cf . get ( "<STR_LIT>" , "<STR_LIT>" ) , "<STR_LIT>" ) <EOL> eq ( cf . get ( "<STR_LIT>" , "<STR_LIT:bar>" ) , <EOL> "<STR_LIT>" ) <EOL> eq ( cf . get ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" ) <EOL> eq ( cf . get ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" ) <EOL> eq ( cf . get ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" ) <EOL> def test_items ( self ) : <EOL> self . check_items_config ( [ ( '<STR_LIT:default>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:key>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:name>' , '<STR_LIT:value>' ) ] ) <EOL> def test_set_nonstring_types ( self ) : <EOL> cf = self . newconfig ( ) <EOL> cf . add_section ( '<STR_LIT>' ) <EOL> cf . set ( '<STR_LIT>' , '<STR_LIT:int>' , <NUM_LIT:1> ) <EOL> cf . set ( '<STR_LIT>' , '<STR_LIT:list>' , [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:5> , <NUM_LIT:8> , <NUM_LIT> ] ) <EOL> cf . set ( '<STR_LIT>' , '<STR_LIT>' , { '<STR_LIT>' : <NUM_LIT> } ) <EOL> self . assertEqual ( cf . get ( '<STR_LIT>' , '<STR_LIT:int>' ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( cf . get ( '<STR_LIT>' , '<STR_LIT:list>' ) , <EOL> [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:5> , <NUM_LIT:8> , <NUM_LIT> ] ) <EOL> self . assertEqual ( cf . get ( '<STR_LIT>' , '<STR_LIT>' ) , { '<STR_LIT>' : <NUM_LIT> } ) <EOL> class SafeConfigParserTestCase ( ConfigParserTestCase ) : <EOL> config_class = ConfigParser . SafeConfigParser <EOL> def test_safe_interpolation ( self ) : <EOL> cf = self . fromstring ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . assertEqual ( cf . get ( "<STR_LIT>" , "<STR_LIT>" ) , "<STR_LIT>" ) <EOL> self . assertEqual ( cf . get ( "<STR_LIT>" , "<STR_LIT>" ) , "<STR_LIT>" ) <EOL> def test_set_nonstring_types ( self ) : <EOL> cf = self . fromstring ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . assertRaises ( TypeError , cf . set , "<STR_LIT>" , "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . assertRaises ( TypeError , cf . set , "<STR_LIT>" , "<STR_LIT>" , <NUM_LIT:1.0> ) <EOL> self . assertRaises ( TypeError , cf . set , "<STR_LIT>" , "<STR_LIT>" , object ( ) ) <EOL> self . assertRaises ( TypeError , cf . set , "<STR_LIT>" , "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . assertRaises ( TypeError , cf . set , "<STR_LIT>" , "<STR_LIT>" , <NUM_LIT:1.0> ) <EOL> self . assertRaises ( TypeError , cf . set , "<STR_LIT>" , "<STR_LIT>" , object ( ) ) <EOL> def test_main ( ) : <EOL> test_support . run_unittest ( <EOL> ConfigParserTestCase , <EOL> RawConfigParserTestCase , <EOL> SafeConfigParserTestCase <EOL> ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> test_main ( ) </s>
