def test_get_option_list(self): parser = config.editConfParser('') parser.read_string(self.config) get_list = parser.GetOptionList self.assertCountEqual(get_list('one'), ['one', 'two', 'three']) self.assertCountEqual(get_list('two'), ['one', 'two', 'three']) self.assertEqual(get_list('not exist'), [])
def test_load_file(self): # Borrow test/cfgparser.1 from test_configparser. config_path = findfile('cfgparser.1') parser = config.editConfParser(config_path) parser.Load() self.assertEqual(parser.Get('Foo Bar', 'foo'), 'newbar') self.assertEqual(parser.GetOptionList('Foo Bar'), ['foo'])
def mock_config(self): """Return a mocked editConf Both default and user config used the same config-*.def """ conf = config.editConf(_utest=True) for ctype in conf.config_types: conf.defaultCfg[ctype] = config.editConfParser('') conf.defaultCfg[ctype].read_string(self.config_string[ctype]) conf.userCfg[ctype] = config.editUserConfParser('') conf.userCfg[ctype].read_string(self.config_string[ctype]) return conf
def test_get(self): parser = config.editConfParser('') parser.read_string(self.config) eq = self.assertEqual # Test with type argument. self.assertIs(parser.Get('one', 'one', type='bool'), False) self.assertIs(parser.Get('one', 'two', type='bool'), True) eq(parser.Get('one', 'three', type='int'), 10) eq(parser.Get('two', 'one'), 'a string') self.assertIs(parser.Get('two', 'two', type='bool'), True) self.assertIs(parser.Get('two', 'three', type='bool'), False) # Test without type should fallback to string. eq(parser.Get('two', 'two'), 'true') eq(parser.Get('two', 'three'), 'false') # If option not exist, should return None, or default. self.assertIsNone(parser.Get('not', 'exist')) eq(parser.Get('not', 'exist', default='DEFAULT'), 'DEFAULT')
def test_load_cfg_files(self): conf = self.new_config(_utest=True) # Borrow test/cfgparser.1 from test_configparser. config_path = findfile('cfgparser.1') conf.defaultCfg['foo'] = config.editConfParser(config_path) conf.userCfg['foo'] = config.editUserConfParser(config_path) # Load all config from path conf.LoadCfgFiles() eq = self.assertEqual # Check defaultCfg is loaded eq(conf.defaultCfg['foo'].Get('Foo Bar', 'foo'), 'newbar') eq(conf.defaultCfg['foo'].GetOptionList('Foo Bar'), ['foo']) # Check userCfg is loaded eq(conf.userCfg['foo'].Get('Foo Bar', 'foo'), 'newbar') eq(conf.userCfg['foo'].GetOptionList('Foo Bar'), ['foo'])
def test_load_nothing(self): parser = config.editConfParser('') parser.Load() self.assertEqual(parser.sections(), [])