Click here to Skip to main content
15,881,248 members
Articles / Desktop Programming / WPF

Catel - Part 4 of n: Unit testing with Catel

Rate me:
Please Sign up or sign in to vote.
4.55/5 (10 votes)
28 Jan 2011CPOL11 min read 48.8K   572   11  
This article explains how to write unit tests for MVVM using Catel.
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Catel.Test.Helpers
{
	[TestClass]
	public class TypeHelperTest
	{
		#region Test classes
		private class PropertyInfoTestClass
		{
			private bool PrivateProperty { get; set; }

			public bool PublicProperty { get; set; }
		}
		#endregion

		#region FormatType
		[TestMethod]
		public void FormatType()
		{
			string expectedValue = "Catel.Test.Helpers.TypeHelperTest, Catel.Test";

			string actualValue = TypeHelper.FormatType("Catel.Test", "Catel.Test.Helpers.TypeHelperTest");

			Assert.AreEqual(expectedValue, actualValue);
		}
		#endregion

		#region FormatInnerTypes
		[TestMethod]
		public void FormatInnerTypes()
		{
			string expectedValue = "[string],[string],[int]";

			string actualValue = TypeHelper.FormatInnerTypes(new string[] {"string", "string", "int"});

			Assert.AreEqual(expectedValue, actualValue);
		}
		#endregion

		#region IsTypeNullable
		[TestMethod]
		public void IsTypeNullableWithNullableType()
		{
			Assert.IsTrue(TypeHelper.IsTypeNullable(typeof(object)));
		}

		[TestMethod]
		public void IsTypeNullableWithValueType()
		{
			Assert.IsFalse(TypeHelper.IsTypeNullable(typeof(int)));
		}

		[TestMethod]
		public void IsTypeNullableWithNullValue()
		{
			Assert.IsFalse(TypeHelper.IsTypeNullable(null));
		}

        [TestMethod]
        public void IsTypeNullableWithNullableValueType()
        {
            Assert.IsTrue(TypeHelper.IsTypeNullable(typeof(bool?)));
        }
		#endregion

		#region AreObjectsEqual
		[TestMethod]
		public void AreObjectsEqualWithBoxedEqualIntegers()
		{
			object obj1 = 5;
			object obj2 = 5;

			Assert.IsTrue(TypeHelper.AreObjectsEqual(obj1, obj2));
			Assert.IsTrue(TypeHelper.AreObjectsEqual(obj2, obj1));
		}

		[TestMethod]
		public void AreObjectsEqualWithBoxedUnequalIntegers()
		{
			object obj1 = 5;
			object obj2 = 6;

			Assert.IsFalse(TypeHelper.AreObjectsEqual(obj1, obj2));
			Assert.IsFalse(TypeHelper.AreObjectsEqual(obj2, obj1));
		}

		[TestMethod] 
		public void AreObjectsEqualWithTwoNullValues()
		{
			object obj1 = null;
			object obj2 = null;

			Assert.IsTrue(TypeHelper.AreObjectsEqual(obj1, obj2));
			Assert.IsTrue(TypeHelper.AreObjectsEqual(obj2, obj1));
		}

		[TestMethod]
		public void AreObjectsEqualWithOneNullValue()
		{
			object obj1 = 5;
			object obj2 = null;

			Assert.IsFalse(TypeHelper.AreObjectsEqual(obj1, obj2));
			Assert.IsFalse(TypeHelper.AreObjectsEqual(obj2, obj1));
		}
		#endregion

		#region GetPropertyInfo
		[TestMethod]
		public void GetPropertyInfoForExistingPublicProperty()
		{
			Assert.IsNotNull(TypeHelper.GetPropertyInfo(typeof(PropertyInfoTestClass), "PublicProperty"));
		}

		[TestMethod]
		public void GetPropertyInfoForExistingPrivateProperty()
		{
			Assert.IsNotNull(TypeHelper.GetPropertyInfo(typeof(PropertyInfoTestClass), "PrivateProperty"));
		}

		[TestMethod]
		public void GetPropertyInfoForNonExistingProperty()
		{
			Assert.IsNull(TypeHelper.GetPropertyInfo(typeof(PropertyInfoTestClass), "NonExistingProperty"));
		}
		#endregion

		#region SizeOf
		// TODO: Write unit tests
		#endregion

		#region ReflectiveSizeOf
		// TODO: Write unit tests
		#endregion

		#region TryCast
		// TODO: Write unit tests
		#endregion

		#region Cast
		// TODO: Write unit tests
		#endregion

		#region PropertyGet
		// TODO: Write unit tests
		#endregion

		#region GetPropertyValue
		// TODO: Write unit tests
		#endregion

		#region GetField
		// TODO: Write unit tests
		#endregion

		#region SetField
		// TODO: Write unit tests
		#endregion

		#region GetMemberValue
		// TODO: Write unit tests
		#endregion

		#region CombineHash
		// TODO: Write unit tests
		#endregion

		#region ClearAllEvents
		// TODO: Write unit tests
		#endregion

		#region ClearEvent
		// TODO: Write unit tests
		#endregion
	}
}

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 Code Project Open License (CPOL)


Written By
Software Developer
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions