FizzBuzz Guided Kata
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

FizzBuzz Guided Kata

on

  • 4,887 views

 

Statistics

Views

Total Views
4,887
Views on SlideShare
4,310
Embed Views
577

Actions

Likes
3
Downloads
43
Comments
0

5 Embeds 577

http://blog.softwareontheside.com 545
http://www.agilecodegames.com 16
http://feeds.feedburner.com 9
http://7414512759580466_9b3378270d5380bba828e28eee71721220422263.blogspot.com 4
http://theoldreader.com 3

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • Refactor step also includes refactoring tests!
  • Make it non-static

FizzBuzz Guided Kata Presentation 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) ...