feat: add CLI with build and run commands
This commit is contained in:
42
tests/test_cli.py
Normal file
42
tests/test_cli.py
Normal file
@@ -0,0 +1,42 @@
|
||||
"""Test CLI functionality."""
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def test_build_command():
|
||||
"""Test build command."""
|
||||
result = subprocess.run(
|
||||
[sys.executable, "-m", "tampy", "build", "test.tampy"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
env={**dict(os.environ), "PYTHONPATH": "src"}
|
||||
)
|
||||
assert result.returncode == 0
|
||||
assert "Generated:" in result.stdout
|
||||
|
||||
|
||||
def test_run_command():
|
||||
"""Test run command."""
|
||||
result = subprocess.run(
|
||||
[sys.executable, "-m", "tampy", "run", "test.tampy"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
env={**dict(os.environ), "PYTHONPATH": "src"}
|
||||
)
|
||||
assert result.returncode == 0
|
||||
assert "y is greater" in result.stdout
|
||||
|
||||
|
||||
def test_file_extension_replacement():
|
||||
"""Test that .tampy is replaced with .py."""
|
||||
result = subprocess.run(
|
||||
[sys.executable, "-m", "tampy", "build", "test.tampy"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
env={**dict(os.environ), "PYTHONPATH": "src"}
|
||||
)
|
||||
assert result.returncode == 0
|
||||
assert Path("test.py").exists()
|
||||
@@ -1,45 +0,0 @@
|
||||
"""Test parser functionality."""
|
||||
|
||||
import ast
|
||||
import pytest
|
||||
from src.tampy.transpiler import TamilParser
|
||||
|
||||
|
||||
def test_parse_simple_python():
|
||||
"""Test parsing simple Python code."""
|
||||
code = "print('hello')"
|
||||
keywords = {"print": "இருப்பு"}
|
||||
|
||||
parser = TamilParser(keywords)
|
||||
tree = parser.parse(code)
|
||||
|
||||
assert isinstance(tree, ast.Module)
|
||||
|
||||
|
||||
def test_parse_import_statement():
|
||||
"""Test parsing import statement."""
|
||||
code = "import sys"
|
||||
keywords = {"import": "மேற்கோள்கள்"}
|
||||
|
||||
parser = TamilParser(keywords)
|
||||
tree = parser.parse(code)
|
||||
|
||||
assert isinstance(tree, ast.Module)
|
||||
|
||||
|
||||
def test_parse_function_definition():
|
||||
"""Test parsing function definition."""
|
||||
code = "def foo(): pass"
|
||||
keywords = {"def": "வரையறை"}
|
||||
|
||||
parser = TamilParser(keywords)
|
||||
tree = parser.parse(code)
|
||||
|
||||
assert isinstance(tree, ast.Module)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_parse_simple_python()
|
||||
test_parse_import_statement()
|
||||
test_parse_function_definition()
|
||||
print("All tests passed")
|
||||
79
tests/test_transpiler.py
Normal file
79
tests/test_transpiler.py
Normal file
@@ -0,0 +1,79 @@
|
||||
"""Test simple transpiler."""
|
||||
|
||||
import ast
|
||||
import pytest
|
||||
from src.tampy.transpiler import SimpleTranspiler
|
||||
|
||||
|
||||
def test_simple_transpile():
|
||||
"""Test basic transpilation."""
|
||||
transpiler = SimpleTranspiler()
|
||||
code = "x = 1"
|
||||
result = transpiler.transpile(code)
|
||||
assert "x = 1" in result
|
||||
|
||||
|
||||
def test_function_transpile():
|
||||
"""Test function transpilation."""
|
||||
transpiler = SimpleTranspiler()
|
||||
code = "def foo(): return 1"
|
||||
result = transpiler.transpile(code)
|
||||
assert "def foo()" in result
|
||||
|
||||
|
||||
def test_if_transpile():
|
||||
"""Test if statement transpilation."""
|
||||
transpiler = SimpleTranspiler()
|
||||
code = "if True: pass"
|
||||
result = transpiler.transpile(code)
|
||||
assert "if True:" in result
|
||||
|
||||
|
||||
def test_for_transpile():
|
||||
"""Test for loop transpilation."""
|
||||
transpiler = SimpleTranspiler()
|
||||
code = "for i in range(10): pass"
|
||||
result = transpiler.transpile(code)
|
||||
assert "for i in range(10):" in result
|
||||
|
||||
|
||||
def test_class_transpile():
|
||||
"""Test class transpilation."""
|
||||
transpiler = SimpleTranspiler()
|
||||
code = "class Foo: pass"
|
||||
result = transpiler.transpile(code)
|
||||
assert "class Foo:" in result
|
||||
|
||||
|
||||
def test_import_transpile():
|
||||
"""Test import transpilation."""
|
||||
transpiler = SimpleTranspiler()
|
||||
code = "import sys"
|
||||
result = transpiler.transpile(code)
|
||||
assert "import sys" in result
|
||||
|
||||
|
||||
def test_roundtrip():
|
||||
"""Test parsing and regenerating code."""
|
||||
transpiler = SimpleTranspiler()
|
||||
|
||||
code = """
|
||||
x = 1
|
||||
y = 2
|
||||
if x > y:
|
||||
print(x)
|
||||
"""
|
||||
result = transpiler.transpile(code)
|
||||
assert "x = 1" in result
|
||||
assert "if" in result
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_simple_transpile()
|
||||
test_function_transpile()
|
||||
test_if_transpile()
|
||||
test_for_transpile()
|
||||
test_class_transpile()
|
||||
test_import_transpile()
|
||||
test_roundtrip()
|
||||
print("All tests passed")
|
||||
Reference in New Issue
Block a user