"""Tests for syntax checking module.""" import pytest from codexchange.models import Language from codexchange.utils.syntax_check import ( check_python_syntax, check_typescript_syntax, check_javascript_syntax, check_java_syntax, verify_syntax, ) class TestPythonSyntax: """Tests for Python syntax checking.""" def test_valid_python(self): """Test valid Python code.""" code = """ def hello(): print("Hello") class Test: pass """ is_valid, errors = check_python_syntax(code) assert is_valid is True assert errors == [] def test_invalid_python_syntax(self): """Test invalid Python syntax.""" code = """ def hello( print("Hello") """ is_valid, errors = check_python_syntax(code) assert is_valid is False assert errors is not None assert len(errors) > 0 assert "line" in errors[0].lower() def test_mismatched_parens(self): """Test mismatched parentheses.""" code = "print((1 + 2" is_valid, errors = check_python_syntax(code) assert is_valid is False class TestTypeScriptSyntax: """Tests for TypeScript syntax checking.""" def test_valid_typescript(self): """Test valid TypeScript code.""" code = """ function hello(): void { console.log("Hello"); } interface User { name: string; age: number; } """ is_valid, warnings = check_typescript_syntax(code) assert is_valid is True or len(warnings) > 0 def test_invalid_typescript(self): """Test invalid TypeScript code.""" code = """ function hello(): void { console.log("Hello); } """ is_valid, warnings = check_typescript_syntax(code) assert not is_valid or len(warnings) > 0 or is_valid class TestJavaScriptSyntax: """Tests for JavaScript syntax checking.""" def test_valid_javascript(self): """Test valid JavaScript code.""" code = """ function hello() { console.log("Hello"); } const x = 1; """ is_valid, warnings = check_javascript_syntax(code) assert is_valid is True or len(warnings) > 0 def test_invalid_javascript(self): """Test invalid JavaScript code.""" code = """ function hello() { console.log("Hello); } """ is_valid, warnings = check_javascript_syntax(code) assert not is_valid or len(warnings) > 0 class TestJavaSyntax: """Tests for Java syntax checking.""" def test_valid_java(self): """Test valid Java code.""" code = """ public class HelloWorld { public static void main(String[] args) { System.out.println("Hello"); } } """ is_valid, warnings = check_java_syntax(code) assert is_valid is True assert len(warnings) == 0 def test_java_without_class(self): """Test Java without public class.""" code = """ void hello() { System.out.println("Hello"); } """ is_valid, warnings = check_java_syntax(code) assert not is_valid def test_java_mismatched_braces(self): """Test Java with mismatched braces.""" code = """ public class Test { public void hello() { System.out.println("Hello"); """ is_valid, warnings = check_java_syntax(code) assert not is_valid class TestVerifySyntax: """Tests for verify_syntax function.""" def test_verify_python(self): """Test verifying Python syntax.""" code = """ def hello(): return "Hello" """ is_valid, warnings = verify_syntax(code, Language.PYTHON) assert is_valid is True def test_verify_typescript(self): """Test verifying TypeScript syntax.""" code = """ function hello(): string { return "Hello"; } """ is_valid, warnings = verify_syntax(code, Language.TYPESCRIPT) assert is_valid is True or len(warnings) > 0 def test_verify_javascript(self): """Test verifying JavaScript syntax.""" code = """ function hello() { return "Hello"; } """ is_valid, warnings = verify_syntax(code, Language.JAVASCRIPT) assert is_valid is True or len(warnings) > 0 def test_verify_java(self): """Test verifying Java syntax.""" code = """ public class Test { public void hello() {} } """ is_valid, warnings = verify_syntax(code, Language.JAVA) assert is_valid is True def test_empty_code(self): """Test verifying empty code.""" is_valid, warnings = verify_syntax("", Language.PYTHON) assert is_valid is False assert "Empty" in warnings[0] def test_unsupported_language(self): """Test verifying with unsupported language.""" is_valid, warnings = verify_syntax("code", Language.PYTHON) assert is_valid is True