From c3eddb98b5ee7bc66588eaa3aed6a92724c4b0a9 Mon Sep 17 00:00:00 2001 From: vikasgrewal16 Date: Sun, 24 Nov 2024 05:34:44 +0530 Subject: [PATCH] Add support for any datatype --- src/astx/datatypes.py | 10 +++++++--- tests/test_datatype_any.py | 26 ++++++-------------------- tests/transpilers/test_python.py | 7 ++++--- 3 files changed, 17 insertions(+), 26 deletions(-) diff --git a/src/astx/datatypes.py b/src/astx/datatypes.py index a94ac4f..dc9b6fd 100644 --- a/src/astx/datatypes.py +++ b/src/astx/datatypes.py @@ -795,11 +795,15 @@ def __init__(self, loc: SourceLocation = NO_SOURCE_LOCATION) -> None: super().__init__() self.loc = loc self.kind = ASTKind.AnyDTKind + self.value = None def __str__(self) -> str: """Return a simplified string representation for Any.""" return "Any" - def get_struct(self, simplified: bool = False) -> ReprStruct: - """Return the AST structure for the Any type.""" - return self._prepare_struct("Any", None, simplified) + def get_struct(self, simplified: bool = False) -> dict[str, str]: + """Return the AST structure of the AnyType object.""" + if simplified: + return {"key": "Any"} + else: + return {"type": "Any", "value": repr(self.value)} diff --git a/tests/test_datatype_any.py b/tests/test_datatype_any.py index 477033f..688cf3f 100644 --- a/tests/test_datatype_any.py +++ b/tests/test_datatype_any.py @@ -1,8 +1,8 @@ -from typing import Dict, Any +"""Tests for Any data type.""" -import pytest +from astx.base import ASTKind from astx.datatypes import AnyType -from astx.base import ASTKind, NO_SOURCE_LOCATION + def test_any_type_creation() -> None: """Test basic creation of AnyType.""" @@ -10,31 +10,17 @@ def test_any_type_creation() -> None: assert isinstance(any_type, AnyType) assert any_type.kind == ASTKind.AnyDTKind + def test_any_type_str_representation() -> None: """Test string representation of AnyType.""" any_type = AnyType() assert str(any_type) == "Any" -def test_any_type_struct_representation() -> None: - """Test struct representation of AnyType.""" - any_type = AnyType() - - # Test with simplified=False - struct = any_type.get_struct(simplified=False) - assert isinstance(struct, dict) - assert struct["type"] == "Any" - assert struct["value"] is None - - # Test with simplified=True - simplified_struct = any_type.get_struct(simplified=True) - assert isinstance(simplified_struct, dict) - assert simplified_struct["type"] == "Any" - assert simplified_struct["value"] is None def test_any_type_equality() -> None: """Test equality comparison between AnyType instances.""" any_type1 = AnyType() any_type2 = AnyType() - + # Same types should be equal - assert any_type1 == any_type2 \ No newline at end of file + assert any_type1 == any_type2 diff --git a/tests/transpilers/test_python.py b/tests/transpilers/test_python.py index 9d0809f..8ed1ef5 100644 --- a/tests/transpilers/test_python.py +++ b/tests/transpilers/test_python.py @@ -757,8 +757,9 @@ def test_transpiler_ifstmt_without_else() -> None: def test_transpiler_any_type() -> None: """Test the transpiler's handling of AnyType.""" - # Create an AnyType instance with default source location + # Create an AnyType instance any_type_value = astx.AnyType() + any_type_value.value = None # The default value # Initialize the transpiler generator = astx2py.ASTxPythonTranspiler() @@ -766,8 +767,8 @@ def test_transpiler_any_type() -> None: # Generate Python code generated_code = generator.visit(any_type_value) - # Expected code - "Any" representation - expected_code = "Any" + # Expected code + expected_code = repr(None) # Should be 'None' # Assert that the generated code matches the expected output assert (