Index: Lib/test/test_cfgparser.py =================================================================== --- Lib/test/test_cfgparser.py (revision 83048) +++ Lib/test/test_cfgparser.py (working copy) @@ -506,7 +506,78 @@ "o3 = 2\n" "o4 = 1\n\n") +class DefaultValueTestCase(unittest.TestCase): + def getcfg(self, cls, addsection=True, addoption=True): + cfg = cls() + if addsection: + cfg.add_section("section") + if addsection and addoption: + cfg.set("section", "option", "value") + return cfg + + def test_normal_behavior_raw(self): + cfg = self.getcfg(configparser.RawConfigParser) + self.assertEqual(cfg.get("section", "option", default=-1), "value", + "didn't get option value when 'default' was present") + + def test_normal_behavior(self): + cfg = self.getcfg(configparser.ConfigParser) + self.assertEqual(cfg.get("section", "option", default=-1), "value", + "didn't get option value when 'default' was present") + + def test_normal_behavior_safe(self): + cfg = self.getcfg(configparser.SafeConfigParser) + self.assertEqual(cfg.get("section", "option", default=-1), "value", + "didn't get option value when 'default' was present") + + def test_default_value_raw(self): + cfg = self.getcfg(configparser.RawConfigParser, False) + msg = "didn't get default value when the option was not present" + self.assertEqual(cfg.getint("section", "option", default=1), 1, msg) + + def test_default_value(self): + cfg = self.getcfg(configparser.ConfigParser, False) + msg = "didn't get default value when the option was not present" + self.assertEqual(cfg.getfloat("section", "option", default=1.1), + 1.1, msg) + + def test_default_value_safe(self): + cfg = self.getcfg(configparser.SafeConfigParser, False) + msg = "didn't get default value when the option was not present" + self.assertEqual(cfg.getboolean("section", "option", default="true"), + True, msg) + + def test_no_section_error_raw(self): + cfg = self.getcfg(configparser.RawConfigParser, False) + with self.assertRaises(configparser.NoSectionError): + cfg.get("section", "option") + + def test_no_option_error_raw(self): + cfg = self.getcfg(configparser.RawConfigParser, True, False) + with self.assertRaises(configparser.NoOptionError): + cfg.get("section", "option") + + def test_no_section_error(self): + cfg = self.getcfg(configparser.ConfigParser, False) + with self.assertRaises(configparser.NoSectionError): + cfg.get("section", "option") + + def test_no_option_error(self): + cfg = self.getcfg(configparser.ConfigParser, True, False) + with self.assertRaises(configparser.NoOptionError): + cfg.get("section", "option") + + def test_no_section_error_safe(self): + cfg = self.getcfg(configparser.SafeConfigParser, False) + with self.assertRaises(configparser.NoSectionError): + cfg.get("section", "option") + + def test_no_option_error_safe(self): + cfg = self.getcfg(configparser.SafeConfigParser, True, False) + with self.assertRaises(configparser.NoOptionError): + cfg.get("section", "option") + def test_main(): support.run_unittest( ConfigParserTestCase, @@ -514,6 +585,7 @@ SafeConfigParserTestCase, SortedTestCase, SafeConfigParserTestCaseNoValue, + DefaultValueTestCase )