Created
November 27, 2016 19:14
-
-
Save zkessin/d988285d9d1e2ff8773968fd6b5323c5 to your computer and use it in GitHub Desktop.
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 DecoderTests exposing (..) | |
import Test exposing (..) | |
import Expect | |
import Fuzz | |
import String | |
import Json.Decode exposing (..) | |
import Json.Decode.Extra exposing ((|:)) | |
import Result | |
type Direction = North | South | East | West | |
stringToDirection : String -> Decoder Direction | |
stringToDirection str = | |
case str of | |
"North" -> succeed North | |
"South" -> succeed South | |
"East" -> succeed East | |
"West" -> succeed West | |
_ -> fail ("Value " ++ str ++ "Is not a direction") | |
decodeDirection : Decoder Direction | |
decodeDirection = string|> andThen stringToDirection | |
all : Test | |
all = | |
describe "Decode Direction" | |
[ | |
test "North" <| | |
\() -> | |
Expect.equal (Result.Ok North) <| decodeString decodeDirection "\"North\"" | |
, test "South" <| | |
\() -> | |
Expect.equal (Result.Ok South) <| decodeString decodeDirection "\"South\"" | |
, test "East" <| | |
\() -> | |
Expect.equal (Result.Ok East) <| decodeString decodeDirection "\"East\"" | |
, test "West" <| | |
\() -> | |
Expect.equal (Result.Ok West) <| decodeString decodeDirection "\"West\"" | |
, test "No Direction" <| | |
\() -> | |
Expect.equal Nothing <| Result.toMaybe<| decodeString decodeDirection "\"xxxx\"" | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment