Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 1 | import ConfigParser |
| 2 | import StringIO |
| 3 | |
Fred Drake | 3d5f7e8 | 2000-12-04 16:30:40 +0000 | [diff] [blame^] | 4 | from test_support import TestFailed |
| 5 | |
| 6 | |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 7 | def basic(src): |
| 8 | print |
| 9 | print "Testing basic accessors..." |
| 10 | cf = ConfigParser.ConfigParser() |
| 11 | sio = StringIO.StringIO(src) |
| 12 | cf.readfp(sio) |
| 13 | L = cf.sections() |
| 14 | L.sort() |
| 15 | print L |
| 16 | for s in L: |
| 17 | print "%s: %s" % (s, cf.options(s)) |
| 18 | |
| 19 | # The use of spaces in the section names serves as a regression test for |
| 20 | # SourceForge bug #115357. |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 21 | # http://sourceforge.net/bugs/?func=detailbug&group_id=5470&bug_id=115357 |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 22 | print `cf.get('Foo Bar', 'foo', raw=1)` |
| 23 | print `cf.get('Spacey Bar', 'foo', raw=1)` |
| 24 | print `cf.get('Commented Bar', 'foo', raw=1)` |
| 25 | |
| 26 | if '__name__' in cf.options("Foo Bar"): |
| 27 | print '__name__ "option" should not be exposed by the API!' |
| 28 | else: |
| 29 | print '__name__ "option" properly hidden by the API.' |
| 30 | |
Fred Drake | 3d5f7e8 | 2000-12-04 16:30:40 +0000 | [diff] [blame^] | 31 | # Make sure the right things happen for remove_option(); |
| 32 | # added to include check for SourceForge bug #123324: |
| 33 | if not cf.remove_option('Foo Bar', 'foo'): |
| 34 | raise TestFailed( |
| 35 | "remove_option() failed to report existance of option") |
| 36 | if cf.has_option('Foo Bar', 'foo'): |
| 37 | raise TestFailed("remove_option() failed to remove option") |
| 38 | if cf.remove_option('Foo Bar', 'foo'): |
| 39 | raise TestFailed( |
| 40 | "remove_option() failed to report non-existance of option" |
| 41 | " that was removed") |
| 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 | |
| 51 | |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 52 | def interpolation(src): |
| 53 | print |
| 54 | print "Testing value interpolation..." |
| 55 | cf = ConfigParser.ConfigParser({"getname": "%(__name__)s"}) |
| 56 | sio = StringIO.StringIO(src) |
| 57 | cf.readfp(sio) |
| 58 | print `cf.get("Foo", "getname")` |
| 59 | print `cf.get("Foo", "bar")` |
| 60 | print `cf.get("Foo", "bar9")` |
| 61 | print `cf.get("Foo", "bar10")` |
| 62 | expect_get_error(cf, ConfigParser.InterpolationDepthError, "Foo", "bar11") |
| 63 | |
| 64 | def parse_errors(): |
| 65 | print |
| 66 | print "Testing for parsing errors..." |
| 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 | |
| 80 | def query_errors(): |
| 81 | print |
| 82 | print "Testing query interface..." |
| 83 | cf = ConfigParser.ConfigParser() |
| 84 | print cf.sections() |
| 85 | print "Has section 'Foo'?", cf.has_section("Foo") |
| 86 | try: |
| 87 | cf.options("Foo") |
| 88 | except ConfigParser.NoSectionError, e: |
| 89 | print "Caught expected NoSectionError:", e |
| 90 | else: |
| 91 | print "Failed to catch expected NoSectionError from options()" |
| 92 | try: |
| 93 | cf.set("foo", "bar", "value") |
| 94 | except ConfigParser.NoSectionError, e: |
| 95 | print "Caught expected NoSectionError:", e |
| 96 | else: |
| 97 | print "Failed to catch expected NoSectionError from set()" |
| 98 | expect_get_error(cf, ConfigParser.NoSectionError, "foo", "bar") |
| 99 | cf.add_section("foo") |
| 100 | expect_get_error(cf, ConfigParser.NoOptionError, "foo", "bar") |
| 101 | |
| 102 | def weird_errors(): |
| 103 | print |
| 104 | print "Testing miscellaneous error conditions..." |
| 105 | cf = ConfigParser.ConfigParser() |
| 106 | cf.add_section("Foo") |
| 107 | try: |
| 108 | cf.add_section("Foo") |
| 109 | except ConfigParser.DuplicateSectionError, e: |
| 110 | print "Caught expected DuplicateSectionError:", e |
| 111 | else: |
| 112 | print "Failed to catch expected DuplicateSectionError" |
| 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: |
| 118 | print "Caught expected", exctype.__name__, ":" |
| 119 | print e |
| 120 | else: |
| 121 | print "Failed to catch expected", exctype.__name__ |
| 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: |
| 129 | print "Caught expected exception:", e |
| 130 | else: |
| 131 | print "Failed to catch expected", exctype.__name__ |
| 132 | |
| 133 | basic(r""" |
| 134 | [Foo Bar] |
| 135 | foo=bar |
| 136 | [Spacey Bar] |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 137 | foo = bar |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 138 | [Commented Bar] |
| 139 | foo: bar ; comment |
| 140 | """) |
| 141 | interpolation(r""" |
| 142 | [Foo] |
| 143 | bar=something %(with1)s interpolation (1 step) |
| 144 | bar9=something %(with9)s lots of interpolation (9 steps) |
| 145 | bar10=something %(with10)s lots of interpolation (10 steps) |
| 146 | bar11=something %(with11)s lots of interpolation (11 steps) |
| 147 | with11=%(with10)s |
| 148 | with10=%(with9)s |
| 149 | with9=%(with8)s |
| 150 | with8=%(with7)s |
| 151 | with7=%(with6)s |
| 152 | with6=%(with5)s |
| 153 | with5=%(with4)s |
| 154 | with4=%(with3)s |
| 155 | with3=%(with2)s |
| 156 | with2=%(with1)s |
| 157 | with1=with |
| 158 | |
| 159 | [Mutual Recursion] |
| 160 | foo=%(bar)s |
| 161 | bar=%(foo)s |
| 162 | """) |
| 163 | parse_errors() |
| 164 | query_errors() |
| 165 | weird_errors() |