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 | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 51 | def interpolation(src): |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 52 | print "Testing value interpolation..." |
| 53 | cf = ConfigParser.ConfigParser({"getname": "%(__name__)s"}) |
| 54 | sio = StringIO.StringIO(src) |
| 55 | cf.readfp(sio) |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 56 | verify(cf.get("Foo", "getname") == "Foo") |
| 57 | verify(cf.get("Foo", "bar") == "something with interpolation (1 step)") |
| 58 | verify(cf.get("Foo", "bar9") |
| 59 | == "something with lots of interpolation (9 steps)") |
| 60 | verify(cf.get("Foo", "bar10") |
| 61 | == "something with lots of interpolation (10 steps)") |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 62 | expect_get_error(cf, ConfigParser.InterpolationDepthError, "Foo", "bar11") |
| 63 | |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 64 | |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 65 | def parse_errors(): |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 66 | print "Testing parse errors..." |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 67 | expect_parse_error(ConfigParser.ParsingError, |
| 68 | """[Foo]\n extra-spaces: splat\n""") |
| 69 | expect_parse_error(ConfigParser.ParsingError, |
| 70 | """[Foo]\n extra-spaces= splat\n""") |
| 71 | expect_parse_error(ConfigParser.ParsingError, |
| 72 | """[Foo]\noption-without-value\n""") |
| 73 | expect_parse_error(ConfigParser.ParsingError, |
| 74 | """[Foo]\n:value-without-option-name\n""") |
| 75 | expect_parse_error(ConfigParser.ParsingError, |
| 76 | """[Foo]\n=value-without-option-name\n""") |
| 77 | expect_parse_error(ConfigParser.MissingSectionHeaderError, |
| 78 | """No Section!\n""") |
| 79 | |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 80 | |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 81 | def query_errors(): |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 82 | print "Testing query interface..." |
| 83 | cf = ConfigParser.ConfigParser() |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 84 | verify(cf.sections() == [], |
| 85 | "new ConfigParser should have no defined sections") |
| 86 | verify(not cf.has_section("Foo"), |
| 87 | "new ConfigParser should have no acknowledged sections") |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 88 | try: |
| 89 | cf.options("Foo") |
| 90 | except ConfigParser.NoSectionError, e: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 91 | pass |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 92 | else: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 93 | raise TestFailed( |
| 94 | "Failed to catch expected NoSectionError from options()") |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 95 | try: |
| 96 | cf.set("foo", "bar", "value") |
| 97 | except ConfigParser.NoSectionError, e: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 98 | pass |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 99 | else: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 100 | raise TestFailed("Failed to catch expected NoSectionError from set()") |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 101 | expect_get_error(cf, ConfigParser.NoSectionError, "foo", "bar") |
| 102 | cf.add_section("foo") |
| 103 | expect_get_error(cf, ConfigParser.NoOptionError, "foo", "bar") |
| 104 | |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 105 | |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 106 | def weird_errors(): |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 107 | print "Testing miscellaneous error conditions..." |
| 108 | cf = ConfigParser.ConfigParser() |
| 109 | cf.add_section("Foo") |
| 110 | try: |
| 111 | cf.add_section("Foo") |
| 112 | except ConfigParser.DuplicateSectionError, e: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 113 | pass |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 114 | else: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 115 | raise TestFailed("Failed to catch expected DuplicateSectionError") |
| 116 | |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 117 | |
| 118 | def expect_get_error(cf, exctype, section, option, raw=0): |
| 119 | try: |
| 120 | cf.get(section, option, raw=raw) |
| 121 | except exctype, e: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 122 | pass |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 123 | else: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 124 | raise TestFailed("Failed to catch expected " + exctype.__name__) |
| 125 | |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 126 | |
| 127 | def expect_parse_error(exctype, src): |
| 128 | cf = ConfigParser.ConfigParser() |
| 129 | sio = StringIO.StringIO(src) |
| 130 | try: |
| 131 | cf.readfp(sio) |
| 132 | except exctype, e: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 133 | pass |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 134 | else: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 135 | raise TestFailed("Failed to catch expected " + exctype.__name__) |
| 136 | |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 137 | |
| 138 | basic(r""" |
| 139 | [Foo Bar] |
| 140 | foo=bar |
| 141 | [Spacey Bar] |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 142 | foo = bar |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 143 | [Commented Bar] |
| 144 | foo: bar ; comment |
Fred Drake | cc1f951 | 2001-02-14 15:30:31 +0000 | [diff] [blame] | 145 | [Section\with$weird%characters[""" '\t' r"""] |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 146 | [Internationalized Stuff] |
| 147 | foo[bg]: Bulgarian |
| 148 | foo=Default |
| 149 | foo[en]=English |
| 150 | foo[de]=Deutsch |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 151 | """) |
| 152 | interpolation(r""" |
| 153 | [Foo] |
| 154 | bar=something %(with1)s interpolation (1 step) |
| 155 | bar9=something %(with9)s lots of interpolation (9 steps) |
| 156 | bar10=something %(with10)s lots of interpolation (10 steps) |
| 157 | bar11=something %(with11)s lots of interpolation (11 steps) |
| 158 | with11=%(with10)s |
| 159 | with10=%(with9)s |
| 160 | with9=%(with8)s |
| 161 | with8=%(with7)s |
| 162 | with7=%(with6)s |
| 163 | with6=%(with5)s |
| 164 | with5=%(with4)s |
| 165 | with4=%(with3)s |
| 166 | with3=%(with2)s |
| 167 | with2=%(with1)s |
| 168 | with1=with |
| 169 | |
| 170 | [Mutual Recursion] |
| 171 | foo=%(bar)s |
| 172 | bar=%(foo)s |
| 173 | """) |
| 174 | parse_errors() |
| 175 | query_errors() |
| 176 | weird_errors() |