diff --git a/projects/jdwp/codegen/new_type_generator.py b/projects/jdwp/codegen/new_type_generator.py new file mode 100644 index 0000000..8f74a0e --- /dev/null +++ b/projects/jdwp/codegen/new_type_generator.py @@ -0,0 +1,26 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. + +from projects.jdwp.defs.schema import PrimitiveType +import typing + + +def get_python_type(jdwp_type: PrimitiveType) -> str: + """Map JDWP type to Python type.""" + mapping = { + PrimitiveType.STRING: "str", + PrimitiveType.BOOLEAN: "bool", + } + return mapping.get(jdwp_type, "int") + + +def get_type_alias_definition(jdwp_type: PrimitiveType) -> str: + """Return the type alias definition for a given JDWP type.""" + python_type = get_python_type(jdwp_type) + new_type_name = f"{jdwp_type.name.capitalize()}Type" + return f"{new_type_name} = typing.NewType('{new_type_name}', {python_type})" + + +def generate_new_types(): + for jdwp_type in PrimitiveType: + type_alias_definition = get_type_alias_definition(jdwp_type) + print(type_alias_definition) diff --git a/projects/jdwp/tests/test_primitve_type.py b/projects/jdwp/tests/test_primitve_type.py new file mode 100644 index 0000000..a44b2f9 --- /dev/null +++ b/projects/jdwp/tests/test_primitve_type.py @@ -0,0 +1,19 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. + +import unittest +from projects.jdwp.codegen.new_type_generator import get_python_type +from projects.jdwp.defs.schema import PrimitiveType + + +class TestEnumMemberMapping(unittest.TestCase): + def test_enum_member_mappings(self): + for jdwp_type in PrimitiveType: + with self.subTest(jdwp_type=jdwp_type): + result = get_python_type(jdwp_type) + self.assertIsInstance( + result, str, f"Mapping for {jdwp_type} is missing or not a string" + ) + + +if __name__ == "__main__": + unittest.main() diff --git a/projects/jdwp/tests/test_type_alias_definition.py b/projects/jdwp/tests/test_type_alias_definition.py new file mode 100644 index 0000000..7087ac3 --- /dev/null +++ b/projects/jdwp/tests/test_type_alias_definition.py @@ -0,0 +1,28 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. + +import unittest +from projects.jdwp.defs.schema import PrimitiveType +from projects.jdwp.codegen.new_type_generator import get_type_alias_definition + + +class TestTypeAliasDefinition(unittest.TestCase): + def test_specific_type_alias_definitions(self): + expected_string_type_definition = ( + "StringType = typing.NewType('StringType', str)" + ) + self.assertEqual( + get_type_alias_definition(PrimitiveType.STRING), + expected_string_type_definition, + ) + + expected_boolean_type_definition = ( + "BooleanType = typing.NewType('BooleanType', bool)" + ) + self.assertEqual( + get_type_alias_definition(PrimitiveType.BOOLEAN), + expected_boolean_type_definition, + ) + + +if __name__ == "__main__": + unittest.main()