Initial commit: CodeMap v0.1.0 - CLI tool for code analysis and diagram generation
This commit is contained in:
73
tests/test_python_parser.py
Normal file
73
tests/test_python_parser.py
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
import tempfile
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
from codemap.parsers import PythonParser
|
||||||
|
|
||||||
|
|
||||||
|
def test_parse_simple_python_file():
|
||||||
|
parser = PythonParser()
|
||||||
|
code = '''
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
def hello():
|
||||||
|
pass
|
||||||
|
'''
|
||||||
|
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
|
||||||
|
f.write(code)
|
||||||
|
temp_path = f.name
|
||||||
|
|
||||||
|
try:
|
||||||
|
result = parser.parse(Path(temp_path))
|
||||||
|
assert result.module_name == Path(temp_path).stem
|
||||||
|
assert result.file_type == "python"
|
||||||
|
assert len(result.dependencies) >= 2
|
||||||
|
dep_names = [d.module_name for d in result.dependencies]
|
||||||
|
assert "os" in dep_names
|
||||||
|
assert "sys" in dep_names
|
||||||
|
assert "pathlib" in dep_names or "Path" in dep_names
|
||||||
|
finally:
|
||||||
|
os.unlink(temp_path)
|
||||||
|
|
||||||
|
|
||||||
|
def test_parse_python_with_from_import():
|
||||||
|
parser = PythonParser()
|
||||||
|
code = '''
|
||||||
|
from typing import List, Dict
|
||||||
|
from collections import Counter
|
||||||
|
'''
|
||||||
|
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
|
||||||
|
f.write(code)
|
||||||
|
temp_path = f.name
|
||||||
|
|
||||||
|
try:
|
||||||
|
result = parser.parse(Path(temp_path))
|
||||||
|
assert len(result.dependencies) >= 2
|
||||||
|
finally:
|
||||||
|
os.unlink(temp_path)
|
||||||
|
|
||||||
|
|
||||||
|
def test_parse_python_with_syntax_error():
|
||||||
|
parser = PythonParser()
|
||||||
|
code = '''
|
||||||
|
def hello(:
|
||||||
|
pass
|
||||||
|
'''
|
||||||
|
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
|
||||||
|
f.write(code)
|
||||||
|
temp_path = f.name
|
||||||
|
|
||||||
|
try:
|
||||||
|
result = parser.parse(Path(temp_path))
|
||||||
|
assert result.file_type == "python"
|
||||||
|
finally:
|
||||||
|
os.unlink(temp_path)
|
||||||
|
|
||||||
|
|
||||||
|
def test_can_parse():
|
||||||
|
parser = PythonParser()
|
||||||
|
assert parser.can_parse(Path("test.py")) == True
|
||||||
|
assert parser.can_parse(Path("test.pyi")) == True
|
||||||
|
assert parser.can_parse(Path("test.js")) == False
|
||||||
|
assert parser.can_parse(Path("test.go")) == False
|
||||||
Reference in New Issue
Block a user