sharplib/tests/Tests.cs

126 lines
2.9 KiB
C#

using System.Diagnostics;
using System.IO;
namespace test;
public record class SimpleImmutable( string Name, int Age ) : imm.Timed<SimpleImmutable>;
static public class XmlFormatter2
{
public class ClassWithProperties
{
public string ActualProperty { get; set; } = "test_ActualProperty_set_inline";
public string ActualProperty_NotSerialized { get; set; } = "ActualProperty_NotSerialized";
}
[ser.Ser( Types = ser.Types.Implied )]
public partial class ClassContainsClassWithProp
{
[ser.Do]
public bool doBool = true;
[ser.ChildPropsAttribute( "ActualProperty" )]
public ClassWithProperties propHolder = new();
public string doNotSerialize = "test_do_not_serialize";
}
[ser.Ser]
public class ClassHasFields
{
public ClassContainsClassWithProp prop = new();
};
public static void Serialization()
{
lib.XmlFormatter2Cfg cfg = new()
{
};
ClassHasFields classHasFields = new()
{
prop = new()
{
propHolder = new()
{
ActualProperty = "ActualProperty_set_in_cons"
}
}
};
Debug.Assert( classHasFields.prop.propHolder.ActualProperty == "ActualProperty_set_in_cons" );
var memStream = new MemoryStream();
{
var xml = new lib.XmlFormatter2 ( cfg );
xml.Serialize( memStream, classHasFields );
}
memStream.Position = 0;
var strXml = System.Text.Encoding.UTF8.GetString( memStream.ToArray() );
var badXml = "<root>\n <prop doBool=\"True\" />\n</root>";
/*
<root _.t="test.XmlFormatter2+ClassHasFields" _.version.="2">
<prop doBool="True">
<propHolder ActualProperty="ActualProperty_set_in_cons" ActualProperty_NotSerialized="ActualProperty_NotSerialized" ActualProperty="ActualProperty_set_in_cons" />
</prop>
</root>*/
Debug.Assert( strXml != badXml );
memStream.Position = 0;
var classHasFields2 = new ClassHasFields();
classHasFields2.prop.propHolder.ActualProperty_NotSerialized = "ActualProperty_NotSerialized_set_in_test_01";
Debug.Assert( classHasFields2.prop.propHolder.ActualProperty == "test_ActualProperty_set_inline" );
Debug.Assert( classHasFields2.prop.propHolder.ActualProperty_NotSerialized == "ActualProperty_NotSerialized" );
{
var xml = new lib.XmlFormatter2 ( cfg );
classHasFields2 = xml.Deserialize<ClassHasFields>( memStream );
}
Debug.Assert( classHasFields2.prop.propHolder.ActualProperty == "ActualProperty_set_in_cons" );
Debug.Assert( classHasFields2.prop.propHolder.ActualProperty_NotSerialized == "ActualProperty_NotSerialized_set_in_test_01" );
memStream.Position = 0;
var classHasFields3 = new ClassHasFields();
{
var xml = new lib.XmlFormatter2 ( cfg );
xml.DeserializeInto( memStream, classHasFields3 );
}
Debug.Assert( classHasFields3.prop.propHolder.ActualProperty == "ActualProperty_set_in_cons" );
Debug.Assert( classHasFields3.prop.propHolder.ActualProperty_NotSerialized == "ActualProperty_NotSerialized_set_in_test_01" );
}
}