Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 1 | import ConfigParser |
| 2 | import StringIO |
| 3 | |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame^] | 4 | from test_support import TestFailed, verify |
Fred Drake | 3d5f7e8 | 2000-12-04 16:30:40 +0000 | [diff] [blame] | 5 | |
| 6 | |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 7 | def basic(src): |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 8 | print "Testing basic accessors..." |
| 9 | cf = ConfigParser.ConfigParser() |
| 10 | sio = StringIO.StringIO(src) |
| 11 | cf.readfp(sio) |
| 12 | L = cf.sections() |
| 13 | L.sort() |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame^] | 14 | verify(L == ['Commented Bar', 'Foo Bar', |
| 15 | 'Internationalized Stuff', 'Spacey Bar'], |
| 16 | "unexpected list of section names") |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 17 | |
| 18 | # The use of spaces in the section names serves as a regression test for |
| 19 | # SourceForge bug #115357. |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 20 | # http://sourceforge.net/bugs/?func=detailbug&group_id=5470&bug_id=115357 |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame^] | 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') |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 24 | |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame^] | 25 | verify('__name__' not in cf.options("Foo Bar"), |
| 26 | '__name__ "option" should not be exposed by the API!') |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 27 | |
Fred Drake | 3d5f7e8 | 2000-12-04 16:30:40 +0000 | [diff] [blame] | 28 | # Make sure the right things happen for remove_option(); |
| 29 | # added to include check for SourceForge bug #123324: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame^] | 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") |
Fred Drake | 3d5f7e8 | 2000-12-04 16:30:40 +0000 | [diff] [blame] | 37 | try: |
| 38 | cf.remove_option('No Such Section', 'foo') |
| 39 | except ConfigParser.NoSectionError: |
| 40 | pass |
| 41 | else: |
| 42 | raise TestFailed( |
| 43 | "remove_option() failed to report non-existance of option" |
| 44 | " that never existed") |
| 45 | |
| 46 | |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 47 | def interpolation(src): |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 48 | print "Testing value interpolation..." |
| 49 | cf = ConfigParser.ConfigParser({"getname": "%(__name__)s"}) |
| 50 | sio = StringIO.StringIO(src) |
| 51 | cf.readfp(sio) |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame^] | 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)") |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 58 | expect_get_error(cf, ConfigParser.InterpolationDepthError, "Foo", "bar11") |
| 59 | |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame^] | 60 | |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 61 | def parse_errors(): |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame^] | 62 | print "Testing parse errors..." |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 63 | expect_parse_error(ConfigParser.ParsingError, |
| 64 | """[Foo]\n extra-spaces: splat\n""") |
| 65 | expect_parse_error(ConfigParser.ParsingError, |
| 66 | """[Foo]\n extra-spaces= splat\n""") |
| 67 | expect_parse_error(ConfigParser.ParsingError, |
| 68 | """[Foo]\noption-without-value\n""") |
| 69 | expect_parse_error(ConfigParser.ParsingError, |
| 70 | """[Foo]\n:value-without-option-name\n""") |
| 71 | expect_parse_error(ConfigParser.ParsingError, |
| 72 | """[Foo]\n=value-without-option-name\n""") |
| 73 | expect_parse_error(ConfigParser.MissingSectionHeaderError, |
| 74 | """No Section!\n""") |
| 75 | |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame^] | 76 | |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 77 | def query_errors(): |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 78 | print "Testing query interface..." |
| 79 | cf = ConfigParser.ConfigParser() |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame^] | 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") |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 84 | try: |
| 85 | cf.options("Foo") |
| 86 | except ConfigParser.NoSectionError, e: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame^] | 87 | pass |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 88 | else: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame^] | 89 | raise TestFailed( |
| 90 | "Failed to catch expected NoSectionError from options()") |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 91 | try: |
| 92 | cf.set("foo", "bar", "value") |
| 93 | except ConfigParser.NoSectionError, e: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame^] | 94 | pass |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 95 | else: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame^] | 96 | raise TestFailed("Failed to catch expected NoSectionError from set()") |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 97 | expect_get_error(cf, ConfigParser.NoSectionError, "foo", "bar") |
| 98 | cf.add_section("foo") |
| 99 | expect_get_error(cf, ConfigParser.NoOptionError, "foo", "bar") |
| 100 | |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame^] | 101 | |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 102 | def weird_errors(): |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 103 | print "Testing miscellaneous error conditions..." |
| 104 | cf = ConfigParser.ConfigParser() |
| 105 | cf.add_section("Foo") |
| 106 | try: |
| 107 | cf.add_section("Foo") |
| 108 | except ConfigParser.DuplicateSectionError, e: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame^] | 109 | pass |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 110 | else: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame^] | 111 | raise TestFailed("Failed to catch expected DuplicateSectionError") |
| 112 | |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 113 | |
| 114 | def expect_get_error(cf, exctype, section, option, raw=0): |
| 115 | try: |
| 116 | cf.get(section, option, raw=raw) |
| 117 | except exctype, e: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame^] | 118 | pass |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 119 | else: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame^] | 120 | raise TestFailed("Failed to catch expected " + exctype.__name__) |
| 121 | |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 122 | |
| 123 | def expect_parse_error(exctype, src): |
| 124 | cf = ConfigParser.ConfigParser() |
| 125 | sio = StringIO.StringIO(src) |
| 126 | try: |
| 127 | cf.readfp(sio) |
| 128 | except exctype, e: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame^] | 129 | pass |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 130 | else: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame^] | 131 | raise TestFailed("Failed to catch expected " + exctype.__name__) |
| 132 | |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 133 | |
| 134 | basic(r""" |
| 135 | [Foo Bar] |
| 136 | foo=bar |
| 137 | [Spacey Bar] |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 138 | foo = bar |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 139 | [Commented Bar] |
| 140 | foo: bar ; comment |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame^] | 141 | [Internationalized Stuff] |
| 142 | foo[bg]: Bulgarian |
| 143 | foo=Default |
| 144 | foo[en]=English |
| 145 | foo[de]=Deutsch |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 146 | """) |
| 147 | interpolation(r""" |
| 148 | [Foo] |
| 149 | bar=something %(with1)s interpolation (1 step) |
| 150 | bar9=something %(with9)s lots of interpolation (9 steps) |
| 151 | bar10=something %(with10)s lots of interpolation (10 steps) |
| 152 | bar11=something %(with11)s lots of interpolation (11 steps) |
| 153 | with11=%(with10)s |
| 154 | with10=%(with9)s |
| 155 | with9=%(with8)s |
| 156 | with8=%(with7)s |
| 157 | with7=%(with6)s |
| 158 | with6=%(with5)s |
| 159 | with5=%(with4)s |
| 160 | with4=%(with3)s |
| 161 | with3=%(with2)s |
| 162 | with2=%(with1)s |
| 163 | with1=with |
| 164 | |
| 165 | [Mutual Recursion] |
| 166 | foo=%(bar)s |
| 167 | bar=%(foo)s |
| 168 | """) |
| 169 | parse_errors() |
| 170 | query_errors() |
| 171 | weird_errors() |