Last active
September 21, 2017 10:51
-
-
Save hipertracker/12cf3bdb28156cf6a6d72557b92fa2e6 to your computer and use it in GitHub Desktop.
Elm unit testing
This file contains hidden or 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 ShiftYears exposing (Msg(ShiftLeft, ShiftRight), shiftYears) | |
type alias Year = | |
String | |
noYear : Year | |
noYear = | |
"" | |
type Msg | |
= ShiftLeft | |
| ShiftRight | |
shiftYears : Msg -> List Year -> List Year -> List Year | |
shiftYears msg years allYears = | |
let | |
yearsLength = | |
List.length years | |
allYearsIndexed = | |
List.indexedMap (,) allYears | |
firstVisibleYear = | |
case msg of | |
ShiftRight -> | |
Maybe.withDefault noYear (years |> List.drop 1 |> List.head) | |
ShiftLeft -> | |
let | |
firstYear = | |
Maybe.withDefault noYear (List.head years) | |
in | |
allYears | |
|> List.filter (\year -> year > firstYear) | |
|> List.reverse | |
|> List.take 1 | |
|> List.head | |
|> Maybe.withDefault noYear | |
newYears = | |
allYears | |
|> List.filter (\year -> year <= firstVisibleYear) | |
|> List.take yearsLength | |
newYearsLength = | |
List.length newYears | |
in | |
if newYearsLength < yearsLength then | |
years | |
else | |
newYears |
This file contains hidden or 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 Tests exposing (..) | |
import Expect | |
import ShiftYears exposing (Msg(ShiftLeft, ShiftRight), shiftYears) | |
import Test exposing (..) | |
shiftYearsTest : Test | |
shiftYearsTest = | |
let | |
allYears = | |
[ "2017", "2016", "2015", "2014", "2013", "2012", "2011" ] | |
in | |
describe "visibleYears" | |
[ test "shift right" <| | |
\_ -> | |
Expect.equal [ "2015", "2014", "2013", "2012" ] (shiftYears ShiftRight [ "2016", "2015", "2014", "2013" ] allYears) | |
, test "ignore shift right if already shifter to the right side" <| | |
\_ -> | |
Expect.equal [ "2014", "2013", "2012", "2011" ] (shiftYears ShiftRight [ "2014", "2013", "2012", "2011" ] allYears) | |
, test "shift left" <| | |
\_ -> | |
Expect.equal [ "2016", "2015", "2014", "2013" ] (shiftYears ShiftLeft [ "2015", "2014", "2013", "2012" ] allYears) | |
, test "ignore shift left ignored if already shifted to the left side" <| | |
\_ -> | |
Expect.equal [ "2017", "2016", "2015", "2014" ] (shiftYears ShiftLeft [ "2017", "2016", "2015", "2014" ] allYears) | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment