static void Main(string[] args) { PrimitiveUnit.UnitTest(); PrimitiveUnit.UnitTest2(); PrimitiveUnit.UnitTestAsync().Wait(); PrimitiveUnit.UnitTest2Async().Wait(); Console.WriteLine($"{DateTime.Now} start generating serialization code for assembly"); //This is not necessary but can accelerate following serializing Serializer.PrepareForAssembly(typeof(NestedUnit).Assembly); Serializer2.PrepareForAssembly(typeof(NestedUnit).Assembly); Console.WriteLine($"{DateTime.Now} end generating serialization code for assembly"); OptionalFieldUnit.UnitTest(); OptionalFieldUnit.UnitTest2(); OptionalFieldUnit.UnitTestAsync().Wait(); OptionalFieldUnit.UnitTest2Async().Wait(); CollectionUnit.UnitTest(); CollectionUnit.UnitTest2(); CollectionUnit.UnitTestAsync().Wait(); CollectionUnit.UnitTest2Async().Wait(); RefUnit.UnitTest(); PerformanceTest(); Console.WriteLine($"{DateTime.Now} Unit test passed"); }
private static async Task UnitTestAsync <TSerializer>( TSerializer serializer, MemoryStream mem) where TSerializer : ISerializer { Console.WriteLine($"{DateTime.Now} {typeof(TSerializer).Name} async: start testing optional/ignored"); var item1 = new OptionalFieldUnit(); var excepted = false; try { await serializer.SerializeAsync(item1); } catch (Exception) { excepted = true; } Debug.Assert(excepted); //NonNull field/property must be assigned with an object item1.RequiredField = "hello"; item1.FullAccessibleProperty = "property"; mem.Seek(0, SeekOrigin.Begin); await serializer.SerializeAsync(item1); mem.Seek(0, SeekOrigin.Begin); var item2 = await serializer.DeserializeAsync <OptionalFieldUnit>(); Debug.Assert(item1.RequiredField == item2.RequiredField); Debug.Assert(item1.FullAccessibleProperty == item2.FullAccessibleProperty); Debug.Assert(item2.OptionalField == null); Debug.Assert(item2.IgnoredField == null); Debug.Assert(item2.PrivateField == null); Debug.Assert(item2.ReadOnlyProperty == null); item1.OptionalField = "now"; item1.IgnoredField = "Ignored"; item1.PrivateField = "Private"; mem.Seek(0, SeekOrigin.Begin); await serializer.SerializeAsync(item1); mem.Seek(0, SeekOrigin.Begin); item2 = await serializer.DeserializeAsync <OptionalFieldUnit>(); Debug.Assert(item1.OptionalField == item2.OptionalField); Debug.Assert(item2.IgnoredField == null); Debug.Assert(item2.PrivateField == null); Debug.Assert(item2.ReadOnlyProperty == null); Console.WriteLine($"{DateTime.Now} {typeof(TSerializer).Name} async: end testing optional/ignored"); }
private static void UnitTest <TSerializer>( TSerializer serializer, MemoryStream mem) where TSerializer : ISerializer { Console.WriteLine($"{DateTime.Now} {typeof(TSerializer).Name}: start testing optional/ignored"); var item1 = new OptionalFieldUnit(); var excepted = false; try { serializer.Serialize(item1); } catch (TargetInvocationException ex) { excepted = ex.InnerException is ArgumentNullException; } Debug.Assert(excepted); //required field/property must NOT be null item1.RequiredField = "hello"; item1.FullAccessibleProperty = "property"; mem.Seek(0, SeekOrigin.Begin); serializer.Serialize(item1); mem.Seek(0, SeekOrigin.Begin); var item2 = serializer.Deserialize <OptionalFieldUnit>(); Debug.Assert(item1.RequiredField == item2.RequiredField); Debug.Assert(item1.FullAccessibleProperty == item2.FullAccessibleProperty); Debug.Assert(item2.OptionalField == null); //optional field/property can be null Debug.Assert(item2.IgnoredField == null); //ignored field/property will not be serialized Debug.Assert(item2.PrivateField == null); //private field/property will not be serialized Debug.Assert(item2.ReadOnlyProperty == null); //readonly field/property will not be serialized item1.OptionalField = "now"; item1.IgnoredField = "Ignored"; item1.PrivateField = "Private"; mem.Seek(0, SeekOrigin.Begin); serializer.Serialize(item1); mem.Seek(0, SeekOrigin.Begin); item2 = serializer.Deserialize <OptionalFieldUnit>(); Debug.Assert(item1.OptionalField == item2.OptionalField); //optional field/property can be none null Debug.Assert(item2.IgnoredField == null); Debug.Assert(item2.PrivateField == null); Debug.Assert(item2.ReadOnlyProperty == null); Console.WriteLine($"{DateTime.Now} {typeof(TSerializer).Name}: end testing optional/ignored"); }