Skip to content

Instantly share code, notes, and snippets.

@zkessin
Created November 27, 2016 19:14
Show Gist options
  • Save zkessin/d988285d9d1e2ff8773968fd6b5323c5 to your computer and use it in GitHub Desktop.
Save zkessin/d988285d9d1e2ff8773968fd6b5323c5 to your computer and use it in GitHub Desktop.
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