Search code examples
c#wpfunit-testingwpfdatagriddatagridtextcolumn

How to unit test a custom DataGridTextColumn implementation


I subclassed DataGridTextColumn to implement an editable DataGridColumn for numeric values. Because the logic inside got somewhat complex, I'd like to unit test the implementation with NUnit. However, I can't figure out how to fill a single cell of it with test data and trigger its validation logic. Any help would be much appreciated.

This is the complete implementation:

using System;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Presentation
{
    public class DataGridNumericColumn : DataGridTextColumn
    {
        private static readonly Regex NonnumericChars = new Regex(@"\D");
        private static readonly Regex NonnumericCharsExceptFirstMinus = 
            new Regex(@"(?<!^[^-]*)-|[^\d-]");

        public bool AllowNegativeValues { private get; set; }

        protected override object PrepareCellForEdit(
            FrameworkElement editingElement, RoutedEventArgs editingEventArgs)
        {
            var textBox = (TextBox) editingElement;
            textBox.PreviewTextInput += this.OnPreviewTextInput;
            textBox.PreviewLostKeyboardFocus += this.OnPreviewLostKeyboardFocus;
            DataObject.AddPastingHandler(textBox, this.OnPaste);
            return base.PrepareCellForEdit(editingElement, editingEventArgs);
        }

        private void OnPreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (this.AllowNegativeValues && e.Text == "-")
                return;

            if (!this.IsNumeric(e.Text))
                e.Handled = true;
        }

        private void OnPreviewLostKeyboardFocus(
            object sender, KeyboardFocusChangedEventArgs e)
        {
            var textBox = (TextBox) sender;
            textBox.Text = this.ExtractNumericParts(textBox.Text);
        }

        private void OnPaste(object sender, DataObjectPastingEventArgs e)
        {
            if(!e.SourceDataObject.GetDataPresent(DataFormats.Text, true))
                return;

            var textBox = (TextBox)sender;
            var preSelection = textBox.Text.Substring(0, textBox.SelectionStart);
            var postSelection = textBox.Text.Substring(textBox.SelectionStart 
                + textBox.SelectionLength);
            var pastedText = (string)e.SourceDataObject.GetData(DataFormats.Text);

            if (!this.IsNumeric(preSelection + pastedText + postSelection))
                e.CancelCommand();
        }

        private bool IsNumeric(string text)
        {
            int number;
            var isInt32 = Int32.TryParse(text, out number);

            if (!this.AllowNegativeValues)
                return isInt32 && number >= 0;

            return isInt32;
        }

        private string ExtractNumericParts(string text)
        {
            if (this.IsNumeric(text))
                return text;

            text = this.RemoveIllegalChars(text);

            return this.TrimDigitsToInt32Range(text);
        }

        private string RemoveIllegalChars(string text)
        {
            var illegalChars = this.AllowNegativeValues 
                ? NonnumericCharsExceptFirstMinus 
                : NonnumericChars;
            return illegalChars.Replace(text, string.Empty);
        }

        private string TrimDigitsToInt32Range(string numericText)
        {
            if (string.IsNullOrEmpty(numericText))
                return "0";

            return this.IsNumeric(numericText)
                ? numericText
                : this.TrimDigitsToInt32Range(
                    numericText.Remove(numericText.Length - 1));
        }
    }
}

Solution

  • Realized that what I was trying to do is a stupid thing. The solution is to extract the behavior into a testable class.