The document discusses different approaches to testing the FizzBuzz problem using properties and traits. It describes testing FizzBuzz translations using NUnit tests, SpecFlow scenarios, and property-based testing with FsCheck. Examples are provided of generating test data to validate that numbers divisible by 3 return "Fizz", numbers divisible by 5 return "Buzz", and numbers divisible by 15 return "FizzBuzz". The document suggests creating a model to further demonstrate the robustness of the FizzBuzz solution.
26. I N E E D A
F I Z Z B U Z Z T R A N S L AT O R
27. C H E C K P O I N T
1 ) N O B M I ' D I V I S I B L E P O N G 3 F I Z Z J I C H E G H ' O H .
N O B M I ' D I V I S I B L E P O N G 5 B U Z Z J I C H E G H ' O H .
L AT L H S O ' M E H J I C H E G H ‘ O H .
!
2 ) ⎕ I O ← 0
( L , ' F I Z Z ' ' B U Z Z ' ' F I Z Z B U Z Z ' ) [ ¯ 1 +
( L × W = 0 ) + W ← ( 1 0 0 × ~ 0 = W ) + W ← ⊃ + / 1 2 × 0 = 3 5 |
⊂ L ← 1 + ⍳ 1 0 0 ]
34. 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;
}
}
}
39. Feature: FizzBuzzerSpec
In order to be able to communicate with the
FizzBuzzons
As a Starfleet Captain
I want to be able to translate numbers to their
FizzBuzz value
40. Scenario: 2 must translate to the string 2
Given a FizzBuzzer
And the value of '2'
When translate is invoked
Then the result should be '2'
41. Scenario: 3 must translate to the string Fizz
Given a FizzBuzzer
And the value of '3'
When translate is invoked
Then the result should be 'Fizz'
42. Scenario: 5 must translate to the string Buzz
Given a FizzBuzzer
And the value of '5'
When translate is invoked
Then the result should be 'Buzz'
43. Scenario: 15 must translate to the string FizzBuzz
Given a FizzBuzzer
And the value of '15'
When translate is invoked
Then the result should be 'FizzBuzz'
44. Feature: FizzBuzzerSpec
In order to be able to communicate with the FizzBuzzons
As a Starfleet Captain
I want to be able to translate numbers to their FizzBuzz value
Scenario: 2 must translate to the string 2
Given a FizzBuzzer
And the value of '2'
When translate is invoked
Then the result should be '2'
Scenario: 3 must translate to the string Fizz
Given a FizzBuzzer
And the value of '3'
When translate is invoked
Then the result should be 'Fizz'
Scenario: 5 must translate to the string Buzz
Given a FizzBuzzer
And the value of '5'
When translate is invoked
Then the result should be 'Buzz'
Scenario: 15 must translate to the string FizzBuzz
Given a FizzBuzzer
And the value of '15'
When translate is invoked
Then the result should be 'FizzBuzz'
45.
46. Feature: FizzBuzzerSpec
In order to be able to communicate with the
FizzBuzzons
As a Starfleet Captain
I want to be able to translate numbers to their
FizzBuzz value
47. Scenario: 2 must translate to the string 2
Given a FizzBuzzer
And the value of '2'
When translate is invoked
Then the result should be '2'
48. using FizzBuzz;
using TechTalk.SpecFlow;
using NUnit.Framework;
namespace FizzBuzzSpec
{
[Binding]
public class FizzBuzzerSpecSteps
{
[Given(@"a FizzBuzzer")]
public void GivenAFizzBuzzer()
{
ScenarioContext.Current.Add("FizzBuzzer", new FizzBuzzer());
}
[Given(@"the value of '(.*)'")]
public void GivenTheValueOf(int value)
{
ScenarioContext.Current.Add("value", value);
}
[When(@"translate is invoked")]
public void WhenTranslateIsInvoked()
{
var fizzbuzzer = ScenarioContext.Current.Get<FizzBuzzer>("FizzBuzzer");
var value = ScenarioContext.Current.Get<int>("value");
ScenarioContext.Current.Add("actual", fizzbuzzer.Translate(value));
}
[Then(@"the result should be '(.*)'")]
public void ThenTheResultShouldBe(string expected)
{
var actual = ScenarioContext.Current.Get<string>("actual");
Assert.That(expected, Is.EqualTo(actual));
}
}
}
49. Given a FizzBuzzer
!
[Given(@"a FizzBuzzer")]
public void GivenAFizzBuzzer()
{
ScenarioContext.Current.Add(
"FizzBuzzer", new FizzBuzzer());
}
50. And the value of '2'
!
[Given(@"the value of '(.*)'")]
public void GivenTheValueOf(int value)
{
ScenarioContext.Current.Add("value", value);
}
51. When translate is invoked
[When(@"translate is invoked")]
public void WhenTranslateIsInvoked()
{
var fizzbuzzer = ScenarioContext
.Current.Get<FizzBuzzer>("FizzBuzzer");
var value = ScenarioContext
.Current.Get<int>("value");
ScenarioContext
.Current.Add(
"actual", fizzbuzzer.Translate(value));
}
52. Then the result should be '2'
[Then(@"the result should be '(.*)'")]
public void ThenTheResultShouldBe(
string expected)
{
var actual = ScenarioContext
.Current.Get<string>("actual");
Assert.That(expected, Is.EqualTo(actual));
}
53. Scenario: 2 must translate to the string 2
Given a FizzBuzzer
And the value of '2'
When translate is invoked
Then the result should be '2'
54. Scenario: 5 must translate to the string Buzz
Given a FizzBuzzer
And the value of '5'
When translate is invoked
Then the result should be 'Buzz'
55. Given a FizzBuzzer
!
[Given(@"a FizzBuzzer")]
public void GivenAFizzBuzzer()
{
ScenarioContext.Current.Add(
"FizzBuzzer", new FizzBuzzer());
}
56. And the value of '5'
!
[Given(@"the value of '(.*)'")]
public void GivenTheValueOf(int value)
{
ScenarioContext.Current.Add("value", value);
}
57. When translate is invoked
[When(@"translate is invoked")]
public void WhenTranslateIsInvoked()
{
var fizzbuzzer = ScenarioContext
.Current.Get<FizzBuzzer>("FizzBuzzer");
var value = ScenarioContext
.Current.Get<int>("value");
ScenarioContext
.Current.Add(
"actual", fizzbuzzer.Translate(value));
}
58. Then the result should be 'Buzz'
[Then(@"the result should be '(.*)'")]
public void ThenTheResultShouldBe(
string expected)
{
var actual = ScenarioContext
.Current.Get<string>("actual");
Assert.That(expected, Is.EqualTo(actual));
}
59.
60.
61.
62. Scenario: 2 must translate to the string 2
Given a FizzBuzzer
And the value of '2'
When translate is invoked
Then the result should be '2'
63. [NUnit.Framework.TestAttribute()]
[NUnit.Framework.DescriptionAttribute("2 must translate to the string 2")]
public virtual void _2MustTranslateToTheString2()
{
TechTalk.SpecFlow.ScenarioInfo scenarioInfo = new TechTalk.SpecFlow.ScenarioInfo(
"2 must translate to the string 2", ((string[])(null)));
#line 6
this.ScenarioSetup(scenarioInfo);
#line 7
testRunner.Given("a FizzBuzzer", ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "Given ");
#line 8
testRunner.And("the value of '2'", ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "And ");
#line 9
testRunner.When("translate is invoked", ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "When ");
#line 10
testRunner.Then("the result should be '2'", ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "Then ");
#line hidden
this.ScenarioCleanup();
}
67. Scenario Outline: FizzBuzz translation
Given a FizzBuzzer
And the value of '<value>'
When translate is invoked
Then the result should be '<expected value>'
Examples:
| value | expected value |
| 2 | 2 |
| 4 | 4 |
| 3 | Fizz |
| 9 | Fizz |
| 5 | Buzz |
| 25 | Buzz |
| 15 | FizzBuzz |
| 30 | FizzBuzz |
69. [Given(@"the value of '(.*)'")]
public void GivenTheValueOf(int value)
{
ScenarioContext.Current.Add("value", value);
}
70. [When(@"translate is invoked")]
public void WhenTranslateIsInvoked()
{
var fizzbuzzer = ScenarioContext
.Current.Get<FizzBuzzer>("FizzBuzzer");
var value = ScenarioContext
.Current.Get<int>("value");
ScenarioContext
.Current.Add(
"actual", fizzbuzzer.Translate(value));
}
71. [Then(@"the result should be '(.*)'")]
public void ThenTheResultShouldBe(
string expected)
{
var actual = ScenarioContext
.Current.Get<string>("actual");
Assert.That(expected, Is.EqualTo(actual));
}