<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Rash thoughts about .NET, C#, F# and Dynamics NAV. &#187; NaturalSpec</title>
	<atom:link href="http://www.navision-blog.de/tag/naturalspec/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.navision-blog.de</link>
	<description>This Blog is about Microsoft Dynamics NAV (f.k.a Navision incl. C/SIDE and C/AL), C#, F# and .NET in general.</description>
	<lastBuildDate>Wed, 14 Jul 2010 11:12:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Solving KataYahtzee with F# and NaturalSpec</title>
		<link>http://www.navision-blog.de/2010/04/24/solving-kata-yahtzee-with-fsharp-and-naturalspec/</link>
		<comments>http://www.navision-blog.de/2010/04/24/solving-kata-yahtzee-with-fsharp-and-naturalspec/#comments</comments>
		<pubDate>Sat, 24 Apr 2010 19:04:36 +0000</pubDate>
		<dc:creator>Steffen Forkmann</dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[Informatik]]></category>
		<category><![CDATA[Kata]]></category>
		<category><![CDATA[BDD]]></category>
		<category><![CDATA[Code Quality]]></category>
		<category><![CDATA[KataYahtzee]]></category>
		<category><![CDATA[NaturalSpec]]></category>
		<category><![CDATA[nunit]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://www.navision-blog.de/2010/04/24/solving-kata-yahtzee-with-fsharp-and-naturalspec/</guid>
		<description><![CDATA[Today I’m starting a new blog post series about solving code katas in F# and with the help of my NaturalSpec project. A code kata is a programming exercise which helps to improve your skills through practice and repetition. In this series we want to use the Test Driven Development TDD approach which means in [...]]]></description>
			<content:encoded><![CDATA[<p>Today I’m starting a new blog post series about solving code katas in <a href="http://msdn.microsoft.com/en-us/fsharp/default.aspx">F#</a> and with the help of my <a href="http://bitbucket.org/forki/naturalspec/wiki/Home">NaturalSpec</a> project. A code kata is a programming exercise which helps to improve your skills through practice and repetition. In this series we want to use the <a href="http://en.wikipedia.org/wiki/Test-driven_development">Test Driven Development TDD</a> approach which means in the context of NaturalSpec that we have to write our specs before we implement the algorithm.</p>
<h4>Problem Description</h4>
<blockquote><p>“The game of yahtzee is a simple dice game. Each round, each player rolls five six sided dice. The player may choose to reroll some or all of the dice up to three times (including the original roll). The player then places the roll at a category, such as ones, twos, sixes, pair, two pairs etc. If the roll is compatible with the score, the player gets a score for this roll according to the rules. If the roll is not compatible, the player gets a score of zero for this roll. </p>
<p>The kata consists of creating the rules to score a roll in any of a predefined category. Given a roll and a category, the final solution should output the score for this roll placed in this category.”</p>
<p align="right">[<a href="http://www.codingdojo.org/cgi-bin/wiki.pl?KataYahtzee">codingdojo.org</a>]</p>
</blockquote>
<h5>Category 1 – Ones, Twos, Threes, Fours, Fives, Sixes</h5>
<blockquote><p>“Ones, Twos, Threes, Fours, Fives, Sixes: The player scores the sum of the dice that reads one, two, three, four, five or six, respectively. For example, 1, 1, 2, 4, 4 placed on &quot;fours&quot; gives 8 points.”</p>
</blockquote>
<p>After reading this category description we could come up with the following spec:</p>
<div style="font-family: courier new; background: white; color: black; font-size: 10pt">
<p style="margin: 0px"><span style="color: blue">module</span> Yahtzee.Specs</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px"><span style="color: blue">open</span> NaturalSpec</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px"><span style="color: blue">let</span> placed_on category list =</p>
<p style="margin: 0px">&#160;&#160;&#160; printMethod category</p>
<p style="margin: 0px">&#160;&#160;&#160; calcValue category list</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">[&lt;Scenario&gt;]&#160;&#160;&#160;&#160; </p>
<p style="margin: 0px"><span style="color: blue">let</span> &#8220;Given 1,1,2,4,4 placed on &quot;fours&quot; gives 8 points.&#8220; () =&#160;&#160; </p>
<p style="margin: 0px">&#160;&#160;&#160; Given (1, 1, 2, 4, 4)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; When (placed_on Fours)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; It should equal 8</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; Verify</p>
</p></div>
<p>Since we really want to implement six different categories we should also add some more scenarios like this one:</p>
<div style="font-family: courier new; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">[&lt;Scenario&gt;]&#160;&#160;&#160;&#160; </p>
<p style="margin: 0px"><span style="color: blue">let</span> &#8220;Given 1,1,6,4,6 placed on &quot;sixes&quot; gives 12 points.&#8220; ()=&#160;&#160; </p>
<p style="margin: 0px">&#160;&#160;&#160; Given (1, 1, 6, 4, 6)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; When (placed_on Sixes)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; It should equal 12</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; Verify</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">// &#8230;</p>
</p></div>
<p>Now we have some specs but they will all fail since we don’t have anything implemented yet. So we have to come up with a model of dice rolls and categories. As the specs suggests we model the dice roll as a tuple of ints and the category as a <a href="http://msdn.microsoft.com/en-us/library/dd233226.aspx">discriminated union</a>:</p>
<div style="font-family: courier new; background: white; color: black; font-size: 10pt">
<p style="margin: 0px"><span style="color: blue">module</span> Yahtzee.Model</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px"><span style="color: blue">type</span> Roll = int * int * int * int * int</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px"><span style="color: blue">type</span> Category =</p>
<p style="margin: 0px">| Ones</p>
<p style="margin: 0px">| Twos</p>
<p style="margin: 0px">| Threes</p>
<p style="margin: 0px">| Fours</p>
<p style="margin: 0px">| Fives</p>
<p style="margin: 0px">| Sixes&#160;&#160; </p>
</p></div>
<p>The tuple is a natural choice for the dice roll, but for easier calculation we add a helper function which converts it into a list. This allows use to use the standard list functions and therefore summing the values becomes trivial:</p>
<div style="font-family: courier new; background: white; color: black; font-size: 10pt">
<p style="margin: 0px"><span style="color: blue">let</span> toList (roll:Roll) =</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">let</span> a,b,c,d,e = roll</p>
<p style="margin: 0px">&#160;&#160;&#160; [a;b;c;d;e]</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px"><span style="color: blue">let</span> sumNumber number =</p>
<p style="margin: 0px">&#160;&#160;&#160; Seq.filter ((=) number)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; &gt;&gt; Seq.sum</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px"><span style="color: blue">let</span> calcValue category roll =</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">let</span> list = toList roll</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">match</span> category <span style="color: blue">with</span></p>
<p style="margin: 0px">&#160;&#160;&#160; | Ones&#160;&#160; <span style="color: blue">-&gt;</span> sumNumber 1 list</p>
<p style="margin: 0px">&#160;&#160;&#160; | Twos&#160;&#160; <span style="color: blue">-&gt;</span> sumNumber 2 list</p>
<p style="margin: 0px">&#160;&#160;&#160; | Threes <span style="color: blue">-&gt;</span> sumNumber 3 list</p>
<p style="margin: 0px">&#160;&#160;&#160; | Fours&#160; <span style="color: blue">-&gt;</span> sumNumber 4 list</p>
<p style="margin: 0px">&#160;&#160;&#160; | Fives&#160; <span style="color: blue">-&gt;</span> sumNumber 5 list</p>
<p style="margin: 0px">&#160;&#160;&#160; | Sixes&#160; <span style="color: blue">-&gt;</span> sumNumber 6 list</p>
</p></div>
<p>Now we can run our scenarios with any NUnit runner. I’m using the default <a href="http://www.nunit.org/">NUnit</a> GUI runner here, which gives me a picture like this:</p>
<p><img class="bordered" title="Both specs in NUnit" border="0" alt="Both specs in NUnit" src="http://www.navision-blog.de/images/SolvingKataYahtzeewithFandNaturalSpec_11049/image_thumb.png" width="500" height="342" /></p>
<h5>Category 2 – Pair</h5>
<blockquote><p>“Pair: The player scores the sum of the two highest matching dice. For example, 3, 3, 3, 4, 4 placed on &quot;pair&quot; gives 8.”</p>
</blockquote>
<p>The kata description gives us some new scenarios. As seen above we should specify them before writing the code.</p>
<div style="font-family: courier new; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">[&lt;Scenario&gt;]&#160;&#160;&#160;&#160; </p>
<p style="margin: 0px"><span style="color: blue">let</span> &#8220;Given 3,3,3,4,4 placed on &quot;pair&quot; gives 8.&#8220; () =&#160;&#160; </p>
<p style="margin: 0px">&#160;&#160;&#160; Given (3, 3, 3, 4, 4)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; When (placed_on Pair)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; It should equal 8</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; Verify</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">[&lt;Scenario&gt;]&#160;&#160;&#160;&#160; </p>
<p style="margin: 0px"><span style="color: blue">let</span> &#8220;Given 5,3,5,4,4 placed on &quot;pair&quot; gives 10.&#8220; () =&#160;&#160; </p>
<p style="margin: 0px">&#160;&#160;&#160; Given (5, 3, 5, 4, 4)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; When (placed_on Pair)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; It should equal 10</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; Verify&#160;&#160;&#160; </p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">[&lt;Scenario&gt;]&#160;&#160;&#160;&#160; </p>
<p style="margin: 0px"><span style="color: blue">let</span> &#8220;Given 1,2,3,4,5 placed on &quot;pair&quot; gives 0.&#8220; () =&#160;&#160; </p>
<p style="margin: 0px">&#160;&#160;&#160; Given (1, 2, 3, 4, 5)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; When (placed_on Pair)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; It should equal 0</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; Verify&#160;&#160;&#160;&#160;&#160; </p>
</p></div>
<p>Since we use a new category we now have to extend our model and the <strong>calcValue</strong> function:</p>
<div style="font-family: courier new; background: white; color: black; font-size: 10pt">
<p style="margin: 0px"><span style="color: blue">type</span> Category =</p>
<p style="margin: 0px">| Ones</p>
<p style="margin: 0px">&#160; <span style="color: green">// &#8230;</span></p>
<p style="margin: 0px">| Sixes</p>
<p style="margin: 0px">| Pair</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px"><span style="color: green">// &#8230;</span></p>
<p style="margin: 0px"><span style="color: green"></span></p>
<p style="margin: 0px"><span style="color: blue">let</span> sumAsPair list number =</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">let</span> numberCount = </p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; list </p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; |&gt; Seq.filter ((=) number) </p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; |&gt; Seq.length</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">if</span> numberCount &gt;= 2 <span style="color: blue">then</span> 2 * number <span style="color: blue">else</span> 0</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px"><span style="color: blue">let</span> calcValue category roll =</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">let</span> list = toList roll</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">match</span> category <span style="color: blue">with</span></p>
<p style="margin: 0px">&#160;&#160;&#160; | Ones&#160;&#160; <span style="color: blue">-&gt;</span> sumNumber 1 list</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// &#8230;</span></p>
<p style="margin: 0px">&#160;&#160;&#160; | Sixes&#160; <span style="color: blue">-&gt;</span> sumNumber 6 list</p>
<p style="margin: 0px">&#160;&#160;&#160; | Pair&#160;&#160; <span style="color: blue">-&gt;</span> </p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; [1..6]</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; |&gt; Seq.map (sumAsPair list)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; |&gt; Seq.max&#160;&#160; </p>
</p></div>
<h5>Category 3 &#8211; Two pairs</h5>
<blockquote><p>“Two pairs: If there are two pairs of dice with the same number, the player scores the sum of these dice. If not, the player scores 0. For example, 1, 1, 2, 3, 3 placed on &quot;two pairs&quot; gives 8.”</p>
</blockquote>
<p> <strong></strong>
<div style="font-family: courier new; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">[&lt;Scenario&gt;]&#160;&#160;&#160;&#160; </p>
<p style="margin: 0px"><span style="color: blue">let</span> &#8220;Given 1,1,2,3,3 placed on &quot;two pair&quot; gives 8.&#8220; () =&#160;&#160; </p>
<p style="margin: 0px">&#160;&#160;&#160; Given (1, 1, 2, 3, 3)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; When (placed_on TwoPair)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; It should equal 8</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; Verify</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">[&lt;Scenario&gt;]&#160;&#160;&#160;&#160; </p>
<p style="margin: 0px"><span style="color: blue">let</span> &#8220;Given 1,6,6,3,3 placed on &quot;two pair&quot; gives 18.&#8220; () =&#160;&#160; </p>
<p style="margin: 0px">&#160;&#160;&#160; Given (1, 6, 6, 3, 3)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; When (placed_on TwoPair)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; It should equal 18</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; Verify</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">[&lt;Scenario&gt;]&#160;&#160;&#160;&#160; </p>
<p style="margin: 0px"><span style="color: blue">let</span> &#8220;Given 1,1,2,4,3 placed on &quot;two pair&quot; gives 0.&#8220; () =&#160;&#160; </p>
<p style="margin: 0px">&#160;&#160;&#160; Given (1, 1, 2, 4, 3)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; When (placed_on TwoPair)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; It should equal 0</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; Verify&#160; </p>
</p></div>
<p>Implementing this category is a little bit tricky but with the help of our Pair function and some more standard sequence combinators we can get our spec green:</p>
<div style="font-family: courier new; background: white; color: black; font-size: 10pt">
<p style="margin: 0px"><span style="color: blue">type</span> Category =</p>
<p style="margin: 0px">| Ones</p>
<p style="margin: 0px">&#160; <span style="color: green">// &#8230;</span></p>
<p style="margin: 0px">| TwoPair</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px"><span style="color: blue">let</span> allPairs =</p>
<p style="margin: 0px">&#160;&#160;&#160; [<span style="color: blue">for</span> i <span style="color: blue">in</span> 1..6 <span style="color: blue">do</span></p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">for</span> j <span style="color: blue">in</span> 1..6 <span style="color: blue">-&gt;</span> i,j]</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px"><span style="color: blue">let</span> calcValue category roll =</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: green">// &#8230;</span></p>
<p style="margin: 0px">&#160;&#160;&#160; | TwoPair&#160;&#160; <span style="color: blue">-&gt;</span> </p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; allPairs</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; |&gt; Seq.filter (<span style="color: blue">fun</span> (a,b) <span style="color: blue">-&gt;</span> a &lt;&gt; b)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; |&gt; Seq.map (<span style="color: blue">fun</span> (a,b) <span style="color: blue">-&gt;</span> </p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">let</span> a&#8217; = sumAsPair list a</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">let</span> b&#8217; = sumAsPair list b</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> a&#8217; = 0 || b&#8217; = 0 <span style="color: blue">then</span> 0 <span style="color: blue">else</span> a&#8217; + b&#8217;)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; |&gt; Seq.max&#160;&#160;&#160;&#160; </p>
</p></div>
<h5>Category 4 &#8211; Three of a kind</h5>
<blockquote><p>“Three of a kind: If there are three dice with the same number, the player scores the sum of these dice. Otherwise, the player scores 0. For example, 3, 3, 3, 4, 5 places on &quot;three of a kind&quot; gives 9.”</p>
</blockquote>
<div style="font-family: courier new; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">[&lt;Scenario&gt;]&#160;&#160;&#160;&#160; </p>
<p style="margin: 0px"><span style="color: blue">let</span> &#8220;Given 3,3,3,4,5 placed on &quot;three of a kind&quot; gives 9&#8220;()=&#160;&#160; </p>
<p style="margin: 0px">&#160;&#160;&#160; Given (3, 3, 3, 4, 5)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; When (placed_on ThreeOfAKind)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; It should equal 9</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; Verify</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">[&lt;Scenario&gt;]&#160;&#160;&#160;&#160; </p>
<p style="margin: 0px"><span style="color: blue">let</span> &#8220;Given 3,4,3,4,5 placed on &quot;three of a kind&quot; gives 0&#8220;()=&#160;&#160; </p>
<p style="margin: 0px">&#160;&#160;&#160; Given (3, 4, 3, 4, 5)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; When (placed_on ThreeOfAKind)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; It should equal 0</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; Verify </p>
</p></div>
<p>Now it is time to refactor our code. The <b>sumAsPair</b> function should be extended to a sumAsTuple function:</p>
<div style="font-family: courier new; background: white; color: black; font-size: 10pt">
<p style="margin: 0px"><span style="color: blue">type</span> Category =</p>
<p style="margin: 0px">| Ones</p>
<p style="margin: 0px">&#160; <span style="color: green">// &#8230;</span></p>
<p style="margin: 0px">| ThreeOfAKind</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px"><span style="color: blue">let</span> sumAsTuple value list number =</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">let</span> numberCount = </p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; list </p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; |&gt; Seq.filter ((=) number) </p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; |&gt; Seq.length</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px"><span style="color: blue">let</span> takeBestTuple value list =</p>
<p style="margin: 0px">&#160;&#160;&#160; [1..6]</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; |&gt; Seq.map (sumAsTuple value list)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; |&gt; Seq.max&#160; </p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">if</span> numberCount &gt;= value <span style="color: blue">then</span> value * number <span style="color: blue">else</span> 0</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px"><span style="color: blue">let</span> calcValue category roll =</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; <span style="color: green">// &#8230;</span></p>
<p style="margin: 0px">&#160;&#160;&#160; | Pair&#160;&#160; <span style="color: blue">-&gt;</span> takeBestTuple 2 list</p>
<p style="margin: 0px">&#160;&#160;&#160; | TwoPair&#160;&#160; <span style="color: blue">-&gt;</span> </p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; allPairs</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; |&gt; Seq.filter (<span style="color: blue">fun</span> (a,b) <span style="color: blue">-&gt;</span> a &lt;&gt; b)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; |&gt; Seq.map (<span style="color: blue">fun</span> (a,b) <span style="color: blue">-&gt;</span> </p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">let</span> a&#8217; = sumAsTuple 2 list a</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">let</span> b&#8217; = sumAsTuple 2 list b</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> a&#8217; = 0 || b&#8217; = 0 <span style="color: blue">then</span> 0 <span style="color: blue">else</span> a&#8217; + b&#8217;)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; |&gt; Seq.max</p>
<p style="margin: 0px">&#160;&#160;&#160; | ThreeOfAKind –<span style="color: blue">&gt;</span> takeBestTuple 3 list</p>
</p></div>
<h5>Category 5 &#8211; Four of a kind</h5>
<blockquote><p>“Four of a kind: If there are four dice with the same number, the player scores the sum of these dice. Otherwise, the player scores 0. For example, 2, 2, 2, 2, 5 places on &quot;four of a kind&quot; gives 8.”</p>
</blockquote>
<div style="font-family: courier new; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">[&lt;Scenario&gt;]&#160;&#160;&#160;&#160; </p>
<p style="margin: 0px"><span style="color: blue">let</span> &#8220;Given 2,2,2,2,5 placed on &quot;four of a kind&quot; gives 8&#8220; ()=&#160;&#160; </p>
<p style="margin: 0px">&#160;&#160;&#160; Given (2, 2, 2, 2, 5)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; When (placed_on FourOfAKind)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; It should equal 8</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; Verify</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">[&lt;Scenario&gt;]&#160;&#160;&#160;&#160; </p>
<p style="margin: 0px"><span style="color: blue">let</span> &#8220;Given 2,6,2,2,5 placed on &quot;four of a kind&quot; gives 0&#8220; ()=&#160;&#160; </p>
<p style="margin: 0px">&#160;&#160;&#160; Given (2, 6, 2, 2, 5)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; When (placed_on FourOfAKind)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; It should equal 0</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; Verify&#160;&#160; </p>
</p></div>
<p>With the help of the takeBestTuple function this becomes trivial:</p>
<p style="margin: 0px; font-family: courier new; background: white; color: black; font-size: 10pt"><span style="color: blue">type</span> Category =</p>
<p style="margin: 0px; font-family: courier new; background: white; color: black; font-size: 10pt">| Ones</p>
<p style="margin: 0px; font-family: courier new; background: white; color: black; font-size: 10pt">&#160; <span style="color: green">// &#8230;</span></p>
<p style="margin: 0px; font-family: courier new; background: white; color: black; font-size: 10pt">| FourOfAKind</p>
<p style="margin: 0px; font-family: courier new; background: white; color: black; font-size: 10pt">&#160;</p>
<p style="margin: 0px; font-family: courier new; background: white; color: black; font-size: 10pt"><span style="color: blue">let</span> calcValue category roll =</p>
<p style="margin: 0px; font-family: courier new; background: white; color: black; font-size: 10pt">&#160;&#160;&#160;&#160;&#160; <span style="color: green">// &#8230;</span></p>
<p style="margin: 0px; font-family: courier new; background: white; color: black; font-size: 10pt">&#160;&#160;&#160; | FourOfAKind&#160; <span style="color: blue">-&gt;</span> takeBestTuple 4 list</p>
<p style="margin: 0px; font-family: courier new; background: white; color: black; font-size: 10pt">&#160;</p>
<h5>Category 6 – Small straight</h5>
<blockquote><p style="margin: 0px">“Small straight: If the dice read 1,2,3,4,5, the player scores 15 (the sum of all the dice), otherwise 0.”</p>
</blockquote>
<div style="font-family: courier new; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">[&lt;Scenario&gt;]&#160;&#160;&#160;&#160; </p>
<p style="margin: 0px"><span style="color: blue">let</span> &#8220;Given 1,2,3,4,5 placed on &quot;Small Straight&quot; gives 15&#8220;()=&#160;&#160; </p>
<p style="margin: 0px">&#160;&#160;&#160; Given (1,2,3,4,5)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; When (placed_on SmallStraight)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; It should equal 15</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; Verify</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">[&lt;Scenario&gt;]&#160;&#160;&#160;&#160; </p>
<p style="margin: 0px"><span style="color: blue">let</span> &#8220;Given 1,2,5,4,3 placed on &quot;Small Straight&quot; gives 15&#8220;()=&#160;&#160; </p>
<p style="margin: 0px">&#160;&#160;&#160; Given (1,2,5,4,3)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; When (placed_on SmallStraight)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; It should equal 15</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; Verify</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">[&lt;Scenario&gt;]&#160;&#160;&#160;&#160; </p>
<p style="margin: 0px"><span style="color: blue">let</span> &#8220;Given 1,2,6,4,3 placed on &quot;Small Straight&quot; gives 0&#8220;()=&#160;&#160; </p>
<p style="margin: 0px">&#160;&#160;&#160; Given (1,2,6,4,3)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; When (placed_on SmallStraight)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; It should equal 0</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; Verify&#160; </p>
</p></div>
<p>As in all the above scenarios we don’t assume any specific order in our rolls but for this category it is easier to test if the data is sorted:</p>
<div style="font-family: courier new; background: white; color: black; font-size: 10pt">
<p style="margin: 0px"><span style="color: blue">type</span> Category =</p>
<p style="margin: 0px">| Ones</p>
<p style="margin: 0px">&#160; <span style="color: green">// &#8230;</span></p>
<p style="margin: 0px">| SmallStraight</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px"><span style="color: blue">let</span> calcValue category roll =</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; <span style="color: green">// &#8230;</span></p>
<p style="margin: 0px">&#160;&#160;&#160; | SmallStraight <span style="color: blue">-&gt;</span> </p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">match</span> list |&gt; List.sort <span style="color: blue">with</span></p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; | [1;2;3;4;5] <span style="color: blue">-&gt;</span> 15</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; | _ <span style="color: blue">-&gt;</span> 0</p>
</p></div>
<h5>Category 7 – Large straight</h5>
<blockquote><p>“Large straight: If the dice read 2,3,4,5,6, the player scores 20 (the sum of all the dice), otherwise 0.”</p>
</blockquote>
<div style="font-family: courier new; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">[&lt;Scenario&gt;]&#160;&#160;&#160;&#160; </p>
<p style="margin: 0px"><span style="color: blue">let</span> &#8220;Given 2,3,4,5,6 placed on &quot;Large Straight&quot; gives 20&#8220;()=&#160;&#160; </p>
<p style="margin: 0px">&#160;&#160;&#160; Given (2,3,4,5,6)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; When (placed_on LargeStraight)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; It should equal 20</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; Verify</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">[&lt;Scenario&gt;]&#160;&#160;&#160;&#160; </p>
<p style="margin: 0px"><span style="color: blue">let</span> &#8220;Given 6,2,5,4,3 placed on &quot;Large Straight&quot; gives 20&#8220;()=&#160;&#160; </p>
<p style="margin: 0px">&#160;&#160;&#160; Given (6,2,5,4,3)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; When (placed_on LargeStraight)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; It should equal 20</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; Verify</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">[&lt;Scenario&gt;]&#160;&#160;&#160;&#160; </p>
<p style="margin: 0px"><span style="color: blue">let</span> &#8220;Given 1,2,6,4,3 placed on &quot;Large Straight&quot; gives 0&#8220;()=&#160;&#160; </p>
<p style="margin: 0px">&#160;&#160;&#160; Given (1,2,6,4,3)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; When (placed_on LargeStraight)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; It should equal 0</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; Verify&#160; </p>
</p></div>
<p>Of course the implementation is exactly the same as for the small straight:</p>
<div style="font-family: courier new; background: white; color: black; font-size: 10pt">
<p style="margin: 0px"><span style="color: blue">type</span> Category =</p>
<p style="margin: 0px">| Ones</p>
<p style="margin: 0px">&#160; <span style="color: green">// &#8230;</span></p>
<p style="margin: 0px">| LargeStraight</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px"><span style="color: blue">let</span> calcValue category roll =</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; <span style="color: green">// &#8230;</span></p>
<p style="margin: 0px">&#160;&#160;&#160; | LargeStraight <span style="color: blue">-&gt;</span> </p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">match</span> list |&gt; List.sort <span style="color: blue">with</span></p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; | [2;3;4;5;6] <span style="color: blue">-&gt;</span> 20</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; | _ <span style="color: blue">-&gt;</span> 0</p>
</p></div>
<h5>Category 8 – Full house</h5>
<blockquote><p>“Full house: If the dice are two of a kind and three of a kind, the player scores the sum of all the dice. For example, 1,1,2,2,2 placed on &quot;full house&quot; gives 8. 4,4,4,4,4 is not &quot;full house&quot;.”</p>
</blockquote>
<div style="font-family: courier new; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">[&lt;Scenario&gt;]&#160;&#160;&#160;&#160; </p>
<p style="margin: 0px"><span style="color: blue">let</span> &#8220;Given 1,1,2,2,2 placed on &quot;full house&quot; gives 8.&#8220; () =&#160;&#160; </p>
<p style="margin: 0px">&#160;&#160;&#160; Given (1,1,2,2,2)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; When (placed_on FullHouse)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; It should equal 8</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; Verify</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">[&lt;Scenario&gt;]&#160;&#160;&#160;&#160; </p>
<p style="margin: 0px"><span style="color: blue">let</span> &#8220;Given 4,4,4,4,4 placed on &quot;full house&quot; gives 0.&#8220; () =&#160;&#160; </p>
<p style="margin: 0px">&#160;&#160;&#160; Given (4,4,4,4,4)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; When (placed_on FullHouse)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; It should equal 0</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; Verify</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">[&lt;Scenario&gt;]&#160;&#160;&#160;&#160; </p>
<p style="margin: 0px"><span style="color: blue">let</span> &#8220;Given 1,1,2,3,2 placed on &quot;full house&quot; gives 0.&#8220; () =&#160;&#160; </p>
<p style="margin: 0px">&#160;&#160;&#160; Given (1,1,2,3,2)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; When (placed_on FullHouse)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; It should equal 0</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; Verify </p>
</p></div>
<p>Implementing the FullHouse category is easy if we reuse our solutions to the Two pairs category:</p>
<div style="font-family: courier new; background: white; color: black; font-size: 10pt">
<p style="margin: 0px"><span style="color: blue">type</span> Category =</p>
<p style="margin: 0px">| Ones</p>
<p style="margin: 0px">&#160; <span style="color: green">// &#8230;</span></p>
<p style="margin: 0px">| FullHouse</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px"><span style="color: blue">let</span> takeBestCombo value1 value2 list =</p>
<p style="margin: 0px">&#160;&#160;&#160; allPairs</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; |&gt; Seq.filter (<span style="color: blue">fun</span> (a,b) <span style="color: blue">-&gt;</span> a &lt;&gt; b)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; |&gt; Seq.map (<span style="color: blue">fun</span> (a,b) <span style="color: blue">-&gt;</span> </p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">let</span> a&#8217; = sumAsTuple value1 list a</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">let</span> b&#8217; = sumAsTuple value2 list b</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> a&#8217; = 0 || b&#8217; = 0 <span style="color: blue">then</span> 0 <span style="color: blue">else</span> a&#8217; + b&#8217;)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; |&gt; Seq.max</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px"><span style="color: blue">let</span> calcValue category roll =</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; <span style="color: green">// &#8230;</span></p>
<p style="margin: 0px">&#160;&#160;&#160; | TwoPair&#160;&#160; <span style="color: blue">-&gt;</span> takeBestCombo 2 2 list</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; <span style="color: green">// &#8230;</span></p>
<p style="margin: 0px">&#160;&#160;&#160; | FullHouse&#160;&#160; <span style="color: blue">-&gt;</span> takeBestCombo 2 3 list</p>
</p></div>
<h5>Category 9 – Yahtzee</h5>
<blockquote><p>“Yahtzee: If all dice are the have the same number, the player scores 50 points, otherwise 0.”</p>
</blockquote>
<p>Here we can use NaturalSpec&#8217;s ScenarioTemplates in order to specify all Yahtzees:</p>
<div style="font-family: courier new; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">[&lt;ScenarioTemplate(1)&gt;]</p>
<p style="margin: 0px">[&lt;ScenarioTemplate(2)&gt;]</p>
<p style="margin: 0px">[&lt;ScenarioTemplate(3)&gt;]</p>
<p style="margin: 0px">[&lt;ScenarioTemplate(4)&gt;]</p>
<p style="margin: 0px">[&lt;ScenarioTemplate(5)&gt;]</p>
<p style="margin: 0px">[&lt;ScenarioTemplate(6)&gt;]</p>
<p style="margin: 0px"><span style="color: blue">let</span> &#8220;Given n,n,n,n,n placed on &quot;Yahtzee&quot; gives 50.&#8220; n =&#160;&#160; </p>
<p style="margin: 0px">&#160;&#160;&#160; Given (n,n,n,n,n)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; When (placed_on Yahtzee)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; It should equal 50</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; Verify</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">[&lt;Scenario&gt;]&#160;&#160;&#160;&#160; </p>
<p style="margin: 0px"><span style="color: blue">let</span> &#8220;Given 1,1,1,2,1 placed on &quot;Yahtzee&quot; gives 50.&#8220; () =&#160;&#160; </p>
<p style="margin: 0px">&#160;&#160;&#160; Given (1,1,1,2,1)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; When (placed_on Yahtzee)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; It should equal 0</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; Verify</p>
</p></div>
<p>The implementation is pretty easy:</p>
<div style="font-family: courier new; background: white; color: black; font-size: 10pt">
<p style="margin: 0px"><span style="color: blue">type</span> Category =</p>
<p style="margin: 0px">| Ones</p>
<p style="margin: 0px">&#160; <span style="color: green">// &#8230;</span></p>
<p style="margin: 0px">| Yahtzee</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px"><span style="color: blue">let</span> calcValue category roll =</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; <span style="color: green">// &#8230;</span></p>
<p style="margin: 0px">&#160;&#160;&#160; | Yahtzee <span style="color: blue">-&gt;</span> </p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">let</span> a,b,c,d,e = roll</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> a = b &amp;&amp; a = c &amp;&amp; a = d &amp;&amp; a = e <span style="color: blue">then</span> 50 <span style="color: blue">else</span> 0 </p>
</p></div>
<h5>Category 10 – Chance</h5>
<blockquote><p>“Chance: The player gets the sum of all dice, no matter what they read.”</p>
</blockquote>
<div style="font-family: courier new; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">[&lt;Scenario&gt;]&#160;&#160;&#160;&#160; </p>
<p style="margin: 0px"><span style="color: blue">let</span> &#8220;Given 1,1,1,2,1 placed on &quot;Chance&quot; gives 6.&#8220; () =&#160;&#160; </p>
<p style="margin: 0px">&#160;&#160;&#160; Given (1,1,1,2,1)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; When (placed_on Chance)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; It should equal 6</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; Verify</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">[&lt;Scenario&gt;]&#160;&#160;&#160;&#160; </p>
<p style="margin: 0px"><span style="color: blue">let</span> &#8220;Given 1,6,1,2,1 placed on &quot;Chance&quot; gives 11.&#8220; () =&#160;&#160; </p>
<p style="margin: 0px">&#160;&#160;&#160; Given (1,6,1,2,1)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; When (placed_on Chance)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; It should equal 11</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; |&gt; Verify</p>
</p></div>
<p>This seems to be the easiest category as we only have to sum the values:</p>
<div style="font-family: courier new; background: white; color: black; font-size: 10pt">
<p style="margin: 0px"><span style="color: blue">type</span> Category =</p>
<p style="margin: 0px">| Ones</p>
<p style="margin: 0px">&#160; <span style="color: green">// &#8230;</span></p>
<p style="margin: 0px">| Chance</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px"><span style="color: blue">let</span> calcValue category roll =</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160; <span style="color: green">// &#8230;</span></p>
<p style="margin: 0px">&#160;&#160;&#160; | Chance <span style="color: blue">-&gt;</span> List.sum list</p>
</p></div>
<h5>Conclusion</h5>
<p>We used a lot of F#’s sequence combinators, pattern matching and discriminated unions in this kata. I think this shows that F# is very well suited for such a problem and with NaturalSpec we can easily use a TDD/BDD approach.</p>
<p>The complete source code can be found in the <a href="http://github.com/forki/NaturalSpec/tree/master/src/test/Spec.KataYahtzee/">NaturalSpec repository</a>.</p>
<p>If you want to know more about a specific part of the kata or NaturalSpec feel free to contact me.</p>
<p>&copy;2010 <a href="http://www.navision-blog.de">Rash thoughts about .NET, C#, F# and Dynamics NAV.</a>. All Rights Reserved.</p>.]]></content:encoded>
			<wfw:commentRss>http://www.navision-blog.de/2010/04/24/solving-kata-yahtzee-with-fsharp-and-naturalspec/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&#8220;FAKE &#8211; F# Make&#8221; and NaturalSpec released</title>
		<link>http://www.navision-blog.de/2010/04/13/fake-f-sharp-make-and-naturalspec-released/</link>
		<comments>http://www.navision-blog.de/2010/04/13/fake-f-sharp-make-and-naturalspec-released/#comments</comments>
		<pubDate>Tue, 13 Apr 2010 08:06:46 +0000</pubDate>
		<dc:creator>Steffen Forkmann</dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[FAKE - F# Make]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[F-sharp Make]]></category>
		<category><![CDATA[NaturalSpec]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>

		<guid isPermaLink="false">http://www.navision-blog.de/2010/04/13/fake-f-make-and-naturalspec-released/</guid>
		<description><![CDATA[Yesterday Microsoft released the RTM versions of Visual Studio 2010, .NET Framework 4.0 and F# 2.0.0.0 and so it is time to announce the first official releases of “Fake – F# Make” and NaturalSpec. Both projects are now compatible with Visual Studio 2010 RC and RTM and the corresponding F# versions. Fake – F# Make [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday Microsoft released the RTM versions of <a href="http://www.microsoft.com/visualstudio/en-us/download">Visual Studio 2010</a>, .NET Framework 4.0 and <a href="http://msdn.microsoft.com/de-de/fsharp/cc835251%28en-us%29.aspx">F# 2.0.0.0</a> and so it is time to announce the first official releases of “<a href="http://bitbucket.org/forki/fake/wiki/Home">Fake – F# Make</a>” and <a href="http://bitbucket.org/forki/naturalspec/wiki/Home">NaturalSpec</a>. Both projects are now compatible with Visual Studio 2010 RC and RTM and the corresponding F# versions.</p>
<h5>Fake – F# Make version 1.0.0</h5>
<p>&quot;FAKE &#8211; F# Make&quot; is a build automation system, which is intended to provide a much better tooling support than XML-based build languages like MSBuild or NAnt. Due to its integration in F#, all benefits of the .NET Framework and functional programming can be used, including the extensive class library, powerful debuggers and integrated development environments like Visual Studio 2008, Visual Studio 2010 or SharpDevelop, which provide syntax highlighting and code completion.</p>
<p>Like F# itself the new build language was designed to be succinct, typed, declarative, extensible and easy to use.</p>
<ul>
<li><a href="http://bitbucket.org/forki/fake/wiki/Home">Project page</a> with samples and articles </li>
<li><a href="http://bitbucket.org/forki/fake/downloads/">Download page</a> </li>
<li><a href="http://github.com/forki/FAKE">Github repository</a> </li>
</ul>
<h5>NaturalSpec version 1.0.0</h5>
<p>NaturalSpec is a UnitTest framework based on <a href="http://www.nunit.org/">NUnit</a> and completely written in F# &#8211; but you don&#8217;t have to learn F# to use it. The idea is that you can write your spec mostly in a natural language like in the following sample:</p>
</p>
<div style="font-family: courier new; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">[&lt;Scenario&gt;]</p>
<p style="margin: 0px"><span style="color: blue">let</span> &#8220;When removing an element from a list it should not contain the element&#8220;() =</p>
<p style="margin: 0px">&#160; Given [1;2;3;4;5]&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// &quot;Arrange&quot; test context</span></p>
<p style="margin: 0px">&#160;&#160;&#160; |&gt; When removing 3&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// &quot;Act&quot;</span></p>
<p style="margin: 0px">&#160;&#160;&#160; |&gt; It shouldn&#8217;t contain 3&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// &quot;Assert&quot;</span></p>
<p style="margin: 0px">&#160;&#160;&#160; |&gt; It should contain 4&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// another assertion</span></p>
<p style="margin: 0px">&#160;&#160;&#160; |&gt; It should have (Length 4)&#160;&#160;&#160; <span style="color: green">// Assertion for length</span></p>
<p style="margin: 0px">&#160;&#160;&#160; |&gt; It shouldn&#8217;t have Duplicates <span style="color: green">// it contains duplicates ?</span></p>
<p style="margin: 0px">&#160;&#160;&#160; |&gt; Verify&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Verify scenario</span></p>
</p></div>
</p>
<ul>
<li><a href="http://bitbucket.org/forki/naturalspec/wiki/Home">Project page</a> with samples and articles </li>
<li><a href="http://bitbucket.org/forki/naturalspec/downloads/">Download page</a> </li>
<li><a href="http://github.com/forki/NaturalSpec">Github repository</a> </li>
</ul>
<p>If you have any questions about the projects feel free to contact me.</p>
<p>&copy;2010 <a href="http://www.navision-blog.de">Rash thoughts about .NET, C#, F# and Dynamics NAV.</a>. All Rights Reserved.</p>.]]></content:encoded>
			<wfw:commentRss>http://www.navision-blog.de/2010/04/13/fake-f-sharp-make-and-naturalspec-released/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>&quot;Getting started&quot; with NaturalSpec &#8211; (Updated 08.11.2009)</title>
		<link>http://www.navision-blog.de/2009/11/08/getting-started-with-naturalspec/</link>
		<comments>http://www.navision-blog.de/2009/11/08/getting-started-with-naturalspec/#comments</comments>
		<pubDate>Sun, 08 Nov 2009 09:48:00 +0000</pubDate>
		<dc:creator>Steffen Forkmann</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[F#]]></category>
		<category><![CDATA[NaturalSpec]]></category>
		<category><![CDATA[BDD]]></category>
		<category><![CDATA[nunit]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://www.navision-blog.de/2009/02/23/getting-started-with-naturalspec/</guid>
		<description><![CDATA[In my last article (Introducing NaturalSpec – A Domain-specific language (DSL) for testing) I used NaturalSpec in two small samples. This time I will show how we can set up a NaturalSpec environment to write our first automatically testable scenarios. 1. Choosing an IDE The first step is to choose an integrated development environment for [...]]]></description>
			<content:encoded><![CDATA[<p>In my last article (<a href="http://www.navision-blog.de/2009/02/23/introducing-naturalspec-a-dsl-for-testing-part-i/">Introducing NaturalSpec – A Domain-specific language (DSL) for testing</a>) I used NaturalSpec in two small samples. This time I will show how we can set up a <a href="http://code.google.com/p/natural/">NaturalSpec</a> environment to write our first automatically testable scenarios.</p>
<h5>1. Choosing an IDE</h5>
<p>The first step is to choose an integrated development environment for NaturalSpec. At the current project status you should be able to use NaturalSpec with Visual Studio 2008, Visual Studio 2010 beta 2, the freely available <a href="http://msdn.microsoft.com/en-us/vsx2008/products/bb933751.aspx">Visual Studio 2008 Shell</a> or the free IDE <a href="http://www.icsharpcode.net/OpenSource/SD/">SharpDevelop 3.0</a>.</p>
<h5>2. Installing the testing framework</h5>
<p>As NaturalSpec uses <a href="http://www.nunit.org/index.php">NUnit</a> as the underlying testing framework we have to install NUnit 2.5. I also recommend installing <a href="http://testdriven.net/default.aspx">TestDriven.Net</a> in order to get a Unit Test runner within Visual Studio.</p>
<h5>3. Installing F#</h5>
<p>NaturalSpec is completely written in <a href="http://en.wikipedia.org/wiki/F_Sharp_programming_language">F#</a> and all specs will also be written in F#. This doesn’t imply you have to learn programming in F# but we need the F# compiler to get things working. You can download the <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=b55f0532-ac3c-4106-918c-5586a953a7da&amp;displaylang=en">F# October 2009 CTP</a> from the <a href="http://msdn.microsoft.com/en-us/fsharp/default.aspx">Microsoft F# Developer Center</a>.</p>
<h5>4. Downloading the latest version of NaturalSpec</h5>
<p>You can download a .zip with the <a href="http://code.google.com/p/natural/">latest NaturalSpec libraries from GoogleCode</a>.</p>
<h5>5. Creating a spec</h5>
<p>This part is written for using Visual Studio 2008. If you use SharpDevelop or Visual Studio 2008 Shell this might differ in some detail.</p>
<p>Start Visual Studio 2008 and create a new F# class library.</p>
<p><img class="bordered" title="Creating a spec project" border="0" alt="Creating a spec project" src="http://www.navision-blog.de/images/GettingstartedwithNaturalSpec_A7B1/image.png" width="500" height="354" /></p>
<p>Rename <em>Module1.fs</em> in <em>ListSpec.fs</em> and delete <em>script.fsx</em> from the project:</p>
<p><img class="bordered" title="Solution explorer" border="0" alt="Solution explorer" src="http://www.navision-blog.de/images/GettingstartedwithNaturalSpec_A7B1/image_3.png" width="248" height="146" /></p>
<p>Create a folder “Lib” and unzip the NaturalSpec libraries into it.</p>
<p>Add <em>NaturalSpec.dll</em> and <em>nunit.framework.dll</em> as references to your project:</p>
<p><img class="bordered" title="Adding project references" alt="Adding project references" src="http://www.navision-blog.de/images/GettingstartedwithNaturalSpec_A7B1/image_4.png" width="482" height="406" /></p>
<p>Copy the following code into ListSpec.fs:</p>
<pre class="code"><span style="color: blue">module </span>ListSpec</pre>
<pre class="code"><span style="color: blue">open </span>NaturalSpec

[&lt;Scenario&gt;]
<span style="color: blue">let </span>When_removing_an_3_from_a_small_list_it_should_not_contain_3() =
  Given [1;2;3;4;5]
    |&gt; When removing 3
    |&gt; It shouldn't contain 3
    |&gt; Verify</pre>
<p>If you have TestDriven.Net installed you can run your spec via right click in the solution explorer:</p>
<p><img class="bordered" title="Run spec with TestDriven.net" alt="Run spec with TestDriven.net" src="http://www.navision-blog.de/images/GettingstartedwithNaturalSpec_A7B1/image_5.png" width="295" /></p>
<p>If you don’t like the TestDriven.Net test runner you might want to use the NUnit GUI runner. The output should look like:</p>
<p><a href="http://www.navision-blog.de/images/GettingstartedwithNaturalSpec_A7B1/image_6.png"><img class="bordered" title="NUnit Gui runner" border="0" alt="NUnit Gui runner" src="http://www.navision-blog.de/images/GettingstartedwithNaturalSpec_A7B1/image_thumb.png" width="500" height="264" /></a></p>
<p>In addition the test runner should produce a Spec output file with the name “Spec.txt” within the same folder.</p>
<h5>Summary</h5>
<p>In a minimal environment you need <a href="http://www.icsharpcode.net/OpenSource/SD/">SharpDevelop</a>, the <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=b55f0532-ac3c-4106-918c-5586a953a7da&amp;displaylang=en">F# compiler</a>, <a href="http://www.nunit.org/index.php">NUnit</a> and the <a href="http://code.google.com/p/natural/">NaturalSpec libraries</a> for using NaturalSpec. </p>
<p>In the next post I will show how you can <a href="http://www.navision-blog.de/2009/02/23/using-naturalspec-to-create-spec-for-c-projects/">use NaturalSpec to create a spec for C# projects</a>.</p>
<p>&copy;2010 <a href="http://www.navision-blog.de">Rash thoughts about .NET, C#, F# and Dynamics NAV.</a>. All Rights Reserved.</p>.]]></content:encoded>
			<wfw:commentRss>http://www.navision-blog.de/2009/11/08/getting-started-with-naturalspec/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Testing Quicksort with NaturalSpec</title>
		<link>http://www.navision-blog.de/2009/03/01/testing-quicksort-with-naturalspec/</link>
		<comments>http://www.navision-blog.de/2009/03/01/testing-quicksort-with-naturalspec/#comments</comments>
		<pubDate>Sun, 01 Mar 2009 10:42:14 +0000</pubDate>
		<dc:creator>Steffen Forkmann</dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[NaturalSpec]]></category>
		<category><![CDATA[quicksort]]></category>

		<guid isPermaLink="false">http://www.navision-blog.de/2009/03/01/testing-quicksort-with-naturalspec/</guid>
		<description><![CDATA[In my last article I showed two ways to use parameterized scenarios in NaturalSpec. This time I will show how we can combine both to test a small Quicksort function. First of all we define a scenario for sorting: /// predefined sorting scenario let sortingScenario f list = Given list &#124;&#62; When sorting_with f &#124;&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>In my last article I showed two ways to use <a href="http://www.navision-blog.de/2009/02/28/parameterized-scenarios-with-naturalspec/">parameterized scenarios</a> in <a href="http://code.google.com/p/natural/">NaturalSpec</a>. This time I will show how we can combine both to test a small <a href="http://en.wikipedia.org/wiki/Quicksort">Quicksort</a> function.</p>
<p>First of all we define a scenario for sorting:</p>
<pre class="code"><span style="color: green">/// predefined sorting scenario
</span><span style="color: blue">let </span>sortingScenario f list =
  Given list
    |&gt; When sorting_with f
    |&gt; It should be sorted
    |&gt; It should contain_all_elements_from list
    |&gt; It should contain_no_other_elements_than list</pre>
<pre class="code"><span style="color: green">/// predefined Quicksort scenario
</span><span style="color: blue">let </span>quicksortScenario list = sortingScenario QuickSort list</pre>
<p>Now we define some concrete test cases:</p>
<pre class="code">[&lt;Scenario&gt;]
<span style="color: blue">let </span>When_sorting_empty_list() =
  quicksortScenario []
    |&gt; Verify

[&lt;Scenario&gt;]
<span style="color: blue">let </span>When_sorting_small_list() =
  quicksortScenario [2;1;8;15;5;22]
    |&gt; Verify      

[&lt;ScenarioTemplate(100)&gt;]
[&lt;ScenarioTemplate(1000)&gt;]
[&lt;ScenarioTemplate(2500)&gt;]
<span style="color: blue">let </span>When_sorting_ordered_list n =
  quicksortScenario [1..n]
    |&gt; Verify  

[&lt;ScenarioTemplate(100)&gt;]
[&lt;ScenarioTemplate(1000)&gt;]
[&lt;ScenarioTemplate(2500)&gt;]
<span style="color: blue">let </span>When_sorting_random_list n =
  quicksortScenario (list_of_random_ints n)
    |&gt; Verify  </pre>
<p>After we defined our spec the task is now to implement the sorting function. I am using a very short (and very naïve) Quicksort implementation in F#:</p>
<pre class="code"><span style="color: green">/// naive implementation of QuickSort - don't use it
</span><span style="color: blue">let rec </span>quicksort = <span style="color: blue">function
  </span>| [] <span style="color: blue">-&gt; </span>[]
  | pivot :: rest <span style="color: blue">-&gt;
     let </span>small,big = List.partition ((&gt;) pivot) rest
     quicksort small @ [pivot] @ quicksort big

<span style="color: blue">let </span>QuickSort x =
  printMethod <span style="color: maroon">&quot;&quot;
  </span>quicksort x   </pre>
<p>If we run the scenario, we get the following output (I shortened a bit):</p>
<blockquote>
<p>Scenario: When sorting empty list </p>
<p>- Given []<br />
    &#8211; When sorting with QuickSort<br />
    =&gt; It should be sorted<br />
    =&gt; It should contain all elements from []<br />
    =&gt; It should contain no other elements than []<br />
    ==&gt; Result is: []<br />
    ==&gt; OK<br />
    ==&gt; Time: 0.0355s</p>
<p>Scenario: When sorting small list</p>
<p>- Given [2; 1; 8; 15; 5; 22]<br />
    &#8211; When sorting with QuickSort<br />
    =&gt; It should be sorted<br />
    =&gt; It should contain all elements from [2; 1; 8; 15; 5; 22]<br />
    =&gt; It should contain no other elements than [2; 1; 8; 15; 5; 22]<br />
    ==&gt; Result is: [1; 2; 5; 8; 15; 22]<br />
    ==&gt; OK<br />
    ==&gt; Time: 0.0065s</p>
<p>Scenario: When sorting ordered list</p>
<p>[…]&#160; 100 elements<br />
    ==&gt; OK<br />
    ==&gt; Time: 0.0939s</p>
<p>Scenario: When sorting ordered list</p>
<p>[…]&#160; 1000 elements<br />
    ==&gt; OK<br />
    ==&gt; Time: 0.7130s</p>
<p>Scenario: When sorting ordered list</p>
<p>[…]&#160; 2500 elements<br />
    ==&gt; OK<br />
    ==&gt; Time: 3.0631s</p>
<p>Scenario: When sorting random list </p>
<p>[…]&#160; 100 elements<br />
    ==&gt; OK<br />
    ==&gt; Time: 0.0485s</p>
<p>Scenario: When sorting random list</p>
<p>[…]&#160; 1000 elements<br />
    ==&gt; OK<br />
    ==&gt; Time: 0.1878s</p>
<p>Scenario: When sorting random list</p>
<p>[…]&#160; 1000 elements<br />
    ==&gt; OK<br />
    ==&gt; Time: 0.8713s</p>
</blockquote>
<p>As you can see the function is much faster if we sort a random list. This is because of the naïve choice of the pivot element.</p>
<p>I don’t want to give better implementations here (use <a href="http://msdn.microsoft.com/en-us/vcsharp/aa336756.aspx#simple1">LINQ</a> or <a href="http://blogs.msdn.com/pfxteam/archive/2008/06/11/8592301.aspx">PLINQ</a>). I just wanted to show how we can easily verify a test function with NaturalSpec.</p>
<p>&copy;2010 <a href="http://www.navision-blog.de">Rash thoughts about .NET, C#, F# and Dynamics NAV.</a>. All Rights Reserved.</p>.]]></content:encoded>
			<wfw:commentRss>http://www.navision-blog.de/2009/03/01/testing-quicksort-with-naturalspec/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Parameterized Scenarios with NaturalSpec</title>
		<link>http://www.navision-blog.de/2009/02/28/parameterized-scenarios-with-naturalspec/</link>
		<comments>http://www.navision-blog.de/2009/02/28/parameterized-scenarios-with-naturalspec/#comments</comments>
		<pubDate>Sat, 28 Feb 2009 15:46:04 +0000</pubDate>
		<dc:creator>Steffen Forkmann</dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[NaturalSpec]]></category>
		<category><![CDATA[nunit]]></category>
		<category><![CDATA[ScenarioTemplate]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://www.navision-blog.de/2009/02/28/parameterized-scenarios-with-naturalspec/</guid>
		<description><![CDATA[I wrote a lot about NaturalSpec in my last articles. This time I will show how we can use parameterized scenarios. 1. Using predefined scenarios By writing predefined parameterized scenarios we can easily create a scenario suite with lots of different test cases: // predefined scenario let factorialScenario x result = Given x &#124;&#62; When [...]]]></description>
			<content:encoded><![CDATA[<p>I wrote a lot about <a href="http://code.google.com/p/natural/">NaturalSpec</a> in my <a href="http://www.navision-blog.de/tag/naturalspec/">last articles</a>. This time I will show how we can use parameterized scenarios.</p>
<h5>1. Using predefined scenarios</h5>
<p>By writing predefined parameterized scenarios we can easily create a scenario suite with lots of different test cases:</p>
<pre class="code"><span style="color: green">// predefined scenario
</span><span style="color: blue">let </span>factorialScenario x result =
  Given x
    |&gt; When calculating factorial
    |&gt; It should equal result

[&lt;Scenario&gt;]
<span style="color: blue">let </span>When_calculation_factorial_of_1() =
  factorialScenario 1 1
    |&gt; Verify   

[&lt;Scenario&gt;]
<span style="color: blue">let </span>When_calculation_factorial_of_10() =
  factorialScenario 10 3628800
    |&gt; Verify</pre>
<p>If we run these scenarios with <a href="http://nunit.com/index.php">NUnit</a> we will get the following output:</p>
<blockquote>
<p>Scenario: When calculation factorial of 1<br />
    <br />&#160; &#8211; Given 1 </p>
<p>&#160;&#160;&#160; &#8211; When calculating factorial </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160; =&gt; It should equal 1</p>
<p>==&gt; OK<br />
    <br />==&gt; Time: 0.0093s</p>
<p>Scenario: When calculation factorial of 10<br />
    <br />&#160; &#8211; Given 10 </p>
<p>&#160;&#160;&#160; &#8211; When calculating factorial </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160; =&gt; It should equal 3628800</p>
<p>==&gt; OK<br />
    <br />==&gt; Time: 0.0018s</p>
</blockquote>
<h5>2. Using the ScenarioTemplate attribute</h5>
<p>The second and shorter option is to use the <em>ScenarioTemplate</em> attribute, which is inherited from NUnit’s <a href="http://nunit.com/blogs/?p=60">new <em>TestCase</em> attribute</a>:</p>
<pre class="code"><span style="color: green">// with ScenarioTemplate Attribute
</span>[&lt;ScenarioTemplate(1, 1)&gt;]
[&lt;ScenarioTemplate(2, 2)&gt;]
[&lt;ScenarioTemplate(5, 120)&gt;]
[&lt;ScenarioTemplate(10, 3628800)&gt;]
<span style="color: blue">let </span>When_calculating_fac_(x,result) =
  Given x
    |&gt; When calculating factorial
    |&gt; It should equal result
    |&gt; Verify</pre>
<p>This code will create 4 different scenarios, which NUnit will display and report separately:</p>
<p><img class="bordered" title="NUnit runner reports test cases" alt="NUnit runner reports test cases" src="http://www.navision-blog.de/images/ParameterizedScenarioswithNaturalSpec_EBB7/image.png" width="271" border="0" /></p>
<h5>3. Using ScenarioSource</h5>
<p>The third option is to use the <em>ScenarioSource</em> attribute. Here we define a function which generates TestData:</p>
<pre class="code"><span style="color: green">/// with a scenario source
</span><span style="color: blue">let </span>MyTestCases =
  TestWith 12 3 4
    |&gt; And 12 4 3
    |&gt; And 12 6 2
    |&gt; And 1200 40 30
    |&gt; And 0 0 0 |&gt; ShouldFailWith (typeof&lt;System.DivideByZeroException&gt;)</pre>
<p>And then we have to tell NaturalSpec which TestData a scenario should use:</p>
<pre class="code">[&lt;Scenario&gt;]
[&lt;ScenarioSource <span style="color: maroon">&quot;MyTestCases&quot;</span>&gt;]
<span style="color: blue">let </span>When_dividing a b result =
 Given a
   |&gt; When dividing_by b
   |&gt; It should equal result
   |&gt; Verify</pre>
<pre class="code">&#160;<img class="bordered" title="image" alt="NaturalSpec with ScenarioSource" src="http://www.navision-blog.de/images/ParameterizedScenarioswithNaturalSpec_EBB7/image_3.png" width="500" border="0" />  </pre>
<h5>Summary</h5>
<p>Sometime it makes sense to test a scenario with a bunch of different parameters. We can use the <em>ScenarioTemplate</em> attribute to easily parameterize our scenarios. If we want more flexibility we can use predefined scenarios with custom parameters or the ScenarioSource attribute.</p>
<p>&copy;2010 <a href="http://www.navision-blog.de">Rash thoughts about .NET, C#, F# and Dynamics NAV.</a>. All Rights Reserved.</p>.]]></content:encoded>
			<wfw:commentRss>http://www.navision-blog.de/2009/02/28/parameterized-scenarios-with-naturalspec/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mocking objects with NaturalSpec</title>
		<link>http://www.navision-blog.de/2009/02/25/mocking-objects-with-naturalspec/</link>
		<comments>http://www.navision-blog.de/2009/02/25/mocking-objects-with-naturalspec/#comments</comments>
		<pubDate>Wed, 25 Feb 2009 15:56:42 +0000</pubDate>
		<dc:creator>Steffen Forkmann</dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[NaturalSpec]]></category>
		<category><![CDATA[Mocking]]></category>
		<category><![CDATA[Rhino.Mocks]]></category>

		<guid isPermaLink="false">http://www.navision-blog.de/2009/02/25/mocking-objects-with-naturalspec/</guid>
		<description><![CDATA[In my last articles I gave an introduction in NaturalSpec, showed how to get started and demonstrated how we can use NaturalSpec to write automatically testable scenarios for C# projects. This time I will use the same “Car-Dealer”-sample to show how we can mock objects in NaturalSpec. Mocking objects is an important technique in Test-driven [...]]]></description>
			<content:encoded><![CDATA[<p>In my last articles I gave an <a href="http://www.navision-blog.de/2009/02/23/introducing-naturalspec-a-dsl-for-testing-part-i/">introduction</a> in <a href="http://code.google.com/p/natural/">NaturalSpec</a>, showed <a href="http://www.navision-blog.de/2009/11/08/getting-started-with-naturalspec/">how to get started</a> and demonstrated how we can use <a href="http://code.google.com/p/natural/">NaturalSpec</a> to write automatically <a href="http://www.navision-blog.de/2009/02/23/using-naturalspec-to-create-spec-for-c-projects/">testable scenarios for C# projects</a>. This time I will use the same <a href="http://www.navision-blog.de/2009/02/23/using-naturalspec-to-create-spec-for-c-projects/">“Car-Dealer”-sample</a> to show how we can mock objects in NaturalSpec.</p>
<p>Mocking objects is an important technique in Test-driven development (<a href="http://en.wikipedia.org/wiki/Test-driven_development">TDD</a>) and allows us to simulate complex behavior.</p>
<blockquote><p>“If an object has any of the following characteristics, it may be useful to use a mock object in its place:</p>
<ul>
<li>supplies non-deterministic results (e.g. the current time or the current temperature); </li>
<li>has states that are difficult to create or reproduce (e.g. a network error); </li>
<li>is slow (e.g. a complete database, which would have to be initialized before the test); </li>
<li>does not yet exist or may change behavior; </li>
<li>would have to include information and methods exclusively for testing purposes (and not for its actual task).” </li>
</ul>
<p align="right"><a href="http://en.wikipedia.org/wiki/Mock_object">Wikipedia</a></p>
</blockquote>
<p>In our sample we want to mock the <em>Dealer.SellCar()</em> functionality. The first step is to create an C#-Interface for the Dealer:</p>
<pre class="code"><span style="color: blue">namespace </span>CarSellingLib
{
    <span style="color: blue">public interface </span><span style="color: #2b91af">IDealer
    </span>{
        <span style="color: #2b91af">Car </span>SellCar(<span style="color: blue">int </span>amount);
    }
}</pre>
<p>NaturalSpec is using <a href="http://ayende.com/projects/rhino-mocks.aspx">Rhino.Mocks</a> as the underlying mocking framework, so we have to create a reference to Rhino.Mocks.dll in our spec library.</p>
<p>Now we can modify our spec to:</p>
<pre class="code"><span style="color: green">// 1. open module
</span><span style="color: blue">module </span>CarSpec</pre>
<pre class="code"><span style="color: green">// 2. open NaturalSpec-Namespace
</span><span style="color: blue">open </span>NaturalSpec

<span style="color: green">// 3. open project namespace
</span><span style="color: blue">open </span>CarSellingLib

<span style="color: green">// define reusable values
</span><span style="color: blue">let </span>DreamCar = <span style="color: blue">new </span>Car(CarType.BMW, 200)
<span style="color: blue">let </span>LameCar = <span style="color: blue">new </span>Car(CarType.Fiat, 45)

<span style="color: green">// 4. define a mock object and give it a name
</span><span style="color: blue">let </span>Bert = mock&lt;IDealer&gt; <span style="color: maroon">&quot;Bert&quot;

</span><span style="color: green">// 5. create a method in BDD-style
</span><span style="color: blue">let </span>selling_a_car_for amount (dealer:IDealer) =
  printMethod amount
  dealer.SellCar amount

<span style="color: green">// 6. create a scenario
</span>[&lt;Scenario&gt;]
<span style="color: blue">let </span>When_selling_a_car_for_30000_it_should_equal_the_DreamCar_mocked() =
  As Bert
    |&gt; Mock Bert.SellCar 30000 DreamCar  <span style="color: green">// 7. register mocked call
    </span>|&gt; When selling_a_car_for 30000
    |&gt; It should equal DreamCar
    |&gt; It shouldn't equal LameCar
    |&gt; Verify</pre>
<p>As you can see we changed part 4 (in order to get a mocked <em>IDealer</em> instead of the concrete <em>Dealer</em>). In part 7 we register our mocked behavior. We want that whenever <em>Bert.SellCar</em> is called with parameter <em>30000</em> the <em>DreamCar </em>should be returned.</p>
<p>The <em><strong>Verify</strong></em>-function checks if the mocked function has been called. If not the scenario will fail.</p>
<p>If we verify our spec with a NUnit runner we get the following output:</p>
<blockquote>
<p>Scenario: When selling a car for 30000 it should equal the DreamCar mocked</p>
<p>- As Bert<br />
    <br />- With Mocking </p>
<p>- When selling a car for 30000 </p>
<p>=&gt; It should equal BMW (200 HP) </p>
<p>=&gt; It should not equal Fiat (45 HP) </p>
<p>==&gt; OK</p>
</blockquote>
<p>&copy;2010 <a href="http://www.navision-blog.de">Rash thoughts about .NET, C#, F# and Dynamics NAV.</a>. All Rights Reserved.</p>.]]></content:encoded>
			<wfw:commentRss>http://www.navision-blog.de/2009/02/25/mocking-objects-with-naturalspec/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using NaturalSpec to create a spec for C# projects (Updated 08.11.2009)</title>
		<link>http://www.navision-blog.de/2009/02/23/using-naturalspec-to-create-spec-for-c-projects/</link>
		<comments>http://www.navision-blog.de/2009/02/23/using-naturalspec-to-create-spec-for-c-projects/#comments</comments>
		<pubDate>Mon, 23 Feb 2009 17:07:08 +0000</pubDate>
		<dc:creator>Steffen Forkmann</dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[NaturalSpec]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[BDD]]></category>
		<category><![CDATA[spec]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://www.navision-blog.de/2009/02/23/using-naturalspec-to-create-spec-for-c-projects/</guid>
		<description><![CDATA[In my last two articles I gave an introduction in NaturalSpec and showed how to get started. This time I will show how we can use NaturalSpec to write automatically testable scenarios for C# projects. Like the TDD principle “Write the tests first” we should write our spec first and use the “Red-Green-Refactor” method. &#34;Red&#34; [...]]]></description>
			<content:encoded><![CDATA[<p>In my last two articles I gave an <a href="http://www.navision-blog.de/2009/02/23/introducing-naturalspec-a-dsl-for-testing-part-i/">introduction</a> in <a href="http://code.google.com/p/natural/">NaturalSpec</a> and showed <a href="http://www.navision-blog.de/2009/11/08/getting-started-with-naturalspec/">how to get started</a>. This time I will show how we can use <a href="http://code.google.com/p/natural/">NaturalSpec</a> to write automatically testable scenarios for C# projects.</p>
<p>Like the <a href="http://en.wikipedia.org/wiki/Test_Driven_Development">TDD</a> principle “Write the tests first” we should write our spec first and use the “Red-Green-Refactor” method.</p>
<h5>&quot;Red&quot; – Create a spec scenario that fails</h5>
<p>At first I created a F# class library project called “Spec.CarSelling” and added project references to <em>NaturalSpec.dll</em> and <em>nunit.framework.dll</em> (see <a href="http://www.navision-blog.de/2009/02/23/getting-started-with-naturalspec/">“Getting started”</a> for further explanations).</p>
<p>Now I can write my first scenario:</p>
<pre class="code"><span style="color: green">// 1. define the module
</span><span style="color: blue">module </span>CarSpec</pre>
<pre class="code"><span style="color: green">// 2. open the NaturalSpec namespace</span>
<span style="color: blue">open </span>NaturalSpec

<span style="color: green">// 3. open project namespace
</span><span style="color: blue">open </span>CarSellingLib

<span style="color: green">// 4. define a test context
</span><span style="color: blue">let </span>Bert = <span style="color: blue">new </span>Dealer(<span style="color: maroon">&quot;Bert&quot;</span>)

<span style="color: green">// 5. create a method in BDD-style
</span><span style="color: blue">let </span>selling_a_car_for amount (dealer:Dealer) =
  printMethod amount
  dealer.SellCar amount

<span style="color: green">// 6. create a scenario
</span>[&lt;Scenario&gt;]
<span style="color: blue">let </span>When_selling_a_car_for_30000_it_should_equal_my_DreamCar() =
  As Bert
    |&gt; When selling_a_car_for 30000
    |&gt; It should equal (<span style="color: blue">new </span>Car(CarType.BMW, 200))
    |&gt; Verify</pre>
<p>At this stage the scenario is ready but doesn’t compile. This means we are ready with the &quot;Red&quot;-stage.</p>
<h5>&quot;Green&quot; – Make the test the pass</h5>
<p>In order to get the test green we have to create a C# class library called <strong>CarSellingLib</strong> and define the enum <strong>CarType</strong> and the classes <strong>Dealer</strong> and <strong>Car</strong>. Sticking to the <a href="http://en.wikipedia.org/wiki/You_Ain%27t_Gonna_Need_It">YAGNI-principle</a> we implement only the minimum to get the spec green (and ToString()-members for the output functionality).</p>
<pre class="code"><span style="color: blue">namespace </span>CarSellingLib
{
    <span style="color: blue">public enum </span><span style="color: #2b91af">CarType
    </span>{
        BMW
    }
}</pre>
<p>
  </p>
<pre class="code"><span style="color: blue">namespace </span>CarSellingLib
{
    <span style="color: blue">public class </span><span style="color: #2b91af">Car
    </span>{
        <span style="color: blue">public </span>Car(<span style="color: #2b91af">CarType </span>type, <span style="color: blue">int </span>horsePower)
        {
            Type = type;
            HorsePower = horsePower;
        }

        <span style="color: blue">public </span><span style="color: #2b91af">CarType </span>Type { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
        <span style="color: blue">public int </span>HorsePower { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }

        <span style="color: blue">public override string </span>ToString()
        {
            <span style="color: blue">return string</span>.Format(<span style="color: #a31515">&quot;{0} ({1} HP)&quot;</span>, Type, HorsePower);
        }

        <span style="color: blue">public override bool </span>Equals(<span style="color: blue">object </span>obj)
        {
            <span style="color: blue">var </span>y = obj <span style="color: blue">as </span><span style="color: #2b91af">Car</span>;
            <span style="color: blue">if</span>(y == <span style="color: blue">null</span>) <span style="color: blue">return false</span>;
            <span style="color: blue">return </span>Type == y.Type &amp;&amp; HorsePower == y.HorsePower;
        }
<span style="color: blue">    </span>}
}</pre>
<pre class="code"><span style="color: blue">using </span>System;

<span style="color: blue">namespace </span>CarSellingLib
{
    <span style="color: blue">public class </span><span style="color: #2b91af">Dealer
    </span>{
        <span style="color: blue">public </span>Dealer(<span style="color: blue">string </span>name)
        {
            Name = name;
        }

        <span style="color: blue">public string </span>Name { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }

        <span style="color: blue">public </span><span style="color: #2b91af">Car </span>SellCar(<span style="color: blue">int </span>amount)
        {
            <span style="color: blue">return new </span><span style="color: #2b91af">Car</span>(<span style="color: #2b91af">CarType</span>.BMW, 200);
        }

        <span style="color: blue">public override string </span>ToString()
        {
            <span style="color: blue">return </span>Name;
        }
    }
}</pre>
<p>
  <br />When we add a project reference to our spec-project the UnitTests should pass and we have completed the &quot;Green&quot; step. (See &quot;<a href="http://www.navision-blog.de/2009/11/08/getting-started-with-naturalspec/">Getting started</a>&quot; if you don’t know how to run the spec.) Now we can add some more scenarios to our spec: </p>
<p></p>
<pre class="code"><span style="color: green">// 1. define the module </span>
<span style="color: blue">module </span>CarSpec</pre>
<pre class="code"><span style="color: green">// 2. open NaturalSpec-Namespace
</span><span style="color: blue">open </span>NaturalSpec

<span style="color: green">// 3. open project namespace
</span><span style="color: blue">open </span>CarSellingLib

<span style="color: green">// 4. define a test context
</span><span style="color: blue">let </span>Bert = <span style="color: blue">new </span>Dealer(<span style="color: maroon">&quot;Bert&quot;</span>)

<span style="color: green">// define reusable values
</span><span style="color: blue">let </span>DreamCar = <span style="color: blue">new </span>Car(CarType.BMW, 200)
<span style="color: blue">let </span>LameCar = <span style="color: blue">new </span>Car(CarType.Fiat, 45)

<span style="color: green">// 5. create a method in BDD-style
</span><span style="color: blue">let </span>selling_a_car_for amount (dealer:Dealer) =
  printMethod amount
  dealer.SellCar amount

<span style="color: green">// 6. create a scenario
</span>[&lt;Scenario&gt;]
<span style="color: blue">let </span>When_selling_a_car_for_30000_it_should_equal_the_DreamCar() =
  As Bert
    |&gt; When selling_a_car_for 30000
    |&gt; It should equal DreamCar
    |&gt; It shouldn't equal LameCar
    |&gt; Verify      

[&lt;Scenario&gt;]
<span style="color: blue">let </span>When_selling_a_car_for_19000_it_should_equal_the_LameCar() =
  As Bert
    |&gt; When selling_a_car_for 19000
    |&gt; It should equal LameCar
    |&gt; It shouldn't equal DreamCar
    |&gt; Verify</pre>
<p>
  </p>
<pre class="code"><span style="color: green">// create a scenario that expects an error</span>
[&lt;Scenario&gt;]
[&lt;Fails_with <span style="color: #a31515">&quot;Need more money&quot;</span>&gt;]
<span style="color: blue">let </span>When_selling_a_car_for_1000_it_should_fail_with_Need_More_Money() =
  As Bert
    |&gt; When selling_a_car_for 1000
    |&gt; Verify</pre>
<p>Now we are in the “Red”-Phase again.</p>
<h5>&quot;Refactor&quot; &#8211; rearrange your code to eliminate duplication and follow patterns</h5>
<p>After making the spec &quot;Green&quot; and doing some refactoring the project code could look like this:</p>
<pre class="code"><span style="color: blue">namespace </span>CarSellingLib
{
    <span style="color: blue">public enum </span><span style="color: #2b91af">CarType
    </span>{
        Fiat,
        BMW
    }
}</pre>
<p>
  </p>
<pre class="code"><span style="color: blue">namespace </span>CarSellingLib
{
    <span style="color: blue">public class </span><span style="color: #2b91af">Car
    </span>{
        <span style="color: blue">public </span>Car(<span style="color: #2b91af">CarType </span>type, <span style="color: blue">int </span>horsePower)
        {
            Type = type;
            HorsePower = horsePower;
        }

        <span style="color: blue">public </span><span style="color: #2b91af">CarType </span>Type { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
        <span style="color: blue">public int </span>HorsePower { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }

        <span style="color: blue"># region </span>ToString, Equals 

        <span style="color: blue">public override string </span>ToString()
        {
            <span style="color: blue">return string</span>.Format(<span style="color: #a31515">&quot;{0} ({1} HP)&quot;</span>, Type, HorsePower);
        }

        <span style="color: blue">public override bool </span>Equals(<span style="color: blue">object </span>obj)
        {
            <span style="color: blue">var </span>y = obj <span style="color: blue">as </span><span style="color: #2b91af">Car</span>;
            <span style="color: blue">if</span>(y == <span style="color: blue">null</span>) <span style="color: blue">return false</span>;
            <span style="color: blue">return </span>Type == y.Type &amp;&amp; HorsePower == y.HorsePower;
        }

        <span style="color: blue">#endregion
    </span>}
}</pre>
<p>
  </p>
<pre class="code"><span style="color: blue">using </span>System;

<span style="color: blue">namespace </span>CarSellingLib
{
    <span style="color: blue">public class </span><span style="color: #2b91af">Dealer
    </span>{
        <span style="color: blue">public </span>Dealer(<span style="color: blue">string </span>name)
        {
            Name = name;
        }

        <span style="color: blue">public string </span>Name { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }

        <span style="color: blue">public </span><span style="color: #2b91af">Car </span>SellCar(<span style="color: blue">int </span>amount)
        {
            <span style="color: blue">if </span>(amount &gt; 20000)
                <span style="color: blue">return new </span><span style="color: #2b91af">Car</span>(<span style="color: #2b91af">CarType</span>.BMW, 200);

            <span style="color: blue">if </span>(amount &gt; 3000)
                <span style="color: blue">return new </span><span style="color: #2b91af">Car</span>(<span style="color: #2b91af">CarType</span>.Fiat, 45);

            <span style="color: blue">throw new </span><span style="color: #2b91af">Exception</span>(<span style="color: #a31515">&quot;Need more money&quot;</span>);
        }

        <span style="color: blue">public override string </span>ToString()
        {
            <span style="color: blue">return </span>Name;
        }
    }
}</pre>
<p>The spec output should look like the following:</p>
<blockquote>
<p>Scenario: When selling a car for 1000 it should fail with Need More Money</p>
<p>- Should fail&#8230;<br />
    &#8211; As Bert<br />
    &#8211; When selling a car for 1000</p>
<p>Scenario: When selling a car for 19000 it should equal the LameCar</p>
<p>- As Bert<br />
    &#8211; When selling a car for 19000<br />
    =&gt; It should equal Fiat (45 HP)<br />
    =&gt; It should not equal BMW (200 HP)<br />
    ==&gt; OK</p>
<p>Scenario: When selling a car for 30000 it should equal my DreamCar</p>
<p>- As Bert<br />
    &#8211; When selling a car for 30000<br />
    =&gt; It should equal BMW (200 HP)<br />
    ==&gt; OK</p>
<p>Scenario: When selling a car for 30000 it should equal the DreamCar</p>
<p>- As Bert<br />
    &#8211; When selling a car for 30000<br />
    =&gt; It should equal BMW (200 HP)<br />
    =&gt; It should not equal Fiat (45 HP)<br />
    >==&gt; OK</p>
<p>4 passed, 0 failed, 0 skipped, took 1,81 seconds (NUnit 2.5).</p>
</blockquote>
<h5>Summary</h5>
<p>I showed how we can use NaturalSpec for the Red-Green-Refactor process of C# projects and how easy it is to get a spec in natural language.</p>
<p>&copy;2010 <a href="http://www.navision-blog.de">Rash thoughts about .NET, C#, F# and Dynamics NAV.</a>. All Rights Reserved.</p>.]]></content:encoded>
			<wfw:commentRss>http://www.navision-blog.de/2009/02/23/using-naturalspec-to-create-spec-for-c-projects/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Introducing NaturalSpec &#8211; A Domain-specific language (DSL) for testing &#8211; Part I</title>
		<link>http://www.navision-blog.de/2009/02/23/introducing-naturalspec-a-dsl-for-testing-part-i/</link>
		<comments>http://www.navision-blog.de/2009/02/23/introducing-naturalspec-a-dsl-for-testing-part-i/#comments</comments>
		<pubDate>Mon, 23 Feb 2009 10:31:51 +0000</pubDate>
		<dc:creator>Steffen Forkmann</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[F#]]></category>
		<category><![CDATA[NaturalSpec]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[BDD]]></category>
		<category><![CDATA[Behavior-Driven Development]]></category>
		<category><![CDATA[domain-specific language]]></category>
		<category><![CDATA[DSL]]></category>
		<category><![CDATA[Joel test]]></category>
		<category><![CDATA[nunit]]></category>
		<category><![CDATA[spec]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Test-Driven Development]]></category>
		<category><![CDATA[TestDriven.net]]></category>

		<guid isPermaLink="false">http://www.navision-blog.de/2009/02/23/introducing-naturalspec-a-dsl-for-testing-part-i/</guid>
		<description><![CDATA[Test-Driven development (TDD) is a well known software development technique and follows the mantra “Red-Green-Refactor”. Behavior-Driven Development (BDD) is a response to TDD and introduces the idea of using natural language to express the Unit Test scenarios. There are a lot of popular testing frameworks around which can be used for BDD including xUnit.net ,NUnit, [...]]]></description>
			<content:encoded><![CDATA[<p>Test-Driven development (<a href="http://en.wikipedia.org/wiki/Test_Driven_Development">TDD</a>) is a well known software development technique and follows the mantra “<a href="http://jamesshore.com/Blog/Red-Green-Refactor.html">Red-Green-Refactor</a>”. Behavior-Driven Development (<a href="http://en.wikipedia.org/wiki/Behavior_Driven_Development">BDD</a>) is a response to TDD and introduces the idea of using natural language to express the Unit Test scenarios.</p>
<p>There are a lot of popular testing frameworks around which can be used for BDD including <a href="http://www.codeplex.com/xunit">xUnit.net</a> ,<a href="http://www.nunit.org/index.php?p=home">NUnit</a>, <a href="http://www.codeplex.com/StoryQ/">StoryQ</a>, <a href="http://github.com/machine/machine/tree/master">MSpec</a>, <a href="http://nspec.tigris.org/">NSpec</a> and <a href="http://nbehave.org/">NBehave</a>. Most of them can be used with <a href="http://en.wikipedia.org/wiki/Fluent_interface">fluent interfaces</a> and therefore provides a good readability of the sources. Some of them even provide the possibility to generate a spec in natural language out of passed Unit tests.</p>
<h5>What is a spec?</h5>
<blockquote><p>“A <strong>specification</strong> is an explicit set of requirements to be satisfied by a material, product, or service.”</p>
<p align="right">American Society for Testing and Materials (<a href="http://www.astm.org/">ASTM</a>) definition</p>
</blockquote>
<p>A spec is an important document for the communication process – it enables domain experts to communicate with developers. But how can you verify the compliance with the spec? The answer is: you have to write unit tests. Even with the mentioned frameworks there is a lot of work to do in order to translate a spec scenario into a Unit Test.</p>
<blockquote><p>Question 7 in the famous <a href="http://www.joelonsoftware.com/articles/fog0000000043.html">Joel Test</a> is “<strong>Do you have a spec?”.</strong></p>
</blockquote>
<p>The idea of <a href="http://code.google.com/p/natural/">NaturalSpec</a> is to give domain experts the possibility to express their scenarios directly in compilable Unit Test scenarios by using a <a href="http://en.wikipedia.org/wiki/Domain_Specific_Language">Domain-specific language</a> (DSL) for Unit Tests. NaturalSpec is completely written in <a href="http://en.wikipedia.org/wiki/F_Sharp_programming_language">F#</a> – but you don’t have to learn F# to use it. You don’t even have to learn programming at all.</p>
<h5>Example 1 – Specifying a list</h5>
<p>Let’s consider a small example. If we want to test a new List implementation a spec could look like this:</p>
<pre class="code">[&lt;Scenario&gt;]
<span style="color: blue">let </span>When_removing_an_3_from_a_small_list_it_should_not_contain_3() =
  Given [1;2;3;4;5]              <span style="color: green">// “<strong>Arrange</strong>” test context
    </span>|&gt; When removing 3           <span style="color: green">// “<strong>Act</strong>”
    </span>|&gt; It shouldn't contain 3    <span style="color: green">// “<strong>Assert</strong>”
    </span>|&gt; It should contain 4       <span style="color: green">// another assertion
</span>    |&gt; Verify                    <span style="color: green">// Verify scenario</span></pre>
<p>I used <a href="http://en.wikipedia.org/wiki/Behavior_Driven_Development">BDD style</a> here and expressed my scenario in a quite natural language. As the comments are indicating the scenario is following the Arrange Act Assert (“AAA”) pattern.</p>
<p>With the Keyword “<strong>Given</strong>” I can create a test context (the objects I want to test). In this sample I created a list with 5 elements. With the keyword “<strong>When</strong>” I call a function which does something with my test context. In this case I want to remove the value 3. In the Assert section (keywords “<strong>It should</strong>” or “<strong>It shouldn’t</strong>”) I can give some observations, which should hold for my manipulated test context.</p>
<p>When I run this scenario via a NUnit runner (i am using <a href="http://testdriven.net/">TestDriven.Net</a>) I get the following output:</p>
<blockquote>
<p>Scenario: When removing an 3 from a small list it should not contain 3</p>
<p>- Given [1; 2; 3; 4; 5]<br />
    &#8211; When removing 3<br />
    =&gt; It should not contain 3<br />
    =&gt; It should contain 4<br />
    ==&gt; OK</p>
</blockquote>
<h5>Example 2 – Specifying a factorial function</h5>
<p>If you implement <a href="http://en.wikipedia.org/wiki/Factorial">factorial</a> function the spec could look like this:</p>
<pre class="code">[&lt;Scenario&gt;]
<span style="color: blue">let </span>When_calculating_fac_5_it_should_equal_120() =
  Given 5
    |&gt; When calculating factorial
    |&gt; It should equal 120
    |&gt; Verify    

[&lt;Scenario&gt;]
<span style="color: blue">let </span>When_calculating_fac_1_it_should_equal_1() =
  Given 1
    |&gt; When calculating factorial
    |&gt; It should equal 1
    |&gt; Verify          

[&lt;Scenario&gt;]
<span style="color: blue">let </span>When_calculating_fac_0_it_should_equal_0() =
  Given 0
    |&gt; When calculating factorial
    |&gt; It should equal 1
    |&gt; Verify</pre>
<p>And the output of NaturalSpec would look like this:</p>
<blockquote>
<p>Scenario: When calculating fac 0 it should equal 0</p>
<p>- Given 0<br />
    &#8211; When calculating factorial<br />
    =&gt; It should equal 1<br />
    ==&gt; OK</p>
<p>Scenario: When calculating fac 1 it should equal 1</p>
<p>- Given 1<br />
     &#8211; When calculating factorial<br />
     =&gt; It should equal 1<br />
     ==&gt; OK</p>
<p>Scenario: When calculating fac 5 it should equal 120</p>
<p>- Given 5<br />
    &#8211; When calculating factorial<br />
    =&gt; It should equal 120<br />
    ==&gt; OK</p>
</blockquote>
<h5>Getting started</h5>
<p>Of course you can use NaturalSpec to specify C# objects. I see my post &quot;<a href="http://www.navision-blog.de/2009/02/23/using-naturalspec-to-create-spec-for-c-projects/">Using NaturalSpec to create a spec for C# projects</a>&quot; for a small sample.</p>
<p>You can download <a href="http://code.google.com/p/natural/">NaturalSpec at GoogleCode</a> and follow the <a href="http://www.navision-blog.de/2009/11/08/getting-started-with-naturalspec/ ">“Getting started” tutorial</a> in order to write your first automatically testable spec.</p>
<p>I am very interested in your feedback. Do you like the syntax? What should I change? Do you consider using a spec tool like NaturalSpec?</p>
<p>&copy;2010 <a href="http://www.navision-blog.de">Rash thoughts about .NET, C#, F# and Dynamics NAV.</a>. All Rights Reserved.</p>.]]></content:encoded>
			<wfw:commentRss>http://www.navision-blog.de/2009/02/23/introducing-naturalspec-a-dsl-for-testing-part-i/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
