Add utils and tests
This commit is contained in:
93
tests/test_parser.py
Normal file
93
tests/test_parser.py
Normal file
@@ -0,0 +1,93 @@
|
||||
import pytest
|
||||
from pathlib import Path
|
||||
from codechunk.core.parser import CodeParser, LANGUAGE_EXTENSIONS
|
||||
from codechunk.core.chunking import ParsedChunk, ChunkMetadata
|
||||
|
||||
|
||||
class TestCodeParser:
|
||||
"""Tests for CodeParser."""
|
||||
|
||||
def test_detect_language_python(self, tmp_path):
|
||||
"""Test Python language detection."""
|
||||
parser = CodeParser()
|
||||
file_path = tmp_path / "test.py"
|
||||
assert parser.detect_language(file_path) == "python"
|
||||
|
||||
def test_detect_language_javascript(self, tmp_path):
|
||||
"""Test JavaScript language detection."""
|
||||
parser = CodeParser()
|
||||
file_path = tmp_path / "test.js"
|
||||
assert parser.detect_language(file_path) == "javascript"
|
||||
|
||||
def test_detect_language_go(self, tmp_path):
|
||||
"""Test Go language detection."""
|
||||
parser = CodeParser()
|
||||
file_path = tmp_path / "test.go"
|
||||
assert parser.detect_language(file_path) == "go"
|
||||
|
||||
def test_discover_files_python(self, python_project):
|
||||
"""Test file discovery in Python project."""
|
||||
parser = CodeParser()
|
||||
parser.discover_files(
|
||||
python_project,
|
||||
include_patterns=["*.py"],
|
||||
exclude_patterns=[]
|
||||
)
|
||||
|
||||
assert len(parser.files) >= 2
|
||||
file_names = [f.name for f in parser.files]
|
||||
assert "main.py" in file_names
|
||||
assert "utils.py" in file_names
|
||||
|
||||
def test_parse_python_file(self, sample_python_code, tmp_path):
|
||||
"""Test parsing Python code."""
|
||||
parser = CodeParser()
|
||||
file_path = tmp_path / "test.py"
|
||||
file_path.write_text(sample_python_code)
|
||||
|
||||
chunks = parser.parse_file(file_path)
|
||||
|
||||
assert len(chunks) >= 2
|
||||
|
||||
class_chunk = next((c for c in chunks if c.chunk_type == "class"), None)
|
||||
assert class_chunk is not None
|
||||
assert class_chunk.name == "DataProcessor"
|
||||
|
||||
def test_parse_javascript(self, sample_javascript_code, tmp_path):
|
||||
"""Test parsing JavaScript code."""
|
||||
parser = CodeParser()
|
||||
file_path = tmp_path / "test.js"
|
||||
file_path.write_text(sample_javascript_code)
|
||||
|
||||
chunks = parser.parse_file(file_path)
|
||||
|
||||
assert len(chunks) >= 1
|
||||
|
||||
def test_parse_go(self, sample_go_code, tmp_path):
|
||||
"""Test parsing Go code."""
|
||||
parser = CodeParser()
|
||||
file_path = tmp_path / "test.go"
|
||||
file_path.write_text(sample_go_code)
|
||||
|
||||
chunks = parser.parse_file(file_path)
|
||||
|
||||
assert len(chunks) >= 2
|
||||
|
||||
|
||||
class TestLanguageExtensions:
|
||||
"""Tests for language extension mappings."""
|
||||
|
||||
def test_all_extensions_mapped(self):
|
||||
"""Verify all expected extensions are mapped."""
|
||||
expected_extensions = {
|
||||
".py": "python",
|
||||
".js": "javascript",
|
||||
".ts": "typescript",
|
||||
".go": "go",
|
||||
".rs": "rust",
|
||||
".java": "java",
|
||||
".cpp": "cpp",
|
||||
}
|
||||
|
||||
for ext, lang in expected_extensions.items():
|
||||
assert LANGUAGE_EXTENSIONS[ext] == lang
|
||||
Reference in New Issue
Block a user