Skip to content

Commit

Permalink
Added some List tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ncave committed Jul 20, 2020
1 parent 83b0d40 commit 1fd0718
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/fable-library/List.fs
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,8 @@ let slice (lower: int option) (upper: int option) (xs: 'T list) =
else newList <| xs.Values.GetRange(xs.Count - 1 - upper, upper - lower + 1)

let splitAt i (xs: 'T list) =
if i < 0 then invalidArg "i" LanguagePrimitives.ErrorStrings.InputMustBeNonNegativeString
if i > xs.Count then invalidArg "i" "The input sequence has an insufficient number of elements."
if i < 0 then invalidArg "index" LanguagePrimitives.ErrorStrings.InputMustBeNonNegativeString
if i > xs.Count then invalidArg "index" "The input sequence has an insufficient number of elements."
take i xs, skip i xs

let distinctBy (projection: 'T -> 'Key) (xs: 'T list) ([<Inject>] eq: IEqualityComparer<'Key>) =
Expand All @@ -447,8 +447,8 @@ let distinct (xs: 'T list) ([<Inject>] eq: IEqualityComparer<'T>) =

let exactlyOne (xs: 'T list) =
match xs.Count with
| 0 -> invalidArg "list" LanguagePrimitives.ErrorStrings.InputSequenceEmptyString
| 1 -> head xs
| 0 -> invalidArg "list" LanguagePrimitives.ErrorStrings.InputSequenceEmptyString
| _ -> invalidArg "list" "Input list too long"

let groupBy (projection: 'T -> 'Key) (xs: 'T list)([<Inject>] eq: IEqualityComparer<'Key>): ('Key * 'T list) list =
Expand Down
19 changes: 16 additions & 3 deletions tests/Main/ListTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,26 @@ module List =

let tests =
testList "Lists" [
// TODO: Empty lists may be represented as null, make sure they don't conflict with None
testCase "Some [] works" <| fun () ->
let xs: int list option = Some []
let ys: int list option = None
Option.isSome xs |> equal true
Option.isNone ys |> equal true

testCase "List equality works" <| fun () ->
let xs = [1;2;3]
let ys = [1;2;3]
let zs = [1;4;3]
xs = ys |> equal true
xs = zs |> equal false

testCase "List comparison works" <| fun () ->
let xs = [1;2;3]
let ys = [1;2;3]
let zs = [1;4;3]
xs < ys |> equal false
xs < zs |> equal true

testCase "Pattern matching with lists works" <| fun () ->
match [] with [] -> true | _ -> false
|> equal true
Expand Down Expand Up @@ -302,9 +315,9 @@ let tests =
|> List.sum |> equal 9

testCase "List.rev works" <| fun () ->
let xs = [1; 2]
let xs = [1; 2; 3]
let ys = xs |> List.rev
equal 2 ys.Head
equal 3 ys.Head

testCase "List.scan works" <| fun () ->
let xs = [1; 2; 3; 4]
Expand Down

0 comments on commit 1fd0718

Please sign in to comment.