Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MikeMKH/1a5a04c7868d0877d011 to your computer and use it in GitHub Desktop.
Save MikeMKH/1a5a04c7868d0877d011 to your computer and use it in GitHub Desktop.
FizzBuzz kata used to look into what can be done with Nunit.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using NUnit.Framework.Constraints;
namespace NUnit
{
[TestFixture]
public class CharacterizationTests
{
public string FizzBuzz(int value)
{
if(value < 0) throw new ArgumentException("Value must be positive.");
var ret = string.Empty;
if (value%3 == 0) ret += "Fizz";
if (value%5 == 0) ret += "Buzz";
return string.IsNullOrEmpty(ret) ? value.ToString() : ret;
}
[Test, ExpectedException(typeof (ArgumentException))]
public void Negative_Values_Throws_An_ArgumentException()
{
FizzBuzz(-1);
}
[Test, Sequential]
public void In_Order_Test_Fizz_Buzz(
[Values( 1, 2, 3, 4, 5, 15)] int value,
[Values("1", "2", "Fizz", "4", "Buzz", "FizzBuzz")] string expected)
{
Assert.That(FizzBuzz(value), Is.EqualTo(expected));
}
[Test]
public void Generate_A_Range_Of_Fizz_Data(
[Range(3, 300, 3)] int value)
{
var removeBuzz = (value % 5 == 0) ? 3 : value;
Assert.That(FizzBuzz(removeBuzz), Is.EqualTo("Fizz"));
}
[Test, ExpectedException(typeof (ArgumentException))]
public void Generate_A_Range_Of_Invali_Fizz_Data(
[Range(-1000, -1, 1)] int value)
{
FizzBuzz(value);
Assert.Fail("Should have thrown an exception");
}
// Random does not work with NCrunch unless NUnit is set to UseStaticAnalysis see also: http://forum.ncrunch.net/yaf_postst433_NUnit--Random--supported.aspx
[Test]
public void Generate_Buzz_Data(
[Random(1, 10000, 100)] int value)
{
var removeFizz = (value % 3 == 0) ? 5 : value * 5;
Assert.That(FizzBuzz(removeFizz), Is.EqualTo("Buzz"));
}
[TestCase( 0, Result = "FizzBuzz")]
[TestCase( 1, Result = "1")]
[TestCase( 2, Result = "2")]
[TestCase( 3, Result = "Fizz")]
[TestCase( 4, Result = "4")]
[TestCase( 5, Result = "Buzz")]
[TestCase( 6, Result = "Fizz")]
[TestCase(10, Result = "Buzz")]
[TestCase(15, Result = "FizzBuzz")]
[TestCase(45, Result = "FizzBuzz")]
[TestCase(-1, ExpectedException = typeof(ArgumentException))]
public string FizzBuzz_Test_Cases_With_Expected_Results(int value)
{
return FizzBuzz(value);
}
[TestCase( 0, "FizzBuzz")]
[TestCase( 1, "1")]
[TestCase( 2, "2")]
[TestCase( 3, "Fizz")]
[TestCase( 4, "4")]
[TestCase( 5, "Buzz")]
[TestCase( 6, "Fizz")]
[TestCase(10, "Buzz")]
[TestCase(15, "FizzBuzz")]
[TestCase(45, "FizzBuzz")]
[TestCase(-1, "error", ExpectedException = typeof(ArgumentException))]
public void FizzBuzz_Test_Cases(int value, string expected)
{
Assert.That(FizzBuzz(value), Is.EqualTo(expected));
}
[Datapoints] public int[] Values = new[] {-1, 0, 2, 3, 4, 5, 9, 15, 25, 45};
[Theory]
public void Numbers_Divisible_By_15_Will_Return_FizzBuzz(int value)
{
Assume.That(value % 15 == 0);
var actual = FizzBuzz(value);
Assert.That(actual, Is.Not.Null);
Assert.That(actual, Is.EqualTo("FizzBuzz"));
}
[Test]
public void Values_Not_Divisible_By_3_Or_5_Return_ToString_Value()
{
Assert.That(FizzBuzz(7), new EqualConstraint("7"));
var sixteen = 16;
Assert.That(FizzBuzz(sixteen), new EqualConstraint(sixteen.ToString(CultureInfo.InvariantCulture)));
}
public static object[] FizzBuzzTestData =
{
new object[] { 1, "1"},
new object[] { 2, "2"},
new object[] { 3, "Fizz"},
new object[] { 9, "Fizz"},
new object[] { 5, "Buzz"},
new object[] {10, "Buzz"},
new object[] { 0, "FizzBuzz"},
new object[] {15, "FizzBuzz"}
};
[Test, TestCaseSource("FizzBuzzTestData")]
public void FizzBuzz_Test_Data(int value, string expected)
{
Assert.That(FizzBuzz(value), Is.EqualTo(expected));
}
public class FizzBuzzTestCaseDataFactory
{
public static IEnumerable<TestCaseData> TestCaseData
{
get
{
yield return new TestCaseData(1).Returns("1");
yield return new TestCaseData(2).Returns("2");
yield return new TestCaseData(3).Returns("Fizz");
yield return new TestCaseData(33).Returns("Fizz");
yield return new TestCaseData(5).Returns("Buzz");
yield return new TestCaseData(55).Returns("Buzz");
yield return new TestCaseData(15).Returns("FizzBuzz");
yield return new TestCaseData(165).Returns("FizzBuzz");
yield return new TestCaseData(-1).Throws(typeof (ArgumentException));
yield return new TestCaseData(-11).Throws(typeof (ArgumentException));
}
}
}
[Test, TestCaseSource(typeof(FizzBuzzTestCaseDataFactory), "TestCaseData")]
public string Data_Factory_Test_Case(int value)
{
return FizzBuzz(value);
}
[Test,
ExpectedException(typeof (ArgumentException), ExpectedMessage = "Value must be positive.",
MatchType = MessageMatch.Exact)]
public void Given_A_Negative_1_It_Will_Throw_An_ArgumentException_With_The_Message_Of_ValueMustBePostive()
{
FizzBuzz(-1);
Assert.Fail("Should have thrown an execpetion");
}
}
}
@MikeMKH
Copy link
Author

MikeMKH commented Jun 1, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment