Click here to Skip to main content
15,885,366 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="ReferenceToBooleanConverter.cs" company="Catel development team">
//   Copyright (c) 2008 - 2011 Catel development team. All rights reserved.
// </copyright>
// <summary>
//   Implementation of class ReferenceToBooleanConverter
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.Globalization;
using System.Windows.Data;

namespace Catel.Windows.Data.Converters
{
    /// <summary>
    /// Implementation of class ReferenceToBooleanConverter
    /// </summary>
#if !SILVERLIGHT
    [ValueConversion(typeof(object), typeof(bool))]
#endif
    public class ReferenceToBooleanConverter : IValueConverter
    {
        /// <summary>
        /// Convert an object reference to a boolean value.
        /// </summary>
        /// <param name="value">The object reference to evaluate.</param>
        /// <param name="targetType">A targettype, currently not used.</param>
        /// <param name="parameter">A additional parameter</param>
        /// <param name="culture">A culture, currently not used.</param>
        /// <returns>
        /// When <paramref name="value"/> is not null the method returns true else false.
        /// Unless the passed <paramref name="parameter"/> parameter is true, then the
        /// output is inverted; meaning when <paramref name="value"/> is null the method
        /// returns true else false.
        /// </returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool isNull = value == null;
            bool inverseValue = false;

            if (parameter != null)
            {
                inverseValue = TypeHelper.Cast<bool, object>(parameter);
            }

            return inverseValue ? isNull : !isNull;
        }

        /// <summary>
        /// Convert nothing!
        /// </summary>
        /// <param name="value">A value.</param>
        /// <param name="targetType">A targettype, currently not used.</param>
        /// <param name="parameter">A parameter value, currently not used.</param>
        /// <param name="culture">A culture, currently not used.</param>
        /// <returns>Nothing, only throws an exception.</returns>
        public object ConvertBack(object value, Type targetType, object parameter, 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