Click here to Skip to main content
15,867,453 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.6K   572   11  
This article explains how to write unit tests for MVVM using Catel.
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PropertySorter.cs" company="Catel development team">
//   Copyright (c) 2008 - 2011 Catel development team. All rights reserved.
// </copyright>
// <summary>
//   PropertySorter class to allow a user to specify the order of properties in a PropertyGrid.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections;
using System.ComponentModel;

namespace Catel.Reflection
{
	/// <summary>
	/// PropertySorter class to allow a user to specify the order of properties in a PropertyGrid.
	/// </summary>
	public class PropertySorter : ExpandableObjectConverter
	{
		#region Methods
		/// <summary>
		/// Gets a value indicating whether this object supports properties using the specified context.
		/// </summary>
		/// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"/> that provides a format context.</param>
		/// <returns>
		/// true because <see cref="M:System.ComponentModel.TypeConverter.GetProperties(System.object)"/> should be called to find the properties of this object. This method never returns false.
		/// </returns>
		public override bool GetPropertiesSupported(ITypeDescriptorContext context)
		{
			return true;
		}

		/// <summary>
		/// Gets a collection of properties for the type of object specified by the value parameter.
		/// </summary>
		/// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"/> that provides a format context.</param>
		/// <param name="value">An <see cref="T:System.object"/> that specifies the type of object to get the properties for.</param>
		/// <param name="attributes">An array of type <see cref="T:System.Attribute"/> that will be used as a filter.</param>
		/// <returns>
		/// A <see cref="T:System.ComponentModel.PropertyDescriptorCollection"/> with the properties that are exposed for the component, or null if there are no properties.
		/// </returns>
		public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
		{
			// This override returns a list of properties in order
			PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(value, attributes);
			ArrayList orderedProperties = new ArrayList();
			foreach (PropertyDescriptor pd in pdc)
			{
				Attribute attribute = pd.Attributes[typeof(PropertyOrderAttribute)];
				if (attribute != null)
				{
					PropertyOrderAttribute poa = (PropertyOrderAttribute)attribute;
					orderedProperties.Add(new PropertyOrderPair(pd.Name, poa.Order));
				}
				else
				{
					orderedProperties.Add(new PropertyOrderPair(pd.Name, 0));
				}
			}

			// Perform the actual order using the value PropertyOrderPair classes
			// implementation of IComparable to sort
			orderedProperties.Sort();

			// Build a string list of the ordered names
			ArrayList propertyNames = new ArrayList();
			foreach (PropertyOrderPair pop in orderedProperties)
			{
				propertyNames.Add(pop.Name);
			}

			// Pass in the ordered list for the PropertyDescriptorCollection to sort by
			return pdc.Sort((string[])propertyNames.ToArray(typeof(string)));
		}
		#endregion
	}

	#region Helper Class - PropertyOrderAttribute
	/// <summary>
	/// Simple attribute to allow the order of a property to be specified.
	/// </summary>
	[AttributeUsage(AttributeTargets.Property)]
	public class PropertyOrderAttribute : Attribute
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="PropertyOrderAttribute"/> class.
		/// </summary>
		/// <param name="order">The order.</param>
		public PropertyOrderAttribute(int order)
		{
			Order = order;
		}

		/// <summary>
		/// Gets the order.
		/// </summary>
		/// <value>The order.</value>
		public int Order { get; private set; }
	}
	#endregion

	#region Helper Class - PropertyOrderPair
	/// <summary>
	/// Property order pair class.
	/// </summary>
	public class PropertyOrderPair : IComparable
	{
		#region Variables
		#endregion

		#region Constructor & destructor
		/// <summary>
		/// Initializes a new instance of the <see cref="PropertyOrderPair"/> class.
		/// </summary>
		/// <param name="name">The name of the property order pair.</param>
		/// <param name="order">The order of the property.</param>
		public PropertyOrderPair(string name, int order)
		{
			Order = order;
			Name = name;
		}
		#endregion

		#region Properties
		/// <summary>
		/// Gets the name.
		/// </summary>
		/// <value>The name of the property order pair.</value>
		public string Name { get; private set; }

		/// <summary>
		/// Gets or sets the order.
		/// </summary>
		/// <value>The order of the property.</value>
		private int Order { get; set; }
		#endregion

		#region Methods
		/// <summary>
		/// Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
		/// </summary>
		/// <param name="obj">An object to compare with this instance.</param>
		/// <returns>
		/// A 32-bit signed integer that indicates the relative order of the objects being compared. The return value has these meanings:
		/// Value
		/// Meaning
		/// Less than zero
		/// This instance is less than <paramref name="obj"/>.
		/// Zero
		/// This instance is equal to <paramref name="obj"/>.
		/// Greater than zero
		/// This instance is greater than <paramref name="obj"/>.
		/// </returns>
		/// <exception cref="T:System.ArgumentException">
		/// 	<paramref name="obj"/> is not the same type as this instance.
		/// </exception>
		public int CompareTo(object obj)
		{
			// Sort the pair objects by ordering by order value
			// Equal values get the same rank
			int otherOrder = ((PropertyOrderPair)obj).Order;
			if (otherOrder == Order)
			{
				// If order not specified, sort by name
				string otherName = ((PropertyOrderPair)obj).Name;
				return string.Compare(Name, otherName);
			}
			else if (otherOrder > Order)
			{
				return -1;
			}

			return 1;
		}
		#endregion
	}
	#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