Skip to content

Commit

Permalink
refactor: update tests to reflect new schema changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Daquiver1 committed Dec 6, 2023
1 parent 15c5afc commit 60331c0
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 29 deletions.
6 changes: 3 additions & 3 deletions projects/jdwp/codegen/dataclass_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __get_python_type_for(self, struct: Struct, field: Field) -> str:
tagged_union_type = typing.cast(TaggedUnion, type)
union_types = [
self.__struct_to_name[case_struct]
for case_struct in tagged_union_type.cases.values()
for case_struct in tagged_union_type.cases
]
union_types_str = ", ".join(union_types)
return f"typing.Union[{union_types_str}]"
Expand Down Expand Up @@ -71,7 +71,7 @@ def nested_structs(root: Struct) -> typing.Generator[StructLink, None, None]:
yield from nested_structs(array_type.element_type)
case TaggedUnion():
tagged_union_type = typing.cast(TaggedUnion, type)
for struct in tagged_union_type.cases.values():
for struct in tagged_union_type.cases:
yield root, field, struct
yield from nested_structs(struct)
case Struct():
Expand All @@ -90,7 +90,7 @@ def compute_struct_names(root: Struct, name: str) -> typing.Mapping[Struct, str]
names[nested] = f"{names[parent]}{field.name.capitalize()}Element"
case TaggedUnion():
tagged_union_type = typing.cast(TaggedUnion, type)
for case_value, case_struct in tagged_union_type.cases.items():
for case_value, case_struct in tagged_union_type.cases:
case_name = format_enum_name(case_value)
names[
case_struct
Expand Down
85 changes: 59 additions & 26 deletions projects/jdwp/tests/test_dataclass_generator.py
Original file line number Diff line number Diff line change
@@ -1,68 +1,101 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.

import unittest
from projects.jdwp.defs.schema import Struct, Field, Array
from projects.jdwp.defs.schema import ArrayLength, IntegralType, Struct, Field, Array
from projects.jdwp.codegen.dataclass_generator import StructGenerator


class TestStructGenerator(unittest.TestCase):
def test_simple_struct(self):
simple_struct = Struct(fields=[Field(name="id", type="int", description="")])
simple_struct = Struct(
fields=[
Field(name="id", type=IntegralType.INT, description="An integer ID")
]
)

generator = StructGenerator(simple_struct, "SimpleStruct")

result = generator.generate()

expected = [
"@dataclasses.dataclass(frozen=True)\n"
"class SimpleStruct:\n"
" id: int"
]
self.assertEqual(generator.generate(), expected)

self.assertEqual(result, expected)

def test_nested_struct(self):
nested_struct = Struct(
inner_struct = Struct(
fields=[
Field(
name="nested",
type=Struct(
fields=[Field(name="inner", type="int", description="")]
),
description="",
name="inner_field",
type=IntegralType.INT,
description="Inner integer field",
)
]
)
generator = StructGenerator(nested_struct, "NestedStruct")
outer_struct = Struct(
fields=[
Field(name="nested", type=inner_struct, description="Nested structure")
]
)

generator = StructGenerator(outer_struct, "OuterStruct")

result = generator.generate()

expected = [
"@dataclasses.dataclass(frozen=True)\n"
"class NestedStructNested:\n"
" inner: int",
"class OuterStructNested:\n"
" inner_field: int",
"@dataclasses.dataclass(frozen=True)\n"
"class NestedStruct:\n"
" nested: NestedStructNested",
"class OuterStruct:\n"
" nested: OuterStructNested",
]
self.assertEqual(generator.generate(), expected)

self.assertEqual(result, expected)

def test_struct_in_array(self):
# Define a structure
element_struct = Struct(
fields=[
Field(
name="element_field",
type=IntegralType.INT,
description="Element field",
)
]
)

array_length = ArrayLength(type=IntegralType.INT)

array_struct = Struct(
fields=[
Field(
name="arrayField",
name="array",
type=Array(
element_type=Struct(
fields=[
Field(name="elementField", type="int", description="")
]
element_type=element_struct,
length=Field(
name="length", type=array_length, description="Array length"
),
length=1,
),
description="",
description="Array of structures",
)
]
)

generator = StructGenerator(array_struct, "ArrayStruct")

result = generator.generate()

expected = [
"@dataclasses.dataclass(frozen=True)\n"
"class ArrayStructArrayFieldElement:\n"
" elementField: int",
"class ArrayStructArrayElement:\n"
" element_field: int",
"@dataclasses.dataclass(frozen=True)\n"
"class ArrayStruct:\n"
" arrayField: typing.List[ArrayStructArrayFieldElement]",
" array: typing.List[ArrayStructArrayElement]",
]
self.assertEqual(generator.generate(), expected)

self.assertEqual(result, expected)

0 comments on commit 60331c0

Please sign in to comment.