File size: 1,536 Bytes
1afb40b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import pytest
from pathlib import Path
from src.tools.ast_parser import RepositoryASTParser, StructureVisitor

def test_ast_parser_success(tmp_path):
    parser = RepositoryASTParser(repo_root=str(tmp_path))
    test_file = tmp_path / "sample.py"

    code_content = """
import os
from pathlib import Path

class SampleClass:
    def sample_method(self, x):
        return x + 1

def sample_function(y):
    return y * 2
"""
    test_file.write_text(code_content, encoding="utf-8")

    res = parser.parse_file("sample.py")
    assert res["status"] == "success"
    assert "os" in res["imports"]
    assert "pathlib.Path" in res["imports"]
    assert len(res["classes"]) == 1
    assert res["classes"][0]["name"] == "SampleClass"
    assert "sample_method" in res["classes"][0]["methods"]
    assert len(res["functions"]) == 2
    func_names = [f["name"] for f in res["functions"]]
    assert "sample_method" in func_names
    assert "sample_function" in func_names

def test_ast_parser_file_not_found(tmp_path):
    parser = RepositoryASTParser(repo_root=str(tmp_path))
    res = parser.parse_file("nonexistent.py")
    assert res["status"] == "error"
    assert "not found" in res["message"]

def test_ast_parser_syntax_error(tmp_path):
    parser = RepositoryASTParser(repo_root=str(tmp_path))
    test_file = tmp_path / "broken.py"
    test_file.write_text("def broken_syntax( :", encoding="utf-8")
    
    res = parser.parse_file("broken.py")
    assert res["status"] == "error"
    assert "Failed to parse AST" in res["message"]