Skip to main content

Hands on Test Driven Development Katas

In these days, Test Driven Development (TDD) is one of the most growing things in the technical world. Most of us are following Agile methodology where we would like to test our code within code.In this article, we will discuss all about TDD Katas and how we can get hands-on with

C# (10.0 MB)
 
 
 
 
 
5 Star
(1)
223 times
Add to favorites
12/24/2015
E-mail Twitter del.icio.us Digg Facebook

Solution explorer

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using TDD_Katas_project.Common.Utility;

namespace TDD_Katas_project.The_Natural_String_Sorting_Kata
{
    public class NaturalStringSorting
    {
        #region Member variables
        public enum SortOrder
        {
            Ascending, Descending
        }
        #endregion

        #region Public Methods

        public List<string> SortString(string[] strItems)
        {
            return SortString(strItems, SortOrder.Ascending);
        }

        public List<string> SortString(string[] strItems, SortOrder order)
        {
            Func<string, object> convert = str =>
                {
                    try
                    {
                        return int.Parse(str);
                    }
                    catch
                    {
                        return str;
                    }
                };

            return GetSortedList(strItems, order, convert);

        }
        #endregion

        #region Private Method

        private static List<string> GetSortedList(IEnumerable<string> strItems, SortOrder order, Func<string, object> convert)
        {
            List<string> sorted;
            switch (order)
            {
                case SortOrder.Descending:
                    sorted = strItems.OrderByDescending(
                        str => Regex.Split(str.Replace(" ", ""), "([0-9]+)").Select(convert),
                        new EnumerableComparer<object>()).ToList();
                    break;
                default:
                    sorted = strItems.OrderBy(
                        str => Regex.Split(str.Replace(" ", ""), "([0-9]+)").Select(convert),
                        new EnumerableComparer<object>()).ToList();
                    break;
            }
            return sorted;
        }

        #endregion
    }
}