Last active
July 5, 2017 05:16
-
-
Save ryan-senn/05e029afe3b8190456ae9516b0cca969 to your computer and use it in GitHub Desktop.
Testing Array Helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Helpers.Array exposing (IndexError (..), getIndex) | |
import Array exposing (Array) | |
type IndexError | |
= NotFound | |
| NotUnique | |
getIndex : comparable -> Array comparable -> Result IndexError Int | |
getIndex item array = | |
let | |
filteredList = | |
array | |
|> Array.toIndexedList | |
|> List.filter (\elem -> Tuple.second elem == item) | |
|> List.map Tuple.first | |
in | |
case filteredList of | |
[] -> | |
Err NotFound | |
x::xs -> | |
case List.length xs of | |
0 -> | |
Ok x | |
_ -> | |
Err NotUnique |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Helpers.ArrayTest exposing (suite) | |
import Test exposing (..) | |
import Expect exposing (Expectation) | |
import Array exposing (Array) | |
import Helpers.Array | |
suite : Test | |
suite = | |
describe "Array Helper Tests" | |
[ describe "getIndex" | |
[ test "It finds the first index" <| | |
\_ -> getFirstIndex testArray | |
, test "It finds the last index" <| | |
\_ -> getLastIndex testArray | |
, test "It finds the third index" <| | |
\_ -> getThirdIndex testArray | |
, test "It fails if missing" <| | |
\_ -> missingIndex testArray | |
, test "It fails if not unique" <| | |
\_ -> notUnique testArray | |
] | |
] | |
testArray : Array String | |
testArray = | |
Array.fromList | |
[ "entry1" | |
, "entry2" | |
, "entry3" | |
, "entry4" | |
, "entry5" | |
, "entry6" | |
, "entry7" | |
, "entry8" | |
] | |
getFirstIndex : Array String -> Expectation | |
getFirstIndex array = | |
Helpers.Array.getIndex "entry1" array | |
|> Expect.equal (Ok 0) | |
getLastIndex : Array String -> Expectation | |
getLastIndex array = | |
Helpers.Array.getIndex "entry8" array | |
|> Expect.equal (Ok 7) | |
getThirdIndex : Array String -> Expectation | |
getThirdIndex array = | |
Helpers.Array.getIndex "entry4" array | |
|> Expect.equal (Ok 3) | |
missingIndex : Array String -> Expectation | |
missingIndex array = | |
Helpers.Array.getIndex "entry9" array | |
|> Expect.equal (Err Helpers.Array.NotFound) | |
notUnique : Array String -> Expectation | |
notUnique array = | |
let | |
notUniqueArray = | |
["entry4"] | |
|> Array.fromList | |
|> Array.append array | |
in | |
Helpers.Array.getIndex "entry4" notUniqueArray | |
|> Expect.equal (Err Helpers.Array.NotUnique) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment