def test_klass_hierarchy(self):

        from traits.util.toposort import topological_sort

        from pgv.red.api import PythonNamespace

        namespace = PythonNamespace()
        package = namespace.lookup("quickblocks")

        def harvest_klasses(node):
            """ Recursively harvest all klasses from the given node. """

            klasses = []
            for child in node.children:
                if isinstance(child, Klass):
                    klasses.append(child)

                klasses.extend(harvest_klasses(child))

            return klasses

        klasses = harvest_klasses(package)

        # Build a dependency graph.
        graph = {}
        for klass in klasses:
            arcs = graph.setdefault(klass, [])
            # print 'Klass', klass.name, klass.module.filename
            arcs.extend(klass.bases)

        x = topological_sort(graph)
        print x

        return
    def test_no_such_name(self):

        namespace = PythonNamespace()
        module    = namespace.lookup('traits.has_traits.HasTraits')

        self.failUnlessRaises(NameError, module.resolve, 'Bogus')
        
        return
    def test_bug(self):

        from pgv.red.api import PythonNamespace

        namespace = PythonNamespace()
        klass = namespace.lookup("quickblocks.appbase.application.Application")
        klass.bases

        return
    def test_base_classes(self):

        from pgv.red.api import PythonNamespace

        namespace = PythonNamespace()
        klass = namespace.lookup("pgv.red.namespace_node.NamespaceNode")

        self.assertEqual(1, len(klass.bases))
        self.assertEqual("HasTraits", klass.bases[0].name)

        return
    def XXXtest_from_x_import_y(self):

        namespace = PythonNamespace()
        module    = namespace.lookup('pgv.red.namespace_node')
        klass     = module.resolve('HasTraits')

        self.assertTrue(isinstance(klass, Klass))
        self.assertEqual('traits.has_traits', klass.parent.dotted_name)
        self.assertEqual('traits.has_traits', klass.module.dotted_name)
        
        return
    def test_resolve_name_defined_in_node(self):

        namespace = PythonNamespace()
        module    = namespace.lookup('traits.has_traits')
        klass     = module.resolve('HasTraits')

        self.assertTrue(isinstance(klass, Klass))
        self.assertEqual('traits.has_traits.HasTraits', klass.dotted_name)
        self.assertEqual('traits.has_traits', klass.module.dotted_name)
        self.assertEqual('traits.has_traits', klass.parent.dotted_name)
        
        return
    def test_bogus_lookups_in_cartoons_directory(self):

        namespace = PythonNamespace(path=[self.CARTOONS_DIR])

        self.assertEqual(None, namespace.lookup(''))
        self.assertEqual(None, namespace.lookup('.'))
        self.assertEqual(None, namespace.lookup('bogus.'))
        self.assertEqual(None, namespace.lookup('flinstones.'))
        self.assertEqual(None, namespace.lookup('flinstones.bogus'))
        self.assertEqual(None, namespace.lookup('flinstones.fred.bogus'))
        self.assertEqual(None, namespace.lookup('flinstones.fred.Dino.bogus'))

        return
    def test_lookup_in_cartoons_directory(self):

        namespace = PythonNamespace(path=[self.CARTOONS_DIR])

        package = namespace.lookup('flinstones')
        self.assertEqual(Package, type(package))
        self.assertEqual(package.name, 'flinstones')

        module = namespace.lookup('flinstones.fred')
        self.assertEqual(Module, type(module))
        self.assertEqual(module.name, 'fred')
        
        function = namespace.lookup('flinstones.fred.frobnicate')
        self.assertEqual(Function, type(function))
        self.assertEqual(function.name, 'frobnicate')

        klass = namespace.lookup('flinstones.fred.Dino')
        self.assertEqual(Klass, type(klass))
        self.assertEqual(klass.name, 'Dino')

        function = namespace.lookup('flinstones.fred.Dino.eat')
        self.assertEqual(Function, type(function))
        self.assertEqual(function.name, 'eat')

        attribute = namespace.lookup('flinstones.fred.Dino.age')
        self.assertEqual(Attribute, type(attribute))
        self.assertEqual(attribute.name, 'age')

        attribute = namespace.lookup('flinstones.fred.Dino.age')
        self.assertEqual(Attribute, type(attribute))
        self.assertEqual(attribute.name, 'age')

        klass = namespace.lookup('flinstones.fred.Dino.Nested.AndAgain')
        self.assertEqual(Klass, type(klass))
        self.assertEqual(klass.name, 'AndAgain')
        
        return