Пример #1
0
 def _make_tree(self) -> doc_generator_visitor.ApiTree:
     api_tree = doc_generator_visitor.ApiTree()
     api_tree.insert(path=('module', ),
                     py_object=types.ModuleType('module'),
                     aliases=[])
     api_tree.insert(path=('module', 'func1'),
                     py_object=lambda x: x,
                     aliases=[])
     api_tree.insert(path=('module', 'Class'),
                     py_object=types.new_class('Class'),
                     aliases=[])
     api_tree.insert(path=('module', 'Class', 'method'),
                     py_object=lambda x: x,
                     aliases=[])
     api_tree.insert(path=('module', 'Class', 'NestedClass'),
                     py_object=types.new_class('NestedClass'),
                     aliases=[])
     api_tree.insert(path=('module', 'Class', 'NestedClass', 'method2'),
                     py_object=lambda x: x,
                     aliases=[])
     api_tree.insert(path=('module', 'Class', 'constant'),
                     py_object='Just a string.',
                     aliases=[])
     api_tree.insert(path=('module', 'submodule'),
                     py_object=types.ModuleType('submodule'),
                     aliases=[])
     api_tree.insert(path=('module', 'submodule', 'func2'),
                     py_object=lambda x: x,
                     aliases=[])
     api_tree.insert(path=('module', 'submodule', 'constant'),
                     py_object='Another string.',
                     aliases=[])
     return api_tree
Пример #2
0
    def test_from_path_tree(self):
        tf = self._make_fake_module()

        path_tree = doc_generator_visitor.PathTree()
        path_tree[('tf', )] = tf
        path_tree[('tf', 'Parent')] = tf.Parent
        path_tree[('tf', 'Parent2')] = tf.Parent

        result = doc_generator_visitor.ApiTree.from_path_tree(
            path_tree, score_name_fn=lambda name: name)

        expected = doc_generator_visitor.ApiTree()
        expected.insert(path=('tf', ), py_object=tf, aliases=[('tf', )])
        expected.insert(path=('tf', 'Parent'),
                        py_object=tf.Parent,
                        aliases=[('tf', 'Parent'), ('tf', 'Parent2')])

        result = sorted(result.iter_nodes(), key=lambda node: node.path)
        expected = sorted(expected.iter_nodes(), key=lambda node: node.path)

        # Circular references make it hard to compare trees or nodes.
        for e, r in zip(result, expected):
            self.assertEqual(e.path, r.path)
            self.assertIs(e.py_object, r.py_object)
            self.assertCountEqual(e.aliases, r.aliases)
            self.assertCountEqual(e.children.keys(), r.children.keys())
Пример #3
0
    def test_api_tree(self):
        tf = self._make_fake_module()

        api_tree = doc_generator_visitor.ApiTree()
        api_tree.insert(path=('tf', ), py_object=tf, aliases=[('tf', )])
        api_tree.insert(path=('tf', 'Parent'),
                        py_object=tf.Parent,
                        aliases=[('tf', 'Parent'), ('tf', 'Parent2')])
        api_tree.insert(path=('tf', 'seven'),
                        py_object=tf.seven,
                        aliases=[('tf', 'seven')])

        # A node can be looked up by any alias
        self.assertIs(api_tree[('tf', 'Parent')], api_tree[('tf', 'Parent2')])
        # Nodes only show up once when iterating
        self.assertEqual([
            api_tree[('tf', )], api_tree[('tf', 'Parent')],
            api_tree[('tf', 'seven')]
        ], list(api_tree.iter_nodes()))
        # Test lookup by object.
        self.assertIs(api_tree[('tf', 'Parent')],
                      api_tree.node_for_object(tf.Parent))
        # You can't lookup things that maybe singletons.
        self.assertIs(api_tree[('tf', 'seven')].py_object, tf.seven)
        self.assertIsNone(api_tree.node_for_object(tf.seven))
Пример #4
0
    def test_physical_path(self):
        tf = self._make_fake_module()

        api_tree = doc_generator_visitor.ApiTree()
        api_tree.insert(path=('tf', ), py_object=tf, aliases=[('tf', )])
        api_tree.insert(path=('tf', 'sub2'),
                        py_object=tf.sub2,
                        aliases=[('tf', 'sub2')])
        api_tree.insert(path=('tf', 'seven'),
                        py_object=tf.seven,
                        aliases=[('tf', 'seven')])
        api_tree.insert(path=('tf', 'fun1'),
                        py_object=tf.fun1,
                        aliases=[('tf', 'fun1')])
        api_tree.insert(path=('tf', 'sub2', 'Child'),
                        py_object=tf.sub2.Child,
                        aliases=[('tf', 'sub2', 'Child')])

        self.assertEqual(('sub2', ), api_tree[('tf', 'sub2')].physical_path)
        self.assertIsNone(api_tree[('tf', 'seven')].physical_path)
        self.assertEqual(('__main__', 'ApiTreeTest', '_make_fake_module',
                          '<locals>', '<lambda>'),
                         api_tree[('tf', 'fun1')].physical_path)
        self.assertEqual(('__main__', 'ApiTreeTest', '_make_fake_module',
                          '<locals>', 'Child'),
                         api_tree[('tf', 'sub2', 'Child')].physical_path)
    def test_contains(self):
        tf = argparse.Namespace()
        tf.sub = argparse.Namespace()

        tree = doc_generator_visitor.ApiTree()
        tree[('tf', )] = tf
        tree[('tf', 'sub')] = tf.sub

        self.assertIn(('tf', ), tree)
        self.assertIn(('tf', 'sub'), tree)
    def test_duplicate_singleton(self):
        tf = argparse.Namespace()
        tf.sub = argparse.Namespace()
        tf.sub.thing = 999
        tf.sub2 = argparse.Namespace()
        tf.sub2.thing = tf.sub.thing

        tree = doc_generator_visitor.ApiTree()
        tree[('tf', )] = tf
        tree[('tf', 'sub')] = tf.sub
        tree[('tf', 'sub', 'thing')] = tf.sub.thing
        tree[('tf', 'sub2')] = tf.sub2
        tree[('tf', 'sub2', 'thing')] = tf.sub2.thing

        self.assertEmpty(tree.aliases[tf.sub.thing], [])
    def test_node_insertion(self):
        tf = argparse.Namespace()
        tf.sub = argparse.Namespace()
        tf.sub.object = object()

        tree = doc_generator_visitor.ApiTree()
        tree[('tf', )] = tf
        tree[('tf', 'sub')] = tf.sub
        tree[('tf', 'sub', 'thing')] = tf.sub.object

        node = tree[('tf', 'sub')]
        self.assertEqual(node.full_name, 'tf.sub')
        self.assertIs(node.obj, tf.sub)
        self.assertIs(node.parent, tree[('tf', )])
        self.assertLen(node.children, 1)
        self.assertIs(node.children['thing'], tree[('tf', 'sub', 'thing')])
    def test_duplicate(self):
        tf = argparse.Namespace()
        tf.sub = argparse.Namespace()
        tf.sub.thing = object()
        tf.sub2 = argparse.Namespace()
        tf.sub2.thing = tf.sub.thing

        tree = doc_generator_visitor.ApiTree()
        tree[('tf', )] = tf
        tree[('tf', 'sub')] = tf.sub
        tree[('tf', 'sub', 'thing')] = tf.sub.thing
        tree[('tf', 'sub2')] = tf.sub2
        tree[('tf', 'sub2', 'thing')] = tf.sub2.thing

        self.assertCountEqual(
            tree.aliases[id(tf.sub.thing)],
            [tree[('tf', 'sub', 'thing')], tree[('tf', 'sub2', 'thing')]])