#region License
/**
* Ingenious MVC : An MVC framework for .NET 2.0
* Copyright (C) 2006, JDP Group
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Authors: - Kent Boogaart (kentcb@internode.on.net)
*/
#endregion
using System;
using System.Collections.Generic;
using System.ComponentModel.Design.Serialization;
using System.Text;
using NUnit.Framework;
using Ingenious.Mvc;
namespace Ingenious.Mvc.UnitTest.Ids{
/// <summary>
/// Unit tests the <see cref="IdConverter"/> type.
/// </summary>
public sealed class IdConverterTest : UnitTestBase
{
private IdConverter<IdConverterTest> _idConverter;
private Id<IdConverterTest> _id;
protected override void SetUp()
{
base.SetUp();
_idConverter = new IdConverter<IdConverterTest>();
_id = "test";
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void TestCanConvertFromSourceTypeNull()
{
_idConverter.CanConvertFrom(null);
}
[Test]
public void TestCanConvertFromSourceTypeIncorrect()
{
Assert.IsFalse(_idConverter.CanConvertFrom(typeof(int)));
Assert.IsFalse(_idConverter.CanConvertFrom(typeof(double)));
}
[Test]
public void TestCanConvertFromSourceTypeValid()
{
Assert.IsTrue(_idConverter.CanConvertFrom(typeof(string)));
Assert.IsTrue(_idConverter.CanConvertFrom(typeof(DayOfWeek)));
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void TestConvertFromValueNull()
{
_idConverter.ConvertFrom(null);
}
[Test]
[ExpectedException(typeof(NotSupportedException), "IdConverter`1 cannot convert from System.DateTime.")]
public void TestConvertFromValueInvalid()
{
_idConverter.ConvertFrom(DateTime.Now);
}
[Test]
public void TestConvertFrom()
{
_id = (Id<IdConverterTest>) _idConverter.ConvertFrom("a value");
Assert.AreEqual("a value", _id.ToString());
_id = (Id<IdConverterTest>) _idConverter.ConvertFrom(DayOfWeek.Monday);
Assert.AreEqual("Monday", _id.ToString());
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void TestCanConvertToDestinationTypeNull()
{
_idConverter.CanConvertTo(null);
}
[Test]
public void TestCanConvertToDestinationTypeIncorrect()
{
Assert.IsFalse(_idConverter.CanConvertTo(typeof(int)));
Assert.IsFalse(_idConverter.CanConvertTo(typeof(DayOfWeek)));
}
[Test]
public void TestCanConvertToDestinationTypeValid()
{
Assert.IsTrue(_idConverter.CanConvertTo(typeof(string)));
Assert.IsTrue(_idConverter.CanConvertTo(typeof(InstanceDescriptor)));
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void TestConvertToValueNull()
{
_idConverter.ConvertTo(null, typeof(string));
}
[Test]
[ExpectedException(typeof(NotSupportedException), "'IdConverter`1' is unable to convert 'System.DayOfWeek' to 'System.Double'.")]
public void TestConvertToValueInvalid()
{
_idConverter.ConvertTo(DayOfWeek.Saturday, typeof(double));
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void TestConvertToDestinationTypeNull()
{
_idConverter.ConvertTo(_id, null);
}
[Test]
public void TestConvertTo()
{
Assert.AreEqual("test", _idConverter.ConvertTo(_id, typeof(string)));
InstanceDescriptor instanceDescriptor = (InstanceDescriptor) _idConverter.ConvertTo(_id, typeof(InstanceDescriptor));
Assert.IsNotNull(instanceDescriptor);
object o = instanceDescriptor.Invoke();
Assert.IsNotNull(o);
Assert.AreEqual(_id, o);
}
}
}
|