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 | cc1f951 | 2001-02-14 15:30:31 +0000 | [diff] [blame] | 14 | verify(L == [r'Commented Bar', |
| 15 | r'Foo Bar', |
| 16 | r'Internationalized Stuff', |
Andrew M. Kuchling | 1bf7117 | 2002-03-08 18:10:12 +0000 | [diff] [blame^] | 17 | r'Long Line', |
Fred Drake | cc1f951 | 2001-02-14 15:30:31 +0000 | [diff] [blame] | 18 | r'Section\with$weird%characters[' '\t', |
| 19 | r'Spacey Bar', |
| 20 | ], |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 21 | "unexpected list of section names") |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 22 | |
| 23 | # The use of spaces in the section names serves as a regression test for |
| 24 | # SourceForge bug #115357. |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 25 | # http://sourceforge.net/bugs/?func=detailbug&group_id=5470&bug_id=115357 |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 26 | verify(cf.get('Foo Bar', 'foo', raw=1) == 'bar') |
| 27 | verify(cf.get('Spacey Bar', 'foo', raw=1) == 'bar') |
| 28 | verify(cf.get('Commented Bar', 'foo', raw=1) == 'bar') |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 29 | |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 30 | verify('__name__' not in cf.options("Foo Bar"), |
| 31 | '__name__ "option" should not be exposed by the API!') |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 32 | |
Fred Drake | 3d5f7e8 | 2000-12-04 16:30:40 +0000 | [diff] [blame] | 33 | # Make sure the right things happen for remove_option(); |
| 34 | # added to include check for SourceForge bug #123324: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 35 | verify(cf.remove_option('Foo Bar', 'foo'), |
| 36 | "remove_option() failed to report existance of option") |
| 37 | verify(not cf.has_option('Foo Bar', 'foo'), |
| 38 | "remove_option() failed to remove option") |
| 39 | verify(not cf.remove_option('Foo Bar', 'foo'), |
| 40 | "remove_option() failed to report non-existance of option" |
| 41 | " that was removed") |
Fred Drake | 3d5f7e8 | 2000-12-04 16:30:40 +0000 | [diff] [blame] | 42 | try: |
| 43 | cf.remove_option('No Such Section', 'foo') |
| 44 | except ConfigParser.NoSectionError: |
| 45 | pass |
| 46 | else: |
| 47 | raise TestFailed( |
| 48 | "remove_option() failed to report non-existance of option" |
| 49 | " that never existed") |
| 50 | |
Andrew M. Kuchling | 1bf7117 | 2002-03-08 18:10:12 +0000 | [diff] [blame^] | 51 | verify(cf.get('Long Line', 'foo', raw=1) == |
| 52 | 'this line is much, much longer than my editor\nlikes it.') |
Fred Drake | 3d5f7e8 | 2000-12-04 16:30:40 +0000 | [diff] [blame] | 53 | |
Andrew M. Kuchling | 1bf7117 | 2002-03-08 18:10:12 +0000 | [diff] [blame^] | 54 | |
| 55 | def write(src): |
| 56 | print "Testing writing of files..." |
| 57 | cf = ConfigParser.ConfigParser() |
| 58 | sio = StringIO.StringIO(src) |
| 59 | cf.readfp(sio) |
| 60 | output = StringIO.StringIO() |
| 61 | cf.write(output) |
| 62 | verify(output, """[DEFAULT] |
| 63 | foo = another very |
| 64 | long line |
| 65 | |
| 66 | [Long Line] |
| 67 | foo = this line is much, much longer than my editor |
| 68 | likes it. |
| 69 | """) |
| 70 | |
Fred Drake | 3c823aa | 2001-02-26 21:55:34 +0000 | [diff] [blame] | 71 | def case_sensitivity(): |
| 72 | print "Testing case sensitivity..." |
| 73 | cf = ConfigParser.ConfigParser() |
| 74 | cf.add_section("A") |
| 75 | cf.add_section("a") |
| 76 | L = cf.sections() |
| 77 | L.sort() |
| 78 | verify(L == ["A", "a"]) |
| 79 | cf.set("a", "B", "value") |
| 80 | verify(cf.options("a") == ["b"]) |
| 81 | verify(cf.get("a", "b", raw=1) == "value", |
| 82 | "could not locate option, expecting case-insensitive option names") |
| 83 | verify(cf.has_option("a", "b")) |
| 84 | cf.set("A", "A-B", "A-B value") |
| 85 | for opt in ("a-b", "A-b", "a-B", "A-B"): |
| 86 | verify(cf.has_option("A", opt), |
| 87 | "has_option() returned false for option which should exist") |
| 88 | verify(cf.options("A") == ["a-b"]) |
| 89 | verify(cf.options("a") == ["b"]) |
| 90 | cf.remove_option("a", "B") |
| 91 | verify(cf.options("a") == []) |
| 92 | |
Fred Drake | beb6713 | 2001-07-06 17:22:48 +0000 | [diff] [blame] | 93 | # SF bug #432369: |
| 94 | cf = ConfigParser.ConfigParser() |
| 95 | sio = StringIO.StringIO("[MySection]\nOption: first line\n\tsecond line\n") |
| 96 | cf.readfp(sio) |
| 97 | verify(cf.options("MySection") == ["option"]) |
| 98 | verify(cf.get("MySection", "Option") == "first line\nsecond line") |
| 99 | |
Fred Drake | 3c823aa | 2001-02-26 21:55:34 +0000 | [diff] [blame] | 100 | |
Fred Drake | 168bead | 2001-10-08 17:13:12 +0000 | [diff] [blame] | 101 | def boolean(src): |
| 102 | print "Testing interpretation of boolean Values..." |
| 103 | cf = ConfigParser.ConfigParser() |
| 104 | sio = StringIO.StringIO(src) |
| 105 | cf.readfp(sio) |
| 106 | for x in range(1, 5): |
| 107 | verify(cf.getboolean('BOOLTEST', 't%d' % (x)) == 1) |
| 108 | for x in range(1, 5): |
| 109 | verify(cf.getboolean('BOOLTEST', 'f%d' % (x)) == 0) |
| 110 | for x in range(1, 5): |
| 111 | try: |
| 112 | cf.getboolean('BOOLTEST', 'e%d' % (x)) |
| 113 | except ValueError: |
| 114 | pass |
| 115 | else: |
| 116 | raise TestFailed( |
| 117 | "getboolean() failed to report a non boolean value") |
| 118 | |
| 119 | |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 120 | def interpolation(src): |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 121 | print "Testing value interpolation..." |
| 122 | cf = ConfigParser.ConfigParser({"getname": "%(__name__)s"}) |
| 123 | sio = StringIO.StringIO(src) |
| 124 | cf.readfp(sio) |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 125 | verify(cf.get("Foo", "getname") == "Foo") |
| 126 | verify(cf.get("Foo", "bar") == "something with interpolation (1 step)") |
| 127 | verify(cf.get("Foo", "bar9") |
| 128 | == "something with lots of interpolation (9 steps)") |
| 129 | verify(cf.get("Foo", "bar10") |
| 130 | == "something with lots of interpolation (10 steps)") |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 131 | expect_get_error(cf, ConfigParser.InterpolationDepthError, "Foo", "bar11") |
| 132 | |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 133 | |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 134 | def parse_errors(): |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 135 | print "Testing parse errors..." |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 136 | expect_parse_error(ConfigParser.ParsingError, |
| 137 | """[Foo]\n extra-spaces: splat\n""") |
| 138 | expect_parse_error(ConfigParser.ParsingError, |
| 139 | """[Foo]\n extra-spaces= splat\n""") |
| 140 | expect_parse_error(ConfigParser.ParsingError, |
| 141 | """[Foo]\noption-without-value\n""") |
| 142 | expect_parse_error(ConfigParser.ParsingError, |
| 143 | """[Foo]\n:value-without-option-name\n""") |
| 144 | expect_parse_error(ConfigParser.ParsingError, |
| 145 | """[Foo]\n=value-without-option-name\n""") |
| 146 | expect_parse_error(ConfigParser.MissingSectionHeaderError, |
| 147 | """No Section!\n""") |
| 148 | |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 149 | |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 150 | def query_errors(): |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 151 | print "Testing query interface..." |
| 152 | cf = ConfigParser.ConfigParser() |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 153 | verify(cf.sections() == [], |
| 154 | "new ConfigParser should have no defined sections") |
| 155 | verify(not cf.has_section("Foo"), |
| 156 | "new ConfigParser should have no acknowledged sections") |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 157 | try: |
| 158 | cf.options("Foo") |
| 159 | except ConfigParser.NoSectionError, e: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 160 | pass |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 161 | else: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 162 | raise TestFailed( |
| 163 | "Failed to catch expected NoSectionError from options()") |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 164 | try: |
| 165 | cf.set("foo", "bar", "value") |
| 166 | except ConfigParser.NoSectionError, e: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 167 | pass |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 168 | else: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 169 | raise TestFailed("Failed to catch expected NoSectionError from set()") |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 170 | expect_get_error(cf, ConfigParser.NoSectionError, "foo", "bar") |
| 171 | cf.add_section("foo") |
| 172 | expect_get_error(cf, ConfigParser.NoOptionError, "foo", "bar") |
| 173 | |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 174 | |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 175 | def weird_errors(): |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 176 | print "Testing miscellaneous error conditions..." |
| 177 | cf = ConfigParser.ConfigParser() |
| 178 | cf.add_section("Foo") |
| 179 | try: |
| 180 | cf.add_section("Foo") |
| 181 | except ConfigParser.DuplicateSectionError, e: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 182 | pass |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 183 | else: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 184 | raise TestFailed("Failed to catch expected DuplicateSectionError") |
| 185 | |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 186 | |
| 187 | def expect_get_error(cf, exctype, section, option, raw=0): |
| 188 | try: |
| 189 | cf.get(section, option, raw=raw) |
| 190 | except exctype, e: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 191 | pass |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 192 | else: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 193 | raise TestFailed("Failed to catch expected " + exctype.__name__) |
| 194 | |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 195 | |
| 196 | def expect_parse_error(exctype, src): |
| 197 | cf = ConfigParser.ConfigParser() |
| 198 | sio = StringIO.StringIO(src) |
| 199 | try: |
| 200 | cf.readfp(sio) |
| 201 | except exctype, e: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 202 | pass |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 203 | else: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 204 | raise TestFailed("Failed to catch expected " + exctype.__name__) |
| 205 | |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 206 | |
| 207 | basic(r""" |
| 208 | [Foo Bar] |
| 209 | foo=bar |
| 210 | [Spacey Bar] |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 211 | foo = bar |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 212 | [Commented Bar] |
| 213 | foo: bar ; comment |
Andrew M. Kuchling | 1bf7117 | 2002-03-08 18:10:12 +0000 | [diff] [blame^] | 214 | [Long Line] |
| 215 | foo: this line is much, much longer than my editor |
| 216 | likes it. |
Fred Drake | cc1f951 | 2001-02-14 15:30:31 +0000 | [diff] [blame] | 217 | [Section\with$weird%characters[""" '\t' r"""] |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 218 | [Internationalized Stuff] |
| 219 | foo[bg]: Bulgarian |
| 220 | foo=Default |
| 221 | foo[en]=English |
| 222 | foo[de]=Deutsch |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 223 | """) |
Andrew M. Kuchling | 1bf7117 | 2002-03-08 18:10:12 +0000 | [diff] [blame^] | 224 | write("""[Long Line] |
| 225 | foo: this line is much, much longer than my editor |
| 226 | likes it. |
| 227 | [DEFAULT] |
| 228 | foo: another very |
| 229 | long line""") |
Fred Drake | 3c823aa | 2001-02-26 21:55:34 +0000 | [diff] [blame] | 230 | case_sensitivity() |
Fred Drake | 168bead | 2001-10-08 17:13:12 +0000 | [diff] [blame] | 231 | boolean(r""" |
| 232 | [BOOLTEST] |
| 233 | T1=1 |
| 234 | T2=TRUE |
| 235 | T3=True |
| 236 | T4=oN |
| 237 | T5=yes |
| 238 | F1=0 |
| 239 | F2=FALSE |
| 240 | F3=False |
| 241 | F4=oFF |
| 242 | F5=nO |
| 243 | E1=2 |
| 244 | E2=foo |
| 245 | E3=-1 |
| 246 | E4=0.1 |
| 247 | E5=FALSE AND MORE |
| 248 | """) |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 249 | interpolation(r""" |
| 250 | [Foo] |
| 251 | bar=something %(with1)s interpolation (1 step) |
| 252 | bar9=something %(with9)s lots of interpolation (9 steps) |
| 253 | bar10=something %(with10)s lots of interpolation (10 steps) |
| 254 | bar11=something %(with11)s lots of interpolation (11 steps) |
| 255 | with11=%(with10)s |
| 256 | with10=%(with9)s |
| 257 | with9=%(with8)s |
| 258 | with8=%(with7)s |
| 259 | with7=%(with6)s |
| 260 | with6=%(with5)s |
| 261 | with5=%(with4)s |
| 262 | with4=%(with3)s |
| 263 | with3=%(with2)s |
| 264 | with2=%(with1)s |
| 265 | with1=with |
| 266 | |
| 267 | [Mutual Recursion] |
| 268 | foo=%(bar)s |
| 269 | bar=%(foo)s |
| 270 | """) |
| 271 | parse_errors() |
| 272 | query_errors() |
| 273 | weird_errors() |