|
| 1 | +import ConfigParser |
| 2 | +import StringIO |
| 3 | + |
| 4 | +def basic(src): |
| 5 | + print |
| 6 | + print "Testing basic accessors..." |
| 7 | + cf = ConfigParser.ConfigParser() |
| 8 | + sio = StringIO.StringIO(src) |
| 9 | + cf.readfp(sio) |
| 10 | + L = cf.sections() |
| 11 | + L.sort() |
| 12 | + print L |
| 13 | + for s in L: |
| 14 | + print "%s: %s" % (s, cf.options(s)) |
| 15 | + |
| 16 | + # The use of spaces in the section names serves as a regression test for |
| 17 | + # SourceForge bug #115357. |
| 18 | + # |
| 19 | + # http://sourceforge.net/bugs/?func=detailbug&group_id=5470&bug_id=115357 |
| 20 | + # |
| 21 | + print "test for SourceForge bug #115357" |
| 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 | + |
| 31 | +def interpolation(src): |
| 32 | + print |
| 33 | + print "Testing value interpolation..." |
| 34 | + cf = ConfigParser.ConfigParser({"getname": "%(__name__)s"}) |
| 35 | + sio = StringIO.StringIO(src) |
| 36 | + cf.readfp(sio) |
| 37 | + print `cf.get("Foo", "getname")` |
| 38 | + print `cf.get("Foo", "bar")` |
| 39 | + print `cf.get("Foo", "bar9")` |
| 40 | + print `cf.get("Foo", "bar10")` |
| 41 | + expect_get_error(cf, ConfigParser.InterpolationDepthError, "Foo", "bar11") |
| 42 | + |
| 43 | +def parse_errors(): |
| 44 | + print |
| 45 | + print "Testing for parsing errors..." |
| 46 | + expect_parse_error(ConfigParser.ParsingError, |
| 47 | + """[Foo]\n extra-spaces: splat\n""") |
| 48 | + expect_parse_error(ConfigParser.ParsingError, |
| 49 | + """[Foo]\n extra-spaces= splat\n""") |
| 50 | + expect_parse_error(ConfigParser.ParsingError, |
| 51 | + """[Foo]\noption-without-value\n""") |
| 52 | + expect_parse_error(ConfigParser.ParsingError, |
| 53 | + """[Foo]\n:value-without-option-name\n""") |
| 54 | + expect_parse_error(ConfigParser.ParsingError, |
| 55 | + """[Foo]\n=value-without-option-name\n""") |
| 56 | + expect_parse_error(ConfigParser.MissingSectionHeaderError, |
| 57 | + """No Section!\n""") |
| 58 | + |
| 59 | +def query_errors(): |
| 60 | + print |
| 61 | + print "Testing query interface..." |
| 62 | + cf = ConfigParser.ConfigParser() |
| 63 | + print cf.sections() |
| 64 | + print "Has section 'Foo'?", cf.has_section("Foo") |
| 65 | + try: |
| 66 | + cf.options("Foo") |
| 67 | + except ConfigParser.NoSectionError, e: |
| 68 | + print "Caught expected NoSectionError:", e |
| 69 | + else: |
| 70 | + print "Failed to catch expected NoSectionError from options()" |
| 71 | + try: |
| 72 | + cf.set("foo", "bar", "value") |
| 73 | + except ConfigParser.NoSectionError, e: |
| 74 | + print "Caught expected NoSectionError:", e |
| 75 | + else: |
| 76 | + print "Failed to catch expected NoSectionError from set()" |
| 77 | + expect_get_error(cf, ConfigParser.NoSectionError, "foo", "bar") |
| 78 | + cf.add_section("foo") |
| 79 | + expect_get_error(cf, ConfigParser.NoOptionError, "foo", "bar") |
| 80 | + |
| 81 | +def weird_errors(): |
| 82 | + print |
| 83 | + print "Testing miscellaneous error conditions..." |
| 84 | + cf = ConfigParser.ConfigParser() |
| 85 | + cf.add_section("Foo") |
| 86 | + try: |
| 87 | + cf.add_section("Foo") |
| 88 | + except ConfigParser.DuplicateSectionError, e: |
| 89 | + print "Caught expected DuplicateSectionError:", e |
| 90 | + else: |
| 91 | + print "Failed to catch expected DuplicateSectionError" |
| 92 | + |
| 93 | +def expect_get_error(cf, exctype, section, option, raw=0): |
| 94 | + try: |
| 95 | + cf.get(section, option, raw=raw) |
| 96 | + except exctype, e: |
| 97 | + print "Caught expected", exctype.__name__, ":" |
| 98 | + print e |
| 99 | + else: |
| 100 | + print "Failed to catch expected", exctype.__name__ |
| 101 | + |
| 102 | +def expect_parse_error(exctype, src): |
| 103 | + cf = ConfigParser.ConfigParser() |
| 104 | + sio = StringIO.StringIO(src) |
| 105 | + try: |
| 106 | + cf.readfp(sio) |
| 107 | + except exctype, e: |
| 108 | + print "Caught expected exception:", e |
| 109 | + else: |
| 110 | + print "Failed to catch expected", exctype.__name__ |
| 111 | + |
| 112 | +basic(r""" |
| 113 | +[Foo Bar] |
| 114 | +foo=bar |
| 115 | +[Spacey Bar] |
| 116 | +foo = bar |
| 117 | +[Commented Bar] |
| 118 | +foo: bar ; comment |
| 119 | +""") |
| 120 | +interpolation(r""" |
| 121 | +[Foo] |
| 122 | +bar=something %(with1)s interpolation (1 step) |
| 123 | +bar9=something %(with9)s lots of interpolation (9 steps) |
| 124 | +bar10=something %(with10)s lots of interpolation (10 steps) |
| 125 | +bar11=something %(with11)s lots of interpolation (11 steps) |
| 126 | +with11=%(with10)s |
| 127 | +with10=%(with9)s |
| 128 | +with9=%(with8)s |
| 129 | +with8=%(with7)s |
| 130 | +with7=%(with6)s |
| 131 | +with6=%(with5)s |
| 132 | +with5=%(with4)s |
| 133 | +with4=%(with3)s |
| 134 | +with3=%(with2)s |
| 135 | +with2=%(with1)s |
| 136 | +with1=with |
| 137 | +
|
| 138 | +[Mutual Recursion] |
| 139 | +foo=%(bar)s |
| 140 | +bar=%(foo)s |
| 141 | +""") |
| 142 | +parse_errors() |
| 143 | +query_errors() |
| 144 | +weird_errors() |
0 commit comments