FizzBuzz Guided Kata

Download FizzBuzz Guided Kata

Post on 06-May-2015

4.770 views

Category:

Entertainment & Humor

0 download

Embed Size (px)

TRANSCRIPT

<ul><li>1.FizzBuzz Guided Katafor C# and NUnit Mike Clementmike@softwareontheside.com @mdclementhttp://blog.softwareontheside.com</li></ul> <p>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 ReminderTDDSimple 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 namedFizzBuzz Add a reference to NUnit (recommend usingNuGet 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&gt; Rules = newList&gt;{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&gt;{(i, returnString) =&gt; returnString + ((i%7 == 0) ? "Monkey": string.Empty),(i, returnString) =&gt; string.IsNullOrEmpty(returnString) ?i.ToString() : returnString};string result = translator.Translate(input);Assert.That(result, Is.EqualTo(expected));}public static IList&gt; 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&gt; Rules = ...public string Translate(int i) ... </p>