Click here to Skip to main content
13,045,019 members (40,141 online)
Click here to Skip to main content

Stats

30.4K views
13 bookmarked
Posted 25 May 2011

Serializable Extra Types for .NET 4

, 25 May 2011
Serialization for Rapid Application Development.
TestLibraryThree
Properties
TestLibraryThree.csproj.vspscc
TestLibraryTwo
Properties
TestLibraryTwo.csproj.user
TestLibraryTwo.csproj.vspscc
Local.testsettings
SerializableExtraTypes.gpState
SerializableExtraTypes.sln.docstates
SerializableExtraTypes.vsmdi
SerializableExtraTypes.vssscc
TraceAndTestImpact.testsettings
SerializableExtraTypes
AMightyOak.Net.ico
Properties
SerializableExtraTypes.csproj.user
SerializableExtraTypes.csproj.vspscc
SerializableExtraTypes.Tests
Properties
SerializableExtraTypes.Tests.csproj.user
SerializableExtraTypes.Tests.csproj.vspscc
TestLibraryOne
Properties
TestLibraryOne.csproj.user
TestLibraryOne.csproj.vspscc
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)

Share

About the Author

Daniel Gidman
Architect Stackify
United States United States
No Biography provided

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 25 May 2011
Article Copyright 2011 by Daniel Gidman
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid