Last active
August 29, 2015 14:10
-
-
Save MikeMKH/ad7743439cea72446e7f to your computer and use it in GitHub Desktop.
FizzBuzz kata in C# using FsCheck with MS Test
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 FsCheck.Fluent; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
namespace FizzBuzz | |
{ | |
[TestClass] | |
public class FizzBuzzerTests | |
{ | |
[TestMethod] | |
public void GivenAnEvenNumber_ResultMustBeSelfAsString() | |
{ | |
Spec.ForAny<int>(x => true) | |
.Label("Even must return self as string") | |
.And(x => FizzBuzzer.ValueOf(x) == x.ToString()) | |
.When(x => x%2 == 0 && x%3 != 0 && x%5 != 0) | |
.QuickCheckThrowOnFailure(); | |
} | |
[TestMethod] | |
public void GivenNumberDivisibleBy3_ResultMustContainFizz() | |
{ | |
Spec.ForAny<int>(x => true) | |
.Label("Numbers divisible by 3 must contain Fizz") | |
.And(x => FizzBuzzer.ValueOf(x).Contains("Fizz")) | |
.When(x => x%3 == 0) | |
.QuickCheckThrowOnFailure(); | |
} | |
[TestMethod] | |
public void GivenNumberDivisibleBy5_ResultMustContainBuzz() | |
{ | |
Spec.ForAny<int>(x => true) | |
.Label("Numbers divisible by 5 must contain Buzz") | |
.And(x => FizzBuzzer.ValueOf(x).Contains("Buzz")) | |
.When(x => x%5 == 0) | |
.QuickCheckThrowOnFailure(); | |
} | |
[TestMethod] | |
public void GivenNumberDivisibleBy15_ResultMustBeFizzBuzz() | |
{ | |
Spec.ForAny<int>(x => true) | |
.Label("Numbers divisible by 15 must be FizzBuzz") | |
.And(x => FizzBuzzer.ValueOf(x * 15).Contains("FizzBuzz")) | |
.QuickCheckThrowOnFailure(); | |
} | |
[TestMethod] | |
public void GivenNumberNotDivisibleBy3Or5_ResultMustBeSelfAsString() | |
{ | |
Spec.ForAny<int>(x => true) | |
.Label("Numbers not divisible by 3 or 5 must be self as string") | |
.And(x => FizzBuzzer.ValueOf(x) == x.ToString()) | |
.When(x => x%3 != 0 && x % 5 != 0) | |
.QuickCheckThrowOnFailure(); | |
} | |
} | |
public class FizzBuzzer | |
{ | |
public static string ValueOf(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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment