Click here to Skip to main content
15,895,799 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 49.2K   572   11  
This article explains how to write unit tests for MVVM using Catel.
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MultiplyConverter.cs" company="Catel development team">
//   Copyright (c) 2008 - 2011 Catel development team. All rights reserved.
// </copyright>
// <summary>
//   Calculate the product of given value and factor in parameter.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.Windows.Data;

namespace Catel.Windows.Data.Converters
{
    /// <summary>
    /// Calculate the product of given value and factor in parameter.
    /// </summary>
#if !SILVERLIGHT
    [ValueConversion(typeof(int), typeof(int))]
#endif
    public class MultiplyConverter : IValueConverter
    {
        /// <summary>
        /// Calulate the product of given value and factor in parameter.
        /// </summary>
        /// <param name="value">Base value.</param>
        /// <param name="targetType">Not used, supports now int and double.</param>
        /// <param name="parameter">Factor used for multiply (always double).</param>
        /// <param name="culture">Not used.</param>
        /// <returns>Product of specified values.</returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            // Get typed value
            double typedValue = 0d;

            // Check input
            if (value is int)
            {
                typedValue = System.Convert.ToDouble((int)value);
            }
            else if (value is double)
            {
                typedValue = (double)value;
            }

            // Check value
            if (typedValue == 0d)
            {
                return 0d;
            }

            // Get factor
            double factor;
            if (!double.TryParse(parameter as string, out factor))
            {
                return 0d;
            }

            // Multiply
            return typedValue * factor;
        }

        /// <summary>
        /// Calculate value back.
        /// </summary>
        /// <param name="value">Base value.</param>
        /// <param name="targetType">Not used, supports now int and double.</param>
        /// <param name="parameter">Factor used for multiply (always double).</param>
        /// <param name="culture">Not used.</param>
        /// <returns>Original value.</returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            // Get typed value
            double typedValue = 0d;

            // Check input
            if (value is int)
            {
                typedValue = System.Convert.ToDouble((int)value);
            }
            else if (value is double)
            {
                typedValue = (double)value;
            }

            // Check value
            if (typedValue == 0d)
            {
                return 0d;
            }

            // Get factor
            double factor;
            if (!double.TryParse(parameter as string, out factor))
            {
                return 0d;
            }

            // Divide
            return typedValue / factor;
        }
    }
}

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