Ejemplo n.º 1
0
    def test_ILocation(self):
        # XXX: make tester object for ``ILocation`` contract.

        # ``ILocations`` promises ``__name__`` and ``__parent__`` attributes.
        # They are used to define tree hierarchy. As read only arguments they
        # are available at ``name`` and ``parent`` on nodes:
        mynode = create_tree(MyNode)
        self.assertTrue(
            str(mynode).startswith('<MyNode object \'None\' at')
        )
        self.assertEqual(mynode.__name__, None)
        self.assertEqual(mynode.__parent__, None)
        self.assertEqual(mynode.name, None)
        self.assertEqual(mynode.parent, None)
        self.assertEqual(mynode['child_1'].name, 'child_1')
        self.assertTrue(mynode['child_1'].parent is mynode)
        self.assertEqual(mynode['child_1']['subchild_1'].name, 'subchild_1')
        self.assertTrue(
            mynode['child_1']['subchild_1'].parent.parent is mynode
        )

        basenode = create_tree(BaseNode)
        self.assertTrue(
            str(basenode).startswith('<BaseNode object \'None\' at')
        )
        self.assertEqual(basenode.name, None)
        self.assertEqual(basenode.parent, None)
        self.assertEqual(basenode['child_1'].name, 'child_1')
        self.assertTrue(basenode['child_1'].parent is basenode)
        self.assertEqual(basenode['child_1']['subchild_1'].name, 'subchild_1')
        self.assertTrue(
            basenode['child_1']['subchild_1'].parent.parent is basenode
        )

        orderednode = create_tree(OrderedNode)
        self.assertTrue(
            str(orderednode).startswith('<OrderedNode object \'None\' at')
        )
        self.assertEqual(orderednode.name, None)
        self.assertEqual(orderednode.parent, None)
        self.assertEqual(orderednode['child_1'].name, 'child_1')
        self.assertTrue(orderednode['child_1'].parent is orderednode)
        self.assertEqual(
            orderednode['child_1']['subchild_1'].name, 'subchild_1'
        )
        self.assertTrue(
            orderednode['child_1']['subchild_1'].parent.parent is orderednode
        )
Ejemplo n.º 2
0
 def test_create_tree(self):
     self.assertEqual(
         create_tree(odict),
         odict([('child_0',
                 odict([('subchild_0', odict()), ('subchild_1', odict())])),
                ('child_1',
                 odict([('subchild_0', odict()), ('subchild_1', odict())])),
                ('child_2',
                 odict([('subchild_0', odict()),
                        ('subchild_1', odict())]))]))
Ejemplo n.º 3
0
    def test_ILocation(self):
        # XXX: make tester object for ``ILocation`` contract.

        # ``ILocations`` promises ``__name__`` and ``__parent__`` attributes.
        # They are used to define tree hierarchy. As read only arguments they
        # are available at ``name`` and ``parent`` on nodes:
        mynode = create_tree(MyNode)
        self.assertTrue(str(mynode).startswith('<MyNode object \'None\' at'))
        self.assertEqual(mynode.__name__, None)
        self.assertEqual(mynode.__parent__, None)
        self.assertEqual(mynode.name, None)
        self.assertEqual(mynode.parent, None)
        self.assertEqual(mynode['child_1'].name, 'child_1')
        self.assertTrue(mynode['child_1'].parent is mynode)
        self.assertEqual(mynode['child_1']['subchild_1'].name, 'subchild_1')
        self.assertTrue(
            mynode['child_1']['subchild_1'].parent.parent is mynode)

        basenode = create_tree(BaseNode)
        self.assertTrue(
            str(basenode).startswith('<BaseNode object \'None\' at'))
        self.assertEqual(basenode.name, None)
        self.assertEqual(basenode.parent, None)
        self.assertEqual(basenode['child_1'].name, 'child_1')
        self.assertTrue(basenode['child_1'].parent is basenode)
        self.assertEqual(basenode['child_1']['subchild_1'].name, 'subchild_1')
        self.assertTrue(
            basenode['child_1']['subchild_1'].parent.parent is basenode)

        orderednode = create_tree(OrderedNode)
        self.assertTrue(
            str(orderednode).startswith('<OrderedNode object \'None\' at'))
        self.assertEqual(orderednode.name, None)
        self.assertEqual(orderednode.parent, None)
        self.assertEqual(orderednode['child_1'].name, 'child_1')
        self.assertTrue(orderednode['child_1'].parent is orderednode)
        self.assertEqual(orderednode['child_1']['subchild_1'].name,
                         'subchild_1')
        self.assertTrue(
            orderednode['child_1']['subchild_1'].parent.parent is orderednode)
Ejemplo n.º 4
0
 def test_create_tree(self):
     self.assertEqual(create_tree(odict), odict([
         ('child_0', odict([
             ('subchild_0', odict()),
             ('subchild_1', odict())
         ])),
         ('child_1', odict([
             ('subchild_0', odict()),
             ('subchild_1', odict())
         ])),
         ('child_2', odict([
             ('subchild_0', odict()),
             ('subchild_1', odict())
         ]))
     ]))
Ejemplo n.º 5
0
    def test_INode(self):
        # XXX: make tester object for INode contract
        # XXX: decide wether ``aliases`` or ``aliaser`` (still dunno) should be
        #      kept in base interface.

        # printtree
        mynode = create_tree(MyNode)
        self.check_output("""\
        <class 'node.testing.env.MyNode'>: None
          <class 'node.testing.env.MyNode'>: child_0
            <class 'node.testing.env.MyNode'>: subchild_0
            <class 'node.testing.env.MyNode'>: subchild_1
          <class 'node.testing.env.MyNode'>: child_1
            <class 'node.testing.env.MyNode'>: subchild_0
            <class 'node.testing.env.MyNode'>: subchild_1
          <class 'node.testing.env.MyNode'>: child_2
            <class 'node.testing.env.MyNode'>: subchild_0
            <class 'node.testing.env.MyNode'>: subchild_1
        """, mynode.treerepr())

        basenode = create_tree(BaseNode)
        self.check_output("""\
        <class 'node.base.BaseNode'>: None
          <class 'node.base.BaseNode'>: child_...
            <class 'node.base.BaseNode'>: subchild_...
            <class 'node.base.BaseNode'>: subchild_...
          <class 'node.base.BaseNode'>: child_...
            <class 'node.base.BaseNode'>: subchild_...
            <class 'node.base.BaseNode'>: subchild_...
          <class 'node.base.BaseNode'>: child_...
            <class 'node.base.BaseNode'>: subchild_...
            <class 'node.base.BaseNode'>: subchild_...
        """, basenode.treerepr())

        orderednode = create_tree(OrderedNode)
        self.check_output("""\
        <class 'node.base.OrderedNode'>: None
          <class 'node.base.OrderedNode'>: child_0
            <class 'node.base.OrderedNode'>: subchild_0
            <class 'node.base.OrderedNode'>: subchild_1
          <class 'node.base.OrderedNode'>: child_1
            <class 'node.base.OrderedNode'>: subchild_0
            <class 'node.base.OrderedNode'>: subchild_1
          <class 'node.base.OrderedNode'>: child_2
            <class 'node.base.OrderedNode'>: subchild_0
            <class 'node.base.OrderedNode'>: subchild_1
        """, orderednode.treerepr())

        # path
        mynode.__name__ = 'root'
        self.assertEqual(mynode.path, ['root'])
        self.assertEqual(
            mynode['child_1']['subchild_1'].path,
            ['root', 'child_1', 'subchild_1']
        )

        basenode.__name__ = 'root'
        self.assertEqual(basenode.path, ['root'])
        self.assertEqual(
            basenode['child_1']['subchild_1'].path,
            ['root', 'child_1', 'subchild_1']
        )

        orderednode.__name__ = 'root'
        self.assertEqual(orderednode.path, ['root'])
        self.assertEqual(
            orderednode['child_1']['subchild_1'].path,
            ['root', 'child_1', 'subchild_1']
        )

        # root
        self.assertTrue(mynode['child_1']['subchild_1'].root is mynode)
        self.assertTrue(basenode['child_1']['subchild_1'].root is basenode)
        self.assertTrue(
            orderednode['child_1']['subchild_1'].root is orderednode
        )

        # allow_non_node_childs
        self.assertFalse(mynode.allow_non_node_childs)

        def non_node_childs_not_allowed():
            mynode['foo'] = object()
        err = self.expect_error(ValueError, non_node_childs_not_allowed)
        self.assertEqual(str(err), 'Non-node childs are not allowed.')

        def no_classes_as_values_allowed():
            mynode['foo'] = object
        err = self.expect_error(ValueError, no_classes_as_values_allowed)
        expected = 'It isn\'t allowed to use classes as values.'
        self.assertEqual(str(err), expected)

        mynode.allow_non_node_childs = True
        obj = mynode['foo'] = object()
        self.assertEqual(mynode['foo'], obj)

        del mynode['foo']
        mynode.allow_non_node_childs = False

        self.assertFalse(basenode.allow_non_node_childs)

        def non_node_childs_not_allowed2():
            basenode['foo'] = object()
        err = self.expect_error(ValueError, non_node_childs_not_allowed2)
        self.assertEqual(str(err), 'Non-node childs are not allowed.')

        def no_classes_as_values_allowed2():
            basenode['foo'] = object
        err = self.expect_error(ValueError, no_classes_as_values_allowed2)
        expected = 'It isn\'t allowed to use classes as values.'
        self.assertEqual(str(err), expected)

        basenode.allow_non_node_childs = True
        obj = basenode['foo'] = object()
        self.assertEqual(basenode['foo'], obj)

        del basenode['foo']
        basenode.allow_non_node_childs = False

        self.assertFalse(orderednode.allow_non_node_childs)

        def non_node_childs_not_allowed3():
            orderednode['foo'] = object()
        err = self.expect_error(ValueError, non_node_childs_not_allowed3)
        self.assertEqual(str(err), 'Non-node childs are not allowed.')

        def no_classes_as_values_allowed3():
            orderednode['foo'] = object
        err = self.expect_error(ValueError, no_classes_as_values_allowed3)
        expected = 'It isn\'t allowed to use classes as values.'
        self.assertEqual(str(err), expected)

        orderednode.allow_non_node_childs = True
        obj = orderednode['foo'] = object()
        self.assertEqual(orderednode['foo'], obj)

        del orderednode['foo']
        orderednode.allow_non_node_childs = False

        # filteredvalues and filtereditervalues
        class IFilter(Interface):
            pass

        directlyProvides(mynode['child_2'], IFilter)
        self.assertEqual(
            list(mynode.filtereditervalues(IFilter)),
            [mynode['child_2']]
        )
        self.assertEqual(
            mynode.filteredvalues(IFilter),
            [mynode['child_2']]
        )
        noLongerProvides(mynode['child_2'], IFilter)
        self.assertEqual(list(mynode.filtereditervalues(IFilter)), [])
        self.assertEqual(mynode.filteredvalues(IFilter), [])

        directlyProvides(basenode['child_2'], IFilter)
        self.assertEqual(
            list(basenode.filtereditervalues(IFilter)),
            [basenode['child_2']]
        )
        self.assertEqual(
            basenode.filteredvalues(IFilter),
            [basenode['child_2']]
        )
        noLongerProvides(basenode['child_2'], IFilter)
        self.assertEqual(list(basenode.filtereditervalues(IFilter)), [])
        self.assertEqual(basenode.filteredvalues(IFilter), [])

        directlyProvides(orderednode['child_2'], IFilter)
        self.assertEqual(
            list(orderednode.filtereditervalues(IFilter)),
            [orderednode['child_2']]
        )
        self.assertEqual(
            orderednode.filteredvalues(IFilter),
            [orderednode['child_2']]
        )
        noLongerProvides(orderednode['child_2'], IFilter)
        self.assertEqual(list(orderednode.filtereditervalues(IFilter)), [])
        self.assertEqual(orderednode.filteredvalues(IFilter), [])

        # as_attribute_access
        myattrs = mynode.as_attribute_access()
        self.assertEqual(myattrs.__class__, AttributeAccess)
        self.assertEqual(myattrs.child_1, mynode['child_1'])
        myattrs.child_3 = MyNode()
        self.assertEqual(mynode['child_3'], myattrs.child_3)

        def no_classes_as_values_allowed4():
            myattrs.child_4 = object
        err = self.expect_error(ValueError, no_classes_as_values_allowed4)
        expected = 'It isn\'t allowed to use classes as values.'
        self.assertEqual(str(err), expected)

        baseattrs = basenode.as_attribute_access()
        self.assertEqual(baseattrs.__class__, AttributeAccess)
        self.assertEqual(baseattrs.child_1, basenode['child_1'])
        baseattrs.child_3 = BaseNode()
        self.assertEqual(basenode['child_3'], baseattrs.child_3)

        def no_classes_as_values_allowed5():
            baseattrs.child_4 = object
        err = self.expect_error(ValueError, no_classes_as_values_allowed5)
        expected = 'It isn\'t allowed to use classes as values.'
        self.assertEqual(str(err), expected)

        orderedattrs = orderednode.as_attribute_access()
        self.assertEqual(orderedattrs.__class__, AttributeAccess)
        self.assertEqual(orderedattrs.child_1, orderednode['child_1'])
        orderedattrs.child_3 = OrderedNode()
        self.assertEqual(orderednode['child_3'], orderedattrs.child_3)

        def no_classes_as_values_allowed6():
            orderedattrs.child_4 = object
        err = self.expect_error(ValueError, no_classes_as_values_allowed6)
        expected = 'It isn\'t allowed to use classes as values.'
        self.assertEqual(str(err), expected)
Ejemplo n.º 6
0
    def test_INode(self):
        # XXX: make tester object for INode contract
        # XXX: decide wether ``aliases`` or ``aliaser`` (still dunno) should be
        #      kept in base interface.

        # printtree
        mynode = create_tree(MyNode)
        self.checkOutput(
            """\
        <class 'node.testing.env.MyNode'>: None
        __<class 'node.testing.env.MyNode'>: child_0
        ____<class 'node.testing.env.MyNode'>: subchild_0
        ____<class 'node.testing.env.MyNode'>: subchild_1
        __<class 'node.testing.env.MyNode'>: child_1
        ____<class 'node.testing.env.MyNode'>: subchild_0
        ____<class 'node.testing.env.MyNode'>: subchild_1
        __<class 'node.testing.env.MyNode'>: child_2
        ____<class 'node.testing.env.MyNode'>: subchild_0
        ____<class 'node.testing.env.MyNode'>: subchild_1
        """, mynode.treerepr(prefix='_'))

        basenode = create_tree(BaseNode)
        self.checkOutput(
            """\
        <class 'node.base.BaseNode'>: None
        __<class 'node.base.BaseNode'>: child_...
        ____<class 'node.base.BaseNode'>: subchild_...
        ____<class 'node.base.BaseNode'>: subchild_...
        __<class 'node.base.BaseNode'>: child_...
        ____<class 'node.base.BaseNode'>: subchild_...
        ____<class 'node.base.BaseNode'>: subchild_...
        __<class 'node.base.BaseNode'>: child_...
        ____<class 'node.base.BaseNode'>: subchild_...
        ____<class 'node.base.BaseNode'>: subchild_...
        """, basenode.treerepr(prefix='_'))

        orderednode = create_tree(OrderedNode)
        self.checkOutput(
            """\
        <class 'node.base.OrderedNode'>: None
        __<class 'node.base.OrderedNode'>: child_0
        ____<class 'node.base.OrderedNode'>: subchild_0
        ____<class 'node.base.OrderedNode'>: subchild_1
        __<class 'node.base.OrderedNode'>: child_1
        ____<class 'node.base.OrderedNode'>: subchild_0
        ____<class 'node.base.OrderedNode'>: subchild_1
        __<class 'node.base.OrderedNode'>: child_2
        ____<class 'node.base.OrderedNode'>: subchild_0
        ____<class 'node.base.OrderedNode'>: subchild_1
        """, orderednode.treerepr(prefix='_'))

        # path
        mynode.__name__ = 'root'
        self.assertEqual(mynode.path, ['root'])
        self.assertEqual(mynode['child_1']['subchild_1'].path,
                         ['root', 'child_1', 'subchild_1'])

        basenode.__name__ = 'root'
        self.assertEqual(basenode.path, ['root'])
        self.assertEqual(basenode['child_1']['subchild_1'].path,
                         ['root', 'child_1', 'subchild_1'])

        orderednode.__name__ = 'root'
        self.assertEqual(orderednode.path, ['root'])
        self.assertEqual(orderednode['child_1']['subchild_1'].path,
                         ['root', 'child_1', 'subchild_1'])

        # root
        self.assertTrue(mynode['child_1']['subchild_1'].root is mynode)
        self.assertTrue(basenode['child_1']['subchild_1'].root is basenode)
        self.assertTrue(
            orderednode['child_1']['subchild_1'].root is orderednode)

        # allow_non_node_childs
        self.assertFalse(mynode.allow_non_node_childs)

        def non_node_childs_not_allowed():
            mynode['foo'] = object()

        err = self.expectError(ValueError, non_node_childs_not_allowed)
        self.assertEqual(str(err), 'Non-node childs are not allowed.')

        def no_classes_as_values_allowed():
            mynode['foo'] = object

        err = self.expectError(ValueError, no_classes_as_values_allowed)
        expected = 'It isn\'t allowed to use classes as values.'
        self.assertEqual(str(err), expected)

        mynode.allow_non_node_childs = True
        obj = mynode['foo'] = object()
        self.assertEqual(mynode['foo'], obj)

        del mynode['foo']
        mynode.allow_non_node_childs = False

        self.assertFalse(basenode.allow_non_node_childs)

        def non_node_childs_not_allowed2():
            basenode['foo'] = object()

        err = self.expectError(ValueError, non_node_childs_not_allowed2)
        self.assertEqual(str(err), 'Non-node childs are not allowed.')

        def no_classes_as_values_allowed2():
            basenode['foo'] = object

        err = self.expectError(ValueError, no_classes_as_values_allowed2)
        expected = 'It isn\'t allowed to use classes as values.'
        self.assertEqual(str(err), expected)

        basenode.allow_non_node_childs = True
        obj = basenode['foo'] = object()
        self.assertEqual(basenode['foo'], obj)

        del basenode['foo']
        basenode.allow_non_node_childs = False

        self.assertFalse(orderednode.allow_non_node_childs)

        def non_node_childs_not_allowed3():
            orderednode['foo'] = object()

        err = self.expectError(ValueError, non_node_childs_not_allowed3)
        self.assertEqual(str(err), 'Non-node childs are not allowed.')

        def no_classes_as_values_allowed3():
            orderednode['foo'] = object

        err = self.expectError(ValueError, no_classes_as_values_allowed3)
        expected = 'It isn\'t allowed to use classes as values.'
        self.assertEqual(str(err), expected)

        orderednode.allow_non_node_childs = True
        obj = orderednode['foo'] = object()
        self.assertEqual(orderednode['foo'], obj)

        del orderednode['foo']
        orderednode.allow_non_node_childs = False

        # filteredvalues and filtereditervalues
        class IFilter(Interface):
            pass

        directlyProvides(mynode['child_2'], IFilter)
        self.assertEqual(list(mynode.filtereditervalues(IFilter)),
                         [mynode['child_2']])
        self.assertEqual(mynode.filteredvalues(IFilter), [mynode['child_2']])
        noLongerProvides(mynode['child_2'], IFilter)
        self.assertEqual(list(mynode.filtereditervalues(IFilter)), [])
        self.assertEqual(mynode.filteredvalues(IFilter), [])

        directlyProvides(basenode['child_2'], IFilter)
        self.assertEqual(list(basenode.filtereditervalues(IFilter)),
                         [basenode['child_2']])
        self.assertEqual(basenode.filteredvalues(IFilter),
                         [basenode['child_2']])
        noLongerProvides(basenode['child_2'], IFilter)
        self.assertEqual(list(basenode.filtereditervalues(IFilter)), [])
        self.assertEqual(basenode.filteredvalues(IFilter), [])

        directlyProvides(orderednode['child_2'], IFilter)
        self.assertEqual(list(orderednode.filtereditervalues(IFilter)),
                         [orderednode['child_2']])
        self.assertEqual(orderednode.filteredvalues(IFilter),
                         [orderednode['child_2']])
        noLongerProvides(orderednode['child_2'], IFilter)
        self.assertEqual(list(orderednode.filtereditervalues(IFilter)), [])
        self.assertEqual(orderednode.filteredvalues(IFilter), [])

        # as_attribute_access
        myattrs = mynode.as_attribute_access()
        self.assertEqual(myattrs.__class__, AttributeAccess)
        self.assertEqual(myattrs.child_1, mynode['child_1'])
        myattrs.child_3 = MyNode()
        self.assertEqual(mynode['child_3'], myattrs.child_3)

        def no_classes_as_values_allowed4():
            myattrs.child_4 = object

        err = self.expectError(ValueError, no_classes_as_values_allowed4)
        expected = 'It isn\'t allowed to use classes as values.'
        self.assertEqual(str(err), expected)

        baseattrs = basenode.as_attribute_access()
        self.assertEqual(baseattrs.__class__, AttributeAccess)
        self.assertEqual(baseattrs.child_1, basenode['child_1'])
        baseattrs.child_3 = BaseNode()
        self.assertEqual(basenode['child_3'], baseattrs.child_3)

        def no_classes_as_values_allowed5():
            baseattrs.child_4 = object

        err = self.expectError(ValueError, no_classes_as_values_allowed5)
        expected = 'It isn\'t allowed to use classes as values.'
        self.assertEqual(str(err), expected)

        orderedattrs = orderednode.as_attribute_access()
        self.assertEqual(orderedattrs.__class__, AttributeAccess)
        self.assertEqual(orderedattrs.child_1, orderednode['child_1'])
        orderedattrs.child_3 = OrderedNode()
        self.assertEqual(orderednode['child_3'], orderedattrs.child_3)

        def no_classes_as_values_allowed6():
            orderedattrs.child_4 = object

        err = self.expectError(ValueError, no_classes_as_values_allowed6)
        expected = 'It isn\'t allowed to use classes as values.'
        self.assertEqual(str(err), expected)