Exemplo n.º 1
0
        public void TestSimpleInheritance()
        {
            var s = new XmSerializerModel();

            s.AddType(typeof(DtoTestClass));
            s.AddType(typeof(BaseDto));
            var dto    = new DtoTestClass();
            var result = BasicTest.TestSerialization(dto, s);

            Assert.AreEqual(dto.GetBooFar(), result.GetBooFar());
            Assert.AreEqual(dto.GetFooBar(), result.GetFooBar());
        }
Exemplo n.º 2
0
        public void TestOverriddedCallbacks()
        {
            var serializer = new XmSerializerModel();

            serializer.AddType(typeof(OverriddenCallbackTestClass1));
            serializer.AddType(typeof(OverriddenCallbackTestClass2));

            var instance = new OverriddenCallbackTestClass2();
            var xml      = serializer.Serialize(instance);
            var result   = serializer.Deserialize <OverriddenCallbackTestClass2>(xml);

            Assert.AreEqual(11, result.FooCounter);
            Assert.AreEqual(11, result.BarCounter);
            Assert.AreEqual(22, result.FooBarCounter);
        }
Exemplo n.º 3
0
        public void TestIdenticalStructs()
        {
            var serializer = new XmSerializerModel();

            serializer.AddType(typeof(StructDto));
            serializer.AddType(typeof(IdenticalStructsDto));

            var dto = new IdenticalStructsDto();

            dto.Struct1.TestString = "foobar";
            dto.Struct2.TestString = "foobar";
            var result = TestSerialization(dto, serializer);

            Assert.AreEqual(result.Struct1.TestString, dto.Struct2.TestString);
        }
Exemplo n.º 4
0
        public void TestEnum()
        {
            var serializer = new XmSerializerModel();

            serializer.AddType(typeof(EnumDto));
            serializer.AddType(typeof(FooBar));

            var dto = new EnumDto {
                EnumField = FooBar.Bar, EnumFieldSt = FooBar.Foo
            };
            var result = TestSerialization(dto, serializer);

            Assert.AreEqual(result.EnumField, dto.EnumField);
            Assert.AreEqual(result.EnumFieldSt, dto.EnumFieldSt);
        }
Exemplo n.º 5
0
 public ValueTupleSerializer(XmSerializerModel serializerModel) : base(serializerModel)
 {
     foreach (var tupleType in TupleTypes)
     {
         serializerModel.AddType(new TypeSerializingSettings(tupleType));
     }
 }
Exemplo n.º 6
0
        public void TestNestedGeneric()
        {
            var s = new XmSerializerModel();

            s.AddType(typeof(GenericTestClass <, ,>));

            var obj = new GenericTestClass <string, int, bool>();

            obj.Field1 = "test";
            obj.Field2 = 5;
            obj.Field3 = true;

            var obj2 = new GenericTestClass <string, GenericTestClass <string, int, bool>, int>
            {
                Field1 = "ff",
                Field2 = obj,
                Field3 = 12
            };

            var result = BasicTest.TestSerialization(obj2, s);

            Assert.AreEqual(result.Field1, obj2.Field1);
            Assert.AreEqual(result.Field2.Field1, obj.Field1);
            Assert.AreEqual(result.Field2.Field2, obj.Field2);
            Assert.AreEqual(result.Field2.Field3, obj.Field3);
            Assert.AreEqual(result.Field3, obj2.Field3);
        }
Exemplo n.º 7
0
        public void TestReducedNumberOfTypeParametersParsing()
        {
            var s = new XmSerializerModel();

            s.AddType(typeof(Foo <, ,>));

            var s2 = new XmSerializerModel();

            s2.AddType(typeof(Foo <,>));

            var s3 = new XmSerializerModel();

            s3.AddType(typeof(Foo));

            var obj = new Foo <string, int, bool>
            {
                Field1 = "foobar",
                Field2 = 42,
                Field3 = true
            };

            var xml    = s.Serialize(obj);
            var result = (Foo <string, int>)s2.Deserialize <object>(xml);

            Assert.AreEqual(result.Field1, obj.Field1);
            Assert.AreEqual(result.Field2, obj.Field2);
            Assert.AreEqual(result.Field3, obj.Field3);

            var result2 = s3.Deserialize <Foo>(xml);

            Assert.AreEqual(result2.Field1, obj.Field1);
            Assert.AreEqual(result2.Field2, obj.Field2);
            Assert.AreEqual(result2.Field3, obj.Field3);
        }
Exemplo n.º 8
0
        public void TestValueTuple()
        {
            var dto = new ValueTupleTestClass
            {
                Foo = ("Foo", new List <string> {
                    "Foo", "Bar"
                }),
                Bar = (1, 2, 3, 4, 5, 6, 7, 8, 9, (10, 11))
            };

            var s = new XmSerializerModel();

            s.AddType(typeof(ValueTupleTestClass));
            var result = BasicTest.TestSerialization(dto, s);

            Assert.AreEqual(dto.Foo.Item1, result.Foo.Item1);
            Assert.AreEqual(dto.Foo.Item2[0], result.Foo.Item2[0]);
            Assert.AreEqual(dto.Foo.Item2[1], result.Foo.Item2[1]);
            Assert.AreEqual(dto.Bar.Item1, result.Bar.Item1);
            Assert.AreEqual(dto.Bar.Item2, result.Bar.Item2);
            Assert.AreEqual(dto.Bar.Item3, result.Bar.Item3);
            Assert.AreEqual(dto.Bar.Item4, result.Bar.Item4);
            Assert.AreEqual(dto.Bar.Item5, result.Bar.Item5);
            Assert.AreEqual(dto.Bar.Item6, result.Bar.Item6);
            Assert.AreEqual(dto.Bar.Item7, result.Bar.Item7);
            Assert.AreEqual(dto.Bar.Item8, result.Bar.Item8);
            Assert.AreEqual(dto.Bar.Item9, result.Bar.Item9);
            Assert.AreEqual(dto.Bar.Item10.Item1, result.Bar.Item10.Item1);
            Assert.AreEqual(dto.Bar.Item10.Item2, result.Bar.Item10.Item2);
        }
Exemplo n.º 9
0
        public void TestTuple()
        {
            var dto = new TupleTestClass
            {
                Foo = new Tuple <string, List <string> >("Foo", new List <string> {
                    "Foo", "Bar"
                }),
                Bar = new Tuple <int, int, int, int, int, int, int, Tuple <int, int> >(1, 2, 3, 4, 5, 6, 7, new Tuple <int, int>(8, 9))
            };

            var s = new XmSerializerModel();

            s.AddType(typeof(TupleTestClass));
            var result = BasicTest.TestSerialization(dto, s);

            Assert.AreEqual(dto.Foo.Item1, result.Foo.Item1);
            Assert.AreEqual(dto.Foo.Item2[0], result.Foo.Item2[0]);
            Assert.AreEqual(dto.Foo.Item2[1], result.Foo.Item2[1]);
            Assert.AreEqual(dto.Bar.Item1, result.Bar.Item1);
            Assert.AreEqual(dto.Bar.Item2, result.Bar.Item2);
            Assert.AreEqual(dto.Bar.Item3, result.Bar.Item3);
            Assert.AreEqual(dto.Bar.Item4, result.Bar.Item4);
            Assert.AreEqual(dto.Bar.Item5, result.Bar.Item5);
            Assert.AreEqual(dto.Bar.Item6, result.Bar.Item6);
            Assert.AreEqual(dto.Bar.Item7, result.Bar.Item7);
            Assert.AreEqual(dto.Bar.Rest.Item1, result.Bar.Rest.Item1);
            Assert.AreEqual(dto.Bar.Rest.Item2, result.Bar.Rest.Item2);
        }
Exemplo n.º 10
0
        public void TestValueConversion()
        {
            var s = new XmSerializerModel();

            s.AddType(typeof(ValueConversionTestClass1));
            var s2 = new XmSerializerModel();

            s2.AddType(typeof(ValueConversionTestClass2));

            var dto = new ValueConversionTestClass1()
            {
                V1 = 5,
                V2 = 6,
                V3 = 7,
                V4 = 8,
                V5 = 9,
                V6 = 10,
                V7 = 11,
            };
            var xml = s.Serialize(dto);

            var result = s2.Deserialize <ValueConversionTestClass2>(xml);

            Assert.AreEqual(result.V1, dto.V1);
            Assert.AreEqual(result.V2, dto.V2);
            Assert.AreEqual(result.V3, dto.V3);
            Assert.AreEqual(result.V4, dto.V4);
            Assert.AreEqual(result.V5, dto.V5);
            Assert.AreEqual(result.V6, dto.V6);
            Assert.AreEqual(result.V7, dto.V7);
        }
Exemplo n.º 11
0
        public void TestDeactivation()
        {
            var s1 = new XmSerializerModel();

            s1.AddType(typeof(DeactivationTestClass1));
            var s2 = new XmSerializerModel();

            s2.AddType(typeof(DeactivationTestClass2));

            var dto1 = new DeactivationTestClass1 {
                Foobar = "foo"
            };
            var dto2 = new DeactivationTestClass2 {
                Foobar = "bar"
            };

            var xml1    = s1.Serialize(dto1);
            var result1 = s2.Deserialize <DeactivationTestClass2>(xml1);
            var result2 = s1.Deserialize <DeactivationTestClass1>(xml1);

            Assert.AreEqual(result1.Foobar, dto1.Foobar);
            Assert.IsNull(result2.Foobar);

            var xml2    = s2.Serialize(dto2);
            var result3 = s2.Deserialize <DeactivationTestClass2>(xml2);
            var result4 = s1.Deserialize <DeactivationTestClass1>(xml2);

            Assert.IsNull(result3.Foobar);
            Assert.IsNull(result4.Foobar);
        }
Exemplo n.º 12
0
        public void TestDefaultValue()
        {
            var s = new XmSerializerModel();

            s.AddType(typeof(CompatibilityTest3));
            var s2 = new XmSerializerModel();

            s2.AddType(typeof(CompatibilityTest2));
            var s3 = new XmSerializerModel();

            s3.AddType(typeof(CompatibilityTest1));

            var dto  = new CompatibilityTest2();
            var xml  = s2.Serialize(dto);
            var dto2 = s.Deserialize <CompatibilityTest3>(xml);

            Assert.IsNotNull(dto2);
            Assert.AreEqual("barfoo", dto2.Foobar);

            var dto3 = new CompatibilityTest1 {
                Foobar = "foobar"
            };
            var xml2 = s3.Serialize(dto3);
            var dto4 = s.Deserialize <CompatibilityTest3>(xml2);

            Assert.AreEqual("foobar", dto4.Foobar);
        }
Exemplo n.º 13
0
 public PrimitivesSerializer(XmSerializerModel serializerModel) : base(serializerModel)
 {
     foreach (var primitive in Primitives)
     {
         serializerModel.AddType(new TypeSerializingSettings(primitive.Key, primitive.Value));
     }
 }
Exemplo n.º 14
0
        public void TestGenericInheritance()
        {
            var s = new XmSerializerModel();

            s.AddType(typeof(GenericInheritanceBaseClass <>));
            s.AddType(typeof(GenericInheritanceDerivedClass <>));
            var dto = new GenericInheritanceDerivedClass <string>()
            {
                Barfoo = "Barfoo",
                Foobar = "Foobar"
            };

            var result = BasicTest.TestSerialization(dto, s);

            Assert.AreEqual(dto.Barfoo, result.Barfoo);
            Assert.AreEqual(dto.Foobar, result.Foobar);
        }
Exemplo n.º 15
0
        public void TestIncludeAncestors()
        {
            var s = new XmSerializerModel();

            s.AddType(typeof(TestClass1));
            s.AddType(typeof(TestClass2));
            s.AddType(typeof(AncestorTestClass));
            var dto1    = new TestClass1();
            var dto2    = new TestClass2();
            var result1 = BasicTest.TestSerialization(dto1, s);
            var result2 = BasicTest.TestSerialization(dto2, s);

            Assert.AreEqual(dto1.GetBooFar(), result1.GetBooFar());
            Assert.AreEqual(dto2.GetBooFar(), result2.GetBooFar());
            Assert.AreEqual(dto1.GetFooBar(), result1.GetFooBar());
            Assert.IsNull(result2.GetFooBar());
        }
Exemplo n.º 16
0
        public void TestInitializeAfterDeserialization()
        {
            var s = new XmSerializerModel();

            s.AddType(typeof(InitializeAfterDeserializationTestClass));
            s.AddType(typeof(InitializeAfterDeserializationTestClass1));
            s.AddType(typeof(InitializeAfterDeserializationTestClass2));
            s.AddType(typeof(InitializeAfterDeserializationTestClass3));
            var obj    = new InitializeAfterDeserializationTestClass();
            var result = BasicTest.TestSerialization(obj, s);

            Assert.IsNotNull(result.foo);
            Assert.IsNotNull(result.bar);

            Assert.ThrowsException <Exception>(() => BasicTest.TestSerialization(new InitializeAfterDeserializationTestClass1(), s));
            Assert.ThrowsException <Exception>(() => BasicTest.TestSerialization(new InitializeAfterDeserializationTestClass2(), s));
            Assert.ThrowsException <Exception>(() => BasicTest.TestSerialization(new InitializeAfterDeserializationTestClass3(), s));
        }
Exemplo n.º 17
0
        public void TestCustomCollection()
        {
            var s = new XmSerializerModel();

            s.AddType(typeof(CustomCollectionTestClass));
            s.AddType(typeof(CustomCollection <>));
            var obj = new CustomCollectionTestClass();

            obj.CollectionField = new CustomCollection <int> {
                125, 132, 164
            };
            var result = BasicTest.TestSerialization(obj, s);

            Assert.AreEqual(3, result.CollectionField.Count);
            Assert.AreEqual(obj.CollectionField[0], result.CollectionField[0]);
            Assert.AreEqual(obj.CollectionField[1], result.CollectionField[1]);
            Assert.AreEqual(obj.CollectionField[2], result.CollectionField[2]);
        }
Exemplo n.º 18
0
        public void TestReference()
        {
            var s = new XmSerializerModel();

            s.AddType(typeof(ReferenceTestClass));
            s.AddType(typeof(ReferencedClass));

            var dto = new ReferenceTestClass {
                RefernceField = new ReferencedClass {
                    FooBar = "foobar"
                }
            };

            dto.RefernceField2 = dto.RefernceField;
            var result = BasicTest.TestSerialization(dto, s);

            Assert.AreEqual(((ReferencedClass)result.RefernceField).FooBar, ((ReferencedClass)result.RefernceField2).FooBar);
            Assert.AreSame(result.RefernceField, result.RefernceField2);
        }
Exemplo n.º 19
0
        public void TestImplicitFieldAttribute()
        {
            var s = new XmSerializerModel();

            s.AddType(typeof(ImplicitFieldsTestClass));
            var obj = new ImplicitFieldsTestClass {
                TestField = "foobar"
            };
            var result = BasicTest.TestSerialization(obj, s);

            Assert.AreEqual(obj.TestField, result.TestField);
        }
Exemplo n.º 20
0
        public void TestDeserializingPriority()
        {
            var serializer = new XmSerializerModel();

            serializer.AddType(typeof(PriorityCallbackTestClass));

            var instance = new PriorityCallbackTestClass();
            var xml      = serializer.Serialize(instance);
            var result   = serializer.Deserialize <PriorityCallbackTestClass>(xml);

            Assert.AreEqual("-1,0,1,2,9", result.DeserializeOrder);
        }
Exemplo n.º 21
0
        public void TestNullable()
        {
            var s = new XmSerializerModel();

            s.AddType(typeof(NullableDto));

            var dto    = new NullableDto();
            var result = BasicTest.TestSerialization(dto, s);

            Assert.AreEqual(result.Foo, dto.Foo);
            Assert.AreEqual(result.Bar, dto.Bar);
        }
Exemplo n.º 22
0
        public void TestStruct()
        {
            var serializer = new XmSerializerModel();

            serializer.AddType(typeof(StructDto));

            var dto = new StructDto {
                TestString = "foobar"
            };
            var result = TestSerialization(dto, serializer);

            Assert.AreEqual(result.TestString, dto.TestString);
        }
Exemplo n.º 23
0
        public void TestDerivedCallbacks()
        {
            var serializer = new XmSerializerModel();

            serializer.AddType(typeof(DerivedCallbackTestClass));
            serializer.AddType(typeof(CallbackTestClass));

            var instance = new DerivedCallbackTestClass();

            instance.DeSerializeTestValue3 = "test";
            var xml    = serializer.Serialize(instance);
            var result = serializer.Deserialize <DerivedCallbackTestClass>(xml);

            Assert.AreEqual("DerivedOnSerialized", instance.SerializeTestValue);
            Assert.AreEqual("DerivedOnSerialized", instance.SerializeTestValue2);
            Assert.AreEqual("DerivedOnSerializing", result.SerializeTestValue);
            Assert.AreEqual("DerivedOnSerializing", result.SerializeTestValue2);
            Assert.AreEqual("test", result.DeSerializeTestValue3);
            Assert.IsNull(result.DeSerializeTestValue4);
            Assert.AreEqual("DerivedOnDeserialized", result.DeSerializeTestValue);
            Assert.AreEqual("DerivedOnDeserialized", result.DeSerializeTestValue2);
        }
Exemplo n.º 24
0
        public void TestSkipConstructor()
        {
            var s = new XmSerializerModel();

            s.AddType(typeof(ConstructorTestClass2));

            var obj    = new ConstructorTestClass2();
            var xml    = s.Serialize(obj);
            var result = s.Deserialize <ConstructorTestClass2>(xml);

            Assert.IsNull(result.TestField);
            Assert.IsNull(result.TestField2);
        }
Exemplo n.º 25
0
        public void TestConstructor()
        {
            var s = new XmSerializerModel();

            s.AddType(typeof(ConstructorTestClass));

            var obj    = new ConstructorTestClass();
            var xml    = s.Serialize(obj);
            var result = s.Deserialize <ConstructorTestClass>(xml);

            Assert.AreEqual(obj.TestField, result.TestField);
            Assert.AreEqual(obj.TestField2, result.TestField2);
        }
Exemplo n.º 26
0
        public void TestNotSerialized()
        {
            var s = new XmSerializerModel();

            s.AddType(typeof(NotSerializedTestClass));
            var obj = new NotSerializedTestClass {
                TestField = "foobar", NonSerializedTestField = "foobar"
            };
            var result = BasicTest.TestSerialization(obj, s);

            Assert.AreEqual(obj.TestField, result.TestField);
            Assert.IsNull(result.NonSerializedTestField);
        }
Exemplo n.º 27
0
        public void TestPrivateFields()
        {
            var s = new XmSerializerModel();

            s.AddType(typeof(MemberFilterDto1));
            var dto    = new MemberFilterDto1();
            var result = BasicTest.TestSerialization(dto, s);

            Assert.AreEqual("foobar", result.Gett2());
            Assert.AreEqual("foobar", result.t3);
            Assert.AreEqual("foobar", result.Gett4());
            Assert.IsNull(result.t1);
        }
Exemplo n.º 28
0
        public void TestNested()
        {
            var serializer = new XmSerializerModel();

            serializer.AddType(typeof(PrimitivesDto));
            var dto = new PrimitivesDto {
                SubObject = new PrimitivesDto {
                    TestString = "dfsads"
                }
            };
            var result = TestSerialization(dto, serializer);

            Assert.AreEqual(result.SubObject.TestString, dto.SubObject.TestString);
        }
Exemplo n.º 29
0
        public void TestSelfReference()
        {
            var s = new XmSerializerModel();

            s.AddType(typeof(ReferenceTestClass));

            var dto = new ReferenceTestClass();

            dto.RefernceField  = dto;
            dto.RefernceField2 = dto;
            var result = BasicTest.TestSerialization(dto, s);

            Assert.AreSame(result, result.RefernceField);
        }
Exemplo n.º 30
0
        public void TestBackwardCompatibility()
        {
            var s = new XmSerializerModel();

            s.AddType(typeof(CompatibilityTest1));
            var s2 = new XmSerializerModel();

            s2.AddType(typeof(CompatibilityTest2));

            var dto  = new CompatibilityTest2();
            var xml  = s2.Serialize(dto);
            var dto2 = s.Deserialize <CompatibilityTest1>(xml);

            Assert.IsNotNull(dto2);
        }