Click here to Skip to main content
15,885,032 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.9K   572   11  
This article explains how to write unit tests for MVVM using Catel.
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="GetFirstValidationErrorConverter.cs" company="Catel development team">
//   Copyright (c) 2008 - 2011 Catel development team. All rights reserved.
// </copyright>
// <summary>
//   Converts a collection containing <see cref="ValidationError" /> objects to return the first error
//   or an empty string in case there are no errors.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Data;

namespace Catel.Windows.Data.Converters
{
	/// <summary>
	/// Converts a collection containing <see cref="ValidationError"/> objects to return the first error
	/// or an empty string in case there are no errors.
	/// </summary>
#if !SILVERLIGHT
	[ValueConversion(typeof(ICollection<ValidationError>), typeof(string))]
#endif
	public class GetFirstValidationErrorConverter : IValueConverter
	{
		/// <summary>
		/// Converts a collection containing <see cref="ValidationError"/> objects to return the first error
		/// or an empty string in case there are no errors.
		/// </summary>
		/// <param name="value">Values to check for. Both collections and arrays are supported.</param>
		/// <param name="targetType">Not used.</param>
		/// <param name="parameter">Not used.</param>
		/// <param name="culture">Not used.</param>
		/// <returns>String containing the first error or <see cref="string.Empty"/> in case there are not errors.</returns>
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			// Check if we have a valid value
			if (value == null)
			{
			    return string.Empty;
			}

			// Convert value
			ICollection<ValidationError> errorCollection = value as ICollection<ValidationError>;
			if (errorCollection == null)
			{
			    return string.Empty;
			}

			// Return error
			return (errorCollection.Count > 0) ? errorCollection.First().ErrorContent.ToString() : string.Empty;
		}

		/// <summary>
		/// This method isn't supported.
		/// </summary>
		/// <param name="value">Not used.</param>
		/// <param name="targetType">Not used.</param>
		/// <param name="parameter">Not used.</param>
		/// <param name="culture">Not used.</param>
		/// <returns>Always null.</returns>
		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
            return ConverterHelper.DoNothingBindingValue;
		}
	}
}

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