Add tests for json decoding.

This commit is contained in:
Richard Feldman
2016-03-05 06:27:12 -08:00
parent bcff9a8d51
commit 8fb05cab57
2 changed files with 37 additions and 11 deletions

View File

@@ -55,10 +55,11 @@ responseDecoder =
searchResultDecoder : Decoder SearchResult
searchResultDecoder =
Json.Decode.object2
Json.Decode.object3
SearchResult
("id" := Json.Decode.int)
("name" := Json.Decode.string)
("stargazers_count" := Json.Decode.int)
type alias Model =
@@ -70,6 +71,7 @@ type alias Model =
type alias SearchResult =
{ id : ResultId
, name : String
, stars : Int
}
@@ -107,7 +109,11 @@ defaultValue str =
viewSearchResult : SearchResult -> Html
viewSearchResult result =
div [] [ text result.name ]
div
[]
[ div [ class "star-count" ] [ text (toString result.stars) ]
, div [] [ text result.name ]
]
type Action

View File

@@ -1,15 +1,35 @@
module Tests where
module Tests (..) where
import ElmTest exposing (..)
import String
import ElmHub exposing (responseDecoder)
import Json.Decode exposing (decodeString)
all : Test
all =
suite "A Test Suite"
[
test "Addition" (assertEqual (3 + 7) 10),
test "String.left" (assertEqual "a" (String.left 1 "abcdefg")),
test "This test should fail" (assert False)
suite
"Decoding responses from GitHub"
[ test "they can decode empty responses"
<| let
emptyResponse =
"""{ "items": [] }"""
in
assertEqual
(decodeString responseDecoder emptyResponse)
(Ok [])
, test "they can decode responses with results in them"
<| let
response =
"""{ "items": [
{ "id": 5, "name": "foo", "stargazers_count": 42 },
{ "id": 3, "name": "bar", "stargazers_count": 77 }
] }"""
in
assertEqual
(decodeString responseDecoder response)
(Ok
[ { id = 5, name = "foo", stars = 42 }
, { id = 3, name = "bar", stars = 77 }
]
)
]