Click here to Skip to main content
15,894,720 members
Articles / Programming Languages / C#

Serializable Extra Types for .NET 4

Rate me:
Please Sign up or sign in to vote.
4.85/5 (9 votes)
25 May 2011GPL33 min read 53.9K   335   13  
Serialization for Rapid Application Development.
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestLibraryOne;
using TestLibraryThree;

namespace SerializableExtraTypes.Tests
{
    /// <summary>
    ///This is a test class for SerializableExtensionMethodsTest and is intended
    ///to contain all SerializableExtensionMethodsTest Unit Tests
    ///</summary>
    [TestClass(), System.Runtime.InteropServices.GuidAttribute("0242E3D8-F7EB-4AC2-B78A-948D1B1EC2BD")]
    public class SerializableExtensionMethodsTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes

        //
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //

        #endregion Additional test attributes

        #region SerializeEx

        /// <summary>
        ///A test for SerializeEx
        ///</summary>
        public void SerializeExTestHelper<T>(T obj, string expected)
        {
            string actual = SerializableExtensionMethods.SerializeEx<T>(obj);
            Assert.AreEqual(expected, actual);
        }

        public void SerializeExTestHelper<T>(T obj, string expected, params Type[] root)
        {
            string actual = SerializableExtensionMethods.SerializeEx<T>(obj, root);
            Assert.AreEqual(expected, actual);
        }

        [TestMethod()]
        public void SerializeExTest()
        {
            SerializeExTestHelper<Animal>(TestObjects.Whale, TestObjects.whale);
        }

        [TestMethod()]
        public void SerializeExTest_DifferentRoot()
        {
            SerializeExTestHelper(TestObjects.Animals, TestObjects.animals, typeof(Animal));
        }

        [TestMethod]
        public void SerializeExTest_MulitRoots()
        {
            SerializeExTestHelper(TestObjects.BionicAnimals, TestObjects.bionicAnimals, typeof(Animal), typeof(Bionics));
        }

        [TestMethod]
        public void SerializeExTest_3LevelsDeep()
        {
            SerializeExTestHelper(TestObjects.BionicAnimals2, TestObjects.bionicAnimals2);
        }

        #endregion SerializeEx

        #region DeserializeEx

        /// <summary>
        ///A test for DeserializeEx
        ///</summary>
        public void DeserializeExTestHelper<T>(T expected, string xml)
        {
            string actual;
            actual = SerializableExtensionMethods.DeserializeEx<T>(xml).SerializeEx();
            Assert.AreEqual(xml, actual);
        }

        public void DeserializeExTestHelper<T>(T expected, string xml, params Type[] root)
        {
            string actual;
            actual = SerializableExtensionMethods.DeserializeEx<T>(xml, root).SerializeEx(root);
            Assert.AreEqual(xml, actual);
        }

        [TestMethod]
        public void DeserializeExTest()
        {
            DeserializeExTestHelper(TestObjects.Whale, TestObjects.whale);
        }

        [TestMethod()]
        public void DeserializeExTest_DifferentRoot()
        {
            SerializeExTestHelper(TestObjects.Animals, TestObjects.animals, typeof(Animal));
        }

        [TestMethod]
        public void DeserializeExTest_MulitRoots()
        {
            DeserializeExTestHelper(TestObjects.BionicAnimals, TestObjects.bionicAnimals, typeof(Animal), typeof(Bionics));
        }

        [TestMethod]
        public void DeserializeExTest_3LevelsDeep()
        {
            DeserializeExTestHelper(TestObjects.BionicAnimals2, TestObjects.bionicAnimals2);
        }

        [TestMethod]
        [ExpectedException(typeof(FormatException))]
        public void DeserializeExTest_MismatchType()
        {
            DeserializeExTestHelper(TestObjects.BionicAnimals, TestObjects.bionicAnimals2);
        }

        #endregion DeserializeEx
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Architect Stackify
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions