def test_trailing_whitespace(self): """ Trailing whitespace """ original = StructuredField() original.set("name", "value") # Test with both space and tab appended after the section tag for char in [" ", "\t"]: spaced = re.sub(r"\]\n", "]{0}\n".format(char), original.save()) copy = StructuredField(spaced) self.assertEqual(original.get("name"), copy.get("name"))
def test_section_tags_in_header(self): """ Section tags in header """ field = StructuredField("\n".join( ["[something]", self.start, self.one, self.end])) self.assertTrue("something" not in field) self.assertTrue("one" in field) self.assertEqual(field.get("one"), "1\n")
def test_multiple_values(self): """ Multiple values """ # Reading multiple values section = "[section]\nkey=val1 # comment\nkey = val2\n key = val3 " text = "\n".join([self.start, section, self.end]) field = StructuredField(text, multi=True) self.assertEqual(field.get("section", "key"), ["val1", "val2", "val3"]) # Writing multiple values values = ['1', '2', '3'] field = StructuredField(multi=True) field.set("section", values, "key") self.assertEqual(field.get("section", "key"), values) self.assertTrue("key = 1\nkey = 2\nkey = 3" in field.save()) # Remove multiple values field.remove("section", "key") self.assertTrue("key = 1\nkey = 2\nkey = 3" not in field.save()) self.assertRaises(StructuredFieldError, field.get, "section", "key")
def test_section_tag_escaping(self): """ Section tag escaping """ field = StructuredField() field.set("section", "\n[content]\n") reloaded = StructuredField(field.save()) self.assertTrue("section" in reloaded) self.assertTrue("content" not in reloaded) self.assertEqual(reloaded.get("section"), "\n[content]\n")
def test_nesting(self): """ Nesting """ # Prepare structure parent -> child -> grandchild grandchild = StructuredField() grandchild.set('name', "Grand Child\n") child = StructuredField() child.set('name', "Child Name\n") child.set("child", grandchild.save()) parent = StructuredField() parent.set("name", "Parent Name\n") parent.set("child", child.save()) # Reload back and check the names parent = StructuredField(parent.save()) child = StructuredField(parent.get("child")) grandchild = StructuredField(child.get("child")) self.assertEqual(parent.get("name"), "Parent Name\n") self.assertEqual(child.get("name"), "Child Name\n") self.assertEqual(grandchild.get("name"), "Grand Child\n")
def test_unicode_section_name(self): """ Unicode in section name """ chars = u"ěščřžýáíéů" text = "\n".join([self.start, u"[{0}]\nx".format(chars), self.end]) field = StructuredField(text) self.assertEqual(field.get(chars).strip(), "x")
def test_unicode_section_content(self): """ Unicode in section content """ chars = u"ěščřžýáíéů" text = "\n".join([self.start, "[section]", chars, self.end]) field = StructuredField(text) self.assertEqual(field.get("section").strip(), chars)
def test_section_item_get(self): """ Get section item """ text = "\n".join([self.start, "[section]\nx = 3\n", self.end]) field = StructuredField(text) self.assertEqual(field.get("section", "x"), "3")
def test_empty_section(self): """ Empty section """ field = StructuredField() field.set("section", "") reloaded = StructuredField(field.save()) self.assertEqual(reloaded.get("section"), "")