예제 #1
0
    def test_parent(self):
        s = Section("Section")
        self.assertIsNone(s.parent)
        s.parent = None
        self.assertIsNone(s.parent)
        s.parent = Document()
        self.assertIsInstance(s.parent, BaseDocument)
        self.assertIsInstance(s.parent._sections[0], BaseSection)

        """ Test if child is removed from _sections of a parent after assigning
            a new parent to the child """
        p = s.parent
        s.parent = Section("S")
        self.assertEqual(len(p._sections), 0)

        s = Section("section_doc", parent=Document())
        self.assertIsInstance(s.parent, BaseDocument)
        self.assertEqual(len(s.parent._sections), 1)
        p = s.parent
        s.parent = None
        self.assertEqual(len(p._sections), 0)
        self.assertEqual(s.parent, None)

        s = Section("section_sec", parent=Section("S"))
        self.assertIsInstance(s.parent, BaseSection)
        self.assertEqual(len(s.parent._sections), 1)
        p = s.parent
        s.parent = None
        self.assertEqual(len(p._sections), 0)
        self.assertEqual(s.parent, None)

        with self.assertRaises(ValueError):
            Section("section_property", parent=Property("P"))
예제 #2
0
    def test_contains(self):
        sec = Section(name="root")

        subsec = Section(name="subsec", type="test")
        prop = Property(name="prop")

        # Test not contains on empty child-lists.
        self.assertIsNone(sec.contains(subsec))
        self.assertIsNone(sec.contains(prop))

        # Test contains of Section and Property
        subsec.parent = sec
        simisec = Section(name="subsec", type="test")
        self.assertEqual(sec.contains(simisec).name, subsec.name)

        prop.parent = sec
        simiprop = Property(name="prop")
        self.assertEqual(sec.contains(simiprop).name, prop.name)

        # Test not contains on mismatching Section name/type and Property name
        self.assertIsNone(sec.contains(Section(name="subsec", type="typetest")))
        self.assertIsNone(sec.contains(Section(name="typesec", type="test")))
        self.assertIsNone(sec.contains(Property(name="prop_two")))

        # Test fail on non-Section/Property objects
        with self.assertRaises(ValueError):
            sec.contains(Document())

        with self.assertRaises(ValueError):
            sec.contains("some info")
예제 #3
0
    def test_path(self):
        sec = Section(name="center")
        self.assertEqual(sec.get_path(), "/")

        subsec = Section(name="leaf", parent=sec)
        self.assertEqual(subsec.get_path(), "/leaf")

        doc = Document()
        sec.parent = doc
        self.assertEqual(sec.get_path(), "/center")
        self.assertEqual(subsec.get_path(), "/center/leaf")

        top = Section(name="top", parent=doc)
        sec.parent = top
        self.assertEqual(sec.get_path(), "/top/center")
        self.assertEqual(subsec.get_path(), "/top/center/leaf")

        subsec.parent = None
        self.assertEqual(subsec.get_path(), "/")
예제 #4
0
    def test_children(self):
        sec = Section(name="sec")

        # Test set sections
        subsec = Section(name="subsec", parent=sec)
        newsec = Section(name="newsec")

        self.assertEqual(subsec.parent, sec)
        self.assertEqual(sec.sections[0], subsec)
        self.assertEqual(len(sec.sections), 1)
        self.assertIsNone(newsec.parent)

        sec.sections[0] = newsec
        self.assertEqual(newsec.parent, sec)
        self.assertEqual(sec.sections[0], newsec)
        self.assertEqual(len(sec.sections), 1)
        self.assertIsNone(subsec.parent)

        # Test parent cleanup
        root = Section(name="root")
        sec.parent = root
        subsec.parent = newsec

        self.assertEqual(len(newsec.sections), 1)
        self.assertEqual(newsec.sections[0], subsec)
        self.assertEqual(subsec.parent, newsec)
        self.assertEqual(len(root.sections), 1)
        self.assertEqual(root.sections[0], sec)

        subsec.parent = root
        self.assertEqual(len(newsec.sections), 0)
        self.assertEqual(subsec.parent, root)
        self.assertEqual(len(root.sections), 2)
        self.assertEqual(root.sections[1], subsec)

        # Test set section fails
        with self.assertRaises(ValueError):
            sec.sections[0] = Document()
        with self.assertRaises(ValueError):
            sec.sections[0] = Property("fail")
        with self.assertRaises(ValueError):
            sec.sections[0] = "subsec"

        # Test set properties
        prop = Property(name="prop", parent=sec)
        newprop = Property(name="newprop")

        self.assertEqual(prop.parent, sec)
        self.assertEqual(sec.properties[0], prop)
        self.assertEqual(len(sec.properties), 1)
        self.assertIsNone(newprop.parent)

        sec.properties[0] = newprop
        self.assertEqual(newprop.parent, sec)
        self.assertEqual(sec.properties[0], newprop)
        self.assertEqual(len(sec.properties), 1)
        self.assertIsNone(prop.parent)

        # Test set property fails
        with self.assertRaises(ValueError):
            sec.properties[0] = Document()
        with self.assertRaises(ValueError):
            sec.properties[0] = newsec
        with self.assertRaises(ValueError):
            sec.properties[0] = "prop"

        # same tests with props alias
        prop = Property(name="prop2", parent=sec)
        newprop = Property(name="newprop2")

        self.assertEqual(prop.parent, sec)
        self.assertEqual(sec.props[1], prop)
        self.assertEqual(len(sec.props), 2)
        self.assertIsNone(newprop.parent)

        sec.props[1] = newprop
        self.assertEqual(newprop.parent, sec)
        self.assertEqual(sec.props[1], newprop)
        self.assertEqual(len(sec.props), 2)
        self.assertIsNone(prop.parent)

        # Test set property fails
        with self.assertRaises(ValueError):
            sec.props[1] = Document()
        with self.assertRaises(ValueError):
            sec.props[1] = newsec
        with self.assertRaises(ValueError):
            sec.props[1] = "prop2"