Created
May 2, 2019 08:18
-
-
Save heemskerkerik/dfa1ca96737354c29282bc881a9d19e8 to your computer and use it in GitHub Desktop.
F# BDD tests using TickSpec and FsUnit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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