Writing Fizzbuzz Kata
In the current Tech era we must test our code as a unit test. TDD katas is a way to ensure that we are delivering good and stable/reliable code. Here we'll write a simple C# program and then write its Kata.
The followings are the basic requirements:
Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers that are multiples of both three and five print "FizzBuzz".
How to write the code
We need a procedure to write the program.
Let's divide this into the following multiple steps so we can easily write and test this:
- Print numbers from 1 to 100
- Print "Fizz" instead of the number that is divisible by 3
- Print "Buzz" instead of the number that is divisible by 5
- Print "FizzBuzz" instead of the number that is divisible by both 3 and 5
- public static string PrintFizzBuzz()
-
- {
-
- var resultFizzBuzz = string.Empty;
-
- resultFizzBuzz = GetNumbers(resultFizzBuzz);
-
- return resultFizzBuzz;
-
- }
In the preceding, we wrote a simple PrintFizzBuzz() method that is doing the stuff. So, follow our procedure and the following is the complete code:
- public class FizzBuzz
- {
- #region Public Methods
-
-
- public static string PrintFizzBuzz()
- {
- var resultFizzBuzz = string.Empty;
- resultFizzBuzz = GetNumbers(resultFizzBuzz);
- return resultFizzBuzz;
- }
- public static string PrintFizzBuzz(int number)
- {
- CanThrowArgumentExceptionWhenNumberNotInRule(number);
-
- var result = GetFizzBuzzResult(number);
-
- if (string.IsNullOrEmpty(result))
- result = GetFizzResult(number);
- if (string.IsNullOrEmpty(result))
- result = GetBuzzResult(number);
-
- return string.IsNullOrEmpty(result) ? number.ToString(CultureInfo.InvariantCulture) : result;
- }
- #endregion
-
- #region Private Methods
- private static string GetFizzBuzzResult(int number)
- {
- string result = null;
- if (IsFizz(number) && IsBuzz(number)) result = "FizzBuzz";
- return result;
- }
-
- private static string GetBuzzResult(int number)
- {
- string result = null;
- if (IsBuzz(number)) result = "Buzz";
- return result;
- }
-
- private static string GetFizzResult(int number)
- {
- string result = null;
- if (IsFizz(number)) result = "Fizz";
- return result;
- }
-
- private static void CanThrowArgumentExceptionWhenNumberNotInRule(int number)
- {
- if (number > 100 || number < 1)
- throw new ArgumentException(
- string.Format(
- "entered number is [{0}], which does not meet rule, entered number should be between 1 to 100.", number));
- }
-
- private static string GetNumbers(string resultFizzBuzz)
- {
-
- for (var i = 1; i <= 100; i++)
- {
- var printNumber = string.Empty;
- if (IsFizz(i)) printNumber += "Fizz";
- if (IsBuzz(i)) printNumber += "Buzz";
- if (IsNumber(printNumber))
- printNumber = (i).ToString(CultureInfo.InvariantCulture);
- resultFizzBuzz += " " + printNumber;
- }
- return resultFizzBuzz.Trim();
- }
- private static string GetNumbersUsingLinq(string resultFizzBuzz)
- {
- Enumerable.Range(1, 100)
- .Select(fb => String.Format("{0}{1}", fb % 3 == 0 ? "Fizz" : "", fb % 5 == 0 ? "Buzz" : ""))
- .Select(fb => fb != null ? (String.IsNullOrEmpty(fb) ? fb.ToString(CultureInfo.InvariantCulture) : fb) : null)
- .ToList()
- .ForEach(x => resultFizzBuzz = x);
- return resultFizzBuzz.Trim();
- }
-
- private static bool IsNumber(string printNumber)
- {
- return String.IsNullOrEmpty(printNumber);
- }
-
- private static bool IsBuzz(int i)
- {
- return i % 5 == 0;
- }
-
- private static bool IsFizz(int i)
- {
- return i % 3 == 0;
- }
- #endregion
- }
How to write TDD
Please note that here we are following Red Green and the Refactor approach of Test Driven Develoment.
- In Red stage: We pass the failed test
- Green Stage: When the test has passed it is a green stage
- Refactorin stage: Here we determine the possibilities to refactor the code.
The following are the tests:
- Create a method to accept single number
- Create test to verify supplied number within the range 1 to 100
- Create test to verify number and return result Fizz or Buzz or FizzBuzz per above criteria
Here are the complete tests:
- using System;
- using NUnit.Framework;
-
- namespace TDD_Katas_project.FizzBuzzKata
- {
- [TestFixture]
- [Category("TheFizzBuzzKata")]
- public class TestFizzBuzz
- {
- #region Private members
- private string _resultFizz;
- #endregion
-
- #region Setup/TearDown
- [TestFixtureSetUp]
- public void Setup()
- {
- _resultFizz = @"1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz 43 44 FizzBuzz 46 47 Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56 Fizz 58 59 FizzBuzz 61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71 Fizz 73 74 FizzBuzz 76 77 Fizz 79 Buzz Fizz 82 83 Fizz Buzz 86 Fizz 88 89 FizzBuzz 91 92 Fizz 94 Buzz Fizz 97 98 Fizz Buzz";
- }
- [TestFixtureTearDown]
- public void TearDown()
- {
- _resultFizz = null;
- }
- #endregion
-
- #region Test Methods
-
- [Test]
- public void CanTestFizz()
- {
- Console.WriteLine(FizzBuzz.PrintFizzBuzz());
- Assert.That(FizzBuzz.PrintFizzBuzz(), Is.EqualTo(_resultFizz));
- }
- [Test]
- [TestCase(-1)]
- [TestCase(101)]
- [TestCase(0)]
- public void CanThrowArgumentExceptionWhenSuppliedNumberDoesNotMeetRule(int number)
- {
- var exception = Assert.Throws<ArgumentException>(() => FizzBuzz.PrintFizzBuzz(number));
-
- Assert.That(exception.Message, Is.EqualTo(string.Format("entered number is [{0}], which does not meet rule, entered number should be between 1 to 100.", number)));
-
- }
- [Test]
- [TestCase(1, "1")]
- [TestCase(3, "Fizz")]
- [TestCase(5, "Buzz")]
- [TestCase(15, "FizzBuzz")]
- [TestCase(30, "FizzBuzz")]
- public void CanTestSingleNumber(int number, string expectedresult)
- {
- var actualresult = FizzBuzz.PrintFizzBuzz(number);
- Assert.That(actualresult, Is.EqualTo(expectedresult),
- string.Format("result of entered number [{0}] is [{1}] but it should be [{2}]", number,
- actualresult, expectedresult));
- }
- #endregion
- }
- }
Please note that in the above we used the Nunit framework for TDD.
You can download the entire collection of TDDs from GitHub:
https://github.com/garora/TDD-Katas.