From aba8fddd8975f32dda3ce2411186a429c31a2ece Mon Sep 17 00:00:00 2001 From: Jamal Siddiqui Date: Mon, 22 Jun 2026 00:51:00 -0400 Subject: [PATCH 01/10] fix: wrap generated Java builder chain --- compiler/fory_compiler/generators/java.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/compiler/fory_compiler/generators/java.py b/compiler/fory_compiler/generators/java.py index 9b9b7906d8..7ee72f3a12 100644 --- a/compiler/fory_compiler/generators/java.py +++ b/compiler/fory_compiler/generators/java.py @@ -2180,9 +2180,12 @@ def generate_module_file( lines.append(" }") lines.append("") lines.append(" private static ThreadSafeFory createFory() {") - lines.append( - " return Fory.builder().withXlang(true).withCompatible(true).withRefTracking(true).withModule(INSTANCE).buildThreadSafeFory();" - ) + lines.append(" return Fory.builder()") + lines.append(" .withXlang(true)") + lines.append(" .withCompatible(true)") + lines.append(" .withRefTracking(true)") + lines.append(" .withModule(INSTANCE)") + lines.append(" .buildThreadSafeFory();") lines.append(" }") lines.append("") lines.append(" private static class Holder {") From c31a2e78c991d2532a05084f963d78aa209e62ac Mon Sep 17 00:00:00 2001 From: Jamal Siddiqui Date: Mon, 22 Jun 2026 01:08:49 -0400 Subject: [PATCH 02/10] fix: wrap generated Go fory options --- compiler/fory_compiler/generators/go.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/compiler/fory_compiler/generators/go.py b/compiler/fory_compiler/generators/go.py index 383dd8a6e3..7d24002267 100644 --- a/compiler/fory_compiler/generators/go.py +++ b/compiler/fory_compiler/generators/go.py @@ -1525,9 +1525,11 @@ def generate_registration(self) -> List[str]: def generate_fory_helpers(self) -> List[str]: lines: List[str] = [] lines.append("func createFory() *fory.Fory {") - lines.append( - "\tf := fory.New(fory.WithXlang(true), fory.WithRefTracking(true), fory.WithCompatible(true))" - ) + lines.append("\tf := fory.New(") + lines.append("\t\tfory.WithXlang(true),") + lines.append("\t\tfory.WithRefTracking(true),") + lines.append("\t\tfory.WithCompatible(true),") + lines.append("\t)") for alias, _, _ in self._collect_imported_type_infos(): lines.append(f"\tif err := {alias}.RegisterTypes(f); err != nil {{") lines.append("\t\tpanic(err)") From 29bd5feea8f5b851432d1a53583ec19e98fc3b34 Mon Sep 17 00:00:00 2001 From: Jamal Siddiqui Date: Mon, 22 Jun 2026 01:13:25 -0400 Subject: [PATCH 03/10] fix: wrap generated Rust helper signatures --- compiler/fory_compiler/generators/rust.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/compiler/fory_compiler/generators/rust.py b/compiler/fory_compiler/generators/rust.py index 54ba2f7588..18f1eaa017 100644 --- a/compiler/fory_compiler/generators/rust.py +++ b/compiler/fory_compiler/generators/rust.py @@ -615,16 +615,16 @@ def _format_imported_type_name( def generate_bytes_impl(self, type_name: str) -> List[str]: lines = [] lines.append(f"impl {type_name} {{") - lines.append( - " pub fn to_bytes(&self) -> ::std::result::Result<::std::vec::Vec, ::fory::Error> {" - ) + lines.append(" pub fn to_bytes(") + lines.append(" &self,") + lines.append(" ) -> ::std::result::Result<::std::vec::Vec, ::fory::Error> {") lines.append(" let fory = detail::get_fory();") lines.append(" fory.serialize(self)") lines.append(" }") lines.append("") - lines.append( - f" pub fn from_bytes(data: &[u8]) -> ::std::result::Result<{type_name}, ::fory::Error> {{" - ) + lines.append(" pub fn from_bytes(") + lines.append(" data: &[u8],") + lines.append(f" ) -> ::std::result::Result<{type_name}, ::fory::Error> {{") lines.append(" let fory = detail::get_fory();") lines.append(" fory.deserialize(data)") lines.append(" }") @@ -1622,9 +1622,9 @@ def generate_registration(self) -> List[str]: """Generate the Fory registration function.""" lines = [] - lines.append( - "pub fn register_types(fory: &mut ::fory::Fory) -> ::std::result::Result<(), ::fory::Error> {" - ) + lines.append("pub fn register_types(") + lines.append(" fory: &mut ::fory::Fory,") + lines.append(") -> ::std::result::Result<(), ::fory::Error> {") # Register enums (top-level) for enum in self.schema.enums: @@ -1655,9 +1655,8 @@ def generate_fory_helpers(self) -> List[str]: lines.append(" use super::*;") lines.append("") lines.append(" pub(super) fn get_fory() -> &'static ::fory::Fory {") - lines.append( - " static FORY: ::std::sync::OnceLock<::fory::Fory> = ::std::sync::OnceLock::new();" - ) + lines.append(" static FORY: ::std::sync::OnceLock<::fory::Fory> =") + lines.append(" ::std::sync::OnceLock::new();") lines.append(" FORY.get_or_init(|| {") lines.append(" let mut fory = ::fory::Fory::builder()") lines.append(" .xlang(true)") From 08048c177c87927b1bf0267c2c164ca3fbb46e78 Mon Sep 17 00:00:00 2001 From: Jamal Siddiqui Date: Mon, 29 Jun 2026 00:30:26 -0400 Subject: [PATCH 04/10] fix: wrap generated Python field lines --- compiler/fory_compiler/generators/python.py | 31 +++++++++++++++++---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/compiler/fory_compiler/generators/python.py b/compiler/fory_compiler/generators/python.py index 442c3c7ba1..f13cabff26 100644 --- a/compiler/fory_compiler/generators/python.py +++ b/compiler/fory_compiler/generators/python.py @@ -623,14 +623,26 @@ def generate_field( field_args.append(f"default_factory={default_factory}") else: field_args.append(f"default={default_expr}") - field_default = f"pyfory.field({', '.join(field_args)}){trailing_comment}" + + field_line = ( + f"{field_name}: {python_type} = " + f"pyfory.field({', '.join(field_args)}){trailing_comment}" + ) + + if len(f" {field_line}") > 80: + lines.append(f"{field_name}: {python_type} = pyfory.field(") + for arg in field_args: + lines.append(f" {arg},") + lines.append(f"){trailing_comment}") + else: + lines.append(field_line) else: if default_factory is not None: field_default = f"field(default_factory={default_factory})" else: field_default = f"{default_expr}{trailing_comment}" - lines.append(f"{field_name}: {python_type} = {field_default}") + lines.append(f"{field_name}: {python_type} = {field_default}") return lines @@ -683,7 +695,14 @@ def generate_repr_method(self, message: Message, indent: int = 0) -> List[str]: expr = f"({placeholder_literal} if self.{field_name} is not None else 'None')" else: expr = f"repr(self.{field_name})" - lines.append(f'{ind} parts.append("{field_name}=" + {expr})') + line = f'{ind} parts.append("{field_name}=" + {expr})' + if len(line) > 80: + lines.append(f"{ind} parts.append(") + lines.append(f'{ind} "{field_name}="') + lines.append(f"{ind} + {expr}") + lines.append(f"{ind} )") + else: + lines.append(line) lines.append(f'{ind} return "{message.name}(" + ", ".join(parts) + ")"') return lines @@ -1013,9 +1032,9 @@ def generate_fory_helpers(self) -> List[str]: lines.append(" if _threadsafe_fory is None:") lines.append(" with _fory_lock:") lines.append(" if _threadsafe_fory is None:") - lines.append( - " _threadsafe_fory = pyfory.ThreadSafeFory(fory_factory=_create_fory)" - ) + lines.append(" _threadsafe_fory = pyfory.ThreadSafeFory(") + lines.append(" fory_factory=_create_fory") + lines.append(" )") lines.append(" return _threadsafe_fory") return lines From fe403a0ebcbf6ebf2a45c25249a20071f2d02053 Mon Sep 17 00:00:00 2001 From: Jamal Siddiqui Date: Mon, 29 Jun 2026 00:37:56 -0400 Subject: [PATCH 05/10] fix: wrap generated Rust map fields --- compiler/fory_compiler/generators/rust.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/compiler/fory_compiler/generators/rust.py b/compiler/fory_compiler/generators/rust.py index 18f1eaa017..41fd32728e 100644 --- a/compiler/fory_compiler/generators/rust.py +++ b/compiler/fory_compiler/generators/rust.py @@ -1364,7 +1364,20 @@ def generate_field( ) field_name = self.get_field_identifier(parent_stack[-1], field) - lines.append(f"pub {field_name}: {rust_type},") + field_line = f"pub {field_name}: {rust_type}," + + if len(f" {field_line}") > 80 and rust_type.startswith( + "::std::collections::HashMap<" + ): + inner_type = rust_type.removeprefix("::std::collections::HashMap<").removesuffix(">") + key_type, value_type = inner_type.split(", ", 1) + + lines.append(f"pub {field_name}: ::std::collections::HashMap<") + lines.append(f" {key_type},") + lines.append(f" {value_type},") + lines.append(">,") + else: + lines.append(field_line) return lines From e1a3d218edbc839597eea22b658f3c9a826f085c Mon Sep 17 00:00:00 2001 From: Jamal Siddiqui Date: Mon, 29 Jun 2026 00:52:53 -0400 Subject: [PATCH 06/10] fix: wrap generated C++ lines --- compiler/fory_compiler/generators/cpp.py | 54 +++++++++++++++--------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/compiler/fory_compiler/generators/cpp.py b/compiler/fory_compiler/generators/cpp.py index 91e6b49fdd..aa69dd5001 100644 --- a/compiler/fory_compiler/generators/cpp.py +++ b/compiler/fory_compiler/generators/cpp.py @@ -222,8 +222,9 @@ def generate_bytes_methods(self, class_name: str, indent: str) -> List[str]: lines.append(f"{indent}}}") lines.append("") lines.append( - f"{indent}static fory::Result<{class_name}, fory::Error> from_bytes(const std::vector& data) {{" + f"{indent}static fory::Result<{class_name}, fory::Error> from_bytes(" ) + lines.append(f"{indent} const std::vector& data) {{") lines.append( f"{indent} return detail::get_fory().deserialize<{class_name}>(data);" ) @@ -834,13 +835,13 @@ def generate_field_accessors( f"{indent}void set_{field_name}(Arg&& arg, Args&&... args) {{" ) if field.optional: - lines.append( - f"{indent} {member_name}.emplace(std::forward(arg), std::forward(args)...);" - ) + lines.append(f"{indent} {member_name}.emplace(") + lines.append(f"{indent} std::forward(arg),") + lines.append(f"{indent} std::forward(args)...);") else: - lines.append( - f"{indent} {member_name} = {value_type}(std::forward(arg), std::forward(args)...);" - ) + lines.append(f"{indent} {member_name} = {value_type}(") + lines.append(f"{indent} std::forward(arg),") + lines.append(f"{indent} std::forward(args)...);") lines.append(f"{indent}}}") else: lines.append(f"{indent}void set_{field_name}({value_type} value) {{") @@ -921,7 +922,14 @@ def generate_message_definition( conditions = [ self.get_field_eq_expression(field, lineage) for field in message.fields ] - lines.append(f"{body_indent} return {' && '.join(conditions)};") + return_line = f"{body_indent} return {' && '.join(conditions)};" + if len(return_line) > 80: + lines.append(f"{body_indent} return") + for index, condition in enumerate(conditions): + suffix = " &&" if index + 1 < len(conditions) else ";" + lines.append(f"{body_indent} {condition}{suffix}") + else: + lines.append(return_line) else: lines.append(f"{body_indent} return true;") lines.append(f"{body_indent}}}") @@ -939,12 +947,19 @@ def generate_message_definition( lines.append(f"{field_indent}{field_type} {member_name};") lines.append("") lines.append(f"{body_indent}public:") - field_members = ", ".join( - self.get_field_macro_entry(f) for f in message.fields - ) - lines.append( - f"{body_indent}FORY_STRUCT({struct_type_name}, {field_members});" - ) + macro_entries = [self.get_field_macro_entry(f) for f in message.fields] + field_members = ", ".join(macro_entries) + macro_line = f"{body_indent}FORY_STRUCT({struct_type_name}, {field_members});" + + if len(macro_line) > 80: + lines.append(f"{body_indent}FORY_STRUCT(") + lines.append(f"{body_indent} {struct_type_name},") + for index, entry in enumerate(macro_entries): + suffix = "," if index + 1 < len(macro_entries) else "" + lines.append(f"{body_indent} {entry}{suffix}") + lines.append(f"{body_indent});") + else: + lines.append(macro_line) else: lines.append(f"{body_indent}FORY_STRUCT({struct_type_name});") @@ -2014,11 +2029,12 @@ def generate_registration(self) -> List[str]: lines.append("") lines.append("namespace detail {") lines.append("inline fory::serialization::ThreadSafeFory& get_fory() {") - lines.append( - " static fory::serialization::ThreadSafeFory fory = " - "fory::serialization::Fory::builder().xlang(true).track_ref(true)" - ".compatible(true).build_thread_safe();" - ) + lines.append(" static fory::serialization::ThreadSafeFory fory =") + lines.append(" fory::serialization::Fory::builder()") + lines.append(" .xlang(true)") + lines.append(" .track_ref(true)") + lines.append(" .compatible(true)") + lines.append(" .build_thread_safe();") lines.append(" static const bool initialized = []() {") for ns in self._collect_imported_namespaces(): lines.append(f" {ns}::register_types(fory);") From 106077ab4a7f041890a673db63835e96227b36d7 Mon Sep 17 00:00:00 2001 From: Jamal Siddiqui Date: Mon, 20 Jul 2026 23:53:54 -0400 Subject: [PATCH 07/10] test: update generated formatting expectations --- .../tests/test_generated_code.py | 35 +++++++++++++------ 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/compiler/fory_compiler/tests/test_generated_code.py b/compiler/fory_compiler/tests/test_generated_code.py index 51ba17632a..403aaa0145 100644 --- a/compiler/fory_compiler/tests/test_generated_code.py +++ b/compiler/fory_compiler/tests/test_generated_code.py @@ -231,8 +231,12 @@ def test_rust_nested_container_ref_uses_correct_pointer_type(): ) assert "::std::vec::Vec<::std::vec::Vec<::std::rc::Rc>>" not in rust_output assert ( - "pub nodes: ::std::collections::HashMap<::std::string::String, " - "::std::collections::HashMap<::std::string::String, ::std::sync::Arc>>," + " pub nodes: ::std::collections::HashMap<\n" + " ::std::string::String,\n" + " ::std::collections::HashMap<" + "::std::string::String, " + "::std::sync::Arc>,\n" + " >," in rust_output ) assert ( @@ -1095,12 +1099,15 @@ def test_cpp_nested_integer_specs_in_generic_containers(): ) cpp_output = render_files(generate_files(schema, CppGenerator)) assert ( - "FORY_STRUCT(NestedIntegerSpecs, " - "(values_, ::fory::F(1).map(::fory::T::uint32().fixed(), " - "::fory::T::list(::fory::T::inner(::fory::T::uint64().tagged())))));" - in cpp_output - ) - + " FORY_STRUCT(\n" + " NestedIntegerSpecs,\n" + " (values_, ::fory::F(1).map(" + "::fory::T::uint32().fixed(), " + "::fory::T::list(" + "::fory::T::inner(::fory::T::uint64().tagged()))))\n" + " );" + in cpp_output +) def test_cpp_escapes_keywords_and_generated_helpers(): schema = parse_fdl( @@ -1662,7 +1669,10 @@ def test_rust_generated_code_uses_absolute_paths(): assert "pub value: ::std::string::String," in rust_output assert "pub items: ::std::vec::Vec<::std::string::String>," in rust_output assert ( - "pub labels: ::std::collections::HashMap<::std::string::String, ::std::string::String>," + " pub labels: ::std::collections::HashMap<\n" + " ::std::string::String,\n" + " ::std::string::String,\n" + " >," in rust_output ) assert ( @@ -1670,7 +1680,12 @@ def test_rust_generated_code_uses_absolute_paths(): in rust_output ) assert "pub parent: ::fory::ArcWeak," in rust_output - assert "pub fn register_types(fory: &mut ::fory::Fory)" in rust_output + assert ( + "pub fn register_types(\n" + " fory: &mut ::fory::Fory,\n" + ") -> ::std::result::Result<(), ::fory::Error> {" + in rust_output + ) assert "static FORY: ::std::sync::OnceLock<::fory::Fory>" in rust_output From fd6b59c21fa62414859c4fd3d3ca2f3641c69f69 Mon Sep 17 00:00:00 2001 From: Jamal Siddiqui Date: Tue, 21 Jul 2026 00:10:35 -0400 Subject: [PATCH 08/10] style: format compiler changes --- compiler/fory_compiler/generators/cpp.py | 12 +- compiler/fory_compiler/generators/rust.py | 8 +- .../tests/test_generated_code.py | 432 ++++++------------ 3 files changed, 143 insertions(+), 309 deletions(-) diff --git a/compiler/fory_compiler/generators/cpp.py b/compiler/fory_compiler/generators/cpp.py index 7b430632dd..edaecd4fef 100644 --- a/compiler/fory_compiler/generators/cpp.py +++ b/compiler/fory_compiler/generators/cpp.py @@ -1710,7 +1710,9 @@ def generate_message_definition( self.get_field_macro_entry(message, f) for f in message.fields ] field_members = ", ".join(macro_entries) - macro_line = f"{body_indent}FORY_STRUCT({struct_type_name}, {field_members});" + macro_line = ( + f"{body_indent}FORY_STRUCT({struct_type_name}, {field_members});" + ) if len(macro_line) > 80: lines.append(f"{body_indent}FORY_STRUCT(") @@ -1764,9 +1766,11 @@ def generate_union_definition( self.get_union_case_type(field, parent_stack) for field in union.fields ] case_aliases = [ - f"ForyCase{self.to_pascal_case(field.name)}Type" - if "," in case_type - else None + ( + f"ForyCase{self.to_pascal_case(field.name)}Type" + if "," in case_type + else None + ) for field, case_type in zip(union.fields, raw_case_types) ] case_types = [ diff --git a/compiler/fory_compiler/generators/rust.py b/compiler/fory_compiler/generators/rust.py index 41fd32728e..230a8450b4 100644 --- a/compiler/fory_compiler/generators/rust.py +++ b/compiler/fory_compiler/generators/rust.py @@ -617,7 +617,9 @@ def generate_bytes_impl(self, type_name: str) -> List[str]: lines.append(f"impl {type_name} {{") lines.append(" pub fn to_bytes(") lines.append(" &self,") - lines.append(" ) -> ::std::result::Result<::std::vec::Vec, ::fory::Error> {") + lines.append( + " ) -> ::std::result::Result<::std::vec::Vec, ::fory::Error> {" + ) lines.append(" let fory = detail::get_fory();") lines.append(" fory.serialize(self)") lines.append(" }") @@ -1369,7 +1371,9 @@ def generate_field( if len(f" {field_line}") > 80 and rust_type.startswith( "::std::collections::HashMap<" ): - inner_type = rust_type.removeprefix("::std::collections::HashMap<").removesuffix(">") + inner_type = rust_type.removeprefix( + "::std::collections::HashMap<" + ).removesuffix(">") key_type, value_type = inner_type.split(", ", 1) lines.append(f"pub {field_name}: ::std::collections::HashMap<") diff --git a/compiler/fory_compiler/tests/test_generated_code.py b/compiler/fory_compiler/tests/test_generated_code.py index 403aaa0145..fa17472663 100644 --- a/compiler/fory_compiler/tests/test_generated_code.py +++ b/compiler/fory_compiler/tests/test_generated_code.py @@ -41,7 +41,6 @@ from fory_compiler.ir.ast import Schema from fory_compiler.ir.validator import SchemaValidator - GENERATOR_CLASSES: Tuple[Type[BaseGenerator], ...] = ( JavaGenerator, PythonGenerator, @@ -90,9 +89,9 @@ def assert_language_outputs_equal( baseline_label = label baseline_files = files continue - assert files == baseline_files, ( - f"{generator_cls.language_name} output mismatch for {label} vs {baseline_label}" - ) + assert ( + files == baseline_files + ), f"{generator_cls.language_name} output mismatch for {label} vs {baseline_label}" def assert_all_languages_equal(schemas: Dict[str, Schema]) -> None: @@ -101,8 +100,7 @@ def assert_all_languages_equal(schemas: Dict[str, Schema]) -> None: def test_generated_code_scalar_types_equivalent(): - fdl = dedent( - """ + fdl = dedent(""" package gen; message ScalarTypes { @@ -115,10 +113,8 @@ def test_generated_code_scalar_types_equivalent(): float64 f64 = 7; string name = 8; } - """ - ) - proto = dedent( - """ + """) + proto = dedent(""" syntax = "proto3"; package gen; @@ -132,10 +128,8 @@ def test_generated_code_scalar_types_equivalent(): double f64 = 7; string name = 8; } - """ - ) - fbs = dedent( - """ + """) + fbs = dedent(""" namespace gen; table ScalarTypes { @@ -148,8 +142,7 @@ def test_generated_code_scalar_types_equivalent(): f64:double; name:string; } - """ - ) + """) schemas = { "fdl": parse_fdl(fdl), "proto": parse_proto(proto), @@ -159,9 +152,7 @@ def test_generated_code_scalar_types_equivalent(): def test_rust_generated_code_uses_fory_temporal_carriers(): - schema = parse_fdl( - dedent( - """ + schema = parse_fdl(dedent(""" package gen; message TemporalTypes { @@ -169,9 +160,7 @@ def test_rust_generated_code_uses_fory_temporal_carriers(): timestamp instant = 2; duration elapsed = 3; } - """ - ) - ) + """)) rust_output = render_files(generate_files(schema, RustGenerator)) assert "pub day: ::fory::Date," in rust_output @@ -181,9 +170,7 @@ def test_rust_generated_code_uses_fory_temporal_carriers(): def test_rust_generated_code_can_use_chrono_temporal_types(): - schema = parse_fdl( - dedent( - """ + schema = parse_fdl(dedent(""" package gen; option rust_use_chrono_temporal_types = true; @@ -192,9 +179,7 @@ def test_rust_generated_code_can_use_chrono_temporal_types(): timestamp instant = 2; duration elapsed = 3; } - """ - ) - ) + """)) rust_output = render_files(generate_files(schema, RustGenerator)) assert "pub day: ::chrono::NaiveDate," in rust_output @@ -206,9 +191,7 @@ def test_rust_generated_code_can_use_chrono_temporal_types(): def test_rust_nested_container_ref_uses_correct_pointer_type(): - schema = parse_fdl( - dedent( - """ + schema = parse_fdl(dedent(""" package gen; message Node { @@ -219,9 +202,7 @@ def test_rust_nested_container_ref_uses_correct_pointer_type(): list> groups = 1; map> nodes = 2; } - """ - ) - ) + """)) rust_output = render_files(generate_files(schema, RustGenerator)) @@ -236,8 +217,7 @@ def test_rust_nested_container_ref_uses_correct_pointer_type(): " ::std::collections::HashMap<" "::std::string::String, " "::std::sync::Arc>,\n" - " >," - in rust_output + " >," in rust_output ) assert ( "::std::collections::HashMap<::std::string::String, " @@ -247,8 +227,7 @@ def test_rust_nested_container_ref_uses_correct_pointer_type(): def test_generated_code_integer_encoding_variants_equivalent(): - fdl = dedent( - """ + fdl = dedent(""" package gen; message EncodingTypes { @@ -259,10 +238,8 @@ def test_generated_code_integer_encoding_variants_equivalent(): tagged int64 ti64 = 5; tagged uint64 tu64 = 6; } - """ - ) - proto = dedent( - """ + """) + proto = dedent(""" syntax = "proto3"; package gen; @@ -274,8 +251,7 @@ def test_generated_code_integer_encoding_variants_equivalent(): int64 ti64 = 5 [(fory).type = "tagged int64"]; uint64 tu64 = 6 [(fory).type = "tagged uint64"]; } - """ - ) + """) schemas = { "fdl": parse_fdl(fdl), "proto": parse_proto(proto), @@ -288,8 +264,7 @@ def test_generated_code_integer_encoding_variants_equivalent(): def test_generated_code_list_modifier_aliases_equivalent(): - repeated = dedent( - """ + repeated = dedent(""" package gen; message Item { @@ -303,10 +278,8 @@ def test_generated_code_list_modifier_aliases_equivalent(): ref repeated Item items = 4; repeated ref Item children = 5; } - """ - ) - list_syntax = dedent( - """ + """) + list_syntax = dedent(""" package gen; message Item { @@ -320,8 +293,7 @@ def test_generated_code_list_modifier_aliases_equivalent(): ref list items = 4; list children = 5; } - """ - ) + """) schemas = { "repeated": parse_fdl(repeated), "list": parse_fdl(list_syntax), @@ -330,8 +302,7 @@ def test_generated_code_list_modifier_aliases_equivalent(): def test_generated_code_repeated_primitives_are_lists(): - fdl = dedent( - """ + fdl = dedent(""" package gen; message ArrayTypes { @@ -347,10 +318,8 @@ def test_generated_code_repeated_primitives_are_lists(): repeated float32 f32s = 10; repeated float64 f64s = 11; } - """ - ) - proto = dedent( - """ + """) + proto = dedent(""" syntax = "proto3"; package gen; @@ -367,8 +336,7 @@ def test_generated_code_repeated_primitives_are_lists(): repeated float f32s = 10; repeated double f64s = 11; } - """ - ) + """) schemas = { "fdl": parse_fdl(fdl), "proto": parse_proto(proto), @@ -377,8 +345,7 @@ def test_generated_code_repeated_primitives_are_lists(): def test_generated_code_flatbuffers_primitive_vectors_are_arrays(): - fbs = dedent( - """ + fbs = dedent(""" namespace gen; table ArrayTypes { @@ -394,8 +361,7 @@ def test_generated_code_flatbuffers_primitive_vectors_are_arrays(): f32s:[float]; f64s:[double]; } - """ - ) + """) schema = parse_fbs(fbs) java_output = render_files(generate_files(schema, JavaGenerator)) @@ -410,8 +376,7 @@ def test_generated_code_flatbuffers_primitive_vectors_are_arrays(): def test_generated_code_list_types_equivalent(): - fdl = dedent( - """ + fdl = dedent(""" package gen; message ListItem { @@ -423,10 +388,8 @@ def test_generated_code_list_types_equivalent(): repeated bool flags = 2; repeated ListItem items = 3; } - """ - ) - proto = dedent( - """ + """) + proto = dedent(""" syntax = "proto3"; package gen; @@ -439,8 +402,7 @@ def test_generated_code_list_types_equivalent(): repeated bool flags = 2; repeated ListItem items = 3; } - """ - ) + """) schemas = { "fdl": parse_fdl(fdl), "proto": parse_proto(proto), @@ -449,8 +411,7 @@ def test_generated_code_list_types_equivalent(): def test_generated_code_map_types_equivalent(): - fdl = dedent( - """ + fdl = dedent(""" package gen; message MapValue { @@ -463,10 +424,8 @@ def test_generated_code_map_types_equivalent(): map weak_entries = 3; optional int32 version = 4; } - """ - ) - proto = dedent( - """ + """) + proto = dedent(""" syntax = "proto3"; package gen; @@ -484,8 +443,7 @@ def test_generated_code_map_types_equivalent(): ]; optional sint32 version = 4; } - """ - ) + """) # FlatBuffers does not support maps, compare FDL vs proto only. schemas = { "fdl": parse_fdl(fdl), @@ -514,9 +472,7 @@ def test_generated_code_map_types_equivalent(): def test_rust_generated_ref_pointer_default_and_opt_out(): - schema = parse_fdl( - dedent( - """ + schema = parse_fdl(dedent(""" package gen; message Node { @@ -531,9 +487,7 @@ def test_rust_generated_ref_pointer_default_and_opt_out(): list default_ref_list = 5; list rc_ref_list = 6; } - """ - ) - ) + """)) rust_output = render_files(generate_files(schema, RustGenerator)) assert "pub default_ref: ::std::sync::Arc," in rust_output assert "pub rc_ref: ::std::rc::Rc," in rust_output @@ -546,8 +500,7 @@ def test_rust_generated_ref_pointer_default_and_opt_out(): def test_generated_code_nested_messages_equivalent(): - fdl = dedent( - """ + fdl = dedent(""" package gen; message Outer { @@ -559,10 +512,8 @@ def test_generated_code_nested_messages_equivalent(): Inner inner = 2; } - """ - ) - proto = dedent( - """ + """) + proto = dedent(""" syntax = "proto3"; package gen; @@ -575,8 +526,7 @@ def test_generated_code_nested_messages_equivalent(): Inner inner = 2; } - """ - ) + """) # FlatBuffers does not support nested message declarations. schemas = { "fdl": parse_fdl(fdl), @@ -586,8 +536,7 @@ def test_generated_code_nested_messages_equivalent(): def test_java_nested_name_registration_uses_owner_namespace(): - schema = parse_fdl( - """ + schema = parse_fdl(""" option enable_auto_type_id = false; package demo; @@ -611,8 +560,7 @@ def test_java_nested_name_registration_uses_owner_namespace(): Choice choice = 3; list payloads = 4; } - """ - ) + """) output = render_files(generate_files(schema, JavaGenerator)) assert "import org.apache.fory.annotation.Ref;" in output @@ -630,8 +578,7 @@ def test_java_nested_name_registration_uses_owner_namespace(): def test_generated_registration_uses_single_name_for_dart_python_swift(): - schema = parse_fdl( - """ + schema = parse_fdl(""" option enable_auto_type_id = false; package demo; @@ -654,8 +601,7 @@ def test_generated_registration_uses_single_name_for_dart_python_swift(): Kind kind = 2; Choice choice = 3; } - """ - ) + """) dart_output = render_files(generate_files(schema, DartGenerator)) assert ( @@ -702,23 +648,19 @@ def test_generated_registration_uses_single_name_for_dart_python_swift(): def test_java_default_package_import_registers_dependency(tmp_path): common = tmp_path / "common.fdl" - common.write_text( - """ + common.write_text(""" message Common [id=200] { string name = 1; } - """ - ) + """) main = tmp_path / "main.fdl" - main.write_text( - """ + main.write_text(""" import "common.fdl"; message Holder [id=201] { Common common = 1; } - """ - ) + """) schema = resolve_imports(main) validator = SchemaValidator(schema) @@ -734,24 +676,20 @@ def test_java_default_package_import_registers_dependency(tmp_path): def test_java_rejects_mixed_default_and_named_imports(tmp_path): default_dir = tmp_path / "default_in_named" default_dir.mkdir() - (default_dir / "common.fdl").write_text( - """ + (default_dir / "common.fdl").write_text(""" message Common [id=200] { string name = 1; } - """ - ) + """) default_main = default_dir / "main.fdl" - default_main.write_text( - """ + default_main.write_text(""" package app; import "common.fdl"; message Holder [id=201] { Common common = 1; } - """ - ) + """) with pytest.raises(ValueError, match="default-package"): JavaGenerator( @@ -760,25 +698,21 @@ def test_java_rejects_mixed_default_and_named_imports(tmp_path): named_dir = tmp_path / "named_in_default" named_dir.mkdir() - (named_dir / "common.fdl").write_text( - """ + (named_dir / "common.fdl").write_text(""" package shared; message Common [id=200] { string name = 1; } - """ - ) + """) named_main = named_dir / "main.fdl" - named_main.write_text( - """ + named_main.write_text(""" import "common.fdl"; message Holder [id=201] { optional Common common = 1; } - """ - ) + """) with pytest.raises(ValueError, match="default-package"): JavaGenerator( @@ -787,8 +721,7 @@ def test_java_rejects_mixed_default_and_named_imports(tmp_path): def test_java_nested_enum_shadowing_does_not_emit_ref_annotations(): - schema = parse_fdl( - """ + schema = parse_fdl(""" package demo; message Node { @@ -805,8 +738,7 @@ def test_java_nested_enum_shadowing_does_not_emit_ref_annotations(): list kinds = 2; map indexed = 3; } - """ - ) + """) output = render_files(generate_files(schema, JavaGenerator)) assert "import org.apache.fory.annotation.Ref;" not in output @@ -817,8 +749,7 @@ def test_java_nested_enum_shadowing_does_not_emit_ref_annotations(): def test_generated_code_tree_ref_options_equivalent(): - fdl = dedent( - """ + fdl = dedent(""" package tree; message TreeNode { @@ -828,10 +759,8 @@ def test_generated_code_tree_ref_options_equivalent(): repeated ref TreeNode children = 3; ref(weak=true) TreeNode parent = 4; } - """ - ) - proto = dedent( - """ + """) + proto = dedent(""" syntax = "proto3"; package tree; @@ -842,10 +771,8 @@ def test_generated_code_tree_ref_options_equivalent(): repeated TreeNode children = 3 [(fory).ref = true]; TreeNode parent = 4 [(fory).weak_ref = true]; } - """ - ) - fbs = dedent( - """ + """) + fbs = dedent(""" namespace tree; table TreeNode { @@ -854,8 +781,7 @@ def test_generated_code_tree_ref_options_equivalent(): children: [TreeNode] (fory_ref: true); parent: TreeNode (fory_weak_ref: true); } - """ - ) + """) # Tree ref options should produce identical outputs across frontends. schemas = { "fdl": parse_fdl(fdl), @@ -883,18 +809,14 @@ def test_generated_code_tree_ref_options_equivalent(): def test_java_float16_equals_hash_contract_generation(): - schema = parse_fdl( - dedent( - """ + schema = parse_fdl(dedent(""" package gen; message Float16Contract { float16 f16 = 1; optional float16 opt_f16 = 2; } - """ - ) - ) + """)) java_output = render_files(generate_files(schema, JavaGenerator)) assert "Objects.equals(f16, that.f16)" in java_output assert "Objects.equals(optF16, that.optF16)" in java_output @@ -902,26 +824,20 @@ def test_java_float16_equals_hash_contract_generation(): def test_java_repeated_float16_generation_uses_float16_list(): - schema = parse_fdl( - dedent( - """ + schema = parse_fdl(dedent(""" package gen; message RepeatedFloat16 { list vals = 1; } - """ - ) - ) + """)) java_output = render_files(generate_files(schema, JavaGenerator)) assert "import org.apache.fory.collection.Float16List;" in java_output assert "private Float16List vals;" in java_output def test_java_nested_array_values_use_deep_equals_hash_generation(): - schema = parse_fdl( - dedent( - """ + schema = parse_fdl(dedent(""" package gen; message NestedArrays { @@ -933,9 +849,7 @@ def test_java_nested_array_values_use_deep_equals_hash_generation(): list> groups = 1; map> bytes_by_name = 2; } - """ - ) - ) + """)) java_output = render_files(generate_files(schema, JavaGenerator)) assert ( "private static boolean deepValueEquals(Object left, Object right)" @@ -950,9 +864,7 @@ def test_java_nested_array_values_use_deep_equals_hash_generation(): def test_java_unsigned_carriers_and_integer_encoding_annotations(): - schema = parse_fdl( - dedent( - """ + schema = parse_fdl(dedent(""" package gen; message UnsignedCarriers { @@ -963,9 +875,7 @@ def test_java_unsigned_carriers_and_integer_encoding_annotations(): optional uint32 maybe_u32 = 5; fixed int32 fixed_i32 = 6; } - """ - ) - ) + """)) java_output = render_files(generate_files(schema, JavaGenerator)) assert "import org.apache.fory.annotation.Nullable;" in java_output assert "import org.apache.fory.config.Int32Encoding;" in java_output @@ -984,9 +894,7 @@ def test_java_unsigned_carriers_and_integer_encoding_annotations(): def test_java_nullable_type_use_annotation_targets_top_level_type(): - schema = parse_fdl( - dedent( - """ + schema = parse_fdl(dedent(""" package gen; message Money { @@ -998,9 +906,7 @@ def test_java_nullable_type_use_annotation_targets_top_level_type(): optional bytes payload = 2; optional decimal amount = 3; } - """ - ) - ) + """)) java_output = render_files(generate_files(schema, JavaGenerator)) assert "private @Nullable Money money;" in java_output assert "private byte @Nullable [] payload;" in java_output @@ -1008,9 +914,7 @@ def test_java_nullable_type_use_annotation_targets_top_level_type(): def test_java_evolving_false_generation_uses_struct_evolution_enum(): - schema = parse_fdl( - dedent( - """ + schema = parse_fdl(dedent(""" package gen; message DefaultEvolving { @@ -1024,9 +928,7 @@ def test_java_evolving_false_generation_uses_struct_evolution_enum(): int32 id = 1; } } - """ - ) - ) + """)) java_output = render_files(generate_files(schema, JavaGenerator)) assert "import org.apache.fory.annotation.ForyStruct;" in java_output assert "import org.apache.fory.annotation.ForyStruct.Evolution;" in java_output @@ -1036,17 +938,13 @@ def test_java_evolving_false_generation_uses_struct_evolution_enum(): def test_java_nested_integer_annotations_in_generic_containers(): - schema = parse_fdl( - dedent( - """ + schema = parse_fdl(dedent(""" package gen; message NestedIntegerAnnotations { map> values = 1; } - """ - ) - ) + """)) java_output = render_files(generate_files(schema, JavaGenerator)) assert ( "private Map<@UInt32Type(encoding = Int32Encoding.FIXED) Long, " @@ -1062,18 +960,14 @@ def test_java_nested_integer_annotations_in_generic_containers(): def test_python_nested_integer_schema_aliases_in_generic_containers(): - schema = parse_fdl( - dedent( - """ + schema = parse_fdl(dedent(""" package gen; message NestedIntegerSchemaAliases { map> signed_values = 1; map> unsigned_values = 2; } - """ - ) - ) + """)) python_output = render_files(generate_files(schema, PythonGenerator)) assert ( "signed_values: Dict[pyfory.FixedInt32, List[pyfory.TaggedInt64]]" @@ -1086,33 +980,27 @@ def test_python_nested_integer_schema_aliases_in_generic_containers(): def test_cpp_nested_integer_specs_in_generic_containers(): - schema = parse_fdl( - dedent( - """ + schema = parse_fdl(dedent(""" package gen; message NestedIntegerSpecs { map> values = 1; } - """ - ) - ) + """)) cpp_output = render_files(generate_files(schema, CppGenerator)) assert ( - " FORY_STRUCT(\n" - " NestedIntegerSpecs,\n" - " (values_, ::fory::F(1).map(" - "::fory::T::uint32().fixed(), " - "::fory::T::list(" - "::fory::T::inner(::fory::T::uint64().tagged()))))\n" - " );" - in cpp_output -) + " FORY_STRUCT(\n" + " NestedIntegerSpecs,\n" + " (values_, ::fory::F(1).map(" + "::fory::T::uint32().fixed(), " + "::fory::T::list(" + "::fory::T::inner(::fory::T::uint64().tagged()))))\n" + " );" in cpp_output + ) + def test_cpp_escapes_keywords_and_generated_helpers(): - schema = parse_fdl( - dedent( - """ + schema = parse_fdl(dedent(""" package class.demo; message private { @@ -1128,9 +1016,7 @@ class = 0; string visit = 1; string class = 2; } - """ - ) - ) + """)) cpp_output = render_files(generate_files(schema, CppGenerator)) assert "namespace class_::demo {" in cpp_output @@ -1273,20 +1159,14 @@ def test_cpp_rejects_normalized_name_collisions(): def test_cpp_imported_type_uses_sanitized_namespace_and_identifier(tmp_path: Path): imported_fdl = tmp_path / "imported.fdl" main_fdl = tmp_path / "main.fdl" - imported_fdl.write_text( - dedent( - """ + imported_fdl.write_text(dedent(""" package class.imported; message private { string value = 1; } - """ - ) - ) - main_fdl.write_text( - dedent( - """ + """)) + main_fdl.write_text(dedent(""" package demo; import "imported.fdl"; @@ -1294,9 +1174,7 @@ def test_cpp_imported_type_uses_sanitized_namespace_and_identifier(tmp_path: Pat message Holder { private value = 1; } - """ - ) - ) + """)) schema = resolve_imports(main_fdl, [tmp_path]) cpp_output = render_files(generate_files(schema, CppGenerator)) @@ -1307,9 +1185,7 @@ def test_cpp_imported_type_uses_sanitized_namespace_and_identifier(tmp_path: Pat def test_cpp_generator_supports_decimal_fields_and_unions(): - schema = parse_fdl( - dedent( - """ + schema = parse_fdl(dedent(""" package gen; message Money { @@ -1320,9 +1196,7 @@ def test_cpp_generator_supports_decimal_fields_and_unions(): decimal amount = 1; Money money = 2; } - """ - ) - ) + """)) cpp_output = render_files(generate_files(schema, CppGenerator)) assert '#include "fory/serialization/decimal_serializers.h"' in cpp_output @@ -1333,9 +1207,7 @@ def test_cpp_generator_supports_decimal_fields_and_unions(): def test_cpp_union_aliases_comma_payload_types(): - schema = parse_fdl( - dedent( - """ + schema = parse_fdl(dedent(""" package gen; union MapChoice { @@ -1364,9 +1236,7 @@ def test_cpp_union_aliases_comma_payload_types(): date day = 16; timestamp ts = 17; } - """ - ) - ) + """)) cpp_output = render_files(generate_files(schema, CppGenerator)) assert ( @@ -1400,9 +1270,7 @@ def test_cpp_union_aliases_comma_payload_types(): def test_cpp_omits_equality_for_any_types(): - schema = parse_fdl( - dedent( - """ + schema = parse_fdl(dedent(""" package gen; message Inner { @@ -1466,9 +1334,7 @@ def test_cpp_omits_equality_for_any_types(): string name = 1; int32 code = 2; } - """ - ) - ) + """)) cpp_output = render_files(generate_files(schema, CppGenerator)) assert "bool operator==(const Inner& other) const" not in cpp_output @@ -1488,9 +1354,7 @@ def test_cpp_omits_equality_for_any_types(): def test_cpp_nested_container_ref_uses_correct_pointer_type(): - schema = parse_fdl( - dedent( - """ + schema = parse_fdl(dedent(""" package gen; message Node { @@ -1503,9 +1367,7 @@ def test_cpp_nested_container_ref_uses_correct_pointer_type(): list> weak_groups = 3; map> weak_nodes = 4; } - """ - ) - ) + """)) cpp_output = render_files(generate_files(schema, CppGenerator)) assert ( @@ -1534,9 +1396,7 @@ def test_cpp_nested_container_ref_uses_correct_pointer_type(): def test_cpp_temporal_map_keys_use_fory_owned_wrappers(): - schema = parse_fdl( - dedent( - """ + schema = parse_fdl(dedent(""" package demo; message Holder { @@ -1544,9 +1404,7 @@ def test_cpp_temporal_map_keys_use_fory_owned_wrappers(): map timestamps = 2; map dates = 3; } - """ - ) - ) + """)) cpp_output = render_files(generate_files(schema, CppGenerator)) assert ( @@ -1564,18 +1422,14 @@ def test_cpp_temporal_map_keys_use_fory_owned_wrappers(): def test_java_enum_generation_uses_fory_enum_ids(): - schema = parse_fdl( - dedent( - """ + schema = parse_fdl(dedent(""" package gen; enum Status { UNKNOWN = 4096; OK = 8192; } - """ - ) - ) + """)) java_output = render_files(generate_files(schema, JavaGenerator)) assert "import org.apache.fory.annotation.ForyEnumId;" in java_output assert "public enum Status {" in java_output @@ -1590,8 +1444,7 @@ def test_java_enum_generation_uses_fory_enum_ids(): def test_go_bfloat16_generation(): - idl = dedent( - """ + idl = dedent(""" package bfloat16_test; message BFloat16Message { @@ -1599,8 +1452,7 @@ def test_go_bfloat16_generation(): optional bfloat16 opt_val = 2; list list_val = 3; } - """ - ) + """) schema = parse_fdl(idl) files = generate_files(schema, GoGenerator) @@ -1620,27 +1472,21 @@ def test_go_bfloat16_generation(): def test_go_generator_distinguishes_bytes_from_uint8_lists(): - schema = parse_fdl( - dedent( - """ + schema = parse_fdl(dedent(""" package gen; message ByteSemantics { bytes payload = 1; list values = 2; } - """ - ) - ) + """)) go_output = render_files(generate_files(schema, GoGenerator)) assert 'Payload []byte `fory:"id=1,type=bytes"`' in go_output assert 'Values []uint8 `fory:"id=2"`' in go_output def test_rust_generated_code_uses_absolute_paths(): - schema = parse_fdl( - dedent( - """ + schema = parse_fdl(dedent(""" package foo; message String { @@ -1654,9 +1500,7 @@ def test_rust_generated_code_uses_absolute_paths(): union Fory { String text = 1; } - """ - ) - ) + """)) rust_output = render_files(generate_files(schema, RustGenerator)) assert "use fory::" not in rust_output assert "use std::" not in rust_output @@ -1672,8 +1516,7 @@ def test_rust_generated_code_uses_absolute_paths(): " pub labels: ::std::collections::HashMap<\n" " ::std::string::String,\n" " ::std::string::String,\n" - " >," - in rust_output + " >," in rust_output ) assert ( "pub payload: ::std::sync::Arc," @@ -1683,16 +1526,13 @@ def test_rust_generated_code_uses_absolute_paths(): assert ( "pub fn register_types(\n" " fory: &mut ::fory::Fory,\n" - ") -> ::std::result::Result<(), ::fory::Error> {" - in rust_output + ") -> ::std::result::Result<(), ::fory::Error> {" in rust_output ) assert "static FORY: ::std::sync::OnceLock<::fory::Fory>" in rust_output def test_rust_union_conflicting_payload_uses_self_path(): - schema = parse_fdl( - dedent( - """ + schema = parse_fdl(dedent(""" package foo; message Dog { @@ -1702,9 +1542,7 @@ def test_rust_union_conflicting_payload_uses_self_path(): union Animal { Dog dog = 1; } - """ - ) - ) + """)) rust_output = render_files(generate_files(schema, RustGenerator)) assert "Dog(self::Dog)," in rust_output @@ -1715,9 +1553,7 @@ def test_rust_union_conflicting_payload_uses_self_path(): def test_rust_escapes_keywords(): - schema = parse_fdl( - dedent( - """ + schema = parse_fdl(dedent(""" package demo; message type { @@ -1731,9 +1567,7 @@ def test_rust_escapes_keywords(): message _1 { string value = 1; } - """ - ) - ) + """)) rust_files = generate_files(schema, RustGenerator) rust_output = render_files(rust_files) @@ -1786,28 +1620,20 @@ def test_rust_rejects_same_output_path_collisions( first_fdl = tmp_path / "first.fdl" second_fdl = tmp_path / "second.fdl" rust_out = tmp_path / "rust" - first_fdl.write_text( - dedent( - """ + first_fdl.write_text(dedent(""" package foo.bar; message First { string value = 1; } - """ - ) - ) - second_fdl.write_text( - dedent( - """ + """)) + second_fdl.write_text(dedent(""" package foo_bar; message Second { string value = 1; } - """ - ) - ) + """)) exit_code = foryc_main( [ str(first_fdl), From f1a56ebf73fc04e40b1df1f18acb87c9760be512 Mon Sep 17 00:00:00 2001 From: Jamal Siddiqui Date: Tue, 21 Jul 2026 00:19:01 -0400 Subject: [PATCH 09/10] style: apply ruff formatting --- .../tests/test_generated_code.py | 174 ++++++++++++------ 1 file changed, 115 insertions(+), 59 deletions(-) diff --git a/compiler/fory_compiler/tests/test_generated_code.py b/compiler/fory_compiler/tests/test_generated_code.py index fa17472663..2dd5f187c3 100644 --- a/compiler/fory_compiler/tests/test_generated_code.py +++ b/compiler/fory_compiler/tests/test_generated_code.py @@ -89,9 +89,9 @@ def assert_language_outputs_equal( baseline_label = label baseline_files = files continue - assert ( - files == baseline_files - ), f"{generator_cls.language_name} output mismatch for {label} vs {baseline_label}" + assert files == baseline_files, ( + f"{generator_cls.language_name} output mismatch for {label} vs {baseline_label}" + ) def assert_all_languages_equal(schemas: Dict[str, Schema]) -> None: @@ -152,7 +152,8 @@ def test_generated_code_scalar_types_equivalent(): def test_rust_generated_code_uses_fory_temporal_carriers(): - schema = parse_fdl(dedent(""" + schema = parse_fdl( + dedent(""" package gen; message TemporalTypes { @@ -160,7 +161,8 @@ def test_rust_generated_code_uses_fory_temporal_carriers(): timestamp instant = 2; duration elapsed = 3; } - """)) + """) + ) rust_output = render_files(generate_files(schema, RustGenerator)) assert "pub day: ::fory::Date," in rust_output @@ -170,7 +172,8 @@ def test_rust_generated_code_uses_fory_temporal_carriers(): def test_rust_generated_code_can_use_chrono_temporal_types(): - schema = parse_fdl(dedent(""" + schema = parse_fdl( + dedent(""" package gen; option rust_use_chrono_temporal_types = true; @@ -179,7 +182,8 @@ def test_rust_generated_code_can_use_chrono_temporal_types(): timestamp instant = 2; duration elapsed = 3; } - """)) + """) + ) rust_output = render_files(generate_files(schema, RustGenerator)) assert "pub day: ::chrono::NaiveDate," in rust_output @@ -191,7 +195,8 @@ def test_rust_generated_code_can_use_chrono_temporal_types(): def test_rust_nested_container_ref_uses_correct_pointer_type(): - schema = parse_fdl(dedent(""" + schema = parse_fdl( + dedent(""" package gen; message Node { @@ -202,7 +207,8 @@ def test_rust_nested_container_ref_uses_correct_pointer_type(): list> groups = 1; map> nodes = 2; } - """)) + """) + ) rust_output = render_files(generate_files(schema, RustGenerator)) @@ -472,7 +478,8 @@ def test_generated_code_map_types_equivalent(): def test_rust_generated_ref_pointer_default_and_opt_out(): - schema = parse_fdl(dedent(""" + schema = parse_fdl( + dedent(""" package gen; message Node { @@ -487,7 +494,8 @@ def test_rust_generated_ref_pointer_default_and_opt_out(): list default_ref_list = 5; list rc_ref_list = 6; } - """)) + """) + ) rust_output = render_files(generate_files(schema, RustGenerator)) assert "pub default_ref: ::std::sync::Arc," in rust_output assert "pub rc_ref: ::std::rc::Rc," in rust_output @@ -809,14 +817,16 @@ def test_generated_code_tree_ref_options_equivalent(): def test_java_float16_equals_hash_contract_generation(): - schema = parse_fdl(dedent(""" + schema = parse_fdl( + dedent(""" package gen; message Float16Contract { float16 f16 = 1; optional float16 opt_f16 = 2; } - """)) + """) + ) java_output = render_files(generate_files(schema, JavaGenerator)) assert "Objects.equals(f16, that.f16)" in java_output assert "Objects.equals(optF16, that.optF16)" in java_output @@ -824,20 +834,23 @@ def test_java_float16_equals_hash_contract_generation(): def test_java_repeated_float16_generation_uses_float16_list(): - schema = parse_fdl(dedent(""" + schema = parse_fdl( + dedent(""" package gen; message RepeatedFloat16 { list vals = 1; } - """)) + """) + ) java_output = render_files(generate_files(schema, JavaGenerator)) assert "import org.apache.fory.collection.Float16List;" in java_output assert "private Float16List vals;" in java_output def test_java_nested_array_values_use_deep_equals_hash_generation(): - schema = parse_fdl(dedent(""" + schema = parse_fdl( + dedent(""" package gen; message NestedArrays { @@ -849,7 +862,8 @@ def test_java_nested_array_values_use_deep_equals_hash_generation(): list> groups = 1; map> bytes_by_name = 2; } - """)) + """) + ) java_output = render_files(generate_files(schema, JavaGenerator)) assert ( "private static boolean deepValueEquals(Object left, Object right)" @@ -864,7 +878,8 @@ def test_java_nested_array_values_use_deep_equals_hash_generation(): def test_java_unsigned_carriers_and_integer_encoding_annotations(): - schema = parse_fdl(dedent(""" + schema = parse_fdl( + dedent(""" package gen; message UnsignedCarriers { @@ -875,7 +890,8 @@ def test_java_unsigned_carriers_and_integer_encoding_annotations(): optional uint32 maybe_u32 = 5; fixed int32 fixed_i32 = 6; } - """)) + """) + ) java_output = render_files(generate_files(schema, JavaGenerator)) assert "import org.apache.fory.annotation.Nullable;" in java_output assert "import org.apache.fory.config.Int32Encoding;" in java_output @@ -894,7 +910,8 @@ def test_java_unsigned_carriers_and_integer_encoding_annotations(): def test_java_nullable_type_use_annotation_targets_top_level_type(): - schema = parse_fdl(dedent(""" + schema = parse_fdl( + dedent(""" package gen; message Money { @@ -906,7 +923,8 @@ def test_java_nullable_type_use_annotation_targets_top_level_type(): optional bytes payload = 2; optional decimal amount = 3; } - """)) + """) + ) java_output = render_files(generate_files(schema, JavaGenerator)) assert "private @Nullable Money money;" in java_output assert "private byte @Nullable [] payload;" in java_output @@ -914,7 +932,8 @@ def test_java_nullable_type_use_annotation_targets_top_level_type(): def test_java_evolving_false_generation_uses_struct_evolution_enum(): - schema = parse_fdl(dedent(""" + schema = parse_fdl( + dedent(""" package gen; message DefaultEvolving { @@ -928,7 +947,8 @@ def test_java_evolving_false_generation_uses_struct_evolution_enum(): int32 id = 1; } } - """)) + """) + ) java_output = render_files(generate_files(schema, JavaGenerator)) assert "import org.apache.fory.annotation.ForyStruct;" in java_output assert "import org.apache.fory.annotation.ForyStruct.Evolution;" in java_output @@ -938,13 +958,15 @@ def test_java_evolving_false_generation_uses_struct_evolution_enum(): def test_java_nested_integer_annotations_in_generic_containers(): - schema = parse_fdl(dedent(""" + schema = parse_fdl( + dedent(""" package gen; message NestedIntegerAnnotations { map> values = 1; } - """)) + """) + ) java_output = render_files(generate_files(schema, JavaGenerator)) assert ( "private Map<@UInt32Type(encoding = Int32Encoding.FIXED) Long, " @@ -960,14 +982,16 @@ def test_java_nested_integer_annotations_in_generic_containers(): def test_python_nested_integer_schema_aliases_in_generic_containers(): - schema = parse_fdl(dedent(""" + schema = parse_fdl( + dedent(""" package gen; message NestedIntegerSchemaAliases { map> signed_values = 1; map> unsigned_values = 2; } - """)) + """) + ) python_output = render_files(generate_files(schema, PythonGenerator)) assert ( "signed_values: Dict[pyfory.FixedInt32, List[pyfory.TaggedInt64]]" @@ -980,13 +1004,15 @@ def test_python_nested_integer_schema_aliases_in_generic_containers(): def test_cpp_nested_integer_specs_in_generic_containers(): - schema = parse_fdl(dedent(""" + schema = parse_fdl( + dedent(""" package gen; message NestedIntegerSpecs { map> values = 1; } - """)) + """) + ) cpp_output = render_files(generate_files(schema, CppGenerator)) assert ( " FORY_STRUCT(\n" @@ -1000,7 +1026,8 @@ def test_cpp_nested_integer_specs_in_generic_containers(): def test_cpp_escapes_keywords_and_generated_helpers(): - schema = parse_fdl(dedent(""" + schema = parse_fdl( + dedent(""" package class.demo; message private { @@ -1016,7 +1043,8 @@ class = 0; string visit = 1; string class = 2; } - """)) + """) + ) cpp_output = render_files(generate_files(schema, CppGenerator)) assert "namespace class_::demo {" in cpp_output @@ -1159,14 +1187,17 @@ def test_cpp_rejects_normalized_name_collisions(): def test_cpp_imported_type_uses_sanitized_namespace_and_identifier(tmp_path: Path): imported_fdl = tmp_path / "imported.fdl" main_fdl = tmp_path / "main.fdl" - imported_fdl.write_text(dedent(""" + imported_fdl.write_text( + dedent(""" package class.imported; message private { string value = 1; } - """)) - main_fdl.write_text(dedent(""" + """) + ) + main_fdl.write_text( + dedent(""" package demo; import "imported.fdl"; @@ -1174,7 +1205,8 @@ def test_cpp_imported_type_uses_sanitized_namespace_and_identifier(tmp_path: Pat message Holder { private value = 1; } - """)) + """) + ) schema = resolve_imports(main_fdl, [tmp_path]) cpp_output = render_files(generate_files(schema, CppGenerator)) @@ -1185,7 +1217,8 @@ def test_cpp_imported_type_uses_sanitized_namespace_and_identifier(tmp_path: Pat def test_cpp_generator_supports_decimal_fields_and_unions(): - schema = parse_fdl(dedent(""" + schema = parse_fdl( + dedent(""" package gen; message Money { @@ -1196,7 +1229,8 @@ def test_cpp_generator_supports_decimal_fields_and_unions(): decimal amount = 1; Money money = 2; } - """)) + """) + ) cpp_output = render_files(generate_files(schema, CppGenerator)) assert '#include "fory/serialization/decimal_serializers.h"' in cpp_output @@ -1207,7 +1241,8 @@ def test_cpp_generator_supports_decimal_fields_and_unions(): def test_cpp_union_aliases_comma_payload_types(): - schema = parse_fdl(dedent(""" + schema = parse_fdl( + dedent(""" package gen; union MapChoice { @@ -1236,7 +1271,8 @@ def test_cpp_union_aliases_comma_payload_types(): date day = 16; timestamp ts = 17; } - """)) + """) + ) cpp_output = render_files(generate_files(schema, CppGenerator)) assert ( @@ -1270,7 +1306,8 @@ def test_cpp_union_aliases_comma_payload_types(): def test_cpp_omits_equality_for_any_types(): - schema = parse_fdl(dedent(""" + schema = parse_fdl( + dedent(""" package gen; message Inner { @@ -1334,7 +1371,8 @@ def test_cpp_omits_equality_for_any_types(): string name = 1; int32 code = 2; } - """)) + """) + ) cpp_output = render_files(generate_files(schema, CppGenerator)) assert "bool operator==(const Inner& other) const" not in cpp_output @@ -1354,7 +1392,8 @@ def test_cpp_omits_equality_for_any_types(): def test_cpp_nested_container_ref_uses_correct_pointer_type(): - schema = parse_fdl(dedent(""" + schema = parse_fdl( + dedent(""" package gen; message Node { @@ -1367,7 +1406,8 @@ def test_cpp_nested_container_ref_uses_correct_pointer_type(): list> weak_groups = 3; map> weak_nodes = 4; } - """)) + """) + ) cpp_output = render_files(generate_files(schema, CppGenerator)) assert ( @@ -1396,7 +1436,8 @@ def test_cpp_nested_container_ref_uses_correct_pointer_type(): def test_cpp_temporal_map_keys_use_fory_owned_wrappers(): - schema = parse_fdl(dedent(""" + schema = parse_fdl( + dedent(""" package demo; message Holder { @@ -1404,7 +1445,8 @@ def test_cpp_temporal_map_keys_use_fory_owned_wrappers(): map timestamps = 2; map dates = 3; } - """)) + """) + ) cpp_output = render_files(generate_files(schema, CppGenerator)) assert ( @@ -1422,14 +1464,16 @@ def test_cpp_temporal_map_keys_use_fory_owned_wrappers(): def test_java_enum_generation_uses_fory_enum_ids(): - schema = parse_fdl(dedent(""" + schema = parse_fdl( + dedent(""" package gen; enum Status { UNKNOWN = 4096; OK = 8192; } - """)) + """) + ) java_output = render_files(generate_files(schema, JavaGenerator)) assert "import org.apache.fory.annotation.ForyEnumId;" in java_output assert "public enum Status {" in java_output @@ -1472,21 +1516,24 @@ def test_go_bfloat16_generation(): def test_go_generator_distinguishes_bytes_from_uint8_lists(): - schema = parse_fdl(dedent(""" + schema = parse_fdl( + dedent(""" package gen; message ByteSemantics { bytes payload = 1; list values = 2; } - """)) + """) + ) go_output = render_files(generate_files(schema, GoGenerator)) assert 'Payload []byte `fory:"id=1,type=bytes"`' in go_output assert 'Values []uint8 `fory:"id=2"`' in go_output def test_rust_generated_code_uses_absolute_paths(): - schema = parse_fdl(dedent(""" + schema = parse_fdl( + dedent(""" package foo; message String { @@ -1500,7 +1547,8 @@ def test_rust_generated_code_uses_absolute_paths(): union Fory { String text = 1; } - """)) + """) + ) rust_output = render_files(generate_files(schema, RustGenerator)) assert "use fory::" not in rust_output assert "use std::" not in rust_output @@ -1532,7 +1580,8 @@ def test_rust_generated_code_uses_absolute_paths(): def test_rust_union_conflicting_payload_uses_self_path(): - schema = parse_fdl(dedent(""" + schema = parse_fdl( + dedent(""" package foo; message Dog { @@ -1542,7 +1591,8 @@ def test_rust_union_conflicting_payload_uses_self_path(): union Animal { Dog dog = 1; } - """)) + """) + ) rust_output = render_files(generate_files(schema, RustGenerator)) assert "Dog(self::Dog)," in rust_output @@ -1553,7 +1603,8 @@ def test_rust_union_conflicting_payload_uses_self_path(): def test_rust_escapes_keywords(): - schema = parse_fdl(dedent(""" + schema = parse_fdl( + dedent(""" package demo; message type { @@ -1567,7 +1618,8 @@ def test_rust_escapes_keywords(): message _1 { string value = 1; } - """)) + """) + ) rust_files = generate_files(schema, RustGenerator) rust_output = render_files(rust_files) @@ -1620,20 +1672,24 @@ def test_rust_rejects_same_output_path_collisions( first_fdl = tmp_path / "first.fdl" second_fdl = tmp_path / "second.fdl" rust_out = tmp_path / "rust" - first_fdl.write_text(dedent(""" + first_fdl.write_text( + dedent(""" package foo.bar; message First { string value = 1; } - """)) - second_fdl.write_text(dedent(""" + """) + ) + second_fdl.write_text( + dedent(""" package foo_bar; message Second { string value = 1; } - """)) + """) + ) exit_code = foryc_main( [ str(first_fdl), From 9d137daf909ceb2a1848275254d0acf5677299bf Mon Sep 17 00:00:00 2001 From: Jamal Siddiqui Date: Tue, 21 Jul 2026 00:44:35 -0400 Subject: [PATCH 10/10] test: add focused generated wrapping coverage --- .../tests/test_generated_code.py | 469 +++++++++++++----- 1 file changed, 355 insertions(+), 114 deletions(-) diff --git a/compiler/fory_compiler/tests/test_generated_code.py b/compiler/fory_compiler/tests/test_generated_code.py index 2dd5f187c3..d7dcf0b858 100644 --- a/compiler/fory_compiler/tests/test_generated_code.py +++ b/compiler/fory_compiler/tests/test_generated_code.py @@ -41,6 +41,7 @@ from fory_compiler.ir.ast import Schema from fory_compiler.ir.validator import SchemaValidator + GENERATOR_CLASSES: Tuple[Type[BaseGenerator], ...] = ( JavaGenerator, PythonGenerator, @@ -100,7 +101,8 @@ def assert_all_languages_equal(schemas: Dict[str, Schema]) -> None: def test_generated_code_scalar_types_equivalent(): - fdl = dedent(""" + fdl = dedent( + """ package gen; message ScalarTypes { @@ -113,8 +115,10 @@ def test_generated_code_scalar_types_equivalent(): float64 f64 = 7; string name = 8; } - """) - proto = dedent(""" + """ + ) + proto = dedent( + """ syntax = "proto3"; package gen; @@ -128,8 +132,10 @@ def test_generated_code_scalar_types_equivalent(): double f64 = 7; string name = 8; } - """) - fbs = dedent(""" + """ + ) + fbs = dedent( + """ namespace gen; table ScalarTypes { @@ -142,7 +148,8 @@ def test_generated_code_scalar_types_equivalent(): f64:double; name:string; } - """) + """ + ) schemas = { "fdl": parse_fdl(fdl), "proto": parse_proto(proto), @@ -153,7 +160,8 @@ def test_generated_code_scalar_types_equivalent(): def test_rust_generated_code_uses_fory_temporal_carriers(): schema = parse_fdl( - dedent(""" + dedent( + """ package gen; message TemporalTypes { @@ -161,7 +169,8 @@ def test_rust_generated_code_uses_fory_temporal_carriers(): timestamp instant = 2; duration elapsed = 3; } - """) + """ + ) ) rust_output = render_files(generate_files(schema, RustGenerator)) @@ -173,7 +182,8 @@ def test_rust_generated_code_uses_fory_temporal_carriers(): def test_rust_generated_code_can_use_chrono_temporal_types(): schema = parse_fdl( - dedent(""" + dedent( + """ package gen; option rust_use_chrono_temporal_types = true; @@ -182,7 +192,8 @@ def test_rust_generated_code_can_use_chrono_temporal_types(): timestamp instant = 2; duration elapsed = 3; } - """) + """ + ) ) rust_output = render_files(generate_files(schema, RustGenerator)) @@ -196,7 +207,8 @@ def test_rust_generated_code_can_use_chrono_temporal_types(): def test_rust_nested_container_ref_uses_correct_pointer_type(): schema = parse_fdl( - dedent(""" + dedent( + """ package gen; message Node { @@ -207,7 +219,8 @@ def test_rust_nested_container_ref_uses_correct_pointer_type(): list> groups = 1; map> nodes = 2; } - """) + """ + ) ) rust_output = render_files(generate_files(schema, RustGenerator)) @@ -233,7 +246,8 @@ def test_rust_nested_container_ref_uses_correct_pointer_type(): def test_generated_code_integer_encoding_variants_equivalent(): - fdl = dedent(""" + fdl = dedent( + """ package gen; message EncodingTypes { @@ -244,8 +258,10 @@ def test_generated_code_integer_encoding_variants_equivalent(): tagged int64 ti64 = 5; tagged uint64 tu64 = 6; } - """) - proto = dedent(""" + """ + ) + proto = dedent( + """ syntax = "proto3"; package gen; @@ -257,7 +273,8 @@ def test_generated_code_integer_encoding_variants_equivalent(): int64 ti64 = 5 [(fory).type = "tagged int64"]; uint64 tu64 = 6 [(fory).type = "tagged uint64"]; } - """) + """ + ) schemas = { "fdl": parse_fdl(fdl), "proto": parse_proto(proto), @@ -270,7 +287,8 @@ def test_generated_code_integer_encoding_variants_equivalent(): def test_generated_code_list_modifier_aliases_equivalent(): - repeated = dedent(""" + repeated = dedent( + """ package gen; message Item { @@ -284,8 +302,10 @@ def test_generated_code_list_modifier_aliases_equivalent(): ref repeated Item items = 4; repeated ref Item children = 5; } - """) - list_syntax = dedent(""" + """ + ) + list_syntax = dedent( + """ package gen; message Item { @@ -299,7 +319,8 @@ def test_generated_code_list_modifier_aliases_equivalent(): ref list items = 4; list children = 5; } - """) + """ + ) schemas = { "repeated": parse_fdl(repeated), "list": parse_fdl(list_syntax), @@ -308,7 +329,8 @@ def test_generated_code_list_modifier_aliases_equivalent(): def test_generated_code_repeated_primitives_are_lists(): - fdl = dedent(""" + fdl = dedent( + """ package gen; message ArrayTypes { @@ -324,8 +346,10 @@ def test_generated_code_repeated_primitives_are_lists(): repeated float32 f32s = 10; repeated float64 f64s = 11; } - """) - proto = dedent(""" + """ + ) + proto = dedent( + """ syntax = "proto3"; package gen; @@ -342,7 +366,8 @@ def test_generated_code_repeated_primitives_are_lists(): repeated float f32s = 10; repeated double f64s = 11; } - """) + """ + ) schemas = { "fdl": parse_fdl(fdl), "proto": parse_proto(proto), @@ -351,7 +376,8 @@ def test_generated_code_repeated_primitives_are_lists(): def test_generated_code_flatbuffers_primitive_vectors_are_arrays(): - fbs = dedent(""" + fbs = dedent( + """ namespace gen; table ArrayTypes { @@ -367,7 +393,8 @@ def test_generated_code_flatbuffers_primitive_vectors_are_arrays(): f32s:[float]; f64s:[double]; } - """) + """ + ) schema = parse_fbs(fbs) java_output = render_files(generate_files(schema, JavaGenerator)) @@ -382,7 +409,8 @@ def test_generated_code_flatbuffers_primitive_vectors_are_arrays(): def test_generated_code_list_types_equivalent(): - fdl = dedent(""" + fdl = dedent( + """ package gen; message ListItem { @@ -394,8 +422,10 @@ def test_generated_code_list_types_equivalent(): repeated bool flags = 2; repeated ListItem items = 3; } - """) - proto = dedent(""" + """ + ) + proto = dedent( + """ syntax = "proto3"; package gen; @@ -408,7 +438,8 @@ def test_generated_code_list_types_equivalent(): repeated bool flags = 2; repeated ListItem items = 3; } - """) + """ + ) schemas = { "fdl": parse_fdl(fdl), "proto": parse_proto(proto), @@ -417,7 +448,8 @@ def test_generated_code_list_types_equivalent(): def test_generated_code_map_types_equivalent(): - fdl = dedent(""" + fdl = dedent( + """ package gen; message MapValue { @@ -430,8 +462,10 @@ def test_generated_code_map_types_equivalent(): map weak_entries = 3; optional int32 version = 4; } - """) - proto = dedent(""" + """ + ) + proto = dedent( + """ syntax = "proto3"; package gen; @@ -449,7 +483,8 @@ def test_generated_code_map_types_equivalent(): ]; optional sint32 version = 4; } - """) + """ + ) # FlatBuffers does not support maps, compare FDL vs proto only. schemas = { "fdl": parse_fdl(fdl), @@ -479,7 +514,8 @@ def test_generated_code_map_types_equivalent(): def test_rust_generated_ref_pointer_default_and_opt_out(): schema = parse_fdl( - dedent(""" + dedent( + """ package gen; message Node { @@ -494,7 +530,8 @@ def test_rust_generated_ref_pointer_default_and_opt_out(): list default_ref_list = 5; list rc_ref_list = 6; } - """) + """ + ) ) rust_output = render_files(generate_files(schema, RustGenerator)) assert "pub default_ref: ::std::sync::Arc," in rust_output @@ -508,7 +545,8 @@ def test_rust_generated_ref_pointer_default_and_opt_out(): def test_generated_code_nested_messages_equivalent(): - fdl = dedent(""" + fdl = dedent( + """ package gen; message Outer { @@ -520,8 +558,10 @@ def test_generated_code_nested_messages_equivalent(): Inner inner = 2; } - """) - proto = dedent(""" + """ + ) + proto = dedent( + """ syntax = "proto3"; package gen; @@ -534,7 +574,8 @@ def test_generated_code_nested_messages_equivalent(): Inner inner = 2; } - """) + """ + ) # FlatBuffers does not support nested message declarations. schemas = { "fdl": parse_fdl(fdl), @@ -544,7 +585,8 @@ def test_generated_code_nested_messages_equivalent(): def test_java_nested_name_registration_uses_owner_namespace(): - schema = parse_fdl(""" + schema = parse_fdl( + """ option enable_auto_type_id = false; package demo; @@ -568,7 +610,8 @@ def test_java_nested_name_registration_uses_owner_namespace(): Choice choice = 3; list payloads = 4; } - """) + """ + ) output = render_files(generate_files(schema, JavaGenerator)) assert "import org.apache.fory.annotation.Ref;" in output @@ -586,7 +629,8 @@ def test_java_nested_name_registration_uses_owner_namespace(): def test_generated_registration_uses_single_name_for_dart_python_swift(): - schema = parse_fdl(""" + schema = parse_fdl( + """ option enable_auto_type_id = false; package demo; @@ -609,7 +653,8 @@ def test_generated_registration_uses_single_name_for_dart_python_swift(): Kind kind = 2; Choice choice = 3; } - """) + """ + ) dart_output = render_files(generate_files(schema, DartGenerator)) assert ( @@ -656,19 +701,23 @@ def test_generated_registration_uses_single_name_for_dart_python_swift(): def test_java_default_package_import_registers_dependency(tmp_path): common = tmp_path / "common.fdl" - common.write_text(""" + common.write_text( + """ message Common [id=200] { string name = 1; } - """) + """ + ) main = tmp_path / "main.fdl" - main.write_text(""" + main.write_text( + """ import "common.fdl"; message Holder [id=201] { Common common = 1; } - """) + """ + ) schema = resolve_imports(main) validator = SchemaValidator(schema) @@ -684,20 +733,24 @@ def test_java_default_package_import_registers_dependency(tmp_path): def test_java_rejects_mixed_default_and_named_imports(tmp_path): default_dir = tmp_path / "default_in_named" default_dir.mkdir() - (default_dir / "common.fdl").write_text(""" + (default_dir / "common.fdl").write_text( + """ message Common [id=200] { string name = 1; } - """) + """ + ) default_main = default_dir / "main.fdl" - default_main.write_text(""" + default_main.write_text( + """ package app; import "common.fdl"; message Holder [id=201] { Common common = 1; } - """) + """ + ) with pytest.raises(ValueError, match="default-package"): JavaGenerator( @@ -706,21 +759,25 @@ def test_java_rejects_mixed_default_and_named_imports(tmp_path): named_dir = tmp_path / "named_in_default" named_dir.mkdir() - (named_dir / "common.fdl").write_text(""" + (named_dir / "common.fdl").write_text( + """ package shared; message Common [id=200] { string name = 1; } - """) + """ + ) named_main = named_dir / "main.fdl" - named_main.write_text(""" + named_main.write_text( + """ import "common.fdl"; message Holder [id=201] { optional Common common = 1; } - """) + """ + ) with pytest.raises(ValueError, match="default-package"): JavaGenerator( @@ -729,7 +786,8 @@ def test_java_rejects_mixed_default_and_named_imports(tmp_path): def test_java_nested_enum_shadowing_does_not_emit_ref_annotations(): - schema = parse_fdl(""" + schema = parse_fdl( + """ package demo; message Node { @@ -746,7 +804,8 @@ def test_java_nested_enum_shadowing_does_not_emit_ref_annotations(): list kinds = 2; map indexed = 3; } - """) + """ + ) output = render_files(generate_files(schema, JavaGenerator)) assert "import org.apache.fory.annotation.Ref;" not in output @@ -757,7 +816,8 @@ def test_java_nested_enum_shadowing_does_not_emit_ref_annotations(): def test_generated_code_tree_ref_options_equivalent(): - fdl = dedent(""" + fdl = dedent( + """ package tree; message TreeNode { @@ -767,8 +827,10 @@ def test_generated_code_tree_ref_options_equivalent(): repeated ref TreeNode children = 3; ref(weak=true) TreeNode parent = 4; } - """) - proto = dedent(""" + """ + ) + proto = dedent( + """ syntax = "proto3"; package tree; @@ -779,8 +841,10 @@ def test_generated_code_tree_ref_options_equivalent(): repeated TreeNode children = 3 [(fory).ref = true]; TreeNode parent = 4 [(fory).weak_ref = true]; } - """) - fbs = dedent(""" + """ + ) + fbs = dedent( + """ namespace tree; table TreeNode { @@ -789,7 +853,8 @@ def test_generated_code_tree_ref_options_equivalent(): children: [TreeNode] (fory_ref: true); parent: TreeNode (fory_weak_ref: true); } - """) + """ + ) # Tree ref options should produce identical outputs across frontends. schemas = { "fdl": parse_fdl(fdl), @@ -818,14 +883,16 @@ def test_generated_code_tree_ref_options_equivalent(): def test_java_float16_equals_hash_contract_generation(): schema = parse_fdl( - dedent(""" + dedent( + """ package gen; message Float16Contract { float16 f16 = 1; optional float16 opt_f16 = 2; } - """) + """ + ) ) java_output = render_files(generate_files(schema, JavaGenerator)) assert "Objects.equals(f16, that.f16)" in java_output @@ -835,13 +902,15 @@ def test_java_float16_equals_hash_contract_generation(): def test_java_repeated_float16_generation_uses_float16_list(): schema = parse_fdl( - dedent(""" + dedent( + """ package gen; message RepeatedFloat16 { list vals = 1; } - """) + """ + ) ) java_output = render_files(generate_files(schema, JavaGenerator)) assert "import org.apache.fory.collection.Float16List;" in java_output @@ -850,7 +919,8 @@ def test_java_repeated_float16_generation_uses_float16_list(): def test_java_nested_array_values_use_deep_equals_hash_generation(): schema = parse_fdl( - dedent(""" + dedent( + """ package gen; message NestedArrays { @@ -862,7 +932,8 @@ def test_java_nested_array_values_use_deep_equals_hash_generation(): list> groups = 1; map> bytes_by_name = 2; } - """) + """ + ) ) java_output = render_files(generate_files(schema, JavaGenerator)) assert ( @@ -879,7 +950,8 @@ def test_java_nested_array_values_use_deep_equals_hash_generation(): def test_java_unsigned_carriers_and_integer_encoding_annotations(): schema = parse_fdl( - dedent(""" + dedent( + """ package gen; message UnsignedCarriers { @@ -890,7 +962,8 @@ def test_java_unsigned_carriers_and_integer_encoding_annotations(): optional uint32 maybe_u32 = 5; fixed int32 fixed_i32 = 6; } - """) + """ + ) ) java_output = render_files(generate_files(schema, JavaGenerator)) assert "import org.apache.fory.annotation.Nullable;" in java_output @@ -911,7 +984,8 @@ def test_java_unsigned_carriers_and_integer_encoding_annotations(): def test_java_nullable_type_use_annotation_targets_top_level_type(): schema = parse_fdl( - dedent(""" + dedent( + """ package gen; message Money { @@ -923,7 +997,8 @@ def test_java_nullable_type_use_annotation_targets_top_level_type(): optional bytes payload = 2; optional decimal amount = 3; } - """) + """ + ) ) java_output = render_files(generate_files(schema, JavaGenerator)) assert "private @Nullable Money money;" in java_output @@ -933,7 +1008,8 @@ def test_java_nullable_type_use_annotation_targets_top_level_type(): def test_java_evolving_false_generation_uses_struct_evolution_enum(): schema = parse_fdl( - dedent(""" + dedent( + """ package gen; message DefaultEvolving { @@ -947,7 +1023,8 @@ def test_java_evolving_false_generation_uses_struct_evolution_enum(): int32 id = 1; } } - """) + """ + ) ) java_output = render_files(generate_files(schema, JavaGenerator)) assert "import org.apache.fory.annotation.ForyStruct;" in java_output @@ -959,13 +1036,15 @@ def test_java_evolving_false_generation_uses_struct_evolution_enum(): def test_java_nested_integer_annotations_in_generic_containers(): schema = parse_fdl( - dedent(""" + dedent( + """ package gen; message NestedIntegerAnnotations { map> values = 1; } - """) + """ + ) ) java_output = render_files(generate_files(schema, JavaGenerator)) assert ( @@ -983,14 +1062,16 @@ def test_java_nested_integer_annotations_in_generic_containers(): def test_python_nested_integer_schema_aliases_in_generic_containers(): schema = parse_fdl( - dedent(""" + dedent( + """ package gen; message NestedIntegerSchemaAliases { map> signed_values = 1; map> unsigned_values = 2; } - """) + """ + ) ) python_output = render_files(generate_files(schema, PythonGenerator)) assert ( @@ -1005,13 +1086,15 @@ def test_python_nested_integer_schema_aliases_in_generic_containers(): def test_cpp_nested_integer_specs_in_generic_containers(): schema = parse_fdl( - dedent(""" + dedent( + """ package gen; message NestedIntegerSpecs { map> values = 1; } - """) + """ + ) ) cpp_output = render_files(generate_files(schema, CppGenerator)) assert ( @@ -1027,7 +1110,8 @@ def test_cpp_nested_integer_specs_in_generic_containers(): def test_cpp_escapes_keywords_and_generated_helpers(): schema = parse_fdl( - dedent(""" + dedent( + """ package class.demo; message private { @@ -1043,7 +1127,8 @@ class = 0; string visit = 1; string class = 2; } - """) + """ + ) ) cpp_output = render_files(generate_files(schema, CppGenerator)) @@ -1188,16 +1273,19 @@ def test_cpp_imported_type_uses_sanitized_namespace_and_identifier(tmp_path: Pat imported_fdl = tmp_path / "imported.fdl" main_fdl = tmp_path / "main.fdl" imported_fdl.write_text( - dedent(""" + dedent( + """ package class.imported; message private { string value = 1; } - """) + """ + ) ) main_fdl.write_text( - dedent(""" + dedent( + """ package demo; import "imported.fdl"; @@ -1205,7 +1293,8 @@ def test_cpp_imported_type_uses_sanitized_namespace_and_identifier(tmp_path: Pat message Holder { private value = 1; } - """) + """ + ) ) schema = resolve_imports(main_fdl, [tmp_path]) @@ -1218,7 +1307,8 @@ def test_cpp_imported_type_uses_sanitized_namespace_and_identifier(tmp_path: Pat def test_cpp_generator_supports_decimal_fields_and_unions(): schema = parse_fdl( - dedent(""" + dedent( + """ package gen; message Money { @@ -1229,7 +1319,8 @@ def test_cpp_generator_supports_decimal_fields_and_unions(): decimal amount = 1; Money money = 2; } - """) + """ + ) ) cpp_output = render_files(generate_files(schema, CppGenerator)) @@ -1242,7 +1333,8 @@ def test_cpp_generator_supports_decimal_fields_and_unions(): def test_cpp_union_aliases_comma_payload_types(): schema = parse_fdl( - dedent(""" + dedent( + """ package gen; union MapChoice { @@ -1271,7 +1363,8 @@ def test_cpp_union_aliases_comma_payload_types(): date day = 16; timestamp ts = 17; } - """) + """ + ) ) cpp_output = render_files(generate_files(schema, CppGenerator)) @@ -1307,7 +1400,8 @@ def test_cpp_union_aliases_comma_payload_types(): def test_cpp_omits_equality_for_any_types(): schema = parse_fdl( - dedent(""" + dedent( + """ package gen; message Inner { @@ -1371,7 +1465,8 @@ def test_cpp_omits_equality_for_any_types(): string name = 1; int32 code = 2; } - """) + """ + ) ) cpp_output = render_files(generate_files(schema, CppGenerator)) @@ -1393,7 +1488,8 @@ def test_cpp_omits_equality_for_any_types(): def test_cpp_nested_container_ref_uses_correct_pointer_type(): schema = parse_fdl( - dedent(""" + dedent( + """ package gen; message Node { @@ -1406,7 +1502,8 @@ def test_cpp_nested_container_ref_uses_correct_pointer_type(): list> weak_groups = 3; map> weak_nodes = 4; } - """) + """ + ) ) cpp_output = render_files(generate_files(schema, CppGenerator)) @@ -1437,7 +1534,8 @@ def test_cpp_nested_container_ref_uses_correct_pointer_type(): def test_cpp_temporal_map_keys_use_fory_owned_wrappers(): schema = parse_fdl( - dedent(""" + dedent( + """ package demo; message Holder { @@ -1445,7 +1543,8 @@ def test_cpp_temporal_map_keys_use_fory_owned_wrappers(): map timestamps = 2; map dates = 3; } - """) + """ + ) ) cpp_output = render_files(generate_files(schema, CppGenerator)) @@ -1465,14 +1564,16 @@ def test_cpp_temporal_map_keys_use_fory_owned_wrappers(): def test_java_enum_generation_uses_fory_enum_ids(): schema = parse_fdl( - dedent(""" + dedent( + """ package gen; enum Status { UNKNOWN = 4096; OK = 8192; } - """) + """ + ) ) java_output = render_files(generate_files(schema, JavaGenerator)) assert "import org.apache.fory.annotation.ForyEnumId;" in java_output @@ -1488,7 +1589,8 @@ def test_java_enum_generation_uses_fory_enum_ids(): def test_go_bfloat16_generation(): - idl = dedent(""" + idl = dedent( + """ package bfloat16_test; message BFloat16Message { @@ -1496,7 +1598,8 @@ def test_go_bfloat16_generation(): optional bfloat16 opt_val = 2; list list_val = 3; } - """) + """ + ) schema = parse_fdl(idl) files = generate_files(schema, GoGenerator) @@ -1517,14 +1620,16 @@ def test_go_bfloat16_generation(): def test_go_generator_distinguishes_bytes_from_uint8_lists(): schema = parse_fdl( - dedent(""" + dedent( + """ package gen; message ByteSemantics { bytes payload = 1; list values = 2; } - """) + """ + ) ) go_output = render_files(generate_files(schema, GoGenerator)) assert 'Payload []byte `fory:"id=1,type=bytes"`' in go_output @@ -1533,7 +1638,8 @@ def test_go_generator_distinguishes_bytes_from_uint8_lists(): def test_rust_generated_code_uses_absolute_paths(): schema = parse_fdl( - dedent(""" + dedent( + """ package foo; message String { @@ -1547,7 +1653,8 @@ def test_rust_generated_code_uses_absolute_paths(): union Fory { String text = 1; } - """) + """ + ) ) rust_output = render_files(generate_files(schema, RustGenerator)) assert "use fory::" not in rust_output @@ -1581,7 +1688,8 @@ def test_rust_generated_code_uses_absolute_paths(): def test_rust_union_conflicting_payload_uses_self_path(): schema = parse_fdl( - dedent(""" + dedent( + """ package foo; message Dog { @@ -1591,7 +1699,8 @@ def test_rust_union_conflicting_payload_uses_self_path(): union Animal { Dog dog = 1; } - """) + """ + ) ) rust_output = render_files(generate_files(schema, RustGenerator)) @@ -1604,7 +1713,8 @@ def test_rust_union_conflicting_payload_uses_self_path(): def test_rust_escapes_keywords(): schema = parse_fdl( - dedent(""" + dedent( + """ package demo; message type { @@ -1618,7 +1728,8 @@ def test_rust_escapes_keywords(): message _1 { string value = 1; } - """) + """ + ) ) rust_files = generate_files(schema, RustGenerator) rust_output = render_files(rust_files) @@ -1673,22 +1784,26 @@ def test_rust_rejects_same_output_path_collisions( second_fdl = tmp_path / "second.fdl" rust_out = tmp_path / "rust" first_fdl.write_text( - dedent(""" + dedent( + """ package foo.bar; message First { string value = 1; } - """) + """ + ) ) second_fdl.write_text( - dedent(""" + dedent( + """ package foo_bar; message Second { string value = 1; } - """) + """ + ) ) exit_code = foryc_main( [ @@ -1704,3 +1819,129 @@ def test_rust_rejects_same_output_path_collisions( assert exit_code == 1 assert "Rust output path collision" in captured.err + + +def test_java_generated_helper_builder_is_wrapped(): + schema = parse_fdl( + dedent( + """ + package gen; + + message Person { + string first_name = 1; + } + """ + ) + ) + + java_output = render_files(generate_files(schema, JavaGenerator)) + + assert ( + "return Fory.builder()\n" + " .withXlang(true)\n" + " .withCompatible(true)\n" + " .withRefTracking(true)\n" + " .withModule(INSTANCE)\n" + " .buildThreadSafeFory();" in java_output + ) + + +def test_go_generated_helper_options_are_wrapped(): + schema = parse_fdl( + dedent( + """ + package gen; + + message Person { + string first_name = 1; + } + """ + ) + ) + + go_output = render_files(generate_files(schema, GoGenerator)) + + assert ( + "f := fory.New(\n" + "\t\tfory.WithXlang(true),\n" + "\t\tfory.WithRefTracking(true),\n" + "\t\tfory.WithCompatible(true),\n" + "\t)" in go_output + ) + + +def test_python_field_wrapping_is_generated(): + schema = parse_fdl( + dedent( + """ + package gen; + + message Person { + map very_long_metadata_field_name_for_wrapping = 1; + } + """ + ) + ) + + python_output = render_files(generate_files(schema, PythonGenerator)) + + assert ( + " very_long_metadata_field_name_for_wrapping: " + "Dict[str, str] = pyfory.field(\n" + " id=1,\n" + " default_factory=dict,\n" + " )" in python_output + ) + + +def test_cpp_equality_and_struct_macros_are_wrapped(): + schema = parse_fdl( + dedent( + """ + package gen; + + message Person { + string first_name = 1; + string last_name = 2; + string email_address = 3; + list tags = 4; + map metadata = 5; + } + """ + ) + ) + + cpp_output = render_files(generate_files(schema, CppGenerator)) + + assert " return\n" in cpp_output + assert " first_name_ == other.first_name_ &&" in cpp_output + assert " FORY_STRUCT(\n" in cpp_output + assert " Person,\n" in cpp_output + + +def test_rust_map_fields_and_helpers_are_wrapped(): + schema = parse_fdl( + dedent( + """ + package gen; + + message Person { + map metadata = 1; + } + """ + ) + ) + + rust_output = render_files(generate_files(schema, RustGenerator)) + + assert ( + " pub metadata: ::std::collections::HashMap<\n" + " ::std::string::String,\n" + " ::std::string::String,\n" + " >," in rust_output + ) + assert ( + "pub fn register_types(\n" + " fory: &mut ::fory::Fory,\n" + ") -> ::std::result::Result<(), ::fory::Error> {" in rust_output + )