Test Driven Development- practicising with The word wrap Kata


In these days Test driven development is growing and TDD Katas is a best way to learn this approach. In this code-snippet, we will see a famous kata called word wrap kata. Firstly, we will check the problem Secondly, we will divide the problem in step(s) Finally, we will write code

Introduction


In this code snippet we are going to write a famous kata i.e. word wrap kata

Problem


We need a function or a tiny program, which breaks words on specified space with new line. Its nothing but merely similar to word-processor.



Solution


We divided the problem in following step(s):

1. Create a public class named WordWrap
2. Create a method 'wrap' it can be a static method
3. Create a function which count the number of spliter words of non-space
4. check for if words already having newline ['\n'] characters - ignore in counts
5. Check for multiple lines
6. Remove blank spaces if any in new line eg. Actual - "this is a test" WrappedText = "this\n is a t\nest"


Following is the complete code snippet, which solve the above problem:



WordWrap.cs




using System;
using System.Globalization;

namespace TDD_Katas_project.The_WordWrap_Kata
{
public class WordWrap
{
#region Public Methods
public static string Wrap(string word, int wordLength)
{
var actualCount = 0;
var wrappedword = string.Empty;

if (IsContainNewLine(word)) return word;

if (IsContainNullEmptyOrWhiteSpaces(word)) return string.Empty;

foreach (var wrd in word)
{
wrappedword = wrappedword + Convert.ToString(wrd);

if (IsWhiteSpaceOrNewLine(wrd)) continue;

if (IsContainNewLine(wrd.ToString(CultureInfo.InvariantCulture))) continue;

actualCount++;

if (actualCount == wordLength)
wrappedword += "\n";
}

wrappedword = GetWrappedwordWithoutBlankSpacesAtStartOfNewLine(wrappedword);

return wrappedword;
}
#endregion

#region MyRegion

private static string GetWrappedwordWithoutBlankSpacesAtStartOfNewLine(string wrappedword)
{
var newWrappedWord = wrappedword;
var spaceCounter = 0;

for (var outCounter = 0; outCounter < wrappedword.Length; outCounter++)
{
if (IsContainNewLine(wrappedword[outCounter].ToString(CultureInfo.InvariantCulture)))
for (var inCounter = outCounter + 1; inCounter < wrappedword.Length; inCounter++)
{
if (char.IsWhiteSpace(wrappedword[inCounter]))
spaceCounter++;
else
break;
}

if (spaceCounter <= 0) continue;

newWrappedWord = RemoveWhiteSpacesFromWrappedWord(wrappedword, outCounter, spaceCounter); //RemoveWhiteSpacesFromWrappedWord(wrappedword, outCounter + 1, spaceCounter);

spaceCounter = 0;
}

return newWrappedWord;
}

private static string RemoveWhiteSpacesFromWrappedWord(string wrappedword, int outCounter, int spaceCounter)
{
return wrappedword.Remove(outCounter + 1, spaceCounter);
}

private static bool IsContainNewLine(string word)
{
return word == "\n";
}

private static bool IsContainNullEmptyOrWhiteSpaces(string word)
{
return (string.IsNullOrEmpty(word)) || (string.IsNullOrWhiteSpace(word));
}

private static bool IsWhiteSpaceOrNewLine(char wrd)
{
return char.IsWhiteSpace(wrd) && (wrd == '\n');
}

#endregion
}
}


WordWrapTest.cs




using NUnit.Framework;

namespace TDD_Katas_project.The_WordWrap_Kata
{
[TestFixture]
[Category("The Word Wrap Kata")]
public class WordWrapTest
{
[Test]
public void CanWrapSingleLine()
{
Assert.That("Let's\nGo", Is.EqualTo(WordWrap.Wrap("Let's Go", 5)));
}
[Test]
public void CanTestForNullWord()
{
Assert.That("", Is.EqualTo(WordWrap.Wrap(null, 5)));
}
[Test]
public void CanTestForNullOrWhiteSpacesWord()
{
Assert.That("", Is.EqualTo(WordWrap.Wrap(null, 5)));
Assert.That("", Is.EqualTo(WordWrap.Wrap(" ", 5)));
}
[Test]
public void CanTestNewLineCharacter()
{
Assert.That("\n", Is.EqualTo(WordWrap.Wrap("\n", 1)));
Assert.That("\nLet's\nGo\noutside.", Is.EqualTo(WordWrap.Wrap("\nLet's Go\noutside.", 5)));
}
[Test]
public void CanWrapMultipleLine()
{
const string inputword = "Today is friday. \nLets go outside.\n Happy weekedn!";
const string expectedword = "Today\n is friday. \nLets go outside.\nHappy weekedn!";
var actualresult = (WordWrap.Wrap(inputword, 5));
Assert.That(expectedword, Is.EqualTo(actualresult),
string.Format("result of entered word [{0}] is [{1}] but it should be [{2}]", inputword,
actualresult, expectedword));
}
}
}


Note that in above snippet I have used NUnit Framework for testing.


Article by Gaurav Aroraa
Gaurav is a Microsoft Technology Specialist professional. He has awarded lifetime membership from Computer Society of India (CSI). He has more than 13yrs of experience in the industry. Currently, he is working in the capacity of Solution Architect with an MNC. He is serving to the various communities since 1999.

Follow Gaurav Aroraa or read 149 articles authored by Gaurav Aroraa

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: