123 lines
3.3 KiB
Python
123 lines
3.3 KiB
Python
from src.file_cache import ASTParser
|
|
|
|
def test_ast_parser_initialization() -> None:
|
|
"""Verify that ASTParser can be initialized with a language string."""
|
|
parser = ASTParser(language="python")
|
|
assert parser.language.name == "python"
|
|
|
|
def test_ast_parser_parse() -> None:
|
|
"""Verify that the parse method returns a tree_sitter.Tree."""
|
|
parser = ASTParser(language="python")
|
|
code = "def hello(): print('world')"
|
|
tree = parser.parse(code)
|
|
assert tree is not None
|
|
assert tree.root_node.type == "module"
|
|
|
|
def test_ast_parser_get_skeleton_python() -> None:
|
|
"""Verify that get_skeleton replaces function bodies with '...' while preserving docstrings."""
|
|
parser = ASTParser(language="python")
|
|
code = '''
|
|
def complex_function(a, b):
|
|
"""This is a docstring."""
|
|
x = a + b
|
|
return x
|
|
|
|
class MyClass:
|
|
def method(self):
|
|
"""Method docstring."""
|
|
pass
|
|
'''
|
|
skeleton = parser.get_skeleton(code)
|
|
assert 'def complex_function(a, b):' in skeleton
|
|
assert '"""This is a docstring."""' in skeleton
|
|
assert '...' in skeleton
|
|
assert 'x = a + b' not in skeleton
|
|
assert 'class MyClass:' in skeleton
|
|
assert 'def method(self):' in skeleton
|
|
assert '"""Method docstring."""' in skeleton
|
|
|
|
def test_ast_parser_invalid_language() -> None:
|
|
"""Verify handling of unsupported or invalid languages."""
|
|
# Currently ASTParser defaults to Python if language not supported or just fails tree-sitter init
|
|
# If it's intended to raise or handle gracefully, test it here.
|
|
pass
|
|
|
|
def test_ast_parser_get_curated_view() -> None:
|
|
"""Verify that get_curated_view preserves function bodies with @core_logic or # [HOT]."""
|
|
parser = ASTParser(language="python")
|
|
code = '''
|
|
def normal_func():
|
|
print("hide me")
|
|
|
|
@core_logic
|
|
def important_func():
|
|
print("keep me")
|
|
|
|
def hot_func():
|
|
# [HOT]
|
|
print("keep me too")
|
|
'''
|
|
curated = parser.get_curated_view(code)
|
|
assert 'print("hide me")' not in curated
|
|
assert 'print("keep me")' in curated
|
|
assert 'print("keep me too")' in curated
|
|
assert '@core_logic' in curated
|
|
assert '# [HOT]' in curated
|
|
|
|
def test_ast_parser_get_targeted_view() -> None:
|
|
"""Verify get_targeted_view includes targeted functions and dependencies."""
|
|
parser = ASTParser(language="python")
|
|
code = '''
|
|
import sys
|
|
|
|
def dep2():
|
|
"""Dep 2"""
|
|
print("dep2")
|
|
|
|
def dep1():
|
|
"""Dep 1"""
|
|
dep2()
|
|
|
|
def targeted():
|
|
"""Targeted"""
|
|
dep1()
|
|
|
|
def unrelated():
|
|
"""Unrelated"""
|
|
print("unrelated")
|
|
|
|
class MyClass:
|
|
def method1(self):
|
|
"""Method 1"""
|
|
targeted()
|
|
def method2(self):
|
|
"""Method 2"""
|
|
pass
|
|
'''
|
|
# Depth 0: targeted
|
|
# Depth 1: dep1 (called by targeted)
|
|
# Depth 2: dep2 (called by dep1)
|
|
view = parser.get_targeted_view(code, ["targeted"])
|
|
assert 'def targeted():' in view
|
|
assert '"""Targeted"""' in view
|
|
assert 'def dep1():' in view
|
|
assert '"""Dep 1"""' in view
|
|
assert 'def dep2():' in view
|
|
assert '"""Dep 2"""' in view
|
|
assert 'def unrelated():' not in view
|
|
assert 'class MyClass:' not in view
|
|
assert 'import sys' in view
|
|
|
|
# Test depth limit
|
|
# Depth 0: MyClass.method1
|
|
# Depth 1: targeted (called by method1)
|
|
# Depth 2: dep1 (called by targeted)
|
|
# Depth 3: dep2 (called by dep1) -> should be elided
|
|
view2 = parser.get_targeted_view(code, ["MyClass.method1"])
|
|
assert 'class MyClass:' in view2
|
|
assert 'def method1(self):' in view2
|
|
assert 'def targeted():' in view2
|
|
assert 'def dep1():' in view2
|
|
assert 'def dep2():' not in view2
|
|
assert 'def method2(self):' not in view2
|