Your SlideShare is downloading. ×
0
FizzBuzz Guided Kata
FizzBuzz Guided Kata
FizzBuzz Guided Kata
FizzBuzz Guided Kata
FizzBuzz Guided Kata
FizzBuzz Guided Kata
FizzBuzz Guided Kata
FizzBuzz Guided Kata
FizzBuzz Guided Kata
FizzBuzz Guided Kata
FizzBuzz Guided Kata
FizzBuzz Guided Kata
FizzBuzz Guided Kata
FizzBuzz Guided Kata
FizzBuzz Guided Kata
FizzBuzz Guided Kata
FizzBuzz Guided Kata
FizzBuzz Guided Kata
FizzBuzz Guided Kata
FizzBuzz Guided Kata
FizzBuzz Guided Kata
FizzBuzz Guided Kata
FizzBuzz Guided Kata
FizzBuzz Guided Kata
FizzBuzz Guided Kata
FizzBuzz Guided Kata
FizzBuzz Guided Kata
FizzBuzz Guided Kata
FizzBuzz Guided Kata
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

FizzBuzz Guided Kata

4,916

Published on

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

No Downloads
Views
Total Views
4,916
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
53
Comments
0
Likes
4
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • Refactor step also includes refactoring tests!
  • Make it non-static
  • Transcript

    • 1. FizzBuzz Guided Kata for C# and NUnit Mike Clement mike@softwareontheside.com @mdclementhttp://blog.softwareontheside.com
    • 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. Quick Concepts ReminderTDD Simple Design• Red • Passes all tests• Green • Clear, expressive, consistent• Refactor • No duplication • Minimal
    • 4. Ways to get Green in TDD• Fake it• Obvious implementation• Triangulation
    • 5. Start!• Create a “Class Library” project named FizzBuzz• Add a reference to NUnit (recommend using NuGet but can use local dll)
    • 6. using NUnit.Framework;[TestFixture]public class FizzBuzzTests{}
    • 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. [Test]public void TranslateOne(){ string result = Translator.Translate(1); Assert.That(result, Is.EqualTo("1"));}public static string Translate(int i){ return "1";}
    • 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. [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. [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. [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. [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. [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. [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. [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. [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. [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. [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. [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. [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. [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. [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. [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. [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. [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. [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. 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) ...

    ×