Code:
public class FizzBuzz
{
public string DoFizzBuzz(uint value)
{
string result = string.Empty;
if (value == 0) return result;
if (value%3 == 0) result = "fizz";
if (value%5 == 0) result = result + "buzz";
if (string.IsNullOrEmpty(result)) result = value.ToString();
return result;
}
public string DoFizzBuzzExtended(uint value)
{
string result = string.Empty;
string text = value.ToString();
if (value == 0) return result;
if (value % 3 == 0 || text.Contains("3"))
{
result = "fizz";
}
if (value % 5 == 0 || text.Contains("5"))
{
result = result + "buzz";
}
if (string.IsNullOrEmpty(result)) return text;
return result;
}
}
Test:
[TestFixture]
public class FizzBuzzTest
{
[TestCase(0, "")]
[TestCase(1, "1")]
[TestCase(2, "2")]
[TestCase(3, "fizz")]
[TestCase(4, "4")]
[TestCase(5, "buzz")]
[TestCase(6, "fizz")]
[TestCase(10, "buzz")]
[TestCase(15, "fizzbuzz")]
public void DoFizzBuzzTest(int value, string expected)
{
// arrange
FizzBuzz target = new FizzBuzz();
// act
string actual = target.DoFizzBuzz((uint)value);
// assert
Assert.AreEqual(expected, actual);
}
[TestCase(0, "")]
[TestCase(1, "1")]
[TestCase(2, "2")]
[TestCase(3, "fizz")]
[TestCase(4, "4")]
[TestCase(5, "buzz")]
[TestCase(6, "fizz")]
[TestCase(10, "buzz")]
[TestCase(13, "fizz")]
[TestCase(15, "fizzbuzz")]
[TestCase(25, "buzz")]
[TestCase(35, "fizzbuzz")]
[TestCase(52, "buzz")]
[TestCase(53, "fizzbuzz")]
public void DoFizzBuzzExtendedTest(int value, string expected)
{
// arrange
FizzBuzz target = new FizzBuzz();
// act
string actual = target.DoFizzBuzzExtended((uint)value);
// assert
Assert.AreEqual(expected, actual);
}
}