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

Function test_polymorphic_map

rust/tests/tests/test_cross_language.rs:1226–1281  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1224#[test]
1225#[ignore]
1226fn test_polymorphic_map() {
1227 let data_file_path = get_data_file();
1228 let bytes = fs::read(&data_file_path).unwrap();
1229
1230 let mut fory = Fory::builder().compatible(true).xlang(true).build();
1231 fory.register::<Dog>(302).unwrap();
1232 fory.register::<Cat>(303).unwrap();
1233 fory.register::<AnimalMapHolder>(305).unwrap();
1234 let mut reader = Reader::new(bytes.as_slice());
1235
1236 // Part 1: Read Map<String, Animal> with polymorphic values
1237 let animal_map: HashMap<String, Box<dyn Any>> = fory.deserialize_from(&mut reader).unwrap();
1238 assert_eq!(animal_map.len(), 2);
1239
1240 let dog1 = animal_map
1241 .get("dog1")
1242 .expect("dog1 should exist")
1243 .downcast_ref::<Dog>()
1244 .expect("dog1 should be Dog");
1245 assert_eq!(dog1.name, Some("Rex".to_string()));
1246 assert_eq!(dog1.age, 2);
1247
1248 let cat1 = animal_map
1249 .get("cat1")
1250 .expect("cat1 should exist")
1251 .downcast_ref::<Cat>()
1252 .expect("cat1 should be Cat");
1253 assert_eq!(cat1.lives, 9);
1254 assert_eq!(cat1.age, 4);
1255
1256 // Part 2: Read AnimalMapHolder (Map<String, Animal> as struct field)
1257 let holder: AnimalMapHolder = fory.deserialize_from(&mut reader).unwrap();
1258 assert_eq!(holder.animal_map.len(), 2);
1259
1260 let my_dog = holder
1261 .animal_map
1262 .get("myDog")
1263 .expect("myDog should exist")
1264 .downcast_ref::<Dog>()
1265 .expect("myDog should be Dog");
1266 assert_eq!(my_dog.name, Some("Fido".to_string()));
1267
1268 let my_cat = holder
1269 .animal_map
1270 .get("myCat")
1271 .expect("myCat should exist")
1272 .downcast_ref::<Cat>()
1273 .expect("myCat should be Cat");
1274 assert_eq!(my_cat.lives, 8);
1275
1276 // Write back
1277 let mut buf = Vec::new();
1278 fory.serialize_to(&mut buf, &animal_map).unwrap();
1279 fory.serialize_to(&mut buf, &holder).unwrap();
1280 fs::write(&data_file_path, buf).unwrap();
1281}
1282
1283// ============================================================================

Callers

nothing calls this directly

Calls 10

deserialize_fromMethod · 0.80
get_data_fileFunction · 0.70
getMethod · 0.65
readFunction · 0.50
writeFunction · 0.50
unwrapMethod · 0.45
buildMethod · 0.45
xlangMethod · 0.45
compatibleMethod · 0.45
serialize_toMethod · 0.45

Tested by

no test coverage detected