annotate Lib/test/test_cfgparser.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents a2492b4e505d
children 6768b2350c57
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47009
diff changeset
1 import ConfigParser
14900
f932c128279b Regression test for ConfigParser module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
2 import StringIO
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
3 import unittest
40418
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
4 import UserDict
14900
f932c128279b Regression test for ConfigParser module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
5
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
6 from test import test_support
15480
3c33f2c357d3 Add test cases for ConfigParser.remove_option() behavior. This includes
Fred Drake <fdrake@acm.org>
parents: 15305
diff changeset
7
40418
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
8 class SortedDict(UserDict.UserDict):
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
9 def items(self):
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
10 result = self.data.items()
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
11 result.sort()
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
12 return result
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
13
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
14 def keys(self):
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
15 result = self.data.keys()
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
16 result.sort()
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
17 return result
40732
5dae0d7f683a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 40418
diff changeset
18
40418
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
19 def values(self):
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
20 result = self.items()
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
21 return [i[1] for i in values]
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
22
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
23 def iteritems(self): return iter(self.items())
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
24 def iterkeys(self): return iter(self.keys())
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
25 __iter__ = iterkeys
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
26 def itervalues(self): return iter(self.values())
22296
9a091d72136a backport akuchling's checkin of
Michael W. Hudson <mwh@python.net>
parents: 20321
diff changeset
27
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
28 class TestCaseBase(unittest.TestCase):
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
29 def newconfig(self, defaults=None):
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
30 if defaults is None:
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
31 self.cf = self.config_class()
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
32 else:
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
33 self.cf = self.config_class(defaults)
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
34 return self.cf
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
35
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
36 def fromstring(self, string, defaults=None):
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
37 cf = self.newconfig(defaults)
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
38 sio = StringIO.StringIO(string)
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
39 cf.readfp(sio)
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
40 return cf
16771
b235160f1346 Make sure ConfigParser uses .optionxform() consistently; this affects
Fred Drake <fdrake@acm.org>
parents: 16595
diff changeset
41
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
42 def test_basic(self):
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
43 cf = self.fromstring(
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
44 "[Foo Bar]\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
45 "foo=bar\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
46 "[Spacey Bar]\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
47 "foo = bar\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
48 "[Commented Bar]\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
49 "foo: bar ; comment\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
50 "[Long Line]\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
51 "foo: this line is much, much longer than my editor\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
52 " likes it.\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
53 "[Section\\with$weird%characters[\t]\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
54 "[Internationalized Stuff]\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
55 "foo[bg]: Bulgarian\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
56 "foo=Default\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
57 "foo[en]=English\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
58 "foo[de]=Deutsch\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
59 "[Spaces]\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
60 "key with spaces : value\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
61 "another with spaces = splat!\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
62 )
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
63 L = cf.sections()
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
64 L.sort()
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
65 eq = self.assertEqual
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
66 eq(L, [r'Commented Bar',
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
67 r'Foo Bar',
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
68 r'Internationalized Stuff',
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
69 r'Long Line',
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
70 r'Section\with$weird%characters[' '\t',
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
71 r'Spaces',
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
72 r'Spacey Bar',
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
73 ])
16771
b235160f1346 Make sure ConfigParser uses .optionxform() consistently; this affects
Fred Drake <fdrake@acm.org>
parents: 16595
diff changeset
74
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
75 # The use of spaces in the section names serves as a
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
76 # regression test for SourceForge bug #583248:
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
77 # http://www.python.org/sf/583248
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
78 eq(cf.get('Foo Bar', 'foo'), 'bar')
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
79 eq(cf.get('Spacey Bar', 'foo'), 'bar')
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
80 eq(cf.get('Commented Bar', 'foo'), 'bar')
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
81 eq(cf.get('Spaces', 'key with spaces'), 'value')
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
82 eq(cf.get('Spaces', 'another with spaces'), 'splat!')
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
83
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
84 self.failIf('__name__' in cf.options("Foo Bar"),
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
85 '__name__ "option" should not be exposed by the API!')
20321
267c6fe0abc2 Added tests that check getboolean() with the newly allowed values from
Fred Drake <fdrake@acm.org>
parents: 18407
diff changeset
86
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
87 # Make sure the right things happen for remove_option();
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
88 # added to include check for SourceForge bug #123324:
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
89 self.failUnless(cf.remove_option('Foo Bar', 'foo'),
51775
af9b6c43804e Issue #5341: Fix a variety of spelling errors.
Mark Dickinson <dickinsm@gmail.com>
parents: 47252
diff changeset
90 "remove_option() failed to report existence of option")
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
91 self.failIf(cf.has_option('Foo Bar', 'foo'),
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
92 "remove_option() failed to remove option")
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
93 self.failIf(cf.remove_option('Foo Bar', 'foo'),
51775
af9b6c43804e Issue #5341: Fix a variety of spelling errors.
Mark Dickinson <dickinsm@gmail.com>
parents: 47252
diff changeset
94 "remove_option() failed to report non-existence of option"
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
95 " that was removed")
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
96
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47009
diff changeset
97 self.assertRaises(ConfigParser.NoSectionError,
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
98 cf.remove_option, 'No Such Section', 'foo')
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
99
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
100 eq(cf.get('Long Line', 'foo'),
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
101 'this line is much, much longer than my editor\nlikes it.')
20321
267c6fe0abc2 Added tests that check getboolean() with the newly allowed values from
Fred Drake <fdrake@acm.org>
parents: 18407
diff changeset
102
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
103 def test_case_sensitivity(self):
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
104 cf = self.newconfig()
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
105 cf.add_section("A")
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
106 cf.add_section("a")
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
107 L = cf.sections()
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
108 L.sort()
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
109 eq = self.assertEqual
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
110 eq(L, ["A", "a"])
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
111 cf.set("a", "B", "value")
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
112 eq(cf.options("a"), ["b"])
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
113 eq(cf.get("a", "b"), "value",
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
114 "could not locate option, expecting case-insensitive option names")
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
115 self.failUnless(cf.has_option("a", "b"))
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
116 cf.set("A", "A-B", "A-B value")
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
117 for opt in ("a-b", "A-b", "a-B", "A-B"):
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
118 self.failUnless(
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
119 cf.has_option("A", opt),
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
120 "has_option() returned false for option which should exist")
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
121 eq(cf.options("A"), ["a-b"])
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
122 eq(cf.options("a"), ["b"])
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
123 cf.remove_option("a", "B")
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
124 eq(cf.options("a"), [])
14900
f932c128279b Regression test for ConfigParser module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
125
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
126 # SF bug #432369:
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
127 cf = self.fromstring(
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
128 "[MySection]\nOption: first line\n\tsecond line\n")
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
129 eq(cf.options("MySection"), ["option"])
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
130 eq(cf.get("MySection", "Option"), "first line\nsecond line")
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
131
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
132 # SF bug #561822:
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
133 cf = self.fromstring("[section]\nnekey=nevalue\n",
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
134 defaults={"key":"value"})
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
135 self.failUnless(cf.has_option("section", "Key"))
25909
0bac66e1b356 Added (very) minimal tests of the RawConfigParser class.
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
136
16576
f654468b9ae8 Added options that use square brackets in their names; this ensures that
Fred Drake <fdrake@acm.org>
parents: 15480
diff changeset
137
33662
cc4684051c46 SF bug #1017864: ConfigParser now correctly handles default keys, processing them with ``ConfigParser.optionxform`` when supplied, consistent with the handling of config file entries and runtime-set options.
David Goodger <goodger@python.org>
parents: 32117
diff changeset
138 def test_default_case_sensitivity(self):
cc4684051c46 SF bug #1017864: ConfigParser now correctly handles default keys, processing them with ``ConfigParser.optionxform`` when supplied, consistent with the handling of config file entries and runtime-set options.
David Goodger <goodger@python.org>
parents: 32117
diff changeset
139 cf = self.newconfig({"foo": "Bar"})
cc4684051c46 SF bug #1017864: ConfigParser now correctly handles default keys, processing them with ``ConfigParser.optionxform`` when supplied, consistent with the handling of config file entries and runtime-set options.
David Goodger <goodger@python.org>
parents: 32117
diff changeset
140 self.assertEqual(
cc4684051c46 SF bug #1017864: ConfigParser now correctly handles default keys, processing them with ``ConfigParser.optionxform`` when supplied, consistent with the handling of config file entries and runtime-set options.
David Goodger <goodger@python.org>
parents: 32117
diff changeset
141 cf.get("DEFAULT", "Foo"), "Bar",
cc4684051c46 SF bug #1017864: ConfigParser now correctly handles default keys, processing them with ``ConfigParser.optionxform`` when supplied, consistent with the handling of config file entries and runtime-set options.
David Goodger <goodger@python.org>
parents: 32117
diff changeset
142 "could not locate option, expecting case-insensitive option names")
cc4684051c46 SF bug #1017864: ConfigParser now correctly handles default keys, processing them with ``ConfigParser.optionxform`` when supplied, consistent with the handling of config file entries and runtime-set options.
David Goodger <goodger@python.org>
parents: 32117
diff changeset
143 cf = self.newconfig({"Foo": "Bar"})
cc4684051c46 SF bug #1017864: ConfigParser now correctly handles default keys, processing them with ``ConfigParser.optionxform`` when supplied, consistent with the handling of config file entries and runtime-set options.
David Goodger <goodger@python.org>
parents: 32117
diff changeset
144 self.assertEqual(
cc4684051c46 SF bug #1017864: ConfigParser now correctly handles default keys, processing them with ``ConfigParser.optionxform`` when supplied, consistent with the handling of config file entries and runtime-set options.
David Goodger <goodger@python.org>
parents: 32117
diff changeset
145 cf.get("DEFAULT", "Foo"), "Bar",
cc4684051c46 SF bug #1017864: ConfigParser now correctly handles default keys, processing them with ``ConfigParser.optionxform`` when supplied, consistent with the handling of config file entries and runtime-set options.
David Goodger <goodger@python.org>
parents: 32117
diff changeset
146 "could not locate option, expecting case-insensitive defaults")
cc4684051c46 SF bug #1017864: ConfigParser now correctly handles default keys, processing them with ``ConfigParser.optionxform`` when supplied, consistent with the handling of config file entries and runtime-set options.
David Goodger <goodger@python.org>
parents: 32117
diff changeset
147
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
148 def test_parse_errors(self):
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
149 self.newconfig()
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47009
diff changeset
150 self.parse_error(ConfigParser.ParsingError,
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
151 "[Foo]\n extra-spaces: splat\n")
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47009
diff changeset
152 self.parse_error(ConfigParser.ParsingError,
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
153 "[Foo]\n extra-spaces= splat\n")
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47009
diff changeset
154 self.parse_error(ConfigParser.ParsingError,
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
155 "[Foo]\noption-without-value\n")
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47009
diff changeset
156 self.parse_error(ConfigParser.ParsingError,
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
157 "[Foo]\n:value-without-option-name\n")
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47009
diff changeset
158 self.parse_error(ConfigParser.ParsingError,
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
159 "[Foo]\n=value-without-option-name\n")
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47009
diff changeset
160 self.parse_error(ConfigParser.MissingSectionHeaderError,
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
161 "No Section!\n")
14900
f932c128279b Regression test for ConfigParser module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
162
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
163 def parse_error(self, exc, src):
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
164 sio = StringIO.StringIO(src)
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
165 self.assertRaises(exc, self.cf.readfp, sio)
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
166
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
167 def test_query_errors(self):
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
168 cf = self.newconfig()
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
169 self.assertEqual(cf.sections(), [],
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
170 "new ConfigParser should have no defined sections")
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
171 self.failIf(cf.has_section("Foo"),
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
172 "new ConfigParser should have no acknowledged sections")
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47009
diff changeset
173 self.assertRaises(ConfigParser.NoSectionError,
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
174 cf.options, "Foo")
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47009
diff changeset
175 self.assertRaises(ConfigParser.NoSectionError,
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
176 cf.set, "foo", "bar", "value")
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47009
diff changeset
177 self.get_error(ConfigParser.NoSectionError, "foo", "bar")
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
178 cf.add_section("foo")
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47009
diff changeset
179 self.get_error(ConfigParser.NoOptionError, "foo", "bar")
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
180
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
181 def get_error(self, exc, section, option):
26928
24cb62225040 Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents: 25918
diff changeset
182 try:
24cb62225040 Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents: 25918
diff changeset
183 self.cf.get(section, option)
24cb62225040 Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents: 25918
diff changeset
184 except exc, e:
24cb62225040 Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents: 25918
diff changeset
185 return e
24cb62225040 Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents: 25918
diff changeset
186 else:
24cb62225040 Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents: 25918
diff changeset
187 self.fail("expected exception type %s.%s"
24cb62225040 Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents: 25918
diff changeset
188 % (exc.__module__, exc.__name__))
16576
f654468b9ae8 Added options that use square brackets in their names; this ensures that
Fred Drake <fdrake@acm.org>
parents: 15480
diff changeset
189
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
190 def test_boolean(self):
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
191 cf = self.fromstring(
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
192 "[BOOLTEST]\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
193 "T1=1\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
194 "T2=TRUE\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
195 "T3=True\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
196 "T4=oN\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
197 "T5=yes\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
198 "F1=0\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
199 "F2=FALSE\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
200 "F3=False\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
201 "F4=oFF\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
202 "F5=nO\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
203 "E1=2\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
204 "E2=foo\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
205 "E3=-1\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
206 "E4=0.1\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
207 "E5=FALSE AND MORE"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
208 )
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
209 for x in range(1, 5):
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
210 self.failUnless(cf.getboolean('BOOLTEST', 't%d' % x))
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
211 self.failIf(cf.getboolean('BOOLTEST', 'f%d' % x))
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
212 self.assertRaises(ValueError,
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
213 cf.getboolean, 'BOOLTEST', 'e%d' % x)
14900
f932c128279b Regression test for ConfigParser module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
214
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
215 def test_weird_errors(self):
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
216 cf = self.newconfig()
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
217 cf.add_section("Foo")
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47009
diff changeset
218 self.assertRaises(ConfigParser.DuplicateSectionError,
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
219 cf.add_section, "Foo")
16576
f654468b9ae8 Added options that use square brackets in their names; this ensures that
Fred Drake <fdrake@acm.org>
parents: 15480
diff changeset
220
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
221 def test_write(self):
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
222 cf = self.fromstring(
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
223 "[Long Line]\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
224 "foo: this line is much, much longer than my editor\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
225 " likes it.\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
226 "[DEFAULT]\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
227 "foo: another very\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
228 " long line"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
229 )
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
230 output = StringIO.StringIO()
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
231 cf.write(output)
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
232 self.assertEqual(
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
233 output.getvalue(),
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
234 "[DEFAULT]\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
235 "foo = another very\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
236 "\tlong line\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
237 "\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
238 "[Long Line]\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
239 "foo = this line is much, much longer than my editor\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
240 "\tlikes it.\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
241 "\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
242 )
14900
f932c128279b Regression test for ConfigParser module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
243
32115
84dfa9a67da1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32114
diff changeset
244 def test_set_string_types(self):
84dfa9a67da1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32114
diff changeset
245 cf = self.fromstring("[sect]\n"
84dfa9a67da1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32114
diff changeset
246 "option1=foo\n")
84dfa9a67da1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32114
diff changeset
247 # Check that we don't get an exception when setting values in
84dfa9a67da1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32114
diff changeset
248 # an existing section using strings:
84dfa9a67da1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32114
diff changeset
249 class mystr(str):
84dfa9a67da1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32114
diff changeset
250 pass
84dfa9a67da1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32114
diff changeset
251 cf.set("sect", "option1", "splat")
84dfa9a67da1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32114
diff changeset
252 cf.set("sect", "option1", mystr("splat"))
84dfa9a67da1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32114
diff changeset
253 cf.set("sect", "option2", "splat")
84dfa9a67da1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32114
diff changeset
254 cf.set("sect", "option2", mystr("splat"))
84dfa9a67da1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32114
diff changeset
255 try:
84dfa9a67da1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32114
diff changeset
256 unicode
84dfa9a67da1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32114
diff changeset
257 except NameError:
84dfa9a67da1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32114
diff changeset
258 pass
84dfa9a67da1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32114
diff changeset
259 else:
84dfa9a67da1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32114
diff changeset
260 cf.set("sect", "option1", unicode("splat"))
84dfa9a67da1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32114
diff changeset
261 cf.set("sect", "option2", unicode("splat"))
84dfa9a67da1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32114
diff changeset
262
32117
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
263 def test_read_returns_file_list(self):
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
264 file1 = test_support.findfile("cfgparser.1")
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
265 # check when we pass a mix of readable and non-readable files:
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
266 cf = self.newconfig()
51775
af9b6c43804e Issue #5341: Fix a variety of spelling errors.
Mark Dickinson <dickinsm@gmail.com>
parents: 47252
diff changeset
267 parsed_files = cf.read([file1, "nonexistent-file"])
32117
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
268 self.assertEqual(parsed_files, [file1])
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
269 self.assertEqual(cf.get("Foo Bar", "foo"), "newbar")
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
270 # check when we pass only a filename:
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
271 cf = self.newconfig()
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
272 parsed_files = cf.read(file1)
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
273 self.assertEqual(parsed_files, [file1])
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
274 self.assertEqual(cf.get("Foo Bar", "foo"), "newbar")
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
275 # check when we pass only missing files:
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
276 cf = self.newconfig()
51775
af9b6c43804e Issue #5341: Fix a variety of spelling errors.
Mark Dickinson <dickinsm@gmail.com>
parents: 47252
diff changeset
277 parsed_files = cf.read(["nonexistent-file"])
32117
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
278 self.assertEqual(parsed_files, [])
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
279 # check when we pass no files:
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
280 cf = self.newconfig()
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
281 parsed_files = cf.read([])
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
282 self.assertEqual(parsed_files, [])
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
283
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
284 # shared by subclasses
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
285 def get_interpolation_config(self):
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
286 return self.fromstring(
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
287 "[Foo]\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
288 "bar=something %(with1)s interpolation (1 step)\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
289 "bar9=something %(with9)s lots of interpolation (9 steps)\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
290 "bar10=something %(with10)s lots of interpolation (10 steps)\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
291 "bar11=something %(with11)s lots of interpolation (11 steps)\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
292 "with11=%(with10)s\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
293 "with10=%(with9)s\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
294 "with9=%(with8)s\n"
32114
70023af2085a ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 28917
diff changeset
295 "with8=%(With7)s\n"
70023af2085a ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 28917
diff changeset
296 "with7=%(WITH6)s\n"
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
297 "with6=%(with5)s\n"
32114
70023af2085a ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 28917
diff changeset
298 "With5=%(with4)s\n"
70023af2085a ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 28917
diff changeset
299 "WITH4=%(with3)s\n"
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
300 "with3=%(with2)s\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
301 "with2=%(with1)s\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
302 "with1=with\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
303 "\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
304 "[Mutual Recursion]\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
305 "foo=%(bar)s\n"
26928
24cb62225040 Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents: 25918
diff changeset
306 "bar=%(foo)s\n"
24cb62225040 Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents: 25918
diff changeset
307 "\n"
24cb62225040 Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents: 25918
diff changeset
308 "[Interpolation Error]\n"
24cb62225040 Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents: 25918
diff changeset
309 "name=%(reference)s\n",
24cb62225040 Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents: 25918
diff changeset
310 # no definition for 'reference'
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
311 defaults={"getname": "%(__name__)s"})
16576
f654468b9ae8 Added options that use square brackets in their names; this ensures that
Fred Drake <fdrake@acm.org>
parents: 15480
diff changeset
312
25917
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
313 def check_items_config(self, expected):
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
314 cf = self.fromstring(
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
315 "[section]\n"
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
316 "name = value\n"
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
317 "key: |%(name)s| \n"
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
318 "getdefault: |%(default)s|\n"
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
319 "getname: |%(__name__)s|",
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
320 defaults={"default": "<default>"})
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
321 L = list(cf.items("section"))
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
322 L.sort()
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
323 self.assertEqual(L, expected)
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
324
14900
f932c128279b Regression test for ConfigParser module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
325
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
326 class ConfigParserTestCase(TestCaseBase):
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47009
diff changeset
327 config_class = ConfigParser.ConfigParser
25909
0bac66e1b356 Added (very) minimal tests of the RawConfigParser class.
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
328
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
329 def test_interpolation(self):
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
330 cf = self.get_interpolation_config()
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
331 eq = self.assertEqual
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
332 eq(cf.get("Foo", "getname"), "Foo")
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
333 eq(cf.get("Foo", "bar"), "something with interpolation (1 step)")
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
334 eq(cf.get("Foo", "bar9"),
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
335 "something with lots of interpolation (9 steps)")
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
336 eq(cf.get("Foo", "bar10"),
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
337 "something with lots of interpolation (10 steps)")
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47009
diff changeset
338 self.get_error(ConfigParser.InterpolationDepthError, "Foo", "bar11")
25909
0bac66e1b356 Added (very) minimal tests of the RawConfigParser class.
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
339
26928
24cb62225040 Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents: 25918
diff changeset
340 def test_interpolation_missing_value(self):
24cb62225040 Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents: 25918
diff changeset
341 cf = self.get_interpolation_config()
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47009
diff changeset
342 e = self.get_error(ConfigParser.InterpolationError,
26928
24cb62225040 Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents: 25918
diff changeset
343 "Interpolation Error", "name")
24cb62225040 Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents: 25918
diff changeset
344 self.assertEqual(e.reference, "reference")
24cb62225040 Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents: 25918
diff changeset
345 self.assertEqual(e.section, "Interpolation Error")
24cb62225040 Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents: 25918
diff changeset
346 self.assertEqual(e.option, "name")
24cb62225040 Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents: 25918
diff changeset
347
25917
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
348 def test_items(self):
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
349 self.check_items_config([('default', '<default>'),
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
350 ('getdefault', '|<default>|'),
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
351 ('getname', '|section|'),
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
352 ('key', '|value|'),
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
353 ('name', 'value')])
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
354
33663
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
355 def test_set_nonstring_types(self):
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
356 cf = self.newconfig()
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
357 cf.add_section('non-string')
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
358 cf.set('non-string', 'int', 1)
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
359 cf.set('non-string', 'list', [0, 1, 1, 2, 3, 5, 8, 13, '%('])
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
360 cf.set('non-string', 'dict', {'pi': 3.14159, '%(': 1,
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
361 '%(list)': '%(list)'})
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
362 cf.set('non-string', 'string_with_interpolation', '%(list)s')
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
363 self.assertEqual(cf.get('non-string', 'int', raw=True), 1)
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
364 self.assertRaises(TypeError, cf.get, 'non-string', 'int')
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
365 self.assertEqual(cf.get('non-string', 'list', raw=True),
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
366 [0, 1, 1, 2, 3, 5, 8, 13, '%('])
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
367 self.assertRaises(TypeError, cf.get, 'non-string', 'list')
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
368 self.assertEqual(cf.get('non-string', 'dict', raw=True),
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
369 {'pi': 3.14159, '%(': 1, '%(list)': '%(list)'})
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
370 self.assertRaises(TypeError, cf.get, 'non-string', 'dict')
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
371 self.assertEqual(cf.get('non-string', 'string_with_interpolation',
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
372 raw=True), '%(list)s')
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
373 self.assertRaises(ValueError, cf.get, 'non-string',
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
374 'string_with_interpolation', raw=False)
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
375
25909
0bac66e1b356 Added (very) minimal tests of the RawConfigParser class.
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
376
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
377 class RawConfigParserTestCase(TestCaseBase):
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47009
diff changeset
378 config_class = ConfigParser.RawConfigParser
14900
f932c128279b Regression test for ConfigParser module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
379
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
380 def test_interpolation(self):
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
381 cf = self.get_interpolation_config()
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
382 eq = self.assertEqual
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
383 eq(cf.get("Foo", "getname"), "%(__name__)s")
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
384 eq(cf.get("Foo", "bar"),
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
385 "something %(with1)s interpolation (1 step)")
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
386 eq(cf.get("Foo", "bar9"),
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
387 "something %(with9)s lots of interpolation (9 steps)")
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
388 eq(cf.get("Foo", "bar10"),
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
389 "something %(with10)s lots of interpolation (10 steps)")
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
390 eq(cf.get("Foo", "bar11"),
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
391 "something %(with11)s lots of interpolation (11 steps)")
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
392
25917
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
393 def test_items(self):
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
394 self.check_items_config([('default', '<default>'),
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
395 ('getdefault', '|%(default)s|'),
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
396 ('getname', '|%(__name__)s|'),
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
397 ('key', '|%(name)s|'),
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
398 ('name', 'value')])
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
399
33663
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
400 def test_set_nonstring_types(self):
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
401 cf = self.newconfig()
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
402 cf.add_section('non-string')
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
403 cf.set('non-string', 'int', 1)
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
404 cf.set('non-string', 'list', [0, 1, 1, 2, 3, 5, 8, 13])
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
405 cf.set('non-string', 'dict', {'pi': 3.14159})
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
406 self.assertEqual(cf.get('non-string', 'int'), 1)
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
407 self.assertEqual(cf.get('non-string', 'list'),
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
408 [0, 1, 1, 2, 3, 5, 8, 13])
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
409 self.assertEqual(cf.get('non-string', 'dict'), {'pi': 3.14159})
33665
5def66f2c9ab Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33663
diff changeset
410
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
411
25918
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25917
diff changeset
412 class SafeConfigParserTestCase(ConfigParserTestCase):
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47009
diff changeset
413 config_class = ConfigParser.SafeConfigParser
25918
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25917
diff changeset
414
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25917
diff changeset
415 def test_safe_interpolation(self):
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25917
diff changeset
416 # See http://www.python.org/sf/511737
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25917
diff changeset
417 cf = self.fromstring("[section]\n"
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25917
diff changeset
418 "option1=xxx\n"
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25917
diff changeset
419 "option2=%(option1)s/xxx\n"
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25917
diff changeset
420 "ok=%(option1)s/%%s\n"
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25917
diff changeset
421 "not_ok=%(option2)s/%%s")
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25917
diff changeset
422 self.assertEqual(cf.get("section", "ok"), "xxx/%s")
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25917
diff changeset
423 self.assertEqual(cf.get("section", "not_ok"), "xxx/xxx/%s")
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25917
diff changeset
424
41157
53ee2795287b Patch #1603688: ConfigParser.SafeConfigParser now checks values that
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
425 def test_set_malformatted_interpolation(self):
53ee2795287b Patch #1603688: ConfigParser.SafeConfigParser now checks values that
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
426 cf = self.fromstring("[sect]\n"
53ee2795287b Patch #1603688: ConfigParser.SafeConfigParser now checks values that
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
427 "option1=foo\n")
53ee2795287b Patch #1603688: ConfigParser.SafeConfigParser now checks values that
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
428
53ee2795287b Patch #1603688: ConfigParser.SafeConfigParser now checks values that
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
429 self.assertEqual(cf.get('sect', "option1"), "foo")
53ee2795287b Patch #1603688: ConfigParser.SafeConfigParser now checks values that
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
430
53ee2795287b Patch #1603688: ConfigParser.SafeConfigParser now checks values that
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
431 self.assertRaises(ValueError, cf.set, "sect", "option1", "%foo")
53ee2795287b Patch #1603688: ConfigParser.SafeConfigParser now checks values that
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
432 self.assertRaises(ValueError, cf.set, "sect", "option1", "foo%")
53ee2795287b Patch #1603688: ConfigParser.SafeConfigParser now checks values that
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
433 self.assertRaises(ValueError, cf.set, "sect", "option1", "f%oo")
53ee2795287b Patch #1603688: ConfigParser.SafeConfigParser now checks values that
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
434
53ee2795287b Patch #1603688: ConfigParser.SafeConfigParser now checks values that
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
435 self.assertEqual(cf.get('sect', "option1"), "foo")
53ee2795287b Patch #1603688: ConfigParser.SafeConfigParser now checks values that
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
436
53108
a2492b4e505d #5741: dont disallow double percent signs in SafeConfigParser.set() keys.
Georg Brandl <georg@python.org>
parents: 51775
diff changeset
437 # bug #5741: double percents are *not* malformed
a2492b4e505d #5741: dont disallow double percent signs in SafeConfigParser.set() keys.
Georg Brandl <georg@python.org>
parents: 51775
diff changeset
438 cf.set("sect", "option2", "foo%%bar")
a2492b4e505d #5741: dont disallow double percent signs in SafeConfigParser.set() keys.
Georg Brandl <georg@python.org>
parents: 51775
diff changeset
439 self.assertEqual(cf.get("sect", "option2"), "foo%bar")
a2492b4e505d #5741: dont disallow double percent signs in SafeConfigParser.set() keys.
Georg Brandl <georg@python.org>
parents: 51775
diff changeset
440
33663
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
441 def test_set_nonstring_types(self):
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
442 cf = self.fromstring("[sect]\n"
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
443 "option1=foo\n")
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
444 # Check that we get a TypeError when setting non-string values
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
445 # in an existing section:
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
446 self.assertRaises(TypeError, cf.set, "sect", "option1", 1)
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
447 self.assertRaises(TypeError, cf.set, "sect", "option1", 1.0)
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
448 self.assertRaises(TypeError, cf.set, "sect", "option1", object())
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
449 self.assertRaises(TypeError, cf.set, "sect", "option2", 1)
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
450 self.assertRaises(TypeError, cf.set, "sect", "option2", 1.0)
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
451 self.assertRaises(TypeError, cf.set, "sect", "option2", object())
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
452
45445
dda92a097e8c Issue 1781. Now ConfigParser.add_section does not let you add a
Facundo Batista <facundobatista@gmail.com>
parents: 41157
diff changeset
453 def test_add_section_default_1(self):
dda92a097e8c Issue 1781. Now ConfigParser.add_section does not let you add a
Facundo Batista <facundobatista@gmail.com>
parents: 41157
diff changeset
454 cf = self.newconfig()
dda92a097e8c Issue 1781. Now ConfigParser.add_section does not let you add a
Facundo Batista <facundobatista@gmail.com>
parents: 41157
diff changeset
455 self.assertRaises(ValueError, cf.add_section, "default")
dda92a097e8c Issue 1781. Now ConfigParser.add_section does not let you add a
Facundo Batista <facundobatista@gmail.com>
parents: 41157
diff changeset
456
dda92a097e8c Issue 1781. Now ConfigParser.add_section does not let you add a
Facundo Batista <facundobatista@gmail.com>
parents: 41157
diff changeset
457 def test_add_section_default_2(self):
dda92a097e8c Issue 1781. Now ConfigParser.add_section does not let you add a
Facundo Batista <facundobatista@gmail.com>
parents: 41157
diff changeset
458 cf = self.newconfig()
dda92a097e8c Issue 1781. Now ConfigParser.add_section does not let you add a
Facundo Batista <facundobatista@gmail.com>
parents: 41157
diff changeset
459 self.assertRaises(ValueError, cf.add_section, "DEFAULT")
dda92a097e8c Issue 1781. Now ConfigParser.add_section does not let you add a
Facundo Batista <facundobatista@gmail.com>
parents: 41157
diff changeset
460
40418
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
461 class SortedTestCase(RawConfigParserTestCase):
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
462 def newconfig(self, defaults=None):
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
463 self.cf = self.config_class(defaults=defaults, dict_type=SortedDict)
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
464 return self.cf
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
465
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
466 def test_sorted(self):
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
467 self.fromstring("[b]\n"
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
468 "o4=1\n"
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
469 "o3=2\n"
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
470 "o2=3\n"
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
471 "o1=4\n"
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
472 "[a]\n"
40732
5dae0d7f683a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 40418
diff changeset
473 "k=v\n")
40418
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
474 output = StringIO.StringIO()
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
475 self.cf.write(output)
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
476 self.assertEquals(output.getvalue(),
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
477 "[a]\n"
40732
5dae0d7f683a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 40418
diff changeset
478 "k = v\n\n"
40418
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
479 "[b]\n"
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
480 "o1 = 4\n"
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
481 "o2 = 3\n"
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
482 "o3 = 2\n"
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
483 "o4 = 1\n\n")
25918
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25917
diff changeset
484
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
485 def test_main():
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 26928
diff changeset
486 test_support.run_unittest(
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 26928
diff changeset
487 ConfigParserTestCase,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 26928
diff changeset
488 RawConfigParserTestCase,
40418
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
489 SafeConfigParserTestCase,
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
490 SortedTestCase
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 26928
diff changeset
491 )
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
492
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
493 if __name__ == "__main__":
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
494 test_main()