Day 1 of Code Kata 2

So as I stated yesterday, I am starting the Code Kata 2. So here is the good, the bad and the ugly of Day 1 of Code Kata 2.

Did you actually follow your Test Driven Development?

Well sort of. I did start by setting up a Day1Chop object and a Day1Chop_Tests object. Then I setup the unit test’s and set out the first Test Cases. I set the first test case as:

[TestCase(-1, 3, null)]
public void isDay1Chop_Returns_ExpectedValue(int expectedValue,int seekedValue, int[] ints)
{
   List inputs = new List();
   if(ints != null) 
   {
      inputs.AddRange(ints); 
   } 
   int result = d1.chop(seekedValue, inputs); 
   Assert.AreEqual(expectedValue,result, "Test Failed"); 
}

Then ran my test, it failed since I did not have a chop function in the Day1Chop. So I added it and…

Wait a damn minute, what the hell are you doing in the unit test. There is converting to lists and conditionals and all sorts of stuff.

Well, I needed to work around a limitation in the NUnit TestCase. I have never found a way of putting IEnumerator in a unit test parameter. There is probably a way but it alluded me when I was typing it up. Instead, I have to use an array. I like working with Lists so that is how I am working this project.

The conditional allows a null value in the test not to throw an exception in the test. So if the ints are null then the list will be null otherwise they are the same.

Anyway, back to the explanation.

…and had it return a negative one since it was an empty of any logic so never found anything regardless of what was submitted.

Next I added:

[TestCase(-1, 3, new[] { 1})]

It passed since the function only returned -1. Then the first success graces our tests and I had to add some logic to get it to pass.

[TestCase(0, 1, new[] { 1 })]

So what logic did you use to solve it on Day 1 of Code Kata 2?

Let’s find out.

public int chop(int numberToSeek, List intList)
{
   int indexCount = 0;
   foreach(int i in intList)
   {
      if(i == numberToSeek)
      {
         return indexCount;
      }
      indexCount++; 
   } 
   return -1; 
}

First since I am using a foreach loop, I needed to track which index was being used for the comparison so that I could return it. I used the indexCount for that. I had the foreach int, i compare to the numberToSeek and returned that int, otherwise continue on with the indexCount. If we ran out of ints in the intList then it returned the -1.

So there it is, Day 1 of Code Kata 2. I will enclose the full functions for Day1Chop.cs and Day1Chop_Test.cs down below.

Day1Chop.cs

using System;
using System.Collections.Generic;
namespace CodeKata2
{
   public class Day1Chop
   {
      public int chop(int numberToSeek, List intList)
      {
         int indexCount = 0;
         foreach(int i in intList)
         {
            if(i == numberToSeek)
            {
               return indexCount;
            }
            indexCount++; 
            } return -1; 
          } 
      }
}

Day1Chop_Tests.cs

using System;
using System.Collections.Generic;
using NUnit.Framework;
using CodeKata2;
namespace CodeKata2_Tests
{
   [TestFixture]
   public class Day1Chop_Tests
   {
      Day1Chop d1;
      [SetUp] 
     public void Setup() { 
        d1 = new Day1Chop(); 
     } 

      [TestCase(-1, 3, null)] 
      [TestCase(-1, 3, new[] { 1})] 
      [TestCase(0, 1, new[] { 1 })] 
      [TestCase(0, 1, new[] { 1,3,5})] 
      [TestCase(1, 3, new[] { 1,3,5})] 
      [TestCase(2, 5, new[] { 1,3,5})] 
      [TestCase(-1, 0, new[] { 1,3,5})] 
      [TestCase(-1, 2, new[] { 1,3,5})] 
      [TestCase(-1, 4, new[] { 1,3,5})] 
      [TestCase(-1, 6, new[] { 1,3,5})] 
      [TestCase(0, 1, new[] { 1,3,5,7})] 
      [TestCase(1, 3, new[] { 1,3,5,7})] 
      [TestCase(2, 5, new[] { 1,3,5,7})] 
      [TestCase(3, 7, new[] { 1,3,5,7})] 
      [TestCase(-1, 0, new[] { 1,3,5,7})] 
      [TestCase(-1, 2, new[] { 1,3,5,7})] 
      [TestCase(-1, 4, new[] { 1,3,5,7})] 
      [TestCase(-1, 6, new[] { 1,3,5,7})] 
      [TestCase(-1, 8, new[] { 1,3,5,7})] 
      public void isDay1Chop_Returns_ExpectedValue(int expectedValue,int seekedValue, int[] ints) 
      { 
         List<int> inputs = new List<int>(); 
         if(ints != null) 
         { 
            inputs.AddRange(ints); 
         } 
         int result = d1.chop(seekedValue, inputs); 
         Assert.AreEqual(expectedValue,result, "Test Failed"); 
       } 
   }
}

Leave a Reply

Your email address will not be published.