Elm 测试说模块声明无效,但为什么呢?
Elm tests says that the module declaration is invalid, but why?
我是 Elm 的新手,我正在使用 Elm 0.19 开始我的第一个宠物项目。
我想在我的应用程序中创建 JSON 解码的测试用例。从服务器返回的 JSON 看起来像这样(所有内容都在名为 Frontend.elm
的文件中定义):
[
{
"gameId": "game1",
"player1": "player1",
"player2": "player2",
"winner": "player1",
"state": "ended"
},
{
"gameId": "game3",
"player1": "player1",
"state": "joinable"
}
]
我的 Elm 模型是这样的:
type Player =
Player String
type State
= Joinable
| Started
| Ended
type alias Game =
{ gameId : String
, player1 : Maybe Player
, player2 : Maybe Player
, winner : Maybe Player
, state : State}
我的解码逻辑是这样定义的:
gameStateDecoder : Decode.Decoder State
gameStateDecoder =
string
|> andThen (\stateAsString ->
case stateAsString of
"joinable" ->
succeed Joinable
"ended" ->
succeed Ended
"started" ->
succeed Started
unknown ->
fail <| "Unknown game state: " ++ unknown
)
playerDecoder : Decode.Decoder (Maybe Player)
playerDecoder =
(maybe string)
|> andThen (\maybePlayerString ->
succeed
<| case maybePlayerString of
Just player ->
Just (Player player)
_ ->
Nothing
)
gameListDecoder : Decode.Decoder (List Game)
gameListDecoder =
Decode.list gameDecoder
gameDecoder : Decode.Decoder Game
gameDecoder =
Decode.map5 Game
(field "gameId" string)
(field "player1" playerDecoder)
(field "player2" playerDecoder)
(field "winner" playerDecoder)
(field "state" gameStateDecoder)
现在我尝试使用 elm-explorations/test 包为此创建一个测试用例:
import Frontend exposing (..)
import Expect exposing (equal)
import Test exposing (Test, test)
import Json.Decode exposing (decodeString)
decodesGameList : Test
decodesGameList =
test "Properly decodes a game list" <|
\() ->
let
json =
"""
[
{
"gameId": "game",
"state": "joinable"
}
]
"""
decodedOutput =
decodeString gameListDecoder json
in
equal
decodedOutput
(Ok
[ Game "game" Nothing Nothing Nothing Joinable ]
)
但是当我尝试使用 elm-test
运行 它时,我得到了这个错误:
<project path>/tests/FrontendTest.elm has an invalid module declaration. Check the first line of the file and make sure it has a valid module declaration there!
我不明白我做错了什么。模块在 Frontend.elm
:
中这样定义
module Frontend exposing (gameListDecoder, State(..), Game, Player(..))
我可以很好地编译它:
$ elm make src/main/elm/Frontend.elm --output src/main/resources/static/index.html
Success! Compiled 1 module.
您实际上可以在 github 找到代码。
我该如何解决这个问题?
Elm 模块应该在文件顶部有一个模块声明,即
module FrontendTest exposing (..)
应该可以解决这种情况下的问题。
我是 Elm 的新手,我正在使用 Elm 0.19 开始我的第一个宠物项目。
我想在我的应用程序中创建 JSON 解码的测试用例。从服务器返回的 JSON 看起来像这样(所有内容都在名为 Frontend.elm
的文件中定义):
[
{
"gameId": "game1",
"player1": "player1",
"player2": "player2",
"winner": "player1",
"state": "ended"
},
{
"gameId": "game3",
"player1": "player1",
"state": "joinable"
}
]
我的 Elm 模型是这样的:
type Player =
Player String
type State
= Joinable
| Started
| Ended
type alias Game =
{ gameId : String
, player1 : Maybe Player
, player2 : Maybe Player
, winner : Maybe Player
, state : State}
我的解码逻辑是这样定义的:
gameStateDecoder : Decode.Decoder State
gameStateDecoder =
string
|> andThen (\stateAsString ->
case stateAsString of
"joinable" ->
succeed Joinable
"ended" ->
succeed Ended
"started" ->
succeed Started
unknown ->
fail <| "Unknown game state: " ++ unknown
)
playerDecoder : Decode.Decoder (Maybe Player)
playerDecoder =
(maybe string)
|> andThen (\maybePlayerString ->
succeed
<| case maybePlayerString of
Just player ->
Just (Player player)
_ ->
Nothing
)
gameListDecoder : Decode.Decoder (List Game)
gameListDecoder =
Decode.list gameDecoder
gameDecoder : Decode.Decoder Game
gameDecoder =
Decode.map5 Game
(field "gameId" string)
(field "player1" playerDecoder)
(field "player2" playerDecoder)
(field "winner" playerDecoder)
(field "state" gameStateDecoder)
现在我尝试使用 elm-explorations/test 包为此创建一个测试用例:
import Frontend exposing (..)
import Expect exposing (equal)
import Test exposing (Test, test)
import Json.Decode exposing (decodeString)
decodesGameList : Test
decodesGameList =
test "Properly decodes a game list" <|
\() ->
let
json =
"""
[
{
"gameId": "game",
"state": "joinable"
}
]
"""
decodedOutput =
decodeString gameListDecoder json
in
equal
decodedOutput
(Ok
[ Game "game" Nothing Nothing Nothing Joinable ]
)
但是当我尝试使用 elm-test
运行 它时,我得到了这个错误:
<project path>/tests/FrontendTest.elm has an invalid module declaration. Check the first line of the file and make sure it has a valid module declaration there!
我不明白我做错了什么。模块在 Frontend.elm
:
module Frontend exposing (gameListDecoder, State(..), Game, Player(..))
我可以很好地编译它:
$ elm make src/main/elm/Frontend.elm --output src/main/resources/static/index.html
Success! Compiled 1 module.
您实际上可以在 github 找到代码。
我该如何解决这个问题?
Elm 模块应该在文件顶部有一个模块声明,即
module FrontendTest exposing (..)
应该可以解决这种情况下的问题。