Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@heemskerkerik
Created May 2, 2019 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heemskerkerik/dfa1ca96737354c29282bc881a9d19e8 to your computer and use it in GitHub Desktop.
Save heemskerkerik/dfa1ca96737354c29282bc881a9d19e8 to your computer and use it in GitHub Desktop.
F# BDD tests using TickSpec and FsUnit
Feature: Calculator
Scenario: Adding numbers
Given there is a calculator
And the user entered 2 in the calculator
And the user selected + as the operation
And the user entered 2 in the calculator
When the user presses the = button
Then the calculator shows 4 as the result
module Calculator
type Operation = Add | Subtract | Multiply | Divide
type Calculator = {
Operand: decimal;
Operation: Operation;
Subtotal: decimal;
Result: decimal;
}
let create =
{ Operand = 0.0m; Operation = Add; Subtotal = 0.0m; Result = 0.0m }
let enter (c: Calculator) (v: decimal) =
{ c with Operand = v }
let selectOperation (c: Calculator) (op: Operation) =
{ c with Operand = 0m; Operation = op; Subtotal = c.Operand }
let calculate (c: Calculator) =
let result = match c.Operation with
| Add -> c.Subtotal + c.Operand
| Subtract -> c.Subtotal - c.Operand
| Multiply -> c.Subtotal * c.Operand
| Divide -> c.Subtotal / c.Operand
{ c with Operand = 0m; Subtotal = 0m; Result = result }
module CalculatorSteps
open TickSpec
open Calculator
open FsUnit
let [<Given>] ``there is a calculator`` () =
Calculator.create
let [<Given>] ``the user entered (.*) in the calculator`` (n: decimal) (c: Calculator) =
Calculator.enter c n
let [<Given>] ``the user selected (.) as the operation`` (o: char) (c: Calculator) =
let operation = match o with
| '+' -> Add
| '-' -> Subtract
| _ -> failwith "Unknown operation"
Calculator.selectOperation c operation
let [<When>] ``the user presses the = button`` (c: Calculator) =
Calculator.calculate c
let [<Then>] ``the calculator shows (.+) as the result`` (n: decimal) (c: Calculator) =
c.Result |> should equal n
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FSharp.Data" Version="3.1.1" />
<PackageReference Include="FsUnit" Version="3.4.0" />
<PackageReference Include="TickSpec" Version="2.0.0-rc1" />
</ItemGroup>
<ItemGroup>
<Compile Include="Calculator.fs" />
<Compile Include="CalculatorSteps.fs" />
<EmbeddedResource Include="Calculator.feature" />
<Compile Include="Program.fs" />
</ItemGroup>
</Project>
module Program
open System.Reflection
open TickSpec
do
let ass = Assembly.GetExecutingAssembly()
let definitions = StepDefinitions(ass)
[@"FSharpAcceptanceTest.Calculator.feature"]
|> Seq.iter (fun source ->
let s = ass.GetManifestResourceStream(source)
definitions.Execute(source,s)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment