<commit_before>---
layout: post
title: "Types + Properties = Software: properties for the advantage state"
date: 2016-02-12 8:41 UTC
tags: [Unit Testing, Software Design, F#, FsCheck]
---
{% include JB/setup %}

<div id="post">
	<p>
		<em>An example of Property-Based Test-Driven Development.</em>
	</p>
	<p>
		This article is the third in <a href="http://blog.ploeh.dk/2016/02/10/types-properties-software">a series of articles that demonstrate how to develop software using types and properties</a>. In the <a href="http://blog.ploeh.dk/2016/02/11/types-properties-software-state-transition-properties">previous article</a>, you saw how to get started with Property-Based Testing, using a Test-Driven Development tactic. In this article, you'll see the previous Tennis Kata example continued. This time, you'll see how to express properties for the state when one of the players have the advantage.
	</p>
	<p>
		The source code for this article series is <a href="https://github.com/ploeh/KataTennis">available on GitHub</a>.
	</p>
	<h3 id="352c24dd4e0848b084d08dec67dd7da1">
		Winning the game <a href="#352c24dd4e0848b084d08dec67dd7da1" title="permalink">#</a>
	</h3>
	<p>
		When one of the players have the advantage in tennis, the result can go one of two ways: either the player with the advantage wins the ball, in which case he or she wins the game, or the other player wins, in which case the next score is <em>deuce</em>. This implies that you'll have to write at least two properties: one for each situation. Let's start with the case where the advantaged player wins the ball. Using FsCheck, you can express that property like this:
	</p>
	<p>
		<pre>[&lt;<span style="color:#4ec9b0;">Property</span>&gt;]
<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``Given&nbsp;advantage&nbsp;when&nbsp;advantaged&nbsp;player&nbsp;wins&nbsp;then&nbsp;score&nbsp;is&nbsp;correct``</span>
&nbsp;&nbsp;&nbsp;&nbsp;(advantagedPlayer&nbsp;:&nbsp;<span style="color:#4ec9b0;">Player</span>)&nbsp;=
 
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;:&nbsp;<span style="color:#4ec9b0;">Score</span>&nbsp;=&nbsp;<span style="color:navy;">scoreWhenAdvantage</span>&nbsp;advantagedPlayer&nbsp;advantagedPlayer
 
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;expected&nbsp;=&nbsp;<span style="color:navy;">Game</span>&nbsp;advantagedPlayer
&nbsp;&nbsp;&nbsp;&nbsp;expected&nbsp;=!&nbsp;actual</pre>
	</p>
	<p>
		As explained in the previous article, <a href="http://fscheck.github.io/FsCheck">FsCheck</a> will interpret this function and discover that it'll need to generate arbitrary Player values for the <code>advantagedPlayer</code> argument. Because <a href="http://blog.ploeh.dk/2016/02/10/types-properties-software-designing-with-types">illegal states are unrepresentable</a>, you're guaranteed valid values.<commit_msg>Added Twitter card to article<commit_after>---
layout: post
title: "Types + Properties = Software: properties for the advantage state"
description: "An example of Property-Based Test-Driven Development."
date: 2016-02-12 8:41 UTC
tags: [Unit Testing, Software Design, F#, FsCheck]
---
{% include JB/setup %}

<div id="post">
	<p>
		<em>{{ page.description }}</em>
	</p>
	<p>
		This article is the third in <a href="http://blog.ploeh.dk/2016/02/10/types-properties-software">a series of articles that demonstrate how to develop software using types and properties</a>. In the <a href="http://blog.ploeh.dk/2016/02/11/types-properties-software-state-transition-properties">previous article</a>, you saw how to get started with Property-Based Testing, using a Test-Driven Development tactic. In this article, you'll see the previous Tennis Kata example continued. This time, you'll see how to express properties for the state when one of the players have the advantage.
	</p>
	<p>
		The source code for this article series is <a href="https://github.com/ploeh/KataTennis">available on GitHub</a>.
	</p>
	<h3 id="352c24dd4e0848b084d08dec67dd7da1">
		Winning the game <a href="#352c24dd4e0848b084d08dec67dd7da1" title="permalink">#</a>
	</h3>
	<p>
		When one of the players have the advantage in tennis, the result can go one of two ways: either the player with the advantage wins the ball, in which case he or she wins the game, or the other player wins, in which case the next score is <em>deuce</em>. This implies that you'll have to write at least two properties: one for each situation. Let's start with the case where the advantaged player wins the ball. Using FsCheck, you can express that property like this:
	</p>
	<p>
		<pre>[&lt;<span style="color:#4ec9b0;">Property</span>&gt;]
<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``Given&nbsp;advantage&nbsp;when&nbsp;advantaged&nbsp;player&nbsp;wins&nbsp;then&nbsp;score&nbsp;is&nbsp;correct``</span>
&nbsp;&nbsp;&nbsp;&nbsp;(advantagedPlayer&nbsp;:&nbsp;<span style="color:#4ec9b0;">Player</span>)&nbsp;=
 
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;:&nbsp;<span style="color:#4ec9b0;">Score</span>&nbsp;=&nbsp;<span style="color:navy;">scoreWhenAdvantage</span>&nbsp;advantagedPlayer&nbsp;advantagedPlayer
 
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;expected&nbsp;=&nbsp;<span style="color:navy;">Game</span>&nbsp;advantagedPlayer
&nbsp;&nbsp;&nbsp;&nbsp;expected&nbsp;=!&nbsp;actual</pre>
	</p>
	<p>
		As explained in the previous article, <a href="http://fscheck.github.io/FsCheck">FsCheck</a> will interpret this function and discover that it'll need to generate arbitrary Player values for the <code>advantagedPlayer</code> argument. Because <a href="http://blog.ploeh.dk/2016/02/10/types-properties-software-designing-with-types">illegal states are unrepresentable</a>, you're guaranteed valid values.
{'fraction_non_alphanumeric': 0.14790665751544269, 'fraction_numerical': 0.028826355525051476, 'mean_word_length': 6.160933660933661, 'pattern_counts': {'":': 0, '<': 107, '<?xml version=': 0, '>': 107, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '683594', 'n_tokens_mistral': 2020, 'n_tokens_neox': 1824, 'n_words': 549}