Skip to content

Instantly share code, notes, and snippets.

@MikeMKH
Created July 14, 2014 11:54
Show Gist options
  • Save MikeMKH/37caa6868012b2be0705 to your computer and use it in GitHub Desktop.
Save MikeMKH/37caa6868012b2be0705 to your computer and use it in GitHub Desktop.
FizzBuzz kata in C# using FsCheck and NUnit
using System;
using System.Collections.Generic;
using System.Linq;
using FsCheck.Fluent;
using NUnit.Framework;
namespace FizzBuzz
{
[TestFixture]
public class FizzBuzzerTests
{
[TestCase(3, "Fizz")]
[TestCase(5, "Buzz")]
[TestCase(15, "FizzBuzz")]
public void Given_Value_Translator_Result_Must_Contain_Given(int value, string contains)
{
var fizzbuzzer = new FizzBuzzer();
Spec.For(
Any.OfType<int>().Where(x => x%value == 0),
x => fizzbuzzer.Translate(x).Contains(contains)).QuickCheckThrowOnFailure();
}
[TestCase]
public void Given_A_Value_Not_Divisible_By_3_Nor_5_It_Must_Return_Value()
{
var fizzbuzzer = new FizzBuzzer();
Spec.For(
Any.OfType<int>().Where(x => x%3 != 0).Where(x => x%5 != 0),
x => fizzbuzzer.Translate(x).Equals(x.ToString())).QuickCheckThrowOnFailure();
}
}
public class FizzBuzzer
{
public string Translate(int value)
{
var result = (new List<Func<int, string>>
{
(x => x%3 == 0 ? "Fizz" : string.Empty),
(x => x%5 == 0 ? "Buzz" : string.Empty)
}).Aggregate(string.Empty, (m, f) => m += f(value));
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