F# Unit Testing – Refining the Squirrel Simulation
Tags: fsharp, dotnet, ai
Published: 2019-10-22 04:54:36

---
title: F# Unit Testing – Refining the Squirrel Simulation
published: true
date: 2019-10-22 04:29:06 UTC
tags: fsharp, dotnet, artificialintelligence
series: Emulating Squirrel Brains in F# with Genetic Algorithms
cover_image: https://i2.wp.com/res.cloudinary.com/practicaldev/image/fetch/s--ZLXHp8E6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/cnie8fys8rhydgvndn6i.png?w=770&ssl=1
canonical_url: https://killalldefects.com/2019/10/22/f-unit-testing-refining-the-squirrel-simulation/
---

[![](https://i0.wp.com/killalldefects.com/wp-content/uploads/2019/10/2019-10-22_0-19-25.gif?fit=768%2C528&ssl=1)](https://killalldefects.com/2019/10/22/f-unit-testing-refining-the-squirrel-simulation/ "F# Unit Testing – Refining the Squirrel Simulation")

This article on F# Unit Testing is part 3 in an ongoing series in which I walk you through building a genetic algorithm in F# that emulates a squirrel’s “brain” as it solves a simplistic puzzle.

Yes, really.

Previously [we created an F# Console application](https://killalldefects.com/2019/09/29/creating-a-net-core-fsharp-console-app/) that rendered small grid with a single squirrel in it. Next, we made that application a bit more functional and [introduced other actor types and world generation](https://killalldefects.com/2019/10/05/f-sharp-squirrel-brains-adding-actors-and-getting-functional/).

## Learning Objectives

In this article, I’m going to walk you through:

- Unit testing in F#
- Improving unit tests with FsUnit
- Writing unit tests for the core game logic
- Implementing specific features of note in the logic
- Improving the console app by giving it color

By the end of the article we’ll have a console app where the player can control the squirrel actor, get the acorn, and return to the tree to win the game. Alternatively the player could choose to let the dog eat the squirrel or just not eat anything and starve to death – either choice ends in a loss.

![The end result F# application](https://i1.wp.com/killalldefects.com/wp-content/uploads/2019/10/crop-0-0-1000-687-0-2019-10-22_0-19-25.gif?resize=500%2C344&ssl=1)

The final product isn’t intended to be played by humans. In future articles I’ll walk you through setting up the genetic algorithm and fitness function in order to get the squirrel to figure out how to navigate its environment and win the game on its own.

But for now, let’s get the core simulation logic sorted out so we can focus on the artificial intelligence aspects in future articles.

If you’d like to play along with the finished result, [the code is available on GitHub](https://github.com/IntegerMan/FSharpGeneticAlgorithm/tree/article3).

## F# Unit Testing

This article assumes you’re already familiar with the basic concept of a unit test, but maybe not in F#. If you’re not, take a look at [the Wikipedia entry for Unit Testing](https://en.wikipedia.org/wiki/Unit_testing) to familiarize yourself.

In F#, much like in other languages, we start by creating a new unit test project to our solution, making sure to choose F# as the language.

[![Create a new F# Unit Test Project](https://i2.wp.com/res.cloudinary.com/practicaldev/image/fetch/s--ExHzVTg8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/1t48j2gzgst367c25ynf.png?w=770&ssl=1)](https://i2.wp.com/res.cloudinary.com/practicaldev/image/fetch/s--ExHzVTg8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/1t48j2gzgst367c25ynf.png?ssl=1)

We’ll select an F# xUnit project. Either XUnit, NUnit, or MSTest could work, but XUnit is most frequently used with .NET Core.

Click Next, then name the project and click create.

Next we’ll expand the project in Solution Explorer, right click on Dependencies and click `Add Reference...`, then select the project reference for the logic project containing most of core logic. Click OK and the reference will be added.

## Adding an XUnit Test

We’re going to be using a different framework in a moment, but I want to start with testing using XUnit to show the difference later.

We’ll start with a few test cases around logic for determining that points are adjacent to one another.

In the `Tests.fs` file, change it to the following code:

{% gist https://gist.github.com/IntegerMan/93772e7e20fa5fa3afac0c78a9546e37 %}

Here we’re defining an XUnit _theory_, which is a parameterized test case that is invoked once per set of data. In this case we’re using _InlineData_ to pass in positions as well as if we expect these points to be adjacent or not adjacent. These pieces of data come in via the x1, y1, x2, y2, and expectedAdjacent parameters.

We then follow an arrange / act / assert style pattern where we set up two points and check `isAdjacentTo`, the function under test.

Finally, we do an assert comparing the calculated value to the expected value.

Running this test can be accomplished using the Visual Studio Test Explorer view as pictured below.

[![F# Unit Testing in Visual Studio 2019](https://i2.wp.com/res.cloudinary.com/practicaldev/image/fetch/s--ZLXHp8E6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/cnie8fys8rhydgvndn6i.png?w=770&ssl=1)](https://i2.wp.com/res.cloudinary.com/practicaldev/image/fetch/s--ZLXHp8E6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/cnie8fys8rhydgvndn6i.png?ssl=1)

## Improving Tests with FsUnit

Next, let’s look at integrating [FsUnit](http://fsprojects.github.io/FsUnit/) into the test. This is an open source library to improve the quality of F# unit tests.

Right click on the dependencies node for your test project in Solution Explorer and select _Manage NuGet Packages…_.

Click on Browse, then search for FsUnit

[![Adding a Reference](https://i0.wp.com/res.cloudinary.com/practicaldev/image/fetch/s--W-rSKLEx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/3ldfwha2oasgtopt4qd4.png?w=770&ssl=1)](https://i0.wp.com/res.cloudinary.com/practicaldev/image/fetch/s--W-rSKLEx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/3ldfwha2oasgtopt4qd4.png?ssl=1)

Select `FsUnit` and click Install. You will need to accept the external party license to install some packages.

Next, add `open FsUnit` to your list of imports in your test file.

You can now clean up your test to the following syntax:

{% gist https://gist.github.com/IntegerMan/9aa3f09d59bbb8d06845fedb518af7bb %}

Here we remove the `Assert` syntax which was difficult read even in C# and VB .NET. We’ll stick with this more functional assertion syntax going forward.

## Testing Rabbit Movement

Now that we’ve gotten a handle on testing in F#, let’s add some tests around the core rules of simulation logic for the rabbit entity. Those rules are somewhat simple:

- The rabbit cannot move off the edge of the map
- The rabbit will move to a random adjacent tile that doesn’t have another actor on it

Simulating this will give us a foundation for writing tests for the more complicated actors.

Let’s take a look:

{% gist https://gist.github.com/IntegerMan/54bbe1ad7b5450d712df5d02144776f6 %}

Here we define a simple function to serve as a “random number generator” stand-in function during the test run. This is faster than relying on `Random` and also consistent.

Next we define `buildTestState` to define the starting point of the simulation for most tests.

Both of these functions are defined outside of an individual test as we want to reuse them in multiple tests.

Next we set up our state and grab the rabbit’s initial position. Once `simulateActors` is called, we check that the rabbit’s position is **not** its initial position (yes, I know we’re not testing for randomness, but we are checking for movement).

The `should not' (equal originalPos)` syntax is a bit odd. No, the apostrophe is not a typo, that’s needed to use the not function, as are the parentheses. This appears to be a quirk around how F# unit testing works with FsUnit’s functions.

### Implementing Rabbit Motion

The code to move the rabbit is fairly simple:

{% gist https://gist.github.com/IntegerMan/e16fb0335df491007ad064fb9142ea7b %}

What we’re doing here is defining a `moveRandomly` function that can move any actor in any direction.

It first checks `getCandidates` to get a sequence of adjacent cells that the actor is allowed to move into. It then sorts each possibility by a random number (which in our tests will always be 42), and finally it grabs the first (or head) element of that sequence.

Once the destination is picked, `moveActor` is invoked to carry out the move and build out and return the new state:

![MoveActor defined](https://i0.wp.com/killalldefects.com/wp-content/uploads/2019/10/image-7.png?w=770&ssl=1)<figcaption>Nested functions collapsed for now for readability</figcaption>

Let’s ignore the 3 collapsed nested functions for now and focus on the tail end of this function. We match on the target to see if something is present. If not, go ahead and perform the move, which will change the actor’s position:

{% gist https://gist.github.com/IntegerMan/9e051c0e2ebe8a94d9e04c6984baa2d7 %}

_Note: I’m not wild about this code in general. Actor management is likely the area I’d like to improve the most of the code thus far._

If another actor _was_ detected in the position the actor was about to move into, the simulation checks to make sure that the executing actor is allowed to enter the other actor’s tile. For example, a dog is not allowed to enter a tree due to its limited climbing ability.

In cases where there is an actor and the movement is allowed, we match on which actor is actually moving and we invoke one of two special commands, which we’ll explore later on as we discuss the dog and the squirrel.

## Testing Dog Movement

Okay, so now that we looked at how the rabbit works and how movement works in general, let’s look a bit more closely at the dog:

{% gist https://gist.github.com/IntegerMan/d0b997eb50d622277177622fed282926 %}

The dog’s logic is to remain stationary and eat the rabbit or the squirrel if they come near it.

For this test we’re going to manipulate the test’s starting state to have the squirrel already adjacent to the dog. We’re then going to call `simulateActors` and ensure that the resulting state has the following traits:

- The dog is in the squirrel’s former tile
- The squirrel is no more
- The game is now lost

We accomplish these things in the `handleDogMove` function which was mentioned earlier but not expanded. Let’s look at this function now:

{% gist https://gist.github.com/IntegerMan/103c58cda354f3e32eb09854b5b425ef %}

Again we see a bit of special-casing based on which actor is being eaten. This repetition or near repetition is a definite code smell and one I’ll look to correct as the series goes on.

Essentially what we’re doing here is modifying the game state to have a new snapshot of the world in which the dog has changed positions and the squirrel or rabbit is dead (depending on which it ate). If the squirrel died, the game should also now be over.

That simple logic is all we need to have a dog that sits and waits for something to come near it to be eaten.

## Implementing a Timer

While we’re on the subject of things that can kill the squirrel, let’s talk about hunger.

Hunger is a concept we’re implementing to enforce a time limit on the simulation to either get the acorn and win or meet the dog and lose. This is important since a large number of our randomized squirrel genomes won’t be effective and we don’t want those trying too long before giving up.

The unit tests around hunger are as follows:

{% gist https://gist.github.com/IntegerMan/e3155a63596cfd18bbf1cb619f4202db %}

Both of these are similar, in nature, and both rely on the turn processing logic baked into the simulator:

{% gist https://gist.github.com/IntegerMan/d5170b10329dfd62b55aad8014101e47 %}

## Testing Squirrel Movement

The squirrel is where the remaining bits of core logic are going to exist.

Recall from last article that the simulation ends in a win when the squirrel enters the acorn tile to get the acorn, then goes to the tree tile. Let’s write some tests around this behavior.

### F# Unit Testing and Squirrel Brains

{% gist https://gist.github.com/IntegerMan/cab33f6534660112b3b38a0ee122009b %}

Here we test that a squirrel without an acorn (indicated by `ActorKind = Squirrel false`), if positioned to the right of an acorn in a test map, should get the acorn if it moves left.

We evaluate success by the boolean `hasAcorn` field on the `Squirrel` `ActorKind` and expect it to be true, indicating the squirrel has the acorn.

This is accomplished via the `handleSquirrelMove` function we discussed earlier. I’ll show that more in a moment, but first lets look at the return to tree with the acorn test:

{% gist https://gist.github.com/IntegerMan/15b6874c8dd2fe0e66e7c44f8d85a134 %}

This is a very similar test to the last one, only we define the Squirrel as a `Squirrel true` to indicate that it has the acorn. We position it next to the tree and we verify after the move that the squirrel is now in the tree and the game is set to won.

### Squirrel Internals

So how does the squirrel logic work?

{% gist https://gist.github.com/IntegerMan/c1d3e75abb8368a0e32a177d0fad7eb4 %}

It turns out the squirrel works in a fairly simple way. When a squirrel is bumping into another actor, we need to handle one of two special cases differently:

1. The Squirrel doesn’t have the acorn and is entering the acorn tile. In this case the simulation replaces the squirrel with one that has the acorn and sets the acorn to `IsActive = false` so it doesn’t render / trigger events.
2. The squirrel has the acorn and is entering the tree. In this case, the simulation ends with the squirrel in the tree.

That’s it! It’s effectively a reducer, albeit one that’s a bit messy to read. I’d like to find some easier syntax for this sort of operation as I’m not wild about the overall look of this function, but it does the job.

## Bringing Color to the World

Now that we have tests around all of the game rules, lets polish up the console app by adding colors and the timer display.

Let’s start by updating the `displayWorld` function:

{% gist https://gist.github.com/IntegerMan/20b2bb78ebf0507c510a01985f804391 %}

Here starting at line 14, we manipulate `Console.ForegroundColor` based on the type of actor we’re rendering. This is somewhat brittle in cases where we change what character an actor generates, but I didn’t want to put this logic in the shared class library.

Note that all I have to do here is set the `ForegroundColor` and .NET takes care of everything else for me. If I wanted, I could also set `BackgroundColor` here. Both run off of a restricted 16 color palette that you cannot customize, so your rendering options are limited.

Finally, at the end, I set the `ConsoleColor` back to white so we don’t accidentally render UI prompts in an unexpected color.

## End Result and Next Steps

As of the end of this article, we’ve used F# Unit Testing to build a fully-functional squirrel game. It’s incredibly easy and boring, but it’s not really intended for a human to play it.

![The F# console app in action](https://i0.wp.com/killalldefects.com/wp-content/uploads/2019/10/2019-10-22_0-19-25.gif?w=770&ssl=1)<figcaption>Illustrating the various outcomes of the game. Note the dog eating the rabbit early on.</figcaption>

Next time we’ll move from the console world to the desktop. From there, we’ll be able to get into the depths and details on genetic algorithms as we explore how to roll our own artificial intelligence to evolve the perfect squirrel.

Stay tuned.

The post [F# Unit Testing – Refining the Squirrel Simulation](https://killalldefects.com/2019/10/22/f-unit-testing-refining-the-squirrel-simulation/) appeared first on [Kill All Defects](https://killalldefects.com).