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