# Path: pycc/asttools/parse.py
# def parse(source, filename='<unknown>', mode='exec'):
#     """An ast.parse extension.
# 
#     This function behaves identically to the standard ast.parse except that it
#     adds parent and sibling references to each node.
#     """
#     node = ast.parse(source, filename, mode)
#     references.add_parent_references(node)
#     references.add_sibling_references(node)
# 
#     return node
# 
# Path: pycc/asttools/name.py
# NAME_SOURCE = enum.Enum(('DEFINED', 'ADOPTED', 'IMPORTED', 'BUILTIN'))
# def declaration(node, start=None):
# def name_source(node, declared=None):
#     def visit_Name(self, node):
#     def visit_alias(self, node):
#     def visit_FunctionDef(self, node):
#     def visit_Global(self, node):
#     def visit_arguments(self, node):
#     def visit_arg(self, node):
# class NameVisitorMixin(object):
# 
# Path: pycc/asttools/visitor.py
# class NodeVisitor(object):
# class NodeVisitorIter(NodeVisitor):
# class NodeTransformer(NodeVisitor):
#     def __init__(self, node):
#     def generic_visit(self, node):
#     def visit(self):
#     def visit(self):
#     def __init__(self, *args, **kwargs):
#     def modified(self):
#     def _replace(self, new, old):
#     def visit(self):


import pytest
from pycc.asttools import parse
from pycc.asttools import name
from pycc.asttools import visitor
@pytest.fixture
def cls_use(node):
    """Get the usage of a class."""
    return node.body[8].value


@pytest.fixture
def builtin_use(node):
    """Get the usage of a built-in name."""
    return node.body[9].value


@pytest.fixture
def name_iter(node):
    """Get iterable of all names in the source."""
    class TestVisitor(visitor.NodeVisitorIter, name.NameVisitorMixin):
        pass

    return TestVisitor(node).visit()


def test_declaration(decl_node, return_node, assign_node):
    """Test that the declaration of a name can be found."""
    assert name.declaration(return_node) is decl_node
    assert name.declaration(assign_node) is decl_node


def test_declaration_reassign(decl_node, reassign_decl, reassign_use):
    """Test that reassignments don't throw off declarations."""
    assert name.declaration(reassign_use) is not decl_node

# Path: pycc/asttools/parse.py
# def parse(source, filename='<unknown>', mode='exec'):
#     """An ast.parse extension.
# 
#     This function behaves identically to the standard ast.parse except that it
#     adds parent and sibling references to each node.
#     """
#     node = ast.parse(source, filename, mode)
#     references.add_parent_references(node)
#     references.add_sibling_references(node)
# 
#     return node
# 
# Path: pycc/asttools/name.py
# NAME_SOURCE = enum.Enum(('DEFINED', 'ADOPTED', 'IMPORTED', 'BUILTIN'))
# def declaration(node, start=None):
# def name_source(node, declared=None):
#     def visit_Name(self, node):
#     def visit_alias(self, node):
#     def visit_FunctionDef(self, node):
#     def visit_Global(self, node):
#     def visit_arguments(self, node):
#     def visit_arg(self, node):
# class NameVisitorMixin(object):
# 
# Path: pycc/asttools/visitor.py
# class NodeVisitor(object):
# class NodeVisitorIter(NodeVisitor):
# class NodeTransformer(NodeVisitor):
#     def __init__(self, node):
#     def generic_visit(self, node):
#     def visit(self):
#     def visit(self):
#     def __init__(self, *args, **kwargs):
#     def modified(self):
#     def _replace(self, new, old):
#     def visit(self):


# Path: tests/asttools/test_name.py
import pytest
from pycc.asttools import parse
from pycc.asttools import name
from pycc.asttools import visitor

@pytest.fixture
def cls_use(node):
    """Get the usage of a class."""
    return node.body[8].value


@pytest.fixture
def builtin_use(node):
    """Get the usage of a built-in name."""
    return node.body[9].value


@pytest.fixture
def name_iter(node):
    """Get iterable of all names in the source."""
    class TestVisitor(visitor.NodeVisitorIter, name.NameVisitorMixin):
        pass

    return TestVisitor(node).visit()


def test_declaration(decl_node, return_node, assign_node):
    """Test that the declaration of a name can be found."""
    assert name.declaration(return_node) is decl_node
    assert name.declaration(assign_node) is decl_node


def test_declaration_reassign(decl_node, reassign_decl, reassign_use):
    """Test that reassignments don't throw off declarations."""
    assert name.declaration(reassign_use) is not decl_node

    assert name.declaration(reassign_use) is reassign_decl