def _configElementsToSection(self, configElements): section = ConfigSection() for element in configElements: if isinstance(element[0], ConfigSection): section.addSection(element[1], element[0]) else: section.addAttribute(element[1], element[0]) return section
def test_configParser(self): configString = """ attr1 = 'long value'; attr2 = [value1, value2]; [section1] { [section11] { attr111 = ['el1', 'el2', 'el3']; } attr11 = 'val val'; } [section2] { attr21 = val; } dict1 = {val1 : 'long val', 'val val' : value}; """ result = self.parser.parseConfig(configString) section11 = ConfigSection() section11.addAttribute('attr111', ('el1', 'el2', 'el3')) section1 = ConfigSection() section1.addAttribute('attr11', 'val val') section1.addSection('section11', section11) section2 = ConfigSection() section2.addAttribute('attr21', 'val') topSection = ConfigSection() topSection.addSection('section1', section1) topSection.addSection('section2', section2) topSection.addAttribute('attr1', 'long value') topSection.addAttribute('attr2', ('value1', 'value2')) topSection.addAttribute('dict1', {'val1' : 'long val', 'val val' : 'value'}) expected = topSection self._assertEqualSections(result, expected)
class ConfigSectionTest(unittest.TestCase): def setUp(self): self.config = ConfigSection() #def tearDown(self): # self.foo.dispose() # self.foo = None def testGetSection(self): sectionName = "section" self.config.addSection(sectionName, ConfigSection()) self.assertNotEqual(self.config.getSection(sectionName), None, "Added section should exist") def testGetNonExistingSection(self): nonExistingSection ="IDontExist" self.assertEqual(self.config.getSection(nonExistingSection), None, "config.getSection on non existing section should return None") def testDelSection(self): sectionName = "sectionName" self.config.addSection(sectionName, ConfigSection()) self.assertNotEqual(self.config.getSection(sectionName), None, "Added section should exist") self.config.delSection(sectionName) self.assertEqual(self.config.getSection(sectionName), None, "config.getSection on deleted section should return None") def testGetAttribute(self): attributeName = "attributeName" attributeValue = "1234" self.config.addAttribute(attributeName, attributeValue) self.assertEqual(self.config.getAttribute(attributeName), attributeValue, "self.config.getAttribute returned wrong value of attribute") def testGetNonExistingAttribute(self): nonExistingAttribute = "nonExistingAttribute" self.assertEqual(self.config.getAttribute(nonExistingAttribute), None, "Non existing attribute should be equals None") def testDelAttribute(self): attributeName = "sectionName" attributeValue = "4321" self.config.addAttribute(attributeName, attributeValue) self.assertEqual(self.config.getAttribute(attributeName), attributeValue, "Added attributes should exist") self.config.delAttribute(attributeName) self.assertEqual(self.config.getAttribute(attributeName), None, "config.getAttribute on deleted attributes should return None") def testGetDictionary(self): dictionaryName = "dictionary" dictionaryValue = {"key" : "val"} stringName = "str" stringValue = "val" self.config.addAttribute(dictionaryName, dictionaryValue) self.config.addAttribute(stringName, stringValue) result = self.config.getDictionary(dictionaryName) self.assertEqual(result, dictionaryValue, "") self.assertRaises(ConfigParsingException, self.config.getDictionary, stringName) def testGetStringFromList(self): stringName = "name" stringValue = "value" self.config.addAttribute(stringName, stringValue) result = self.config.getStringFromList(stringName, ["value", "value1", "value2"]) self.assertEqual(result, stringValue, "") self.assertRaises(ConfigParsingException, self.config.getStringFromList, stringName, ["wrong", "wrong1", "wrong2"])
def setUp(self): self.config = ConfigSection()