From d2202dc9ee6a5971e704cc1f4bc7e2c44aec9a5d Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Sun, 1 Feb 2026 23:46:54 +0000 Subject: [PATCH] Add utils and tests --- tests/test_parser.py | 93 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 tests/test_parser.py diff --git a/tests/test_parser.py b/tests/test_parser.py new file mode 100644 index 0000000..7a2847c --- /dev/null +++ b/tests/test_parser.py @@ -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