Add tests for json decoding.
This commit is contained in:
@@ -55,10 +55,11 @@ responseDecoder =
|
|||||||
|
|
||||||
searchResultDecoder : Decoder SearchResult
|
searchResultDecoder : Decoder SearchResult
|
||||||
searchResultDecoder =
|
searchResultDecoder =
|
||||||
Json.Decode.object2
|
Json.Decode.object3
|
||||||
SearchResult
|
SearchResult
|
||||||
("id" := Json.Decode.int)
|
("id" := Json.Decode.int)
|
||||||
("name" := Json.Decode.string)
|
("name" := Json.Decode.string)
|
||||||
|
("stargazers_count" := Json.Decode.int)
|
||||||
|
|
||||||
|
|
||||||
type alias Model =
|
type alias Model =
|
||||||
@@ -70,6 +71,7 @@ type alias Model =
|
|||||||
type alias SearchResult =
|
type alias SearchResult =
|
||||||
{ id : ResultId
|
{ id : ResultId
|
||||||
, name : String
|
, name : String
|
||||||
|
, stars : Int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -107,7 +109,11 @@ defaultValue str =
|
|||||||
|
|
||||||
viewSearchResult : SearchResult -> Html
|
viewSearchResult : SearchResult -> Html
|
||||||
viewSearchResult result =
|
viewSearchResult result =
|
||||||
div [] [ text result.name ]
|
div
|
||||||
|
[]
|
||||||
|
[ div [ class "star-count" ] [ text (toString result.stars) ]
|
||||||
|
, div [] [ text result.name ]
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
type Action
|
type Action
|
||||||
|
|||||||
@@ -1,15 +1,35 @@
|
|||||||
module Tests where
|
module Tests (..) where
|
||||||
|
|
||||||
import ElmTest exposing (..)
|
import ElmTest exposing (..)
|
||||||
|
import ElmHub exposing (responseDecoder)
|
||||||
import String
|
import Json.Decode exposing (decodeString)
|
||||||
|
|
||||||
|
|
||||||
all : Test
|
all : Test
|
||||||
all =
|
all =
|
||||||
suite "A Test Suite"
|
suite
|
||||||
[
|
"Decoding responses from GitHub"
|
||||||
test "Addition" (assertEqual (3 + 7) 10),
|
[ test "they can decode empty responses"
|
||||||
test "String.left" (assertEqual "a" (String.left 1 "abcdefg")),
|
<| let
|
||||||
test "This test should fail" (assert False)
|
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 }
|
||||||
|
]
|
||||||
|
)
|
||||||
]
|
]
|
||||||
|
|||||||
Reference in New Issue
Block a user