Beispiel #1
0
        public void TestNullableType()
        {
            int?x = null;
            var s = new CSerializer();

            CSerializationContext.Global.SetVerbose();

            var doc = s.Serialize(x);

            Print(doc);
            var elem = doc.DocumentElement;

            Assert.AreEqual(CSerializationContext.Global.NullAttributeValue,
                            XmlExtensions.GetAttributeValue(elem, CSerializationContext.Global.NullAttributeName),
                            "Should be null");
            Assert.AreEqual("", elem.InnerText, "Should be no innerText");

            PrintLine();

            x   = 69;
            doc = s.Serialize(x);
            Print(doc);
            elem = doc.DocumentElement;
            Assert.AreEqual(x.GetType().AssemblyQualifiedName,
                            XmlExtensions.GetAttributeValue(elem, CSerializationContext.Global.TypeAttributeName),
                            "The Type is wrong");
        }
Beispiel #2
0
        public void TestExternalBaseSurrogate()
        {
            var x = new CMySuperStd
            {
                Name = "Alyssa",
                Age  = 21,
                Sex  = "Yes"
            };

            var c = new CSerializationContext();

            var s   = new CSerializer(c);
            var doc = s.Serialize(x);

            Print(doc);
            var elem = doc.DocumentElement;

            Assert.AreEqual(3, elem.ChildNodes.Count, "Child Node Count is wrong");
            Assert.AreEqual(x.Name, XmlExtensions.GetElementValue(elem, "Name"), "Name");
            Assert.AreEqual(x.Age.ToString(), XmlExtensions.GetElementValue(elem, "Age"), "Age");
            Assert.AreEqual(x.Sex, XmlExtensions.GetElementValue(elem, "Sex"), "Sex");

            PrintLine();

            c.RegisterExternalSurrogate(typeof(CStdBaseObject), new CStdExternalSurrogate());

            doc = s.Serialize(x);
            Print(doc);
            elem = doc.DocumentElement;

            Assert.AreEqual(1, elem.ChildNodes.Count, "Child Node Count is wrong");
            Assert.AreEqual(x.Name, XmlExtensions.GetAttributeValue(elem, "NAME"), "Name");
            Assert.AreEqual(x.Age.ToString(), XmlExtensions.GetAttributeValue(elem, "AGE"), "Age");
            Assert.AreEqual(x.Sex, XmlExtensions.GetElementValue(elem, "Sex"), "Sex");
        }
Beispiel #3
0
        public void TestExternalSurrogate()
        {
            var x = new CStdBaseObject
            {
                Name = "Alyssa",
                Age  = 21
            };
            var c = new CSerializationContext();

            var s   = new CSerializer(c);
            var doc = s.Serialize(x);

            Print(doc);
            var elem = doc.DocumentElement;

            Assert.AreEqual(2, elem.ChildNodes.Count, "Child Node Count is wrong");
            Assert.AreEqual(x.Name, XmlExtensions.GetElementValue(elem, "Name"), "Name");
            Assert.AreEqual(x.Age.ToString(), XmlExtensions.GetElementValue(elem, "Age"), "Age");

            Console.WriteLine(
                "\r\n\r\n-----------------------------------------------------------------------------\r\n\r\n");
            c.RegisterExternalSurrogate(typeof(CStdBaseObject), new CStdExternalSurrogate());

            doc = s.Serialize(x);
            Print(doc);
            elem = doc.DocumentElement;

            Assert.AreEqual(0, elem.ChildNodes.Count, "Child Node Count is wrong");
            Assert.AreEqual(x.Name, XmlExtensions.GetAttributeValue(elem, "NAME"), "Name");
            Assert.AreEqual(x.Age.ToString(), XmlExtensions.GetAttributeValue(elem, "AGE"), "Age");
        }
Beispiel #4
0
        public void TestArraySurrogate()
        {
            var x = new int[] { 1, 3, 5, 7, 9 };

            var c = new CSerializationContext();

            c.SetConcise();
            var helper = new CIntArraySurrogate();

            c.RegisterExternalSurrogate(typeof(int[]), helper);

            var s   = new CSerializer(c);
            var doc = s.Serialize(x);

            Console.WriteLine("Depth of resulting XML: " + XmlExtensions.Depth(doc));
            Console.WriteLine("Length of resulting XML String: " + doc.OuterXml.Length);
            Console.WriteLine("Number of resulting XmlElements: " + XmlExtensions.ElementCount(doc));
            Print(doc);

            var d = new CDeserializer(c);
            var y = (int[])d.Deserialize(doc);

            Assert.AreEqual(x.Length, y.Length - 1, "Length of resulting array is wrong");
            for (var i = 0; i < x.Length; i++)
            {
                Assert.AreEqual(x[i], y[i], "Invalid element at: " + i);
            }
        }
Beispiel #5
0
        public void TestDuplicateReferences()
        {
            var arr = new object[10];
            var p   = new CPerson();
            var p2  = new CPerson();

            arr[1] = p;
            arr[2] = p2;
            arr[5] = p2;
            arr[8] = p;
            arr[9] = p;

            object x = arr;

            var c = new CSerializationContext();

            c.SetConcise();
            c.FixM_ = true;
            c.ArrayElementsIncludeIndicies = true;
            var s   = new CSerializer(c);
            var doc = s.Serialize(x);

            Print(doc);
            var elem = doc.DocumentElement;

            AssureCorrectReference(elem, 1, 8);
            AssureCorrectReference(elem, 1, 9);
            AssureCorrectReference(elem, 2, 5);
        }
Beispiel #6
0
        public void TestGenericDerived()
        {
            var src = new CPersonWithObject <CAddress>()
            {
                SomeObject = new CAddress()
                {
                    m_city   = "Spring",
                    m_street = "Halifax",
                    m_zip    = 37174
                }
            };

            var s   = new CSerializer();
            var xml = s.Serialize(src);

            Print(xml);



            var d    = new CDeserializer();
            var dest = d.Deserialize(xml) as CPersonWithObject <CAddress>;

            Assert.AreEqual(src.m_name, dest.m_name, "Name");
            Assert.AreEqual(src.SomeObject.m_street, dest.SomeObject.m_street, "Generic Street");
            Assert.AreEqual(src.m_address.m_zip, dest.m_address.m_zip, "Zip");
            Assert.AreNotEqual(src.SomeObject, dest.SomeObject);
        }
        public void TestLinkedList()
        {
            var list = new LinkedList <int>();

            for (var i = 1; i < 1000000; i = (int)((i + 1) * 1.5))
            {
                list.AddLast(i);
            }

            var s   = new CSerializer();
            var doc = s.Serialize(list);

            Print(doc);

            var d     = new CDeserializer();
            var list2 = d.Deserialize(doc) as LinkedList <int>;

            Assert.AreEqual(list.Count, list2.Count, "Number of resulting elements is wrong.");

            var ptr = list2.First;

            foreach (var x in list)
            {
                Assert.AreEqual(x, ptr.Value, "The deserialized value is wrong for initial value: " + x);
                ptr = ptr.Next;
            }
        }
        public void TestDictionary()
        {
            var x = new Dictionary <int, string>
            {
                [4]     = "hello",
                [55]    = "Katie",
                [15834] = "=)",
                [324]   = "Homer",
                [-87]   = "Simpson"
            };

            var c = new CSerializationContext();

            c.SetConcise();
            var s   = new CSerializer(c);
            var doc = s.Serialize(x);

            Print(doc);

            var d = new CDeserializer(c);
            var y = (Dictionary <int, string>)d.Deserialize(doc);


            Assert.AreEqual(x.Count, y.Count, "Size of resulting hashtable is wrong");
            foreach (var key in x.Keys)
            {
                Assert.AreEqual(x[key], y[key], "Entry at key " + key + " was wrong.");
            }
        }
        public void TestList()
        {
            var names = new List <string>
            {
                "Homer",
                "Marge",
                "Bart",
                "Lisa",
                "Maggie"
            };

            var c = new CSerializationContext();

            c.SetVerbose();
            var s   = new CSerializer(c);
            var doc = s.Serialize(names);

            Print(doc);

            var d      = new CDeserializer(c);
            var names2 = d.Deserialize <List <string> >(doc);

            Assert.AreEqual(names.Count, names2.Count, "The number of list elements is wrong");
            for (var i = 0; i < names.Count; i++)
            {
                Assert.AreEqual(names[i], names2[i], "The name is wrong at index " + i);
            }
        }
Beispiel #10
0
        public void TestStringCounter()
        {
            var x = new CTypeCounter();

            var c = new CSerializationContext();

            c.SetConcise();
            c.RegisterExternalSurrogate(typeof(string), x);

            var s   = new CSerializer(c);
            var doc = s.Serialize(x);

            Assert.AreEqual(3, x.Count, "There should have been 3 strings counted.");

            Console.WriteLine("Depth of resulting XML: " + XmlExtensions.Depth(doc));
            Console.WriteLine("Length of resulting XML String: " + doc.OuterXml.Length);
            Console.WriteLine("Number of resulting XmlElements: " + XmlExtensions.ElementCount(doc));
            Print(doc);

            var d = new CDeserializer(c);
            var y = d.Deserialize <CTypeCounter>(doc);

            Assert.AreEqual(0, y.Count, "The new object should have no counted strings");
            Assert.AreEqual(6, x.Count, "The initial object should have strings counted for 2 actions");
        }
        public void TestSortedDictionary()
        {
            var names = GenerateRandomNames();
            var dict  = new SortedDictionary <string, CAddress>();

            for (var i = 0; i < 26; i++)
            {
                dict[names[i]] = CAddress.Get();
            }

            var s   = new CSerializer();
            var doc = s.Serialize(dict);

            Print(doc);

            var d  = new CDeserializer();
            var d2 = (SortedDictionary <string, CAddress>)d.Deserialize(doc);

            Assert.AreEqual(dict.Count, d2.Count, "Size of resulting dictionary is wrong");
            Assert.AreEqual(dict.Count, doc.DocumentElement.ChildNodes.Count, "The number of XmlNodes for the collection is wrong");

            foreach (var key in dict.Keys)
            {
                CompareCAddresses(dict[key], d2[key]);
            }
        }
Beispiel #12
0
        public void TestSuperclassedArray()
        {
            var x = new CSuperArray
            {
                Arr = new int[] { 1, 3, 5 }
            };

            var s   = new CSerializer();
            var doc = s.Serialize(x);

            Print(doc);
            var elem = doc.DocumentElement;

            Assert.AreEqual(typeof(CSuperArray).AssemblyQualifiedName,
                            XmlExtensions.GetAttributeValue(elem, s.Context.TypeAttributeName),
                            "Type of the root node is wrong");

            var e = (XmlElement)elem.SelectSingleNode("Arr");

            Assert.IsNotNull(e, "Missing Arr element");
            Assert.AreEqual(x.Arr.GetType().AssemblyQualifiedName,
                            XmlExtensions.GetAttributeValue(e, s.Context.TypeAttributeName),
                            "Type Attribute Error");
            Assert.AreEqual(x.Arr.Length.ToString(),
                            XmlExtensions.GetAttributeValue(e, s.Context.ArrayAttributeName),
                            "Length of Array Attribute is wrong");
            Assert.AreEqual("1,3,5", elem.InnerText, "Inner Text for the array");
        }
Beispiel #13
0
        public void TestMultidimStringArray()
        {
            string[,] x;
            x = (string[, ])Array.CreateInstance(typeof(string), new int[] { 2, 2 }, new int[] { 5, 9 });

            x[5, 9]  = "first";
            x[5, 10] = "second";
            x[6, 9]  = "third";
            x[6, 10] = "fourth";

            var c = new CSerializationContext();

            c.SetConcise();

            var s   = new CSerializer(c);
            var doc = s.Serialize(x);

            Print(doc);
            var elem = doc.DocumentElement;

            Assert.AreEqual(1, elem.ChildNodes.Count, "Should be one child (an XmlText node)");
            Assert.AreEqual("2,2",
                            XmlExtensions.GetAttributeValue(elem, c.ArrayAttributeName),
                            "Array Lengths are wrong");
            Assert.AreEqual("5,9",
                            XmlExtensions.GetAttributeValue(elem, c.ArrayLowerBoundAttribute),
                            "Array Lowerbounds are wrong");
            Assert.AreEqual("first,second,third,fourth", elem.InnerText, "The text for the multidim array is wrong");
        }
Beispiel #14
0
        public void TestFieldRenamer()
        {
            var c = new CSerializationContext
            {
                FieldRenamer = new CFieldRenamer()
            };

            var s   = new CSerializer(c);
            var add = new CAddress();

            var doc = s.Serialize(add);

            Print(doc);

            var root = doc.DocumentElement;

            TestSingleRenamedField(add.m_zip, root["INT_Zip"]);
            TestSingleRenamedField(add.m_city, root["STRING_City"]);
            TestSingleRenamedField(add.m_street, root["STRING_Street"]);


            var d   = new CDeserializer(c);
            var ad2 = d.Deserialize <CAddress>(doc);

            Assert.AreEqual(add.m_city, ad2.m_city, "City");
            Assert.AreEqual(add.m_street, ad2.m_street, "Street");
            Assert.AreEqual(add.m_zip, ad2.m_zip, "Zip");
        }
        public void TestStack()
        {
            var q = new Stack <CAddress>();

            for (var i = 1; i < 11; i++)
            {
                q.Push(CAddress.Get());
            }

            var s   = new CSerializer();
            var doc = s.Serialize(q);

            Print(doc);

            var d  = new CDeserializer();
            var q2 = (Stack <CAddress>)d.Deserialize(doc);

            Assert.AreEqual(q.Count, q2.Count, "Number of resulting elements is wrong.");
            Assert.AreEqual(q.Count,
                            doc.DocumentElement.ChildNodes.Count,
                            "The number of child nodes does not equal the number of elements in the Collection.");

            while (q.Count > 0)
            {
                var a1 = q.Pop();
                var a2 = q2.Pop();
                CompareCAddresses(a1, a2);
            }
        }
Beispiel #16
0
        public void TestExternalSurrogate()
        {
            CBigPerson.GenerateData(100);
            var c = new CSerializationContext();

            c.SetConcise();
            var helper = new CFriendSerializer();

            c.RegisterExternalSurrogate(typeof(CFriend), helper);

            var s   = new CSerializer(c);
            var doc = s.Serialize(CBigPerson.People);

            Console.WriteLine("Depth of resulting XML: " + XmlExtensions.Depth(doc));
            Console.WriteLine("Length of resulting XML String: " + doc.OuterXml.Length);
            Console.WriteLine("Number of resulting XmlElements: " + XmlExtensions.ElementCount(doc));
            Print(doc);

            var d  = new CDeserializer(c);
            var x2 = d.Deserialize <CBigPerson[]>(doc);

            helper.FinishDeserializing(x2);

            CDeserializeTest.AssertEqualBigPeopleArray(CBigPerson.People, x2);
        }
        public void TestAllArrayLists()
        {
            CClassWithArrayLists ca = new CClassWithArrayLists();

            ca.SimpleArrayList   = MakeSampleArrayList();
            ca.ReadOnlyArrayList = ArrayList.ReadOnly(ca.SimpleArrayList);
            // This is really bad because we shouldn't really hold the ref to the "old" object
            // after its been wrapped
            ca.SyncArrayList = ArrayList.Synchronized(ca.SimpleArrayList);
            // This is really bad because we shouldn't really hold the ref to the "old" object
            // after its been wrapped

            Assert.AreNotEqual(ca.SimpleArrayList.GetType(),
                               ca.ReadOnlyArrayList.GetType(),
                               "The Type of the ArrayList is the SAME as the ReadOnly ArrayList.");
            Assert.AreNotEqual(ca.SimpleArrayList.GetType(),
                               ca.SyncArrayList.GetType(),
                               "The Type of the ArrayList is the SAME as the SyncArrayList.");
            Assert.AreNotEqual(ca.SyncArrayList.GetType(),
                               ca.ReadOnlyArrayList.GetType(),
                               "The Type of the SyncArrayList is the SAME as the ReadOnly ArrayList.");

            Console.WriteLine(ca.SimpleArrayList.GetType().AssemblyQualifiedName);
            Console.WriteLine(ca.ReadOnlyArrayList.GetType().AssemblyQualifiedName);
            Console.WriteLine(ca.SyncArrayList.GetType().AssemblyQualifiedName);

            CSerializationContext.Global.UseFullUtcDateTimeStrings = true;
            CSerializer s   = new CSerializer();
            XmlDocument doc = s.Serialize(ca);

            Print(doc);

            CDeserializer        d   = new CDeserializer();
            CClassWithArrayLists ca2 = d.Deserialize <CClassWithArrayLists>(doc);

            Assert.AreEqual(ca.SimpleArrayList.GetType(),
                            ca2.SimpleArrayList.GetType(),
                            "The Type of the resulting ArrayList is different.");
            Assert.AreEqual(ca.ReadOnlyArrayList.GetType(),
                            ca2.ReadOnlyArrayList.GetType(),
                            "The Type of the ReadOnly array is different");
            Assert.AreEqual(ca.SyncArrayList.GetType(),
                            ca2.SyncArrayList.GetType(),
                            "The Type of the Sync array is different");

            Assert.AreNotEqual(ca2.SimpleArrayList.GetType(),
                               ca2.ReadOnlyArrayList.GetType(),
                               "The Type of the ArrayList is the SAME as the ReadOnly ArrayList.");
            Assert.AreNotEqual(ca2.SimpleArrayList.GetType(),
                               ca2.SyncArrayList.GetType(),
                               "The Type of the ArrayList is the SAME as the SyncArrayList.");
            Assert.AreNotEqual(ca2.SyncArrayList.GetType(),
                               ca2.ReadOnlyArrayList.GetType(),
                               "The Type of the SyncArrayList is the SAME as the ReadOnly ArrayList.");

            VerifyArrayListContents(ca.SimpleArrayList, ca2.SimpleArrayList);
            VerifyArrayListContents(ca.ReadOnlyArrayList, ca2.ReadOnlyArrayList);
            VerifyArrayListContents(ca.SyncArrayList, ca2.SyncArrayList);
        }
Beispiel #18
0
        public void TestObjectArray()
        {
            var x = new object[] { null, 47, new CPerson(), new int[] { 3, 1, 4, 1, 5, 9 } };

            var s = new CSerializer();

            CSerializationContext.Global.SetVerbose();

            var doc = s.Serialize(x);

            Print(doc);

            PrintLine();

            CSerializationContext.Global.SetFullNames();
            doc = s.Serialize(x);
            Print(doc);
        }
Beispiel #19
0
 public void EnumValueAsObjectTest()
 {
     var obj = new CClassWithObject()
     {
         Obj = EDispatchMode.Threadpool
     };
     var s   = new CSerializer();
     var doc = s.Serialize(obj);
 }
Beispiel #20
0
 public void TestTypeFieldTest()
 {
     var obj = new CClassWithType()
     {
         Type = GetType()
     };
     var s   = new CSerializer();
     var doc = s.Serialize(obj);
 }
Beispiel #21
0
        public void TestNullableValue()
        {
            int?        x, y;
            var         s = new CSerializer();
            var         d = new CDeserializer();
            XmlDocument doc;

            x   = 5;
            doc = s.Serialize(x);
            Print(doc);
            y = d.Deserialize <int?>(doc);
            Console.WriteLine("Deserialized: {0}", (y == null) ? "<null>" : y.ToString());

            x   = null;
            doc = s.Serialize(x);
            Print(doc);
            y = d.Deserialize <int?>(doc);
            Console.WriteLine("Deserialized: {0}", (y == null) ? "<null>" : y.ToString());
        }
Beispiel #22
0
        public void TestMultidimArray()
        {
            var x = new int[3, 3, 5];

            for (var j = 0; j < 3; j++)
            {
                for (var k = 0; k < 3; k++)
                {
                    for (var l = 0; l < 5; l++)
                    {
                        x[j, k, l] = j * 100 + k * 10 + l;
                    }
                }
            }

            var c = new CSerializationContext();

            c.SetVerbose();

            var s   = new CSerializer(c);
            var doc = s.Serialize(x);

            Print(doc);
            var elem = doc.DocumentElement;

            Assert.AreEqual(3 * 3 * 5, elem.ChildNodes.Count, "Child node count is wrong");
            Assert.AreEqual("3,3,5",
                            XmlExtensions.GetAttributeValue(elem, c.ArrayAttributeName),
                            "Array Lengths are wrong");
            Assert.AreEqual("0,0,0",
                            XmlExtensions.GetAttributeValue(elem, c.ArrayLowerBoundAttribute),
                            "Array lower-bounds are wrong");

            PrintLine();

            c.SetConcise();
            doc = s.Serialize(x);
            Print(doc);
            elem = doc.DocumentElement;

            Assert.AreEqual(1, elem.ChildNodes.Count, "Child node count is wrong");
            Assert.IsTrue(elem.InnerText.StartsWith("0,1,2,3,4,10,11,12"), "The inner text doesn't look right");
        }
Beispiel #23
0
        public void TestNullableValueError()
        {
            int?x = null;

            var s   = new CSerializer();
            var doc = s.Serialize(x);

            Print(doc);

            var d = new CDeserializer();
            var y = (int)d.Deserialize(doc);    // should throw the error
        }
Beispiel #24
0
        public void TestCPersonExplicitArrays()
        {
            var x = new CPerson();
            var c = new CSerializationContext
            {
                FixM_ = true,
                AllArraysHaveExplicitElements = true,
                ArrayElementsIncludeIndicies  = true
            };

            var s   = new CSerializer(c);
            var doc = s.Serialize(x);

            Print(doc);
            var elem = doc.DocumentElement;

            Assert.AreEqual(x.m_name, XmlExtensions.GetElementValue(elem, "Name"), "Name");
            Assert.AreEqual(x.m_age.ToString(), XmlExtensions.GetElementValue(elem, "Age"), "Age");

            var e = (XmlElement)elem.SelectSingleNode("KidsNames");

            Assert.IsNotNull(e, "Missing KidsNames element");
            Assert.AreEqual("3", XmlExtensions.GetAttributeValue(e, s.Context.ArrayAttributeName), "Kids Array Count");
            for (var i = 0; i < x.m_kidsNames.Length; i++)
            {
                Assert.AreEqual(x.m_kidsNames[i], e.ChildNodes[i].InnerText, "Kid " + i);
                Assert.AreEqual(i.ToString(),
                                XmlExtensions.GetAttributeValue(e.ChildNodes[i], s.Context.ArrayIndexAttributeName),
                                "Array Index " + i);
            }

            e = (XmlElement)elem.SelectSingleNode("ANullValue");
            Assert.IsNotNull(e, "Missing ANullValue element");
            Assert.AreEqual(s.Context.NullAttributeValue,
                            XmlExtensions.GetAttributeValue(e, s.Context.NullAttributeName),
                            "Null Attribute Error");

            e = (XmlElement)elem.SelectSingleNode("Address");
            Assert.IsNotNull(e, "Missing Address element");
            Assert.AreEqual(x.m_address.m_city, XmlExtensions.GetElementValue(e, "City"), "Address-City");
            Assert.AreEqual(x.m_address.m_street, XmlExtensions.GetElementValue(e, "Street"), "Address-Street");
            Assert.AreEqual(x.m_address.m_zip.ToString(), XmlExtensions.GetElementValue(e, "Zip"), "Address-Zip");

            e = (XmlElement)elem.SelectSingleNode("OtherAddress");
            Assert.IsNotNull(e, "Other Address Missing");
            var sa = x.m_otherAddress as CSuperAddress;

            Assert.AreEqual(sa.m_country, XmlExtensions.GetElementValue(e, "Country"), "OtherAddress-Country");
            Assert.AreEqual(sa.m_city, XmlExtensions.GetElementValue(e, "City"), "Address-City");
            Assert.AreEqual(sa.m_street, XmlExtensions.GetElementValue(e, "Street"), "Address-Street");
            Assert.AreEqual(sa.m_zip.ToString(), XmlExtensions.GetElementValue(e, "Zip"), "Address-Zip");
        }
Beispiel #25
0
        public void TestNullSerialize()
        {
            var s   = new CSerializer();
            var doc = s.Serialize(null);

            Print(doc);
            var e = doc.DocumentElement;

            Assert.AreEqual(s.Context.RootElementName, e.Name, "Root name is incorrect");
            Assert.AreEqual(s.Context.NullAttributeValue,
                            XmlExtensions.GetAttributeValue(e, s.Context.NullAttributeName),
                            "Null attribute was not there");
        }
Beispiel #26
0
        public void TestEnumFlags()
        {
            var x = ETest.SECOND | ETest.FOURTH;

            var s = new CSerializer();

            CSerializationContext.Global.SetVerbose();

            var doc = s.Serialize(x);

            Print(doc);
            var elem = doc.DocumentElement;

            Assert.AreEqual("SECOND, FOURTH", elem.InnerText, "Enum serialized to wrong string");

            PrintLine();

            x   = 0;
            doc = s.Serialize(x);
            Print(doc);
            elem = doc.DocumentElement;
            Assert.AreEqual("0", elem.InnerText, "invalid Enum value for ZERO");
        }
Beispiel #27
0
        public void TestNonstandardArray()
        {
            var x = Array.CreateInstance(typeof(int), new int[] { 10 }, new int[] { 5 });
            var s = new CSerializer();

            var doc = s.Serialize(x);

            Print(doc);
            var elem = doc.DocumentElement;

            Assert.AreEqual(x.GetLowerBound(0).ToString(),
                            XmlExtensions.GetAttributeValue(elem, s.Context.ArrayLowerBoundAttribute),
                            "Lower Bound not set right");
        }
Beispiel #28
0
        public void TestCPerson()
        {
            var x = new CPerson();

            CSerializationContext.Global.FixM_ = true;

            var s   = new CSerializer();
            var doc = s.Serialize(x);

            Print(doc);
            var elem = doc.DocumentElement;

            Assert.AreEqual(x.m_name, XmlExtensions.GetElementValue(elem, "Name"), "Name");
            Assert.AreEqual(x.m_age.ToString(), XmlExtensions.GetElementValue(elem, "Age"), "Age");

            var e = (XmlElement)elem.SelectSingleNode("KidsNames");

            Assert.IsNotNull(e, "Missing KidsNames element");
            Assert.AreEqual("3", XmlExtensions.GetAttributeValue(e, s.Context.ArrayAttributeName), "Kids Array Count");
            Assert.AreEqual("Maggie,Lisa,Bart", e.InnerText, "Kids Names");

            e = (XmlElement)elem.SelectSingleNode("KidsAges");
            Assert.IsNotNull(e, "Missing KidsNames element");
            Assert.AreEqual("3",
                            XmlExtensions.GetAttributeValue(e, s.Context.ArrayAttributeName),
                            "KidsAges Array Count");
            Assert.AreEqual("1,7,9", e.InnerText, "Kids Names");

            e = (XmlElement)elem.SelectSingleNode("ANullValue");
            Assert.IsNotNull(e, "Missing ANullValue element");
            Assert.AreEqual(s.Context.NullAttributeValue,
                            XmlExtensions.GetAttributeValue(e, s.Context.NullAttributeName),
                            "Null Attribute Error");

            e = (XmlElement)elem.SelectSingleNode("Address");
            Assert.IsNotNull(e, "Missing Address element");
            Assert.AreEqual(x.m_address.m_city, XmlExtensions.GetElementValue(e, "City"), "Address-City");
            Assert.AreEqual(x.m_address.m_street, XmlExtensions.GetElementValue(e, "Street"), "Address-Street");
            Assert.AreEqual(x.m_address.m_zip.ToString(), XmlExtensions.GetElementValue(e, "Zip"), "Address-Zip");

            e = (XmlElement)elem.SelectSingleNode("OtherAddress");
            Assert.IsNotNull(e, "Other Address Missing");
            var sa = x.m_otherAddress as CSuperAddress;

            Assert.AreEqual(sa.m_country, XmlExtensions.GetElementValue(e, "Country"), "OtherAddress-Country");
            Assert.AreEqual(sa.m_city, XmlExtensions.GetElementValue(e, "City"), "Address-City");
            Assert.AreEqual(sa.m_street, XmlExtensions.GetElementValue(e, "Street"), "Address-Street");
            Assert.AreEqual(sa.m_zip.ToString(), XmlExtensions.GetElementValue(e, "Zip"), "Address-Zip");
        }
Beispiel #29
0
        public void TestEnumeration()
        {
            var x = ETestStatus.IMPLICIT_DESERIALIZER_INCOMPLETE;

            var s = new CSerializer();

            CSerializationContext.Global.SetVerbose();

            var doc = s.Serialize(x);

            Print(doc);
            var elem = doc.DocumentElement;

            Assert.AreEqual(x.ToString(), elem.InnerText, "Enum serialized to wrong string");
        }
Beispiel #30
0
        public void TestSerializePrimitive()
        {
            var x = 69;

            var s   = new CSerializer();
            var doc = s.Serialize(x);

            Print(doc);
            var e = doc.DocumentElement;

            Assert.AreEqual(s.Context.RootElementName, e.Name, "Root name is incorrect");
            Assert.AreEqual(x.GetType().AssemblyQualifiedName,
                            XmlExtensions.GetAttributeValue(e, s.Context.TypeAttributeName),
                            "Type attribute is incorrect");
            Assert.AreEqual(x.ToString(), e.InnerText, "The value itself was wrong");
        }