#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.Text;
using NUnit.Framework;
using Ingenious.Mvc;
namespace Ingenious.Mvc.UnitTest.Ids{
/// <summary>
/// Unit tests the <see cref="Id"/> type.
/// </summary>
public sealed class IdTest : UnitTestBase
{
private Id<IdTest> _id;
protected override void SetUp()
{
base.SetUp();
_id = Id<IdTest>.Empty;
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void TestNullString()
{
_id = (string) null;
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void TestNullEnum()
{
_id = (Enum) null;
}
[Test]
[ExpectedException(typeof(ArgumentException), "An empty string cannot be used as an ID.")]
public void TestEmptyString()
{
_id = " ";
}
[Test]
public void TestIsEmpty()
{
Assert.IsTrue(_id.IsEmpty);
_id = "not empty";
Assert.IsFalse(_id.IsEmpty);
}
[Test]
public void TestToString()
{
//empty instance should return null
Assert.IsNull(_id.ToString());
//string ID
_id = "My ID";
Assert.AreEqual("My ID", _id.ToString());
//enum ID
_id = MyEnum.EnumMember;
Assert.AreEqual("EnumMember", _id.ToString());
}
[Test]
public void TestEquals()
{
Id<IdTest> id1;
Id<IdTest> id2;
//two empty ids are equal
Assert.AreEqual(id1, id2);
//change one id - no longer equal
id1 = "My ID";
Assert.IsFalse(id1.Equals(id2));
//change other id to match - now they're equal
id2 = "My ID";
Assert.AreEqual(id1, id2);
//never equal to another type
Assert.IsFalse(id1.Equals(32));
Assert.IsFalse(id1.Equals("whatever"));
}
[Test]
public void TestGetHashCode()
{
Assert.AreEqual(0, _id.GetHashCode());
_id = "My ID";
Assert.AreEqual("My ID".GetHashCode(), _id.GetHashCode());
}
[Test]
public void TestOperatorEqualsNotEquals()
{
Id<IdTest> id1;
Id<IdTest> id2;
//two empty ids are equal
Assert.IsTrue(id1 == id2);
Assert.IsFalse(id1 != id2);
//change one id - no longer equal
id1 = "My ID";
Assert.IsFalse(id1 == id2);
Assert.IsTrue(id1 != id2);
//change other id to match - now they're equal
id2 = "My ID";
Assert.IsTrue(id1 == id2);
Assert.IsFalse(id1 != id2);
}
#region Supporting Types
private enum MyEnum
{
EnumMember
}
#endregion
}
}
|