Created
July 3, 2014 11:42
-
-
Save MikeMKH/f09a7edbdd0ab8a23ace to your computer and use it in GitHub Desktop.
FizzBuzz kata using C# with NUnit and FsCheck
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Text.RegularExpressions; | |
using FsCheck; | |
using FsCheck.Fluent; | |
using NUnit.Framework; | |
namespace FizzBuzz | |
{ | |
public class FizzBuzzer | |
{ | |
public string Translate(int value) | |
{ | |
var result = string.Empty; | |
if (value % 3 == 0) result += "Fizz"; | |
if (value % 5 == 0) result += "Buzz"; | |
return string.IsNullOrEmpty(result) ? value.ToString() : result; | |
} | |
} | |
[TestFixture] | |
public class FizzBuzzerTests | |
{ | |
private FizzBuzzer _fizzBuzzer; | |
private static Gen<int> DivisibleBy(int divisor) | |
{ | |
var divisibleBy = | |
from number in Any.OfType<int>() | |
where number % divisor == 0 | |
select number; | |
return divisibleBy; | |
} | |
[SetUp] | |
public void BeforeEach() | |
{ | |
_fizzBuzzer = new FizzBuzzer(); | |
} | |
[TestCase(2, Result = "2")] | |
[TestCase(3, Result = "Fizz")] | |
[TestCase(5, Result = "Buzz")] | |
[TestCase(15, Result = "FizzBuzz")] | |
public string Given_Value_It_Must_Return_The_Given_Result(int value) | |
{ | |
return _fizzBuzzer.Translate(value); | |
} | |
[Test] | |
public void Given_A_Number_Not_Divisible_By_3_Or_5_It_Must_Return_That_Number() | |
{ | |
var value = | |
from number in Any.OfType<int>() | |
where number%3 != 0 && number%5 != 0 | |
select number; | |
Spec.For(value, v => _fizzBuzzer.Translate(v).Equals(v.ToString())) | |
.QuickCheckThrowOnFailure(); | |
} | |
[TestCase(3, "Fizz")] | |
[TestCase(5, "Buzz")] | |
[TestCase(15, "FizzBuzz")] | |
public void Given_A_Number_Divisible_By_Divisor_It_Must_Contain_Expected(int divisor, string expected) | |
{ | |
Spec.For(DivisibleBy(divisor), d => _fizzBuzzer.Translate(d).Contains(expected)) | |
.QuickCheckThrowOnFailure(); | |
} | |
[Test] | |
public void Given_A_Number_Divisibly_By_3_And_5_It_Must_Contain_Both_Fizz_And_Buzz() | |
{ | |
Spec.For(DivisibleBy(3*5), d => string.IsNullOrEmpty(_fizzBuzzer.Translate(d)) == false) | |
.And(d => _fizzBuzzer.Translate(d).Contains("Fizz")) | |
.And(d => _fizzBuzzer.Translate(d).Contains("Buzz")) | |
.QuickCheckThrowOnFailure(); | |
} | |
[Test] | |
public void Given_A_Number_It_Must_Return_Fizz_Buzz_FizzBuzz_Or_A_Number() | |
{ | |
Spec.ForAny<int>(x => true) | |
.Classify(x => _fizzBuzzer.Translate(x).Equals("Fizz"), "Fizz") | |
.Classify(x => _fizzBuzzer.Translate(x).Equals("Buzz"), "Buzz") | |
.Classify(x => _fizzBuzzer.Translate(x).Equals("FizzBuzz"), "FizzBuzz") | |
.Classify(x => Regex.IsMatch(_fizzBuzzer.Translate(x), @"\d+"), "number") | |
.QuickCheckThrowOnFailure(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See my post which goes with this gist: http://comp-phil.blogspot.com/2014/07/i-have-no-freaking-clue-what-i-am-doing.html