Skip to content

Instantly share code, notes, and snippets.

@lefthandedgoat
Created June 27, 2012 01:30
Show Gist options
  • Save lefthandedgoat/3000685 to your computer and use it in GitHub Desktop.
Save lefthandedgoat/3000685 to your computer and use it in GitHub Desktop.
calendar kata fs
module runner
let mutable tests = []
let test f = tests <- List.append tests [f]
let xtest f = ()
let mutable before = fun () -> ()
let run _ = List.map (fun f -> (before ()
f ())) tests
let (==) value1 value2 = System.Console.WriteLine("{0} expected: {1} got: {2}", (value1 = value2), value2, value1)
let describe description = System.Console.WriteLine(description.ToString())
test (fun _ -> ())
module app
open System
type Event = { summary : string; date : DateTime }
type pattern =
| Monday
| Tuesday
| Thirtieth
| EveryOtherDay
| Christmas
let mutable (events : Event list) = []
let add event =
events <- events @ [event]
let addRecurring (startDate : DateTime) (endDate : DateTime) pattern summary =
let filter f =
let daysBetween = (endDate - startDate).Days
let daysUntilStart = (startDate - DateTime.Today).Days
[for d in daysUntilStart .. daysBetween do yield {summary = summary; date = DateTime.Today.AddDays(Convert.ToDouble(d))}]
|> List.filter (fun date -> (f date) = true)
match pattern with
| Monday -> events <- events @ (filter (fun event -> event.date.DayOfWeek = DayOfWeek.Monday))
| Tuesday -> events <- events @ (filter (fun event -> event.date.DayOfWeek = DayOfWeek.Tuesday))
| Thirtieth -> events <- events @ (filter (fun event -> event.date.Day = 30))
| EveryOtherDay -> events <- events @ (filter (fun event -> (event.date.Date - startDate).Days % 2 = 0 ))
| Christmas -> events <- events @ (filter (fun event -> event.date.Month = 12 && event.date.Day = 25 ))
()
let scheduled (date : DateTime) =
events |> List.exists (fun event -> event.date.Date = date.Date)
let listScheduled (date : DateTime) =
events |> List.filter (fun event -> event.date.Date = date.Date)
let searchScheduled term =
events |> List.filter (fun event -> event.summary.Contains(term))
//tests
let today = DateTime.Today
let tomorrow = (DateTime.Today.AddDays(1.0))
let nextTuesday = (DateTime.Today.AddDays(7.0))
let endOfCurrentYear = DateTime.Parse("12/31/2012")
let oneYearFromNow = (DateTime.Today.AddDays(365.0))
before <- (fun _ -> events <- [])
test (fun _ ->
describe "after adding today we are scheduled for today"
add {summary = "swimming"; date = today}
(scheduled today) == true
)
test (fun _ ->
describe "after adding tomorrow we are not scheduled for today"
add {summary = "fishing"; date = tomorrow }
(scheduled today) == false
)
test (fun _ ->
describe "when adding a recurring schedule for the next year then all scheduled events are on tuesday within the current year but after today"
addRecurring tomorrow endOfCurrentYear Tuesday "skiing"
(scheduled today) == false
(scheduled tomorrow) == false
(scheduled nextTuesday) == true
(scheduled (endOfCurrentYear.AddDays(1.0))) == false
(scheduled (DateTime.Parse("12/25/2012"))) == true
)
test (fun _ ->
describe "when scheduling the 30th of each month february is not scheduled"
addRecurring tomorrow oneYearFromNow Thirtieth "hack club"
List.length events == 11
)
test (fun _ ->
describe "when scheduling multiple events for a day then can find list of all events for the date"
let firstSummary = "wake up"
let secondSummary = "make coffee"
add {summary = firstSummary; date = today}
add {summary = secondSummary; date = today}
List.length (listScheduled today) == 2
List.length (listScheduled tomorrow) == 0
)
test (fun _ ->
describe "searching by summary description works"
let firstSummary = "wake up"
let secondSummary = "make coffee"
add {summary = firstSummary; date = today}
add {summary = secondSummary; date = today}
List.length (searchScheduled "wake") == 1
List.length (searchScheduled "sleep") == 0
)
test (fun _ ->
describe "scheduling every other day"
addRecurring today (today.AddDays(7.0)) EveryOtherDay "explode"
List.length events == 4
(scheduled tomorrow) == false
(scheduled today) == true
(scheduled (today.AddDays(2.0))) == true
(scheduled (today.AddDays(4.0))) == true
(scheduled (today.AddDays(6.0))) == true
)
test (fun _ ->
describe "christmas"
addRecurring today (today.AddDays(730.0)) Christmas "Christmas"
List.length (events) == 2
(scheduled (DateTime.Parse("12/24/2012"))) == false
(scheduled (DateTime.Parse("12/25/2012"))) == true
(scheduled (DateTime.Parse("12/25/2012"))) == true
)
run ()
System.Console.ReadKey()
after adding today we are scheduled for today
True expected: True got: True
after adding tomorrow we are not scheduled for today
True expected: False got: False
when adding a recurring schedule for the next year then all scheduled events are
on tuesday within the current year but after today
True expected: False got: False
True expected: False got: False
True expected: True got: True
True expected: False got: False
True expected: True got: True
when scheduling the 30th of each month february is not scheduled
True expected: 11 got: 11
when scheduling multiple events for a day then can find list of all events for t
he date
True expected: 2 got: 2
True expected: 0 got: 0
searching by summary description works
True expected: 1 got: 1
True expected: 0 got: 0
scheduling every other day
True expected: 4 got: 4
True expected: False got: False
True expected: True got: True
True expected: True got: True
True expected: True got: True
True expected: True got: True
christmas
True expected: 2 got: 2
True expected: False got: False
True expected: True got: True
True expected: True got: True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment