Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Elm 0.19 tests #1

Merged
merged 3 commits into from
Dec 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ under that the Elm source file `Db/Types.elm` will be found.

The decoders we produce require these extra Elm packages installed:

``` sh
elm package install NoRedInk/elm-decode-pipeline
```sh
elm package install NoRedInk/elm-json-decode-pipeline
elm package install krisajenkins/elm-exts
elm-package install justinmimbs/elm-date-extra
elm package install rtfeldman/elm-iso8601-date-strings
```

## Development
Expand Down
4 changes: 3 additions & 1 deletion src/Elm/Decoder.hs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ instance HasDecoderRef ElmPrimitive where
return . parens $
"map2 (,)" <+> parens ("index 0" <+> dx) <+> parens ("index 1" <+> dy)
renderRef EUnit = pure $ parens "succeed ()"
renderRef EDate = pure "Iso8601.decoder"
renderRef EDate = do
require "Iso8601"
pure "Iso8601.decoder"
renderRef EInt = pure "int"
renderRef EBool = pure "bool"
renderRef EChar = pure "char"
Expand Down
4 changes: 3 additions & 1 deletion src/Elm/Encoder.hs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ instance HasEncoder ElmValue where
render _ = error "HasEncoderRef ElmValue: should not happen"

instance HasEncoderRef ElmPrimitive where
renderRef EDate = pure "Iso8601.encode"
renderRef EDate = do
require "Iso8601"
pure "Iso8601.encode"
renderRef EUnit = pure "Json.Encode.null"
renderRef EInt = pure "Json.Encode.int"
renderRef EChar = pure "Json.Encode.char"
Expand Down
5 changes: 3 additions & 2 deletions test/CommentDecoder.elm
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ module CommentDecoder exposing (..)
import CommentType exposing (..)
import Dict
import Exts.Json.Decode exposing (..)
import Iso8601
import Json.Decode exposing (..)
import Json.Decode.Pipeline exposing (..)


decodeComment : Decoder Comment
decodeComment =
decode Comment
Json.Decode.succeed Comment
|> required "postId" int
|> required "text" string
|> required "mainCategories" (map2 (,) (index 0 string) (index 1 string))
|> required "published" bool
|> required "created" decodeDate
|> required "created" Iso8601.decoder
|> required "tags" (map Dict.fromList (list (map2 (,) (index 0 string) (index 1 int))))
5 changes: 3 additions & 2 deletions test/CommentDecoderWithOptions.elm
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ module CommentDecoderWithOptions exposing (..)
import CommentType exposing (..)
import Dict
import Exts.Json.Decode exposing (..)
import Iso8601
import Json.Decode exposing (..)
import Json.Decode.Pipeline exposing (..)


decodeComment : Decoder Comment
decodeComment =
decode Comment
Json.Decode.succeed Comment
|> required "commentPostId" int
|> required "commentText" string
|> required "commentMainCategories" (map2 (,) (index 0 string) (index 1 string))
|> required "commentPublished" bool
|> required "commentCreated" decodeDate
|> required "commentCreated" Iso8601.decoder
|> required "commentTags" (map Dict.fromList (list (map2 (,) (index 0 string) (index 1 int))))
3 changes: 2 additions & 1 deletion test/CommentEncoder.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module CommentEncoder exposing (..)

import CommentType exposing (..)
import Exts.Json.Encode exposing (..)
import Iso8601
import Json.Encode


Expand All @@ -12,6 +13,6 @@ encodeComment x =
, ( "text", Json.Encode.string x.text )
, ( "mainCategories", (Exts.Json.Encode.tuple2 Json.Encode.string Json.Encode.string) x.mainCategories )
, ( "published", Json.Encode.bool x.published )
, ( "created", (Json.Encode.string << toString) x.created )
, ( "created", Iso8601.encode x.created )
, ( "tags", (Exts.Json.Encode.dict Json.Encode.string Json.Encode.int) x.tags )
]
3 changes: 2 additions & 1 deletion test/CommentEncoderWithOptions.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module CommentEncoderWithOptions exposing (..)

import CommentType exposing (..)
import Exts.Json.Encode exposing (..)
import Iso8601
import Json.Encode


Expand All @@ -12,6 +13,6 @@ encodeComment x =
, ( "commentText", Json.Encode.string x.text )
, ( "commentMainCategories", (Exts.Json.Encode.tuple2 Json.Encode.string Json.Encode.string) x.mainCategories )
, ( "commentPublished", Json.Encode.bool x.published )
, ( "commentCreated", (Json.Encode.string << toString) x.created )
, ( "commentCreated", Iso8601.encode x.created )
, ( "commentTags", (Exts.Json.Encode.dict Json.Encode.string Json.Encode.int) x.tags )
]
4 changes: 2 additions & 2 deletions test/CommentType.elm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module CommentType exposing (..)

import Date exposing (Date)
import Time
import Dict exposing (Dict)


Expand All @@ -9,6 +9,6 @@ type alias Comment =
, text : String
, mainCategories : (String, String)
, published : Bool
, created : Date
, created : Time.Posix
, tags : Dict (String) (Int)
}
4 changes: 2 additions & 2 deletions test/CommentTypeWithOptions.elm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module CommentTypeWithOptions exposing (..)

import Date exposing (Date)
import Time
import Dict exposing (Dict)


Expand All @@ -9,6 +9,6 @@ type alias Comment =
, commentText : String
, commentMainCategories : (String, String)
, commentPublished : Bool
, commentCreated : Date
, commentCreated : Time.Posix
, commentTags : Dict (String) (Int)
}
16 changes: 10 additions & 6 deletions test/ExportSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ toElmTypeSpec =
(unlines
[ "module CommentType exposing (..)"
, ""
, "import Date exposing (Date)"
, "import Time"
, "import Dict exposing (Dict)"
, ""
, ""
Expand Down Expand Up @@ -170,7 +170,7 @@ toElmTypeSpec =
(unlines
[ "module CommentTypeWithOptions exposing (..)"
, ""
, "import Date exposing (Date)"
, "import Time"
, "import Dict exposing (Dict)"
, ""
, ""
Expand Down Expand Up @@ -212,6 +212,7 @@ toElmDecoderSpec =
, "import CommentType exposing (..)"
, "import Dict"
, "import Exts.Json.Decode exposing (..)"
, "import Iso8601"
, "import Json.Decode exposing (..)"
, "import Json.Decode.Pipeline exposing (..)"
, ""
Expand Down Expand Up @@ -306,6 +307,7 @@ toElmDecoderSpec =
, "import CommentType exposing (..)"
, "import Dict"
, "import Exts.Json.Decode exposing (..)"
, "import Iso8601"
, "import Json.Decode exposing (..)"
, "import Json.Decode.Pipeline exposing (..)"
, ""
Expand Down Expand Up @@ -367,6 +369,7 @@ toElmEncoderSpec =
, ""
, "import CommentType exposing (..)"
, "import Exts.Json.Encode exposing (..)"
, "import Iso8601"
, "import Json.Encode"
, ""
, ""
Expand Down Expand Up @@ -397,6 +400,7 @@ toElmEncoderSpec =
, ""
, "import CommentType exposing (..)"
, "import Exts.Json.Encode exposing (..)"
, "import Iso8601"
, "import Json.Encode"
, ""
, ""
Expand Down Expand Up @@ -467,7 +471,7 @@ toElmEncoderSpec =
toElmEncoderRef (Proxy :: Proxy Post) `shouldBe` "encodePost"
it "toElmEncoderRef [Comment]" $
toElmEncoderRef (Proxy :: Proxy [Comment]) `shouldBe`
"(Json.Encode.list << List.map encodeComment)"
"(Json.Encode.list encodeComment)"
it "toElmEncoderRef Position" $
toElmEncoderRef (Proxy :: Proxy Position) `shouldBe` "encodePosition"
it "toElmEncoderRef Timing" $
Expand All @@ -481,7 +485,7 @@ toElmEncoderSpec =
"(Maybe.withDefault Json.Encode.null << Maybe.map Json.Encode.string)"
it "toElmEncoderRef [Maybe String]" $
toElmEncoderRef (Proxy :: Proxy [Maybe String]) `shouldBe`
"(Json.Encode.list << List.map (Maybe.withDefault Json.Encode.null << Maybe.map Json.Encode.string))"
"(Json.Encode.list (Maybe.withDefault Json.Encode.null << Maybe.map Json.Encode.string))"
it "toElmEncoderRef (Map String (Maybe String))" $
toElmEncoderRef (Proxy :: Proxy (Map String (Maybe String))) `shouldBe`
"(Exts.Json.Encode.dict Json.Encode.string (Maybe.withDefault Json.Encode.null << Maybe.map Json.Encode.string))"
Expand All @@ -503,10 +507,10 @@ moduleSpecsSpec =
head (declarations mySpec) `shouldBe`
intercalate
"\n"
[ "import Date"
, "import Dict"
[ "import Dict"
, "import Json.Decode exposing (..)"
, "import Json.Decode.Pipeline exposing (..)"
, "import Time"
]

shouldMatchTypeSource
Expand Down
6 changes: 3 additions & 3 deletions test/MonstrosityDecoder.elm
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ decodeMonstrosity =
(\x ->
case x of
"NotSpecial" ->
decode NotSpecial
Json.Decode.succeed NotSpecial

"OkayIGuess" ->
decode OkayIGuess
Json.Decode.succeed OkayIGuess
|> required "contents" decodeMonstrosity

"Ridiculous" ->
decode Ridiculous
Json.Decode.succeed Ridiculous
|> required "contents" (index 0 int)
|> required "contents" (index 1 string)
|> required "contents" (index 2 (list decodeMonstrosity))
Expand Down
4 changes: 2 additions & 2 deletions test/MonstrosityEncoder.elm
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ encodeMonstrosity x =
NotSpecial ->
Json.Encode.object
[ ( "tag", Json.Encode.string "NotSpecial" )
, ( "contents", Json.Encode.list [] )
, ( "contents", Json.Encode.list Json.Encode.bool [] )
]

OkayIGuess y0 ->
Expand All @@ -22,5 +22,5 @@ encodeMonstrosity x =
Ridiculous y0 y1 y2 ->
Json.Encode.object
[ ( "tag", Json.Encode.string "Ridiculous" )
, ( "contents", Json.Encode.list [ Json.Encode.int y0, Json.Encode.string y1, (Json.Encode.list << List.map encodeMonstrosity) y2 ] )
, ( "contents", Json.Encode.list [ Json.Encode.int y0, Json.Encode.string y1, (Json.Encode.list encodeMonstrosity) y2 ] )
]
6 changes: 3 additions & 3 deletions test/PositionDecoder.elm
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ decodePosition =
(\x ->
case x of
"Beginning" ->
decode Beginning
Json.Decode.succeed Beginning

"Middle" ->
decode Middle
Json.Decode.succeed Middle

"End" ->
decode End
Json.Decode.succeed End

_ ->
fail "Constructor not matched"
Expand Down
2 changes: 1 addition & 1 deletion test/PostDecoder.elm
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import PostType exposing (..)

decodePost : Decoder Post
decodePost =
decode Post
Json.Decode.succeed Post
|> required "id" int
|> required "name" string
|> required "age" (nullable float)
Expand Down
2 changes: 1 addition & 1 deletion test/PostDecoderWithOptions.elm
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import PostType exposing (..)

decodePost : Decoder Post
decodePost =
decode Post
Json.Decode.succeed Post
|> required "postId" int
|> required "postName" string
|> required "postAge" (nullable float)
Expand Down
2 changes: 1 addition & 1 deletion test/PostEncoder.elm
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ encodePost x =
[ ( "id", Json.Encode.int x.id )
, ( "name", Json.Encode.string x.name )
, ( "age", (Maybe.withDefault Json.Encode.null << Maybe.map Json.Encode.float) x.age )
, ( "comments", (Json.Encode.list << List.map encodeComment) x.comments )
, ( "comments", (Json.Encode.list encodeComment) x.comments )
, ( "promoted", (Maybe.withDefault Json.Encode.null << Maybe.map encodeComment) x.promoted )
, ( "author", (Maybe.withDefault Json.Encode.null << Maybe.map Json.Encode.string) x.author )
]
2 changes: 1 addition & 1 deletion test/PostEncoderWithOptions.elm
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ encodePost x =
[ ( "postId", Json.Encode.int x.id )
, ( "postName", Json.Encode.string x.name )
, ( "postAge", (Maybe.withDefault Json.Encode.null << Maybe.map Json.Encode.float) x.age )
, ( "postComments", (Json.Encode.list << List.map encodeComment) x.comments )
, ( "postComments", (Json.Encode.list encodeComment) x.comments )
, ( "postPromoted", (Maybe.withDefault Json.Encode.null << Maybe.map encodeComment) x.promoted )
, ( "postAuthor", (Maybe.withDefault Json.Encode.null << Maybe.map Json.Encode.string) x.author )
]
6 changes: 3 additions & 3 deletions test/TimingDecoder.elm
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ decodeTiming =
(\x ->
case x of
"Start" ->
decode Start
Json.Decode.succeed Start

"Continue" ->
decode Continue
Json.Decode.succeed Continue
|> required "contents" float

"Stop" ->
decode Stop
Json.Decode.succeed Stop

_ ->
fail "Constructor not matched"
Expand Down
4 changes: 2 additions & 2 deletions test/TimingEncoder.elm
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ encodeTiming x =
Start ->
Json.Encode.object
[ ( "tag", Json.Encode.string "Start" )
, ( "contents", Json.Encode.list [] )
, ( "contents", Json.Encode.list Json.Encode.bool [] )
]

Continue y0 ->
Expand All @@ -22,5 +22,5 @@ encodeTiming x =
Stop ->
Json.Encode.object
[ ( "tag", Json.Encode.string "Stop" )
, ( "contents", Json.Encode.list [] )
, ( "contents", Json.Encode.list Json.Encode.bool [] )
]
2 changes: 1 addition & 1 deletion test/UselessDecoder.elm
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import UselessType exposing (..)

decodeUseless : Decoder Useless
decodeUseless =
decode Useless
Json.Decode.succeed Useless
(succeed ())