@@ -370,3 +370,362 @@ impl ser::SerializeTupleVariant for VariantSeqSerializer {
370370 Ok ( Yaml :: Hash ( map) )
371371 }
372372}
373+
374+ #[ cfg( test) ]
375+ mod tests {
376+ use super :: * ;
377+ use serde:: ser:: Error as SerdeSerError ;
378+ use serde:: { Serialize , Serializer } ;
379+ use std:: collections:: BTreeMap ;
380+ use yaml_rust2:: yaml:: { Hash , Yaml } ;
381+
382+ fn assert_yaml_serialization < T : Serialize > ( value : T , expected_yaml : Yaml ) {
383+ let result = YamlSerializer :: serialize ( & value) ;
384+ println ! (
385+ "Serialized value: {:?}, Expected value: {:?}" ,
386+ result, expected_yaml
387+ ) ;
388+
389+ assert ! (
390+ result. is_ok( ) ,
391+ "Serialization failed when it should have succeeded. Error: {:?}" ,
392+ result. err( )
393+ ) ;
394+ assert_eq ! (
395+ result. unwrap( ) ,
396+ expected_yaml,
397+ "Serialized YAML did not match expected YAML."
398+ ) ;
399+ }
400+
401+ #[ test]
402+ fn test_serialize_bool ( ) {
403+ assert_yaml_serialization ( true , Yaml :: Boolean ( true ) ) ;
404+ assert_yaml_serialization ( false , Yaml :: Boolean ( false ) ) ;
405+ }
406+
407+ #[ test]
408+ fn test_serialize_integers ( ) {
409+ assert_yaml_serialization ( 42i8 , Yaml :: Integer ( 42 ) ) ;
410+ assert_yaml_serialization ( -100i16 , Yaml :: Integer ( -100 ) ) ;
411+ assert_yaml_serialization ( 123456i32 , Yaml :: Integer ( 123456 ) ) ;
412+ assert_yaml_serialization ( 7890123456789i64 , Yaml :: Integer ( 7890123456789 ) ) ;
413+ assert_yaml_serialization ( 255u8 , Yaml :: Integer ( 255 ) ) ;
414+ assert_yaml_serialization ( 65535u16 , Yaml :: Integer ( 65535 ) ) ;
415+ assert_yaml_serialization ( 4000000000u32 , Yaml :: Integer ( 4000000000 ) ) ;
416+ // u64 is serialized as Yaml::Real(String) in your implementation
417+ assert_yaml_serialization (
418+ 18446744073709551615u64 ,
419+ Yaml :: Real ( "18446744073709551615" . to_string ( ) ) ,
420+ ) ;
421+ }
422+
423+ #[ test]
424+ fn test_serialize_floats ( ) {
425+ assert_yaml_serialization ( 3.14f32 , Yaml :: Real ( "3.14" . to_string ( ) ) ) ;
426+ assert_yaml_serialization ( -0.001f64 , Yaml :: Real ( "-0.001" . to_string ( ) ) ) ;
427+ assert_yaml_serialization ( 1.0e10f64 , Yaml :: Real ( "10000000000" . to_string ( ) ) ) ;
428+ }
429+
430+ #[ test]
431+ fn test_serialize_char ( ) {
432+ assert_yaml_serialization ( 'X' , Yaml :: String ( "X" . to_string ( ) ) ) ;
433+ assert_yaml_serialization ( '✨' , Yaml :: String ( "✨" . to_string ( ) ) ) ;
434+ }
435+
436+ #[ test]
437+ fn test_serialize_str_and_string ( ) {
438+ assert_yaml_serialization ( "hello YAML" , Yaml :: String ( "hello YAML" . to_string ( ) ) ) ;
439+ assert_yaml_serialization ( "" . to_string ( ) , Yaml :: String ( "" . to_string ( ) ) ) ;
440+ }
441+
442+ #[ test]
443+ fn test_serialize_raw_bytes ( ) {
444+ let bytes_slice: & [ u8 ] = & [ 0x48 , 0x65 , 0x6c , 0x6c , 0x6f ] ; // "Hello"
445+ let expected = Yaml :: Array ( vec ! [
446+ Yaml :: Integer ( 72 ) ,
447+ Yaml :: Integer ( 101 ) ,
448+ Yaml :: Integer ( 108 ) ,
449+ Yaml :: Integer ( 108 ) ,
450+ Yaml :: Integer ( 111 ) ,
451+ ] ) ;
452+ assert_yaml_serialization ( bytes_slice, expected. clone ( ) ) ;
453+
454+ let bytes_vec: Vec < u8 > = bytes_slice. to_vec ( ) ;
455+ assert_yaml_serialization ( bytes_vec, expected) ;
456+
457+ let empty_bytes_slice: & [ u8 ] = & [ ] ;
458+ assert_yaml_serialization ( empty_bytes_slice, Yaml :: Array ( vec ! [ ] ) ) ;
459+ }
460+
461+ struct MyBytesWrapper < ' a > ( & ' a [ u8 ] ) ;
462+
463+ impl < ' a > Serialize for MyBytesWrapper < ' a > {
464+ fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
465+ where
466+ S : Serializer ,
467+ {
468+ serializer. serialize_bytes ( self . 0 )
469+ }
470+ }
471+
472+ #[ test]
473+ fn test_custom_wrapper_serializes_bytes_as_base64_string ( ) {
474+ let data: & [ u8 ] = & [ 72 , 101 , 108 , 108 , 111 ] ; // "Hello"
475+ let wrapped_data = MyBytesWrapper ( data) ;
476+
477+ let base64_encoded = BASE64_STANDARD . encode ( data) ;
478+ let expected_yaml = Yaml :: String ( base64_encoded) ;
479+
480+ assert_yaml_serialization ( wrapped_data, expected_yaml) ;
481+
482+ let empty_data: & [ u8 ] = & [ ] ;
483+ let wrapped_empty_data = MyBytesWrapper ( empty_data) ;
484+ let empty_base64_encoded = BASE64_STANDARD . encode ( empty_data) ;
485+ let expected_empty_yaml = Yaml :: String ( empty_base64_encoded) ;
486+ assert_yaml_serialization ( wrapped_empty_data, expected_empty_yaml) ;
487+ }
488+
489+ #[ test]
490+ fn test_serialize_option ( ) {
491+ let val_none: Option < i32 > = None ;
492+ assert_yaml_serialization ( val_none, Yaml :: Null ) ;
493+
494+ let val_some: Option < String > = Some ( "has value" . to_string ( ) ) ;
495+ assert_yaml_serialization ( val_some, Yaml :: String ( "has value" . to_string ( ) ) ) ;
496+ }
497+
498+ #[ test]
499+ fn test_serialize_unit ( ) {
500+ assert_yaml_serialization ( ( ) , Yaml :: Hash ( Hash :: new ( ) ) ) ;
501+ }
502+
503+ #[ test]
504+ fn test_serialize_unit_struct ( ) {
505+ #[ derive( Serialize ) ]
506+ struct MyUnitStruct ;
507+
508+ assert_yaml_serialization ( MyUnitStruct , Yaml :: Hash ( Hash :: new ( ) ) ) ;
509+ }
510+
511+ #[ test]
512+ fn test_serialize_newtype_struct ( ) {
513+ #[ derive( Serialize ) ]
514+ struct MyNewtypeStruct ( u64 ) ;
515+
516+ assert_yaml_serialization ( MyNewtypeStruct ( 12345u64 ) , Yaml :: Real ( "12345" . to_string ( ) ) ) ;
517+ }
518+
519+ #[ test]
520+ fn test_serialize_seq ( ) {
521+ let empty_vec: Vec < i32 > = vec ! [ ] ;
522+ assert_yaml_serialization ( empty_vec, Yaml :: Array ( vec ! [ ] ) ) ;
523+
524+ let simple_vec = vec ! [ 10 , 20 , 30 ] ;
525+ assert_yaml_serialization (
526+ simple_vec,
527+ Yaml :: Array ( vec ! [
528+ Yaml :: Integer ( 10 ) ,
529+ Yaml :: Integer ( 20 ) ,
530+ Yaml :: Integer ( 30 ) ,
531+ ] ) ,
532+ ) ;
533+
534+ let string_vec = vec ! [ "a" . to_string( ) , "b" . to_string( ) ] ;
535+ assert_yaml_serialization (
536+ string_vec,
537+ Yaml :: Array ( vec ! [
538+ Yaml :: String ( "a" . to_string( ) ) ,
539+ Yaml :: String ( "b" . to_string( ) ) ,
540+ ] ) ,
541+ ) ;
542+ }
543+
544+ #[ test]
545+ fn test_serialize_tuple ( ) {
546+ let tuple_val = ( 42i32 , "text" , false ) ;
547+ assert_yaml_serialization (
548+ tuple_val,
549+ Yaml :: Array ( vec ! [
550+ Yaml :: Integer ( 42 ) ,
551+ Yaml :: String ( "text" . to_string( ) ) ,
552+ Yaml :: Boolean ( false ) ,
553+ ] ) ,
554+ ) ;
555+ }
556+
557+ #[ test]
558+ fn test_serialize_tuple_struct ( ) {
559+ #[ derive( Serialize ) ]
560+ struct MyTupleStruct ( String , i64 ) ;
561+
562+ assert_yaml_serialization (
563+ MyTupleStruct ( "value" . to_string ( ) , -500 ) ,
564+ Yaml :: Array ( vec ! [ Yaml :: String ( "value" . to_string( ) ) , Yaml :: Integer ( -500 ) ] ) ,
565+ ) ;
566+ }
567+
568+ #[ test]
569+ fn test_serialize_map ( ) {
570+ let mut map = BTreeMap :: new ( ) ; // BTreeMap for ordered keys, matching yaml::Hash
571+ map. insert ( "key1" . to_string ( ) , 100 ) ;
572+ map. insert ( "key2" . to_string ( ) , 200 ) ;
573+
574+ let mut expected_hash = Hash :: new ( ) ;
575+ expected_hash. insert ( Yaml :: String ( "key1" . to_string ( ) ) , Yaml :: Integer ( 100 ) ) ;
576+ expected_hash. insert ( Yaml :: String ( "key2" . to_string ( ) ) , Yaml :: Integer ( 200 ) ) ;
577+ assert_yaml_serialization ( map, Yaml :: Hash ( expected_hash) ) ;
578+
579+ let empty_map: BTreeMap < String , i32 > = BTreeMap :: new ( ) ;
580+ assert_yaml_serialization ( empty_map, Yaml :: Hash ( Hash :: new ( ) ) ) ;
581+ }
582+
583+ #[ derive( Serialize ) ]
584+ struct SimpleStruct {
585+ id : u32 ,
586+ name : String ,
587+ is_active : bool ,
588+ }
589+
590+ #[ test]
591+ fn test_serialize_struct ( ) {
592+ let s = SimpleStruct {
593+ id : 101 ,
594+ name : "A Struct" . to_string ( ) ,
595+ is_active : true ,
596+ } ;
597+ let mut expected_hash = Hash :: new ( ) ;
598+ expected_hash. insert ( Yaml :: String ( "id" . to_string ( ) ) , Yaml :: Integer ( 101 ) ) ;
599+ expected_hash. insert (
600+ Yaml :: String ( "name" . to_string ( ) ) ,
601+ Yaml :: String ( "A Struct" . to_string ( ) ) ,
602+ ) ;
603+ expected_hash. insert ( Yaml :: String ( "is_active" . to_string ( ) ) , Yaml :: Boolean ( true ) ) ;
604+ assert_yaml_serialization ( s, Yaml :: Hash ( expected_hash) ) ;
605+ }
606+
607+ #[ derive( Serialize ) ]
608+ struct NestedStruct {
609+ description : String ,
610+ data : SimpleStruct ,
611+ tags : Vec < String > ,
612+ }
613+
614+ #[ test]
615+ fn test_serialize_nested_struct ( ) {
616+ let ns = NestedStruct {
617+ description : "Contains another struct and a vec" . to_string ( ) ,
618+ data : SimpleStruct {
619+ id : 202 ,
620+ name : "Inner" . to_string ( ) ,
621+ is_active : false ,
622+ } ,
623+ tags : vec ! [ "nested" . to_string( ) , "complex" . to_string( ) ] ,
624+ } ;
625+
626+ let mut inner_struct_hash = Hash :: new ( ) ;
627+ inner_struct_hash. insert ( Yaml :: String ( "id" . to_string ( ) ) , Yaml :: Integer ( 202 ) ) ;
628+ inner_struct_hash. insert (
629+ Yaml :: String ( "name" . to_string ( ) ) ,
630+ Yaml :: String ( "Inner" . to_string ( ) ) ,
631+ ) ;
632+ inner_struct_hash. insert ( Yaml :: String ( "is_active" . to_string ( ) ) , Yaml :: Boolean ( false ) ) ;
633+
634+ let tags_array = Yaml :: Array ( vec ! [
635+ Yaml :: String ( "nested" . to_string( ) ) ,
636+ Yaml :: String ( "complex" . to_string( ) ) ,
637+ ] ) ;
638+
639+ let mut expected_hash = Hash :: new ( ) ;
640+ expected_hash. insert (
641+ Yaml :: String ( "description" . to_string ( ) ) ,
642+ Yaml :: String ( "Contains another struct and a vec" . to_string ( ) ) ,
643+ ) ;
644+ expected_hash. insert (
645+ Yaml :: String ( "data" . to_string ( ) ) ,
646+ Yaml :: Hash ( inner_struct_hash) ,
647+ ) ;
648+ expected_hash. insert ( Yaml :: String ( "tags" . to_string ( ) ) , tags_array) ;
649+
650+ assert_yaml_serialization ( ns, Yaml :: Hash ( expected_hash) ) ;
651+ }
652+
653+ #[ derive( Serialize ) ]
654+ enum MyEnum {
655+ Unit ,
656+ Newtype ( i32 ) ,
657+ Tuple ( String , bool ) ,
658+ Struct { field_a : u16 , field_b : char } ,
659+ }
660+
661+ #[ test]
662+ fn test_serialize_enum_unit_variant ( ) {
663+ assert_yaml_serialization ( MyEnum :: Unit , Yaml :: String ( "Unit" . to_string ( ) ) ) ;
664+ }
665+
666+ #[ test]
667+ fn test_serialize_enum_newtype_variant ( ) {
668+ let mut expected_hash = Hash :: new ( ) ;
669+ expected_hash. insert ( Yaml :: String ( "Newtype" . to_string ( ) ) , Yaml :: Integer ( 999 ) ) ;
670+ assert_yaml_serialization ( MyEnum :: Newtype ( 999 ) , Yaml :: Hash ( expected_hash) ) ;
671+ }
672+
673+ #[ test]
674+ fn test_serialize_enum_tuple_variant ( ) {
675+ let mut expected_hash = Hash :: new ( ) ;
676+ let inner_array = Yaml :: Array ( vec ! [
677+ Yaml :: String ( "tuple_data" . to_string( ) ) ,
678+ Yaml :: Boolean ( true ) ,
679+ ] ) ;
680+ expected_hash. insert ( Yaml :: String ( "Tuple" . to_string ( ) ) , inner_array) ;
681+ assert_yaml_serialization (
682+ MyEnum :: Tuple ( "tuple_data" . to_string ( ) , true ) ,
683+ Yaml :: Hash ( expected_hash) ,
684+ ) ;
685+ }
686+
687+ #[ test]
688+ fn test_serialize_enum_struct_variant ( ) {
689+ let mut inner_struct_hash = Hash :: new ( ) ;
690+ inner_struct_hash. insert ( Yaml :: String ( "field_a" . to_string ( ) ) , Yaml :: Integer ( 123 ) ) ;
691+ inner_struct_hash. insert (
692+ Yaml :: String ( "field_b" . to_string ( ) ) ,
693+ Yaml :: String ( "Z" . to_string ( ) ) ,
694+ ) ;
695+
696+ let mut expected_hash = Hash :: new ( ) ;
697+ expected_hash. insert (
698+ Yaml :: String ( "Struct" . to_string ( ) ) ,
699+ Yaml :: Hash ( inner_struct_hash) ,
700+ ) ;
701+ assert_yaml_serialization (
702+ MyEnum :: Struct {
703+ field_a : 123 ,
704+ field_b : 'Z' ,
705+ } ,
706+ Yaml :: Hash ( expected_hash) ,
707+ ) ;
708+ }
709+
710+ #[ test]
711+ fn test_yaml_serializer_error_display ( ) {
712+ let error = YamlSerializerError {
713+ msg : "A test error message" . to_string ( ) ,
714+ } ;
715+ assert_eq ! (
716+ format!( "{}" , error) ,
717+ "YamlSerializerError: A test error message"
718+ ) ;
719+ }
720+
721+ #[ test]
722+ fn test_yaml_serializer_error_custom ( ) {
723+ let error = YamlSerializerError :: custom ( "Custom error detail" ) ;
724+ assert_eq ! ( error. msg, "Custom error detail" ) ;
725+ assert_eq ! (
726+ format!( "{}" , error) ,
727+ "YamlSerializerError: Custom error detail"
728+ ) ;
729+ let _err_trait_obj: Box < dyn std:: error:: Error > = Box :: new ( error) ;
730+ }
731+ }
0 commit comments