Exemple #1
0
    def test_preorder_traversal(self):
        ctuple, traversal = \
            super(_TestActiveTupleContainerBase, self).\
            test_preorder_traversal()

        ctuple[1].deactivate()
        self.assertEqual([None, '[0]', '[2]'],
                         [c.name for c in pmo.preorder_traversal(
                             ctuple,
                             active=True)])
        self.assertEqual([id(ctuple),id(ctuple[0]),id(ctuple[2])],
                         [id(c) for c in pmo.preorder_traversal(
                             ctuple,
                             active=True)])

        ctuple[1].deactivate(shallow=False)
        self.assertEqual([c.name for c in traversal if c.active],
                         [c.name for c in pmo.preorder_traversal(
                             ctuple,
                             active=True)])
        self.assertEqual([id(c) for c in traversal if c.active],
                         [id(c) for c in pmo.preorder_traversal(
                             ctuple,
                             active=True)])

        ctuple.deactivate()
        self.assertEqual(len(list(pmo.preorder_traversal(ctuple,
                                                         active=True))),
                         0)
        self.assertEqual(len(list(pmo.generate_names(ctuple,
                                                     active=True))),
                         0)
Exemple #2
0
    def test_preorder_traversal(self):
        ctuple, traversal = \
            super(_TestActiveTupleContainerBase, self).\
            test_preorder_traversal()

        ctuple[1].deactivate()
        self.assertEqual(
            [None, '[0]', '[2]'],
            [c.name for c in pmo.preorder_traversal(ctuple, active=True)])
        self.assertEqual(
            [id(ctuple), id(ctuple[0]),
             id(ctuple[2])],
            [id(c) for c in pmo.preorder_traversal(ctuple, active=True)])

        ctuple[1].deactivate(shallow=False)
        self.assertEqual(
            [c.name for c in traversal if c.active],
            [c.name for c in pmo.preorder_traversal(ctuple, active=True)])
        self.assertEqual(
            [id(c) for c in traversal if c.active],
            [id(c) for c in pmo.preorder_traversal(ctuple, active=True)])

        ctuple.deactivate()
        self.assertEqual(
            len(list(pmo.preorder_traversal(ctuple, active=True))), 0)
        self.assertEqual(len(list(pmo.generate_names(ctuple, active=True))), 0)
Exemple #3
0
    def test_preorder_traversal(self):
        clist, traversal = \
            super(_TestActiveListContainerBase, self).\
            test_preorder_traversal()

        descend = lambda x: not x._is_heterogeneous_container

        clist[1].deactivate()
        self.assertEqual([None, '[0]', '[2]'], [
            c.name for c in pmo.preorder_traversal(
                clist, active=True, descend=descend)
        ])
        self.assertEqual(
            [id(clist), id(clist[0]), id(clist[2])], [
                id(c) for c in pmo.preorder_traversal(
                    clist, active=True, descend=descend)
            ])

        clist[1].deactivate(shallow=False)
        self.assertEqual([c.name for c in traversal if c.active], [
            c.name for c in pmo.preorder_traversal(
                clist, active=True, descend=descend)
        ])
        self.assertEqual([id(c) for c in traversal if c.active], [
            id(c) for c in pmo.preorder_traversal(
                clist, active=True, descend=descend)
        ])

        clist.deactivate()
        self.assertEqual(len(list(pmo.preorder_traversal(clist, active=True))),
                         0)
        self.assertEqual(len(list(pmo.generate_names(clist, active=True))), 0)
Exemple #4
0
    def test_preorder_traversal(self):
        cdict, traversal = \
            super(_TestActiveDictContainerBase, self).\
            test_preorder_traversal()

        cdict[1].deactivate()
        self.assertEqual([None, '[0]', '[2]'],
                         [c.name for c in cdict.preorder_traversal(
                             active=True)])
        self.assertEqual([id(cdict),id(cdict[0]),id(cdict[2])],
                         [id(c) for c in cdict.preorder_traversal(
                             active=True)])

        cdict[1].deactivate(shallow=False)
        self.assertEqual([c.name for c in traversal if c.active],
                         [c.name for c in cdict.preorder_traversal(
                             active=True)])
        self.assertEqual([id(c) for c in traversal if c.active],
                         [id(c) for c in cdict.preorder_traversal(
                             active=True)])

        cdict.deactivate()
        self.assertEqual(len(list(cdict.preorder_traversal(active=True))),
                         0)
        self.assertEqual(len(list(pmo.generate_names(cdict, active=True))),
                         0)
Exemple #5
0
    def test_preorder_traversal(self):
        cdict, traversal = \
            super(_TestActiveDictContainerBase, self).\
            test_preorder_traversal()

        descend = lambda x: not x._is_heterogeneous_container

        cdict[1].deactivate()
        self.assertEqual([None, '[0]', '[2]'],
                         [c.name for c in pmo.preorder_traversal(
                             cdict,
                             active=True,
                             descend=descend)])
        self.assertEqual([id(cdict),id(cdict[0]),id(cdict[2])],
                         [id(c) for c in pmo.preorder_traversal(
                             cdict,
                             active=True,
                             descend=descend)])

        cdict[1].deactivate(shallow=False)
        self.assertEqual([c.name for c in traversal if c.active],
                         [c.name for c in pmo.preorder_traversal(
                             cdict,
                             active=True,
                             descend=descend)])
        self.assertEqual([id(c) for c in traversal if c.active],
                         [id(c) for c in pmo.preorder_traversal(
                             cdict,
                             active=True,
                             descend=descend)])

        cdict.deactivate()
        self.assertEqual(len(list(pmo.preorder_traversal(cdict,
                                                         active=True))),
                         0)
        self.assertEqual(len(list(pmo.generate_names(cdict,
                                                     active=True))),
                         0)
Exemple #6
0
    def test_preorder_traversal_descend_check(self):
        ctuple, traversal = \
            super(_TestActiveTupleContainerBase, self).\
            test_preorder_traversal_descend_check()

        ctuple[1].deactivate()

        def descend(x):
            self.assertTrue(x._is_container)
            descend.seen.append(x)
            return True

        descend.seen = []
        order = list(
            pmo.preorder_traversal(ctuple, active=True, descend=descend))
        self.assertEqual([None, '[0]', '[2]'], [c.name for c in order])
        self.assertEqual([id(ctuple), id(ctuple[0]),
                          id(ctuple[2])], [id(c) for c in order])
        if ctuple.ctype._is_heterogeneous_container:
            self.assertEqual([None, '[0]', '[2]'],
                             [c.name for c in descend.seen])
            self.assertEqual(
                [id(ctuple), id(ctuple[0]),
                 id(ctuple[2])], [id(c) for c in descend.seen])
        else:
            self.assertEqual([None], [c.name for c in descend.seen])
            self.assertEqual([id(ctuple)], [id(c) for c in descend.seen])

        def descend(x):
            self.assertTrue(x._is_container)
            descend.seen.append(x)
            return x.active

        descend.seen = []
        order = list(
            pmo.preorder_traversal(ctuple, active=None, descend=descend))
        self.assertEqual([None, '[0]', '[1]', '[2]'], [c.name for c in order])
        self.assertEqual(
            [id(ctuple),
             id(ctuple[0]),
             id(ctuple[1]),
             id(ctuple[2])], [id(c) for c in order])
        if ctuple.ctype._is_heterogeneous_container:
            self.assertEqual([None, '[0]', '[1]', '[2]'],
                             [c.name for c in descend.seen])
            self.assertEqual(
                [id(ctuple),
                 id(ctuple[0]),
                 id(ctuple[1]),
                 id(ctuple[2])], [id(c) for c in descend.seen])
        else:
            self.assertEqual([None, '[1]'], [c.name for c in descend.seen])
            self.assertEqual([id(ctuple), id(ctuple[1])],
                             [id(c) for c in descend.seen])

        ctuple[1].deactivate(shallow=False)

        def descend(x):
            self.assertTrue(x._is_container)
            descend.seen.append(x)
            return True

        descend.seen = []
        order = list(
            pmo.preorder_traversal(ctuple, active=True, descend=descend))
        self.assertEqual([c.name for c in traversal if c.active],
                         [c.name for c in order])
        self.assertEqual([id(c) for c in traversal if c.active],
                         [id(c) for c in order])
        self.assertEqual([c.name for c in traversal
                          if c.active and \
                          c._is_container],
                         [c.name for c in descend.seen])
        self.assertEqual([id(c) for c in traversal
                          if c.active and \
                          c._is_container],
                         [id(c) for c in descend.seen])

        def descend(x):
            self.assertTrue(x._is_container)
            descend.seen.append(x)
            return x.active

        descend.seen = []
        order = list(
            pmo.preorder_traversal(ctuple, active=None, descend=descend))
        self.assertEqual([None, '[0]', '[1]', '[2]'], [c.name for c in order])
        self.assertEqual(
            [id(ctuple),
             id(ctuple[0]),
             id(ctuple[1]),
             id(ctuple[2])], [id(c) for c in order])
        if ctuple.ctype._is_heterogeneous_container:
            self.assertEqual([None, '[0]', '[1]', '[2]'],
                             [c.name for c in descend.seen])
            self.assertEqual(
                [id(ctuple),
                 id(ctuple[0]),
                 id(ctuple[1]),
                 id(ctuple[2])], [id(c) for c in descend.seen])
        else:
            self.assertEqual([None, '[1]'], [c.name for c in descend.seen])
            self.assertEqual([id(ctuple), id(ctuple[1])],
                             [id(c) for c in descend.seen])

        ctuple.deactivate()

        def descend(x):
            self.assertTrue(x._is_container)
            descend.seen.append(x)
            return True

        descend.seen = []
        order = list(
            pmo.preorder_traversal(ctuple, active=True, descend=descend))
        self.assertEqual(len(descend.seen), 0)
        self.assertEqual(len(list(pmo.generate_names(ctuple, active=True))), 0)

        def descend(x):
            self.assertTrue(x._is_container)
            descend.seen.append(x)
            return x.active

        descend.seen = []
        order = list(
            pmo.preorder_traversal(ctuple, active=None, descend=descend))
        self.assertEqual(len(descend.seen), 1)
        self.assertIs(descend.seen[0], ctuple)

        ctuple.deactivate(shallow=False)

        def descend(x):
            self.assertTrue(x._is_container)
            descend.seen.append(x)
            return True

        descend.seen = []
        order = list(
            pmo.preorder_traversal(ctuple, active=True, descend=descend))
        self.assertEqual(len(descend.seen), 0)
        self.assertEqual(len(list(pmo.generate_names(ctuple, active=True))), 0)

        def descend(x):
            self.assertTrue(x._is_container)
            descend.seen.append(x)
            return x.active

        descend.seen = []
        order = list(
            pmo.preorder_traversal(ctuple, active=None, descend=descend))
        self.assertEqual(len(descend.seen), 1)
        self.assertIs(descend.seen[0], ctuple)
Exemple #7
0
    def test_name(self):
        children = [self._ctype_factory() for i in range(5)]
        children.append(self._container_type([self._ctype_factory()]))

        for c in children:
            self.assertTrue(c.parent is None)
            self.assertEqual(c.local_name, None)
            self.assertEqual(c.name, None)

        ctuple = self._container_type(children)
        self.assertTrue(ctuple.parent is None)
        self.assertEqual(ctuple.local_name, None)
        self.assertEqual(ctuple.name, None)
        names = pmo.generate_names(ctuple)
        for i, c in enumerate(children):
            self.assertTrue(c.parent is ctuple)
            self.assertEqual(c.local_name, "[%s]" % (i))
            self.assertEqual(c.name, "[%s]" % (i))
            self.assertEqual(c.name, names[c])
        for c in ctuple.components():
            self.assertNotEqual(c.name, None)
            self.assertEqual(c.name, names[c])

        model = block()
        model.ctuple = ctuple
        self.assertTrue(model.parent is None)
        self.assertTrue(ctuple.parent is model)
        self.assertEqual(ctuple.local_name, "ctuple")
        self.assertEqual(ctuple.name, "ctuple")
        names = pmo.generate_names(model)
        for i, c in enumerate(children):
            self.assertTrue(c.parent is ctuple)
            self.assertEqual(c.local_name, "[%s]" % (i))
            self.assertEqual(c.name, "ctuple[%s]" % (i))
            self.assertEqual(c.name, names[c])
        for c in ctuple.components():
            self.assertNotEqual(c.name, None)
            self.assertEqual(c.name, names[c])

        b = block()
        b.model = model
        self.assertTrue(b.parent is None)
        self.assertTrue(model.parent is b)
        self.assertTrue(ctuple.parent is model)
        self.assertEqual(ctuple.local_name, "ctuple")
        self.assertEqual(ctuple.name, "model.ctuple")
        names = pmo.generate_names(b)
        for i, c in enumerate(children):
            self.assertTrue(c.parent is ctuple)
            self.assertEqual(c.local_name, "[%s]" % (i))
            self.assertEqual(c.name, "model.ctuple[%s]" % (i))
            self.assertEqual(c.name, names[c])
        for c in ctuple.components():
            self.assertNotEqual(c.name, None)
            self.assertEqual(c.name, names[c])

        blist = block_list()
        blist.append(b)
        self.assertTrue(blist.parent is None)
        self.assertTrue(b.parent is blist)
        self.assertTrue(model.parent is b)
        self.assertTrue(ctuple.parent is model)
        self.assertEqual(ctuple.local_name, "ctuple")
        self.assertEqual(ctuple.name, "[0].model.ctuple")
        for i, c in enumerate(children):
            self.assertTrue(c.parent is ctuple)
            self.assertEqual(c.local_name, "[%s]" % (i))
            self.assertEqual(c.name, "[0].model.ctuple[%s]" % (i))

        m = block()
        m.blist = blist
        self.assertTrue(m.parent is None)
        self.assertTrue(blist.parent is m)
        self.assertTrue(b.parent is blist)
        self.assertTrue(model.parent is b)
        self.assertTrue(ctuple.parent is model)
        self.assertEqual(ctuple.local_name, "ctuple")
        self.assertEqual(ctuple.name, "blist[0].model.ctuple")
        names = pmo.generate_names(m)
        for i, c in enumerate(children):
            self.assertTrue(c.parent is ctuple)
            self.assertEqual(c.local_name, "[%s]" % (i))
            self.assertEqual(c.name, "blist[0].model.ctuple[%s]" % (i))
            self.assertEqual(c.name, names[c])
        for c in ctuple.components():
            self.assertNotEqual(c.name, None)
            self.assertEqual(c.name, names[c])
        names = pmo.generate_names(m)
        for c in m.children():
            self.assertEqual(c.name, names[c])
Exemple #8
0
    def test_name(self):
        children = {}
        children['a'] = self._ctype_factory()
        children[1] = self._ctype_factory()
        children[None] = self._ctype_factory()
        children[(1, )] = self._ctype_factory()
        children[(1, 2)] = self._ctype_factory()
        children['(1,2)'] = self._ctype_factory()
        children['x'] = self._container_type()
        children['x']['y'] = self._ctype_factory()

        for key, c in children.items():
            self.assertTrue(c.parent is None)
            self.assertEqual(c.local_name, None)
            self.assertEqual(c.name, None)

        cdict = self._container_type()
        self.assertTrue(cdict.parent is None)
        self.assertEqual(cdict.local_name, None)
        self.assertEqual(cdict.name, None)
        cdict.update(children)
        names = pmo.generate_names(cdict)
        for key, c in children.items():
            self.assertTrue(c.parent is cdict)
            self.assertEqual(c.getname(fully_qualified=False, convert=str),
                             "[%s]" % (str(key)))
            self.assertEqual(c.getname(fully_qualified=False, convert=repr),
                             "[%s]" % (repr(key)))
            self.assertEqual(c.getname(fully_qualified=True, convert=str),
                             "[%s]" % (str(key)))
            self.assertEqual(c.getname(fully_qualified=True, convert=repr),
                             "[%s]" % (repr(key)))
            self.assertEqual(c.name, names[c])
        for c in cdict.components():
            self.assertNotEqual(c.name, None)
            self.assertEqual(c.name, names[c])

        model = block()
        model.cdict = cdict
        self.assertTrue(model.parent is None)
        self.assertTrue(cdict.parent is model)
        self.assertEqual(cdict.local_name, "cdict")
        self.assertEqual(cdict.name, "cdict")
        names = pmo.generate_names(model)
        for key, c in children.items():
            self.assertTrue(c.parent is cdict)
            self.assertEqual(c.getname(fully_qualified=False, convert=str),
                             "[%s]" % (str(key)))
            self.assertEqual(c.getname(fully_qualified=False, convert=repr),
                             "[%s]" % (repr(key)))
            self.assertEqual(c.getname(fully_qualified=True, convert=str),
                             "cdict[%s]" % (str(key)))
            self.assertEqual(c.getname(fully_qualified=True, convert=repr),
                             "cdict[%s]" % (repr(key)))
            self.assertEqual(c.name, names[c])
        for c in cdict.components():
            self.assertNotEqual(c.name, None)
            self.assertEqual(c.name, names[c])

        b = block()
        b.model = model
        self.assertTrue(b.parent is None)
        self.assertTrue(model.parent is b)
        self.assertTrue(cdict.parent is model)
        self.assertEqual(cdict.local_name, "cdict")
        self.assertEqual(cdict.name, "model.cdict")
        names = pmo.generate_names(b)
        for key, c in children.items():
            self.assertTrue(c.parent is cdict)
            self.assertEqual(c.getname(fully_qualified=False, convert=str),
                             "[%s]" % (str(key)))
            self.assertEqual(c.getname(fully_qualified=False, convert=repr),
                             "[%s]" % (repr(key)))
            self.assertEqual(c.getname(fully_qualified=True, convert=str),
                             "model.cdict[%s]" % (str(key)))
            self.assertEqual(c.getname(fully_qualified=True, convert=repr),
                             "model.cdict[%s]" % (repr(key)))
            self.assertEqual(c.name, names[c])
        for c in cdict.components():
            self.assertNotEqual(c.name, None)
            self.assertEqual(c.name, names[c])

        bdict = block_dict()
        bdict[0] = b
        self.assertTrue(bdict.parent is None)
        self.assertTrue(b.parent is bdict)
        self.assertTrue(model.parent is b)
        self.assertTrue(cdict.parent is model)
        self.assertEqual(cdict.local_name, "cdict")
        self.assertEqual(cdict.name, "[0].model.cdict")
        for key, c in children.items():
            self.assertTrue(c.parent is cdict)
            self.assertEqual(c.getname(fully_qualified=False, convert=str),
                             "[%s]" % (str(key)))
            self.assertEqual(c.getname(fully_qualified=False, convert=repr),
                             "[%s]" % (repr(key)))
            self.assertEqual(c.getname(fully_qualified=True, convert=str),
                             "[0].model.cdict[%s]" % (str(key)))
            self.assertEqual(c.getname(fully_qualified=True, convert=repr),
                             "[0].model.cdict[%s]" % (repr(key)))

        m = block()
        m.bdict = bdict
        self.assertTrue(m.parent is None)
        self.assertTrue(bdict.parent is m)
        self.assertTrue(b.parent is bdict)
        self.assertTrue(model.parent is b)
        self.assertTrue(cdict.parent is model)
        self.assertEqual(cdict.local_name, "cdict")
        self.assertEqual(cdict.name, "bdict[0].model.cdict")
        names = pmo.generate_names(m)
        for key, c in children.items():
            self.assertTrue(c.parent is cdict)
            self.assertEqual(c.getname(fully_qualified=False, convert=str),
                             "[%s]" % (str(key)))
            self.assertEqual(c.getname(fully_qualified=False, convert=repr),
                             "[%s]" % (repr(key)))
            self.assertEqual(c.getname(fully_qualified=True, convert=str),
                             "bdict[0].model.cdict[%s]" % (str(key)))
            self.assertEqual(c.getname(fully_qualified=True, convert=repr),
                             "bdict[0].model.cdict[%s]" % (repr(key)))
            self.assertEqual(c.name, names[c])
        for c in cdict.components():
            self.assertNotEqual(c.name, None)
            self.assertEqual(c.name, names[c])
        names = pmo.generate_names(m)
        for c in m.children():
            self.assertEqual(c.name, names[c])
Exemple #9
0
    def test_preorder_traversal_descend_check(self):
        ctuple, traversal = \
            super(_TestActiveTupleContainerBase, self).\
            test_preorder_traversal_descend_check()

        ctuple[1].deactivate()
        def descend(x):
            self.assertTrue(x._is_container)
            descend.seen.append(x)
            return True
        descend.seen = []
        order = list(pmo.preorder_traversal(ctuple,
                                            active=True,
                                            descend=descend))
        self.assertEqual([None, '[0]', '[2]'],
                         [c.name for c in order])
        self.assertEqual([id(ctuple),id(ctuple[0]),id(ctuple[2])],
                         [id(c) for c in order])
        if ctuple.ctype._is_heterogeneous_container:
            self.assertEqual([None, '[0]', '[2]'],
                             [c.name for c in descend.seen])
            self.assertEqual([id(ctuple),id(ctuple[0]),id(ctuple[2])],
                             [id(c) for c in descend.seen])
        else:
            self.assertEqual([None],
                             [c.name for c in descend.seen])
            self.assertEqual([id(ctuple)],
                             [id(c) for c in descend.seen])

        def descend(x):
            self.assertTrue(x._is_container)
            descend.seen.append(x)
            return x.active
        descend.seen = []
        order = list(pmo.preorder_traversal(ctuple,
                                            active=None,
                                            descend=descend))
        self.assertEqual([None,'[0]','[1]','[2]'],
                         [c.name for c in order])
        self.assertEqual([id(ctuple),id(ctuple[0]),id(ctuple[1]),id(ctuple[2])],
                         [id(c) for c in order])
        if ctuple.ctype._is_heterogeneous_container:
            self.assertEqual([None,'[0]','[1]','[2]'],
                             [c.name for c in descend.seen])
            self.assertEqual([id(ctuple),id(ctuple[0]),id(ctuple[1]),id(ctuple[2])],
                             [id(c) for c in descend.seen])
        else:
            self.assertEqual([None,'[1]'],
                             [c.name for c in descend.seen])
            self.assertEqual([id(ctuple),id(ctuple[1])],
                             [id(c) for c in descend.seen])

        ctuple[1].deactivate(shallow=False)
        def descend(x):
            self.assertTrue(x._is_container)
            descend.seen.append(x)
            return True
        descend.seen = []
        order = list(pmo.preorder_traversal(ctuple,
                                            active=True,
                                            descend=descend))
        self.assertEqual([c.name for c in traversal if c.active],
                         [c.name for c in order])
        self.assertEqual([id(c) for c in traversal if c.active],
                         [id(c) for c in order])
        self.assertEqual([c.name for c in traversal
                          if c.active and \
                          c._is_container],
                         [c.name for c in descend.seen])
        self.assertEqual([id(c) for c in traversal
                          if c.active and \
                          c._is_container],
                         [id(c) for c in descend.seen])

        def descend(x):
            self.assertTrue(x._is_container)
            descend.seen.append(x)
            return x.active
        descend.seen = []
        order = list(pmo.preorder_traversal(ctuple,
                                            active=None,
                                            descend=descend))
        self.assertEqual([None,'[0]','[1]','[2]'],
                         [c.name for c in order])
        self.assertEqual([id(ctuple),id(ctuple[0]),id(ctuple[1]),id(ctuple[2])],
                         [id(c) for c in order])
        if ctuple.ctype._is_heterogeneous_container:
            self.assertEqual([None,'[0]','[1]','[2]'],
                             [c.name for c in descend.seen])
            self.assertEqual([id(ctuple),id(ctuple[0]),id(ctuple[1]),id(ctuple[2])],
                             [id(c) for c in descend.seen])
        else:
            self.assertEqual([None,'[1]'],
                             [c.name for c in descend.seen])
            self.assertEqual([id(ctuple),id(ctuple[1])],
                             [id(c) for c in descend.seen])

        ctuple.deactivate()
        def descend(x):
            self.assertTrue(x._is_container)
            descend.seen.append(x)
            return True
        descend.seen = []
        order = list(pmo.preorder_traversal(ctuple,
                                            active=True,
                                            descend=descend))
        self.assertEqual(len(descend.seen), 0)
        self.assertEqual(len(list(pmo.generate_names(ctuple,
                                                     active=True))),
                         0)

        def descend(x):
            self.assertTrue(x._is_container)
            descend.seen.append(x)
            return x.active
        descend.seen = []
        order = list(pmo.preorder_traversal(ctuple,
                                            active=None,
                                            descend=descend))
        self.assertEqual(len(descend.seen), 1)
        self.assertIs(descend.seen[0], ctuple)

        ctuple.deactivate(shallow=False)
        def descend(x):
            self.assertTrue(x._is_container)
            descend.seen.append(x)
            return True
        descend.seen = []
        order = list(pmo.preorder_traversal(ctuple,
                                            active=True,
                                            descend=descend))
        self.assertEqual(len(descend.seen), 0)
        self.assertEqual(len(list(pmo.generate_names(ctuple,
                                                     active=True))),
                         0)

        def descend(x):
            self.assertTrue(x._is_container)
            descend.seen.append(x)
            return x.active
        descend.seen = []
        order = list(pmo.preorder_traversal(ctuple,
                                            active=None,
                                            descend=descend))
        self.assertEqual(len(descend.seen), 1)
        self.assertIs(descend.seen[0], ctuple)
Exemple #10
0
    def test_name(self):
        children = [self._ctype_factory() for i in range(5)]
        children.append(self._container_type(
            [self._ctype_factory()]))

        for c in children:
            self.assertTrue(c.parent is None)
            self.assertEqual(c.local_name, None)
            self.assertEqual(c.name, None)

        ctuple = self._container_type(children)
        self.assertTrue(ctuple.parent is None)
        self.assertEqual(ctuple.local_name, None)
        self.assertEqual(ctuple.name, None)
        names = pmo.generate_names(ctuple)
        for i, c in enumerate(children):
            self.assertTrue(c.parent is ctuple)
            self.assertEqual(c.local_name, "[%s]" % (i))
            self.assertEqual(c.name, "[%s]" % (i))
            self.assertEqual(c.name, names[c])
        for c in ctuple.components():
            self.assertNotEqual(c.name, None)
            self.assertEqual(c.name, names[c])

        model = block()
        model.ctuple = ctuple
        self.assertTrue(model.parent is None)
        self.assertTrue(ctuple.parent is model)
        self.assertEqual(ctuple.local_name, "ctuple")
        self.assertEqual(ctuple.name, "ctuple")
        names = pmo.generate_names(model)
        for i, c in enumerate(children):
            self.assertTrue(c.parent is ctuple)
            self.assertEqual(c.local_name, "[%s]" % (i))
            self.assertEqual(c.name, "ctuple[%s]" % (i))
            self.assertEqual(c.name, names[c])
        for c in ctuple.components():
            self.assertNotEqual(c.name, None)
            self.assertEqual(c.name, names[c])

        b = block()
        b.model = model
        self.assertTrue(b.parent is None)
        self.assertTrue(model.parent is b)
        self.assertTrue(ctuple.parent is model)
        self.assertEqual(ctuple.local_name, "ctuple")
        self.assertEqual(ctuple.name, "model.ctuple")
        names = pmo.generate_names(b)
        for i, c in enumerate(children):
            self.assertTrue(c.parent is ctuple)
            self.assertEqual(c.local_name, "[%s]" % (i))
            self.assertEqual(c.name, "model.ctuple[%s]" % (i))
            self.assertEqual(c.name, names[c])
        for c in ctuple.components():
            self.assertNotEqual(c.name, None)
            self.assertEqual(c.name, names[c])

        blist = block_list()
        blist.append(b)
        self.assertTrue(blist.parent is None)
        self.assertTrue(b.parent is blist)
        self.assertTrue(model.parent is b)
        self.assertTrue(ctuple.parent is model)
        self.assertEqual(ctuple.local_name, "ctuple")
        self.assertEqual(ctuple.name, "[0].model.ctuple")
        for i, c in enumerate(children):
            self.assertTrue(c.parent is ctuple)
            self.assertEqual(c.local_name, "[%s]" % (i))
            self.assertEqual(c.name,
                             "[0].model.ctuple[%s]" % (i))

        m = block()
        m.blist = blist
        self.assertTrue(m.parent is None)
        self.assertTrue(blist.parent is m)
        self.assertTrue(b.parent is blist)
        self.assertTrue(model.parent is b)
        self.assertTrue(ctuple.parent is model)
        self.assertEqual(ctuple.local_name, "ctuple")
        self.assertEqual(ctuple.name, "blist[0].model.ctuple")
        names = pmo.generate_names(m)
        for i, c in enumerate(children):
            self.assertTrue(c.parent is ctuple)
            self.assertEqual(c.local_name, "[%s]" % (i))
            self.assertEqual(c.name,
                             "blist[0].model.ctuple[%s]" % (i))
            self.assertEqual(c.name, names[c])
        for c in ctuple.components():
            self.assertNotEqual(c.name, None)
            self.assertEqual(c.name, names[c])
        names = pmo.generate_names(m)
        for c in m.children():
            self.assertEqual(c.name, names[c])
Exemple #11
0
    def test_name(self):
        children = {}
        children['a'] = self._ctype_factory()
        children[1] = self._ctype_factory()
        children[None] = self._ctype_factory()
        children[(1,)] = self._ctype_factory()
        children[(1,2)] = self._ctype_factory()
        children['(1,2)'] = self._ctype_factory()
        children['x'] = self._container_type()
        children['x']['y'] = self._ctype_factory()

        for key, c in children.items():
            self.assertTrue(c.parent is None)
            self.assertEqual(c.local_name, None)
            self.assertEqual(c.name, None)

        cdict = self._container_type()
        self.assertTrue(cdict.parent is None)
        self.assertEqual(cdict.local_name, None)
        self.assertEqual(cdict.name, None)
        cdict.update(children)
        names = pmo.generate_names(cdict)
        for key, c in children.items():
            self.assertTrue(c.parent is cdict)
            self.assertEqual(c.getname(fully_qualified=False, convert=str),
                             "[%s]" % (str(key)))
            self.assertEqual(c.getname(fully_qualified=False, convert=repr),
                             "[%s]" % (repr(key)))
            self.assertEqual(c.getname(fully_qualified=True, convert=str),
                             "[%s]" % (str(key)))
            self.assertEqual(c.getname(fully_qualified=True, convert=repr),
                             "[%s]" % (repr(key)))
            self.assertEqual(c.name, names[c])
        for c in cdict.components():
            self.assertNotEqual(c.name, None)
            self.assertEqual(c.name, names[c])

        model = block()
        model.cdict = cdict
        self.assertTrue(model.parent is None)
        self.assertTrue(cdict.parent is model)
        self.assertEqual(cdict.local_name, "cdict")
        self.assertEqual(cdict.name, "cdict")
        names = pmo.generate_names(model)
        for key, c in children.items():
            self.assertTrue(c.parent is cdict)
            self.assertEqual(c.getname(fully_qualified=False, convert=str),
                             "[%s]" % (str(key)))
            self.assertEqual(c.getname(fully_qualified=False, convert=repr),
                             "[%s]" % (repr(key)))
            self.assertEqual(c.getname(fully_qualified=True, convert=str),
                             "cdict[%s]" % (str(key)))
            self.assertEqual(c.getname(fully_qualified=True, convert=repr),
                             "cdict[%s]" % (repr(key)))
            self.assertEqual(c.name, names[c])
        for c in cdict.components():
            self.assertNotEqual(c.name, None)
            self.assertEqual(c.name, names[c])

        b = block()
        b.model = model
        self.assertTrue(b.parent is None)
        self.assertTrue(model.parent is b)
        self.assertTrue(cdict.parent is model)
        self.assertEqual(cdict.local_name, "cdict")
        self.assertEqual(cdict.name, "model.cdict")
        names = pmo.generate_names(b)
        for key, c in children.items():
            self.assertTrue(c.parent is cdict)
            self.assertEqual(c.getname(fully_qualified=False, convert=str),
                             "[%s]" % (str(key)))
            self.assertEqual(c.getname(fully_qualified=False, convert=repr),
                             "[%s]" % (repr(key)))
            self.assertEqual(c.getname(fully_qualified=True, convert=str),
                             "model.cdict[%s]" % (str(key)))
            self.assertEqual(c.getname(fully_qualified=True, convert=repr),
                             "model.cdict[%s]" % (repr(key)))
            self.assertEqual(c.name, names[c])
        for c in cdict.components():
            self.assertNotEqual(c.name, None)
            self.assertEqual(c.name, names[c])

        bdict = block_dict()
        bdict[0] = b
        self.assertTrue(bdict.parent is None)
        self.assertTrue(b.parent is bdict)
        self.assertTrue(model.parent is b)
        self.assertTrue(cdict.parent is model)
        self.assertEqual(cdict.local_name, "cdict")
        self.assertEqual(cdict.name, "[0].model.cdict")
        for key, c in children.items():
            self.assertTrue(c.parent is cdict)
            self.assertEqual(c.getname(fully_qualified=False, convert=str),
                             "[%s]" % (str(key)))
            self.assertEqual(c.getname(fully_qualified=False, convert=repr),
                             "[%s]" % (repr(key)))
            self.assertEqual(c.getname(fully_qualified=True, convert=str),
                             "[0].model.cdict[%s]" % (str(key)))
            self.assertEqual(c.getname(fully_qualified=True, convert=repr),
                             "[0].model.cdict[%s]" % (repr(key)))

        m = block()
        m.bdict = bdict
        self.assertTrue(m.parent is None)
        self.assertTrue(bdict.parent is m)
        self.assertTrue(b.parent is bdict)
        self.assertTrue(model.parent is b)
        self.assertTrue(cdict.parent is model)
        self.assertEqual(cdict.local_name, "cdict")
        self.assertEqual(cdict.name, "bdict[0].model.cdict")
        names = pmo.generate_names(m)
        for key, c in children.items():
            self.assertTrue(c.parent is cdict)
            self.assertEqual(c.getname(fully_qualified=False, convert=str),
                             "[%s]" % (str(key)))
            self.assertEqual(c.getname(fully_qualified=False, convert=repr),
                             "[%s]" % (repr(key)))
            self.assertEqual(c.getname(fully_qualified=True, convert=str),
                             "bdict[0].model.cdict[%s]" % (str(key)))
            self.assertEqual(c.getname(fully_qualified=True, convert=repr),
                             "bdict[0].model.cdict[%s]" % (repr(key)))
            self.assertEqual(c.name, names[c])
        for c in cdict.components():
            self.assertNotEqual(c.name, None)
            self.assertEqual(c.name, names[c])
        names = pmo.generate_names(m)
        for c in m.children():
            self.assertEqual(c.name, names[c])
Exemple #12
0
    def test_preorder_traversal_descend_check(self):
        cdict, traversal = \
            super(_TestActiveDictContainerBase, self).\
            test_preorder_traversal_descend_check()

        cdict[1].deactivate()
        def descend(x):
            self.assertTrue(x._is_container)
            descend.seen.append(x)
            return True
        descend.seen = []
        order = list(cdict.preorder_traversal(active=True,
                                              descend=descend))
        self.assertEqual([None, '[0]', '[2]'],
                         [c.name for c in order])
        self.assertEqual([id(cdict),id(cdict[0]),id(cdict[2])],
                         [id(c) for c in order])
        self.assertEqual([None],
                         [c.name for c in descend.seen])
        self.assertEqual([id(cdict)],
                         [id(c) for c in descend.seen])

        def descend(x):
            self.assertTrue(x._is_container)
            descend.seen.append(x)
            return x.active
        descend.seen = []
        order = list(cdict.preorder_traversal(descend=descend))
        self.assertEqual([None,'[0]','[1]','[2]'],
                         [c.name for c in order])
        self.assertEqual([id(cdict),id(cdict[0]),id(cdict[1]),id(cdict[2])],
                         [id(c) for c in order])
        self.assertEqual([None,'[1]'],
                         [c.name for c in descend.seen])
        self.assertEqual([id(cdict),id(cdict[1])],
                         [id(c) for c in descend.seen])

        cdict[1].deactivate(shallow=False)
        def descend(x):
            descend.seen.append(x)
            return True
        descend.seen = []
        order = list(cdict.preorder_traversal(active=True,
                                              descend=descend))
        self.assertEqual([c.name for c in traversal if c.active],
                         [c.name for c in order])
        self.assertEqual([id(c) for c in traversal if c.active],
                         [id(c) for c in order])
        self.assertEqual([c.name for c in traversal
                          if c.active and \
                          c._is_container and \
                          (not c._is_heterogeneous_container)],
                         [c.name for c in descend.seen])
        self.assertEqual([id(c) for c in traversal
                          if c.active and \
                          c._is_container and \
                          (not c._is_heterogeneous_container)],
                         [id(c) for c in descend.seen])

        def descend(x):
            descend.seen.append(x)
            return x.active
        descend.seen = []
        order = list(cdict.preorder_traversal(descend=descend))
        self.assertEqual([None,'[0]','[1]','[2]'],
                         [c.name for c in order])
        self.assertEqual([id(cdict),id(cdict[0]),id(cdict[1]),id(cdict[2])],
                         [id(c) for c in order])
        self.assertEqual([None,'[1]'],
                         [c.name for c in descend.seen])
        self.assertEqual([id(cdict),id(cdict[1])],
                         [id(c) for c in descend.seen])

        cdict.deactivate()
        def descend(x):
            descend.seen.append(x)
            return True
        descend.seen = []
        order = list(cdict.preorder_traversal(active=True,
                                              descend=descend))
        self.assertEqual(len(descend.seen), 0)
        self.assertEqual(len(list(pmo.generate_names(cdict,
                                                     active=True))),
                         0)

        def descend(x):
            descend.seen.append(x)
            return x.active
        descend.seen = []
        order = list(cdict.preorder_traversal(descend=descend))
        self.assertEqual(len(descend.seen), 1)
        self.assertIs(descend.seen[0], cdict)