MCPcopy Create free account
hub / github.com/apache/fory / union_compatible_enum_xlang_format

Function union_compatible_enum_xlang_format

rust/tests/tests/test_enum.rs:182–245  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

180/// which should be compatible with Java's Union: index + xwriteRef(value)
181#[test]
182fn union_compatible_enum_xlang_format() {
183 use fory_core::serializer::{ForyDefault, Serializer};
184 use fory_core::type_id::TypeId;
185
186 // Define a Union-compatible enum (each variant has exactly one field)
187 #[allow(dead_code)]
188 #[derive(ForyUnion, Debug, PartialEq, Clone)]
189 enum StringOrLong {
190 #[fory(unknown)]
191 Unknown(fory_core::UnknownCase),
192 #[fory(id = 0, default)]
193 Text(String),
194 #[fory(id = 1)]
195 Number(i64),
196 }
197
198 #[allow(dead_code)]
199 #[derive(ForyUnion, Debug, PartialEq, Clone)]
200 enum ForwardStringOrLong {
201 #[fory(unknown)]
202 Unknown(fory_core::UnknownCase),
203 #[fory(id = 0, default)]
204 Text(String),
205 #[fory(id = 1)]
206 Number(i64),
207 }
208
209 // Verify it's recognized as UNION TypeId
210 assert_eq!(
211 StringOrLong::fory_static_type_id(),
212 TypeId::UNION,
213 "Union-compatible enum should have UNION TypeId"
214 );
215 assert_eq!(
216 ForwardStringOrLong::fory_default(),
217 ForwardStringOrLong::Text(String::new())
218 );
219 // Struct containing the Union-compatible enum
220 #[derive(ForyStruct, Debug, PartialEq)]
221 struct StructWithUnion {
222 union_field: StringOrLong,
223 }
224
225 // Test xlang mode serialization
226 let mut fory = Fory::builder().xlang(true).compatible(false).build();
227 fory.register::<StringOrLong>(300).unwrap();
228 fory.register::<StructWithUnion>(301).unwrap();
229
230 // Test with String variant (index 0)
231 let obj1 = StructWithUnion {
232 union_field: StringOrLong::Text("hello".to_string()),
233 };
234 let bin1 = fory.serialize(&obj1).unwrap();
235 let result1: StructWithUnion = fory.deserialize(&bin1).unwrap();
236 assert_eq!(obj1, result1);
237
238 // Test with Long variant (index 1)
239 let obj2 = StructWithUnion {

Callers

nothing calls this directly

Calls 9

TextClass · 0.85
NumberClass · 0.85
serializeMethod · 0.65
deserializeMethod · 0.65
buildMethod · 0.45
compatibleMethod · 0.45
xlangMethod · 0.45
unwrapMethod · 0.45
to_stringMethod · 0.45

Tested by

no test coverage detected