Files
functional-programming-snake/fsharp/tests/M01Util.fs
T

35 lines
1.6 KiB
FSharp

module Snake.Tests.M01Util
open Expecto
open Snake
open Snake.Model
// The JavaScript m01 also has `pipe` and `compose`; in F# they are built in as `|>` and `>>`, so there is nothing to test.
let private sameParity a b = a % 2 = b % 2
[<Tests>]
let tests =
testList "M01 Util" [
testCase "range 0 is empty" (fun () -> Expect.equal (Util.range 0) [] "range 0")
testCase "range 3 counts from zero up to n-1" (fun () -> Expect.equal (Util.range 3) [ 0; 1; 2 ] "range 3")
testCase "dropLast of an empty list is empty" (fun () ->
Expect.equal (Util.dropLast ([]: int list)) [] "dropLast []")
testCase "dropLast removes only the last element" (fun () ->
Expect.equal (Util.dropLast [ 1; 2; 3 ]) [ 1; 2 ] "dropLast [1;2;3]")
testCase "dropLast leaves its input untouched" (fun () ->
let xs = [ 1; 2; 3 ]
Expect.equal (Util.dropLast xs) [ 1; 2 ] "result"
Expect.equal xs [ 1; 2; 3 ] "input")
testCase "contains finds an element using the supplied equality" (fun () ->
Expect.isTrue (Util.contains [ 1; 2; 3 ] 5 sameParity) "5 has the parity of 1")
testCase "contains is false when no element is equal" (fun () ->
Expect.isFalse (Util.contains [ 2; 4 ] 3 sameParity) "no odd element")
testCase "contains is false for an empty list" (fun () ->
Expect.isFalse (Util.contains [] 1 (=)) "empty list")
testCase "contains compares structurally when given a structural equality" (fun () ->
Expect.isTrue (Util.contains [ { X = 1; Y = 2 } ] { X = 1; Y = 2 } (=)) "same point")
]