Imports System
Imports System.Text
Imports System.Collections.Generic
Imports Microsoft.VisualStudio.TestTools.UnitTesting
Imports Mercury.Mobile.Xml.Serialization

<TestClass()> Public Class XmlSerializationHelper_specs

    Private testContextInstance As TestContext

    '''<summary>
    '''Gets or sets the test context which provides
    '''information about and functionality for the current test run.
    '''</summary>
    Public Property TestContext() As TestContext
        Get
            Return testContextInstance
        End Get
        Set(ByVal value As TestContext)
            testContextInstance = Value
        End Set
    End Property

#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 Shared Sub MyClassInitialize(ByVal testContext As TestContext)
    ' End Sub
    '
    ' Use ClassCleanup to run code after all tests in a class have run
    ' <ClassCleanup()> Public Shared Sub MyClassCleanup()
    ' End Sub
    '
    ' Use TestInitialize to run code before running each test
    ' <TestInitialize()> Public Sub MyTestInitialize()
    ' End Sub
    '
    ' Use TestCleanup to run code after each test has run
    ' <TestCleanup()> Public Sub MyTestCleanup()
    ' End Sub
    '
#End Region

    <TestMethod()> _
    Public Sub Can_serialize_type_to_xml()
        Dim t = GetType(List(Of String))
        Dim tXml = XmlSerializationHelper.SerializeType(t, Encoding.Unicode)
        Assert.IsNotNull(tXml)
        ' Deserialize
        Dim t2 = XmlSerializationHelper.DeserializeType(tXml)
        Assert.IsNotNull(t2)
        Assert.AreEqual(t, t2)
    End Sub

    <TestMethod()> _
    Public Sub Can_serialize_type_to_custom_value()
        XmlSerializationHelper.SetCustomTypeFormat(AddressOf _InternalFormatter)
        Dim t = GetType(List(Of String))
        Dim tXml = XmlSerializationHelper.SerializeType(t)
        Dim xml2 = String.Format("<Type>{0}</Type>", t.AssemblyQualifiedName)
        Assert.AreNotEqual(tXml, xml2)
        Dim t2 = XmlSerializationHelper.DeserializeType(tXml)
        Assert.IsNotNull(t2)
        Assert.AreEqual(t, t2)
    End Sub

    Private Function _InternalFormatter(ByVal type As Type) As String
        Dim asm = type.Assembly.GetName().Name
        Return String.Format("{0}, {1}", type.FullName, asm)
    End Function

End Class
