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

FizzBuzz Guided Kata

on

  • 4,603 views

 

Statistics

Views

Total Views
4,603
Views on SlideShare
4,038
Embed Views
565

Actions

Likes
3
Downloads
43
Comments
0

5 Embeds 565

http://blog.softwareontheside.com 536
http://www.agilecodegames.com 13
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 FizzBuzz Guided Kata Presentation Transcript

  • FizzBuzz Guided Kata for C# and NUnit Mike Clement mike@softwareontheside.com @mdclementhttp://blog.softwareontheside.com
  • FizzBuzz• If multiple of 3, get “Fizz”• If multiple of 5, get “Buzz”• If not, return input int as string• Rules are in order
  • Quick Concepts ReminderTDD Simple Design• Red • Passes all tests• Green • Clear, expressive, consistent• Refactor • No duplication • Minimal
  • 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 local dll)
  • using NUnit.Framework;[TestFixture]public class FizzBuzzTests{}
  • [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(); }}
  • [Test]public void TranslateOne(){ string result = Translator.Translate(1); Assert.That(result, Is.EqualTo("1"));}public static string Translate(int i){ return "1";}
  • [Test]public void TranslateTwo(){ string result = Translator.Translate(2); Assert.That(result, Is.EqualTo("2"));}public static string Translate(int i){ return i.ToString();}
  • [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();}
  • [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();}
  • [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();}
  • [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();}
  • [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();}
  • [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();}
  • [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();}
  • [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();}
  • [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();}
  • [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();}
  • [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;}
  • [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;}
  • [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;}
  • [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);}
  • [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);}
  • [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;}
  • [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;}
  • [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) ...
  • 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) ...