FizzBuzz Guided Kata  for C# and NUnit           Mike Clement  mike@softwareontheside.com           @mdclementhttp://blog....
FizzBuzz•   If multiple of 3, get “Fizz”•   If multiple of 5, get “Buzz”•   If not, return input int as string•   Rules ar...
Quick Concepts ReminderTDD                Simple Design• Red              • Passes all tests• Green            • Clear, ex...
Ways to get Green in TDD• Fake it• Obvious implementation• Triangulation
Start!• Create a “Class Library” project named  FizzBuzz• Add a reference to NUnit (recommend using  NuGet but can use loc...
using NUnit.Framework;[TestFixture]public class FizzBuzzTests{}
[Test]public void TranslateOne(){    string result = Translator.Translate(1);    Assert.That(result, Is.EqualTo("1"));}pub...
[Test]public void TranslateOne(){    string result = Translator.Translate(1);    Assert.That(result, Is.EqualTo("1"));}pub...
[Test]public void TranslateTwo(){    string result = Translator.Translate(2);    Assert.That(result, Is.EqualTo("2"));}pub...
[TestCase(1, "1")]public void Translate(int input, string expected){   string result = Translator.Translate(input);   Asse...
[TestCase(1, "1")][TestCase(2, "2")]public void Translate(int input, string expected){    string result = Translator.Trans...
[TestCase(1, "1")][TestCase(2, "2")][TestCase(3, "Fizz")]public void Translate(int input, string expected){    string resu...
[TestCase(1, "1")][TestCase(2, "2")][TestCase(3, "Fizz")]public void Translate(int input, string expected){    string resu...
[TestCase(1, "1")][TestCase(2, "2")][TestCase(3, "Fizz")][TestCase(6, "Fizz")]public void Translate(int input, string expe...
[TestCase(1, "1")][TestCase(2, "2")][TestCase(3, "Fizz")][TestCase(6, "Fizz")]public void Translate(int input, string expe...
[TestCase(1, "1")][TestCase(2, "2")][TestCase(3, "Fizz")][TestCase(5, "Buzz")][TestCase(6, "Fizz")]public void Translate(i...
[TestCase(1, "1")][TestCase(2, "2")][TestCase(3, "Fizz")][TestCase(5, "Buzz")][TestCase(6, "Fizz")]public void Translate(i...
[TestCase(1, "1")][TestCase(2, "2")][TestCase(3, "Fizz")][TestCase(5, "Buzz")][TestCase(6, "Fizz")][TestCase(10, "Buzz")]p...
[TestCase(1, "1")][TestCase(2, "2")][TestCase(3, "Fizz")][TestCase(5, "Buzz")][TestCase(6, "Fizz")][TestCase(10, "Buzz")]p...
[TestCase(1, "1")][TestCase(2, "2")][TestCase(3, "Fizz")][TestCase(5, "Buzz")][TestCase(6, "Fizz")][TestCase(10, "Buzz")]p...
[TestCase(1, "1")][TestCase(2, "2")][TestCase(3, "Fizz")][TestCase(5, "Buzz")][TestCase(6, "Fizz")][TestCase(10, "Buzz")][...
[TestCase(1, "1")][TestCase(2, "2")][TestCase(3, "Fizz")][TestCase(5, "Buzz")][TestCase(6, "Fizz")][TestCase(10, "Buzz")][...
[TestCase(1, "1")][TestCase(2, "2")][TestCase(3, "Fizz")][TestCase(5, "Buzz")][TestCase(6, "Fizz")][TestCase(10, "Buzz")][...
[TestCase(1, "1")][TestCase(2, "2")][TestCase(3, "Fizz")][TestCase(5, "Buzz")][TestCase(6, "Fizz")][TestCase(10, "Buzz")][...
[TestCase(1, "1")][TestCase(2, "2")][TestCase(3, "Fizz")][TestCase(5, "Buzz")][TestCase(6, "Fizz")][TestCase(10, "Buzz")][...
[TestCase(1, "1")][TestCase(2, "2")][TestCase(3, "Fizz")][TestCase(5, "Buzz")][TestCase(6, "Fizz")][TestCase(10, "Buzz")][...
[TestCase(1, "1")][TestCase(2, "2")][TestCase(3, “3")][TestCase(7, "Monkey")][TestCase(14, "Monkey")]public void Translate...
public void Translate(int input, string expected){    var translator = new Translator();    string result = translator.Tra...
FizzBuzz Guided Kata
Upcoming SlideShare
Loading in...5
×

FizzBuzz Guided Kata

5,075

Published on

0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
5,075
On Slideshare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
55
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide
  • Refactor step also includes refactoring tests!
  • Make it non-static
  • Transcript of "FizzBuzz Guided Kata"

    1. 1. FizzBuzz Guided Kata for C# and NUnit Mike Clement mike@softwareontheside.com @mdclementhttp://blog.softwareontheside.com
    2. 2. FizzBuzz• If multiple of 3, get “Fizz”• If multiple of 5, get “Buzz”• If not, return input int as string• Rules are in order
    3. 3. Quick Concepts ReminderTDD Simple Design• Red • Passes all tests• Green • Clear, expressive, consistent• Refactor • No duplication • Minimal
    4. 4. Ways to get Green in TDD• Fake it• Obvious implementation• Triangulation
    5. 5. Start!• Create a “Class Library” project named FizzBuzz• Add a reference to NUnit (recommend using NuGet but can use local dll)
    6. 6. using NUnit.Framework;[TestFixture]public class FizzBuzzTests{}
    7. 7. [Test]public void TranslateOne(){ string result = Translator.Translate(1); Assert.That(result, Is.EqualTo("1"));}public class Translator{ public static string Translate(int i) { throw new NotImplementedException(); }}
    8. 8. [Test]public void TranslateOne(){ string result = Translator.Translate(1); Assert.That(result, Is.EqualTo("1"));}public static string Translate(int i){ return "1";}
    9. 9. [Test]public void TranslateTwo(){ string result = Translator.Translate(2); Assert.That(result, Is.EqualTo("2"));}public static string Translate(int i){ return i.ToString();}
    10. 10. [TestCase(1, "1")]public void Translate(int input, string expected){ string result = Translator.Translate(input); Assert.That(result, Is.EqualTo(expected));}public static string Translate(int i){ return i.ToString();}
    11. 11. [TestCase(1, "1")][TestCase(2, "2")]public void Translate(int input, string expected){ string result = Translator.Translate(input); Assert.That(result, Is.EqualTo(expected));}public static string Translate(int i){ return i.ToString();}
    12. 12. [TestCase(1, "1")][TestCase(2, "2")][TestCase(3, "Fizz")]public void Translate(int input, string expected){ string result = Translator.Translate(input); Assert.That(result, Is.EqualTo(expected));}public static string Translate(int i){ return i.ToString();}
    13. 13. [TestCase(1, "1")][TestCase(2, "2")][TestCase(3, "Fizz")]public void Translate(int input, string expected){ string result = Translator.Translate(input); Assert.That(result, Is.EqualTo(expected));}public static string Translate(int i){ if (i == 3) return "Fizz"; return i.ToString();}
    14. 14. [TestCase(1, "1")][TestCase(2, "2")][TestCase(3, "Fizz")][TestCase(6, "Fizz")]public void Translate(int input, string expected){ string result = Translator.Translate(input); Assert.That(result, Is.EqualTo(expected));}public static string Translate(int i){ if (i == 3) return "Fizz"; return i.ToString();}
    15. 15. [TestCase(1, "1")][TestCase(2, "2")][TestCase(3, "Fizz")][TestCase(6, "Fizz")]public void Translate(int input, string expected){ string result = Translator.Translate(input); Assert.That(result, Is.EqualTo(expected));}public static string Translate(int i){ if (i % 3 == 0) return "Fizz"; return i.ToString();}
    16. 16. [TestCase(1, "1")][TestCase(2, "2")][TestCase(3, "Fizz")][TestCase(5, "Buzz")][TestCase(6, "Fizz")]public void Translate(int input, string expected){ string result = Translator.Translate(input); Assert.That(result, Is.EqualTo(expected));}public static string Translate(int i){ if (i % 3 == 0) return "Fizz"; return i.ToString();}
    17. 17. [TestCase(1, "1")][TestCase(2, "2")][TestCase(3, "Fizz")][TestCase(5, "Buzz")][TestCase(6, "Fizz")]public void Translate(int input, string expected){ string result = Translator.Translate(input); Assert.That(result, Is.EqualTo(expected));}public static string Translate(int i){ if (i % 3 == 0) return "Fizz"; if (i == 5) return "Buzz"; return i.ToString();}
    18. 18. [TestCase(1, "1")][TestCase(2, "2")][TestCase(3, "Fizz")][TestCase(5, "Buzz")][TestCase(6, "Fizz")][TestCase(10, "Buzz")]public void Translate(int input, string expected){ string result = Translator.Translate(input); Assert.That(result, Is.EqualTo(expected));}public static string Translate(int i){ if (i % 3 == 0) return "Fizz"; if (i == 5) return "Buzz"; return i.ToString();}
    19. 19. [TestCase(1, "1")][TestCase(2, "2")][TestCase(3, "Fizz")][TestCase(5, "Buzz")][TestCase(6, "Fizz")][TestCase(10, "Buzz")]public void Translate(int input, string expected){ string result = Translator.Translate(input); Assert.That(result, Is.EqualTo(expected));}public static string Translate(int i){ if (i % 3 == 0) return "Fizz"; if (i % 5 == 0) return "Buzz"; return i.ToString();}
    20. 20. [TestCase(1, "1")][TestCase(2, "2")][TestCase(3, "Fizz")][TestCase(5, "Buzz")][TestCase(6, "Fizz")][TestCase(10, "Buzz")]public static string Translate(int i){ if (ShouldFizz(i)) return "Fizz"; if (ShouldBuzz(i)) return "Buzz"; return i.ToString();}private static bool ShouldBuzz(int i){ return i % 5 == 0;}private static bool ShouldFizz(int i){ return i % 3 == 0;}
    21. 21. [TestCase(1, "1")][TestCase(2, "2")][TestCase(3, "Fizz")][TestCase(5, "Buzz")][TestCase(6, "Fizz")][TestCase(10, "Buzz")][TestCase(15, "FizzBuzz")]public static string Translate(int i){ if (ShouldFizz(i)) return "Fizz"; if (ShouldBuzz(i)) return "Buzz"; return i.ToString();}private static bool ShouldBuzz(int i){ return i % 5 == 0;}private static bool ShouldFizz(int i){ return i % 3 == 0;}
    22. 22. [TestCase(1, "1")][TestCase(2, "2")][TestCase(3, "Fizz")][TestCase(5, "Buzz")][TestCase(6, "Fizz")][TestCase(10, "Buzz")][TestCase(15, "FizzBuzz")]public static string Translate(int i){ string returnString = string.Empty; if (ShouldFizz(i)) returnString += "Fizz"; if (ShouldBuzz(i)) returnString += "Buzz"; if (string.IsNullOrEmpty(returnString)) { returnString = i.ToString(); } return returnString;}
    23. 23. [TestCase(1, "1")][TestCase(2, "2")][TestCase(3, "Fizz")][TestCase(5, "Buzz")][TestCase(6, "Fizz")][TestCase(10, "Buzz")][TestCase(15, "FizzBuzz")]public static string Translate(int i){ string returnString = string.Empty; returnString = Fizzy(i, returnString); if (ShouldBuzz(i)) returnString += "Buzz"; if (string.IsNullOrEmpty(returnString)) { returnString = i.ToString(); } return returnString;}private static string Fizzy(int i, string returnString){ return returnString + (ShouldFizz(i) ? "Fizz" : string.Empty);}
    24. 24. [TestCase(1, "1")][TestCase(2, "2")][TestCase(3, "Fizz")][TestCase(5, "Buzz")][TestCase(6, "Fizz")][TestCase(10, "Buzz")][TestCase(15, "FizzBuzz")]public static string Translate(int i){ string returnString = string.Empty; returnString = Fizzy(i, returnString); returnString = Buzzy(i, returnString); if (string.IsNullOrEmpty(returnString)) { returnString = i.ToString(); } return returnString;}private static string Buzzy(int i, string returnString){ return returnString + (ShouldBuzz(i) ? "Buzz" : string.Empty);}
    25. 25. [TestCase(1, "1")][TestCase(2, "2")][TestCase(3, "Fizz")][TestCase(5, "Buzz")][TestCase(6, "Fizz")][TestCase(10, "Buzz")][TestCase(15, "FizzBuzz")]public static string Translate(int i){ string returnString = string.Empty; returnString = Fizzy(i, returnString); returnString = Buzzy(i, returnString); returnString = Other(i, returnString); return returnString;}private static string Other(int i, string returnString){ return string.IsNullOrEmpty(returnString) ? i.ToString() :returnString;}
    26. 26. [TestCase(1, "1")][TestCase(2, "2")][TestCase(3, "Fizz")][TestCase(5, "Buzz")][TestCase(6, "Fizz")][TestCase(10, "Buzz")][TestCase(15, "FizzBuzz")]public static IList<Func<int, string, string>> Rules = newList<Func<int, string, string>>{ Fizzy, Buzzy, Other};public static string Translate(int i){ string returnString = string.Empty; foreach (var rule in Rules) { returnString = rule(i, returnString); } return returnString;}
    27. 27. [TestCase(1, "1")][TestCase(2, "2")][TestCase(3, “3")][TestCase(7, "Monkey")][TestCase(14, "Monkey")]public void TranslateDifferentRules(int input, string expected){ var translator = new Translator(); translator.Rules = new List<Func<int, string, string>> { (i, returnString) => returnString + ((i%7 == 0) ? "Monkey": string.Empty), (i, returnString) => string.IsNullOrEmpty(returnString) ?i.ToString() : returnString }; string result = translator.Translate(input); Assert.That(result, Is.EqualTo(expected));}public static IList<Func<int, string, string>> Rules = ...public static string Translate(int i) ...
    28. 28. public void Translate(int input, string expected){ var translator = new Translator(); string result = translator.Translate(input); Assert.That(result, Is.EqualTo(expected));}public IList<Func<int, string, string>> Rules = ...public string Translate(int i) ...
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×