-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: update tests to reflect new schema changes
- Loading branch information
Showing
2 changed files
with
62 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |