Skip to content

Instantly share code, notes, and snippets.

@loosechainsaw
Created August 26, 2014 10:59
Show Gist options
  • Save loosechainsaw/f1a82a540e0ef25f2353 to your computer and use it in GitHub Desktop.
Save loosechainsaw/f1a82a540e0ef25f2353 to your computer and use it in GitHub Desktop.
Perth Code Dojo 2
namespace PerthCodeDojo
open System
open NUnit.Framework
[<AutoOpen>]
module Internals =
type Quantity = int
type Product =
| Apples of Quantity
| Cherries of Quantity * Quantity
| Mangos of Quantity
let private calculatePrice item =
match item with
| Apples(v) ->
let q = v / 3
float ( v - q) * 0.5
| Cherries(v,z) ->
let q = v / 3
let good = (float v * 5.0 ) - (7.5 * float q)
let bad = (float z * 0.8 * 5.0)
good + bad
| Mangos(v) -> float v * 3.0
let checkout (l: Product list) = l |> List.fold (fun a b -> calculatePrice(b) + a) 0.0
[<TestFixture>]
type ScenarioOneTests() =
[<Test>]
member x.``When adding cherries to the cart the sum hould be based on the quantity``() =
let cart = Cherries(2,0) :: []
let cost = checkout(cart)
Assert.AreEqual( 10, cost)
[<Test>]
member x.``When adding mangoes to the cart the sum hould be based on the quantity``() =
let cart = Mangos(2) :: []
let cost = checkout(cart)
Assert.AreEqual( 6, cost)
[<Test>]
member x.``When adding 2 apples to the cart the sum hould be based on the quantity``() =
let cart = Apples(2) :: []
let cost = checkout(cart)
Assert.AreEqual( 1, cost)
[<Test>]
member x.``When adding 3 apples to the cart the sum hould be the same as 2 items``() =
let cart = Apples(3) :: []
let cost = checkout(cart)
Assert.AreEqual( 1, cost)
[<Test>]
member x.``When adding 4 apples to the cart the sum hould be the same as 2 items``() =
let cart = Apples(4) :: []
let cost = checkout(cart)
Assert.AreEqual( 1.5, cost)
[<Test>]
member x.``When adding 6 apples to the cart the sum hould be the same as 4 items``() =
let cart = Apples(6) :: []
let cost = checkout(cart)
Assert.AreEqual( 2, cost)
[<Test>]
member x.``When adding 3 cherries to the cart the sum hould be discounted to seven fifty``() =
let cart = Cherries(3,0) :: []
let cost = checkout(cart)
Assert.AreEqual( 7.5, cost)
[<Test>]
member x.``When adding 3 bad cherries to the cart the sum hould be discounted to twelve``() =
let cart = Cherries(0,3) :: []
let cost = checkout(cart)
let intcost = int cost
Assert.AreEqual( 12, intcost)
[<Test>]
member x.``When adding 3 bad cherries and 3 good cherries to the cart the sum hould be discounted to nineteen``() =
let cart = Cherries(3,3) :: []
let cost = checkout(cart)
Assert.AreEqual( 19.5, cost)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment