Skip to content

Instantly share code, notes, and snippets.

@vpolozov
Last active August 29, 2015 14:08
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 vpolozov/e3e8fcdd575033be1bd8 to your computer and use it in GitHub Desktop.
Save vpolozov/e3e8fcdd575033be1bd8 to your computer and use it in GitHub Desktop.
F#
module Customer
open Shop
type ICalendar =
abstract member DayOfWeek : System.DayOfWeek
type Calendar() =
interface ICalendar with
member x.DayOfWeek = System.DateTime.Now.DayOfWeek
type Customer (calendar : ICalendar) =
let mutable goods = []
let mutable isDrunk = false
member x.GoShopping (shop : IShop) =
if shop.CanSell Whiskey then
goods <- shop.Sell [Milk; Whiskey; Whiskey]
else
goods <- shop.Sell [Milk; Vodka; Beer "dark, Vas'ka"]
member x.GetDrunk () =
if calendar.DayOfWeek = System.DayOfWeek.Friday then
let (nonAlco, alco) = List.partition ((=) Good.Milk) goods
isDrunk <- isDrunk || alco <> []
goods <- nonAlco
member x.IsDrunk = isDrunk
module Shop
type Good =
| Vodka
| Beer of string
| Whiskey
| Wine
| Milk
type IShop =
abstract member CanSell : Good -> bool
abstract member Sell : Good list -> Good list
type AlkoShop() =
let rnd = new System.Random()
interface IShop with
member x.CanSell (good : Good) =
let now = System.DateTime.Now
let isAlcoAllowed = now.Hour > 9 && now.Hour < 22
isAlcoAllowed || good = Milk || rnd.Next(10) < 1
member x.Sell (goods:Good list) =
List.filter (x :> IShop).CanSell goods
module ShoppingTests.Tests
open NUnit.Framework
open FsUnit
open Shop
type EmptyShop() =
interface IShop with
member x.CanSell (good : Good) = false
member x.Sell (goods:Good list) = []
type AnytimeFullShop() =
interface IShop with
member x.CanSell (good : Good) = true
member x.Sell (goods:Good list) = goods
type TestCalendar (day : System.DayOfWeek) =
interface Customer.ICalendar with
member x.DayOfWeek = day
let calendar day = new TestCalendar(day) :> Customer.ICalendar
let calendarFriday = calendar System.DayOfWeek.Friday
[<Test>]
let test1 () =
let customer = new Customer.Customer(calendarFriday)
customer.IsDrunk |> should be False
[<Test>]
let test2 () =
let customer = new Customer.Customer(calendarFriday)
let emptyShop = new EmptyShop()
customer.GoShopping emptyShop
customer.GetDrunk ()
customer.IsDrunk |> should be False
[<Test>]
let test3 () =
let customer = new Customer.Customer(calendarFriday)
let allInclusiveShop = new AnytimeFullShop()
customer.GoShopping allInclusiveShop
customer.GetDrunk ()
customer.IsDrunk |> should be True
[<Test>]
let ``test not drunk on Thursday`` () =
let customer = new Customer.Customer(calendar System.DayOfWeek.Thursday)
let allInclusiveShop = new AnytimeFullShop()
customer.GoShopping allInclusiveShop
customer.GetDrunk ()
customer.IsDrunk |> should be False
let shouldDrunk (day : System.DayOfWeek) =
let customer = new Customer.Customer(calendar day)
let allInclusiveShop = new AnytimeFullShop()
customer.GoShopping allInclusiveShop
customer.GetDrunk ()
customer.IsDrunk || day = System.DayOfWeek.Friday
[<Test>]
let testQuick() =
FsCheck.Check.Quick shouldDrunk
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment