2020-03-27 07:43:08 +03:00
|
|
|
module Tests.Path exposing (suite)
|
|
|
|
|
|
|
|
import Expect exposing (Expectation)
|
|
|
|
import Path exposing (Path)
|
|
|
|
import Test exposing (..)
|
|
|
|
|
|
|
|
|
|
|
|
suite : Test
|
|
|
|
suite =
|
|
|
|
describe "Path"
|
2020-03-28 09:16:04 +03:00
|
|
|
[ describe "routingOrder"
|
|
|
|
[ test "prioritizes dynamic last" <|
|
|
|
|
\_ ->
|
|
|
|
Path.routingOrder
|
|
|
|
(Path.fromFilepath "Example/Top.elm")
|
2020-07-14 06:39:10 +03:00
|
|
|
(Path.fromFilepath "Example/Id_Int.elm")
|
2020-03-28 09:16:04 +03:00
|
|
|
|> Expect.equal LT
|
|
|
|
, test "prioritizes files over folders with top" <|
|
|
|
|
\_ ->
|
|
|
|
Path.routingOrder
|
|
|
|
(Path.fromFilepath "Example/Top.elm")
|
|
|
|
(Path.fromFilepath "Example.elm")
|
|
|
|
|> Expect.equal GT
|
|
|
|
, test "works with List.sortWith as expected" <|
|
|
|
|
\_ ->
|
|
|
|
[ Path.fromFilepath "Top.elm"
|
|
|
|
, Path.fromFilepath "About.elm"
|
2020-07-14 06:39:10 +03:00
|
|
|
, Path.fromFilepath "Authors/Author_String/Posts/PostId_Int.elm"
|
|
|
|
, Path.fromFilepath "Posts/Id_Int.elm"
|
2020-03-28 09:16:04 +03:00
|
|
|
, Path.fromFilepath "Posts.elm"
|
|
|
|
, Path.fromFilepath "Posts/Top.elm"
|
|
|
|
]
|
|
|
|
|> List.sortWith Path.routingOrder
|
|
|
|
|> Expect.equalLists
|
|
|
|
[ Path.fromFilepath "Top.elm"
|
|
|
|
, Path.fromFilepath "About.elm"
|
|
|
|
, Path.fromFilepath "Posts.elm"
|
|
|
|
, Path.fromFilepath "Posts/Top.elm"
|
2020-07-14 06:39:10 +03:00
|
|
|
, Path.fromFilepath "Posts/Id_Int.elm"
|
|
|
|
, Path.fromFilepath "Authors/Author_String/Posts/PostId_Int.elm"
|
2020-03-28 09:16:04 +03:00
|
|
|
]
|
|
|
|
]
|
|
|
|
, describe "fromFilepath"
|
2020-03-27 07:43:08 +03:00
|
|
|
[ test "ignores first slash" <|
|
|
|
|
\_ ->
|
|
|
|
"/Top.elm"
|
|
|
|
|> Path.fromFilepath
|
|
|
|
|> Path.toList
|
|
|
|
|> Expect.equalLists [ "Top" ]
|
|
|
|
, test "works without folders" <|
|
|
|
|
\_ ->
|
|
|
|
"Top.elm"
|
|
|
|
|> Path.fromFilepath
|
|
|
|
|> Path.toList
|
|
|
|
|> Expect.equalLists [ "Top" ]
|
|
|
|
, test "works with a single folder" <|
|
|
|
|
\_ ->
|
|
|
|
"Posts/Top.elm"
|
|
|
|
|> Path.fromFilepath
|
|
|
|
|> Path.toList
|
|
|
|
|> Expect.equalLists [ "Posts", "Top" ]
|
|
|
|
, test "works with nested folders" <|
|
|
|
|
\_ ->
|
2020-07-14 06:39:10 +03:00
|
|
|
"Authors/Author_String/Posts/PostId_Int.elm"
|
2020-03-27 07:43:08 +03:00
|
|
|
|> Path.fromFilepath
|
|
|
|
|> Path.toList
|
2020-07-14 06:39:10 +03:00
|
|
|
|> Expect.equalLists [ "Authors", "Author_String", "Posts", "PostId_Int" ]
|
2020-03-27 07:43:08 +03:00
|
|
|
]
|
|
|
|
, describe "toModulePath"
|
|
|
|
[ test "works without folders" <|
|
|
|
|
\_ ->
|
|
|
|
"Top.elm"
|
|
|
|
|> Path.fromFilepath
|
|
|
|
|> Path.toModulePath
|
|
|
|
|> Expect.equal "Top"
|
|
|
|
, test "works with a single folder" <|
|
|
|
|
\_ ->
|
|
|
|
"Posts/Top.elm"
|
|
|
|
|> Path.fromFilepath
|
|
|
|
|> Path.toModulePath
|
|
|
|
|> Expect.equal "Posts.Top"
|
|
|
|
, test "works with nested folders" <|
|
|
|
|
\_ ->
|
2020-07-14 06:39:10 +03:00
|
|
|
"Authors/Author_String/Posts/PostId_Int.elm"
|
2020-03-27 07:43:08 +03:00
|
|
|
|> Path.fromFilepath
|
|
|
|
|> Path.toModulePath
|
2020-07-14 06:39:10 +03:00
|
|
|
|> Expect.equal "Authors.Author_String.Posts.PostId_Int"
|
2020-03-27 07:43:08 +03:00
|
|
|
]
|
|
|
|
, describe "toVariableName"
|
|
|
|
[ test "works without folders" <|
|
|
|
|
\_ ->
|
|
|
|
"Top.elm"
|
|
|
|
|> Path.fromFilepath
|
|
|
|
|> Path.toVariableName
|
|
|
|
|> Expect.equal "top"
|
|
|
|
, test "works correctly with capital letters" <|
|
|
|
|
\_ ->
|
|
|
|
"NotFound.elm"
|
|
|
|
|> Path.fromFilepath
|
|
|
|
|> Path.toVariableName
|
|
|
|
|> Expect.equal "notFound"
|
|
|
|
, test "works with a single folder" <|
|
|
|
|
\_ ->
|
|
|
|
"Posts/Top.elm"
|
|
|
|
|> Path.fromFilepath
|
|
|
|
|> Path.toVariableName
|
2020-07-14 06:39:10 +03:00
|
|
|
|> Expect.equal "posts__top"
|
2020-03-27 07:43:08 +03:00
|
|
|
, test "works with nested folders" <|
|
|
|
|
\_ ->
|
2020-07-14 06:39:10 +03:00
|
|
|
"Authors/Author_String/Posts/PostId_Int.elm"
|
2020-03-27 07:43:08 +03:00
|
|
|
|> Path.fromFilepath
|
|
|
|
|> Path.toVariableName
|
2020-07-14 06:39:10 +03:00
|
|
|
|> Expect.equal "authors__author_string__posts__postId_int"
|
2020-03-27 07:43:08 +03:00
|
|
|
]
|
|
|
|
, describe "toTypeName"
|
|
|
|
[ test "works without folders" <|
|
|
|
|
\_ ->
|
|
|
|
"Top.elm"
|
|
|
|
|> Path.fromFilepath
|
|
|
|
|> Path.toTypeName
|
|
|
|
|> Expect.equal "Top"
|
|
|
|
, test "works with a single folder" <|
|
|
|
|
\_ ->
|
|
|
|
"Posts/Top.elm"
|
|
|
|
|> Path.fromFilepath
|
|
|
|
|> Path.toTypeName
|
2020-07-14 06:39:10 +03:00
|
|
|
|> Expect.equal "Posts__Top"
|
2020-03-27 07:43:08 +03:00
|
|
|
, test "works with nested folders" <|
|
|
|
|
\_ ->
|
2020-07-14 06:39:10 +03:00
|
|
|
"Authors/Author_String/Posts/PostId_Int.elm"
|
2020-03-27 07:43:08 +03:00
|
|
|
|> Path.fromFilepath
|
|
|
|
|> Path.toTypeName
|
2020-07-14 06:39:10 +03:00
|
|
|
|> Expect.equal "Authors__Author_String__Posts__PostId_Int"
|
2020-03-27 07:43:08 +03:00
|
|
|
]
|
|
|
|
, describe "optionalParams"
|
|
|
|
[ test "works without folders" <|
|
|
|
|
\_ ->
|
|
|
|
"Top.elm"
|
|
|
|
|> Path.fromFilepath
|
|
|
|
|> Path.optionalParams
|
|
|
|
|> Expect.equal ""
|
|
|
|
, test "works with a single folder" <|
|
|
|
|
\_ ->
|
2020-07-14 06:39:10 +03:00
|
|
|
"Posts/Id_Int.elm"
|
2020-03-27 07:43:08 +03:00
|
|
|
|> Path.fromFilepath
|
|
|
|
|> Path.optionalParams
|
2020-07-14 06:39:10 +03:00
|
|
|
|> Expect.equal " { id : Int }"
|
2020-03-27 07:43:08 +03:00
|
|
|
, test "works with nested folders" <|
|
|
|
|
\_ ->
|
2020-07-14 06:39:10 +03:00
|
|
|
"Authors/Author_String/Posts/PostId_Int.elm"
|
2020-03-27 07:43:08 +03:00
|
|
|
|> Path.fromFilepath
|
|
|
|
|> Path.optionalParams
|
2020-07-14 06:39:10 +03:00
|
|
|
|> Expect.equal " { author : String, postId : Int }"
|
2020-03-27 07:43:08 +03:00
|
|
|
]
|
|
|
|
, describe "toParser"
|
|
|
|
[ test "works with top" <|
|
|
|
|
\_ ->
|
|
|
|
"Top.elm"
|
|
|
|
|> Path.fromFilepath
|
|
|
|
|> Path.toParser
|
|
|
|
|> Expect.equal "Parser.map Top Parser.top"
|
|
|
|
, test "works with single static path" <|
|
|
|
|
\_ ->
|
|
|
|
"About.elm"
|
|
|
|
|> Path.fromFilepath
|
|
|
|
|> Path.toParser
|
|
|
|
|> Expect.equal "Parser.map About (Parser.s \"about\")"
|
|
|
|
, test "works with multiple static paths" <|
|
|
|
|
\_ ->
|
|
|
|
"About/Team.elm"
|
|
|
|
|> Path.fromFilepath
|
|
|
|
|> Path.toParser
|
2020-07-14 06:39:10 +03:00
|
|
|
|> Expect.equal "Parser.map About__Team (Parser.s \"about\" </> Parser.s \"team\")"
|
2020-03-27 07:43:08 +03:00
|
|
|
, test "works with single dynamic path" <|
|
|
|
|
\_ ->
|
2020-07-14 06:39:10 +03:00
|
|
|
"Posts/Id_Int.elm"
|
2020-03-27 07:43:08 +03:00
|
|
|
|> Path.fromFilepath
|
|
|
|
|> Path.toParser
|
|
|
|
|> Expect.equal (String.trim """
|
2020-07-14 06:39:10 +03:00
|
|
|
(Parser.s "posts" </> Parser.int)
|
|
|
|
|> Parser.map (\\id -> { id = id })
|
|
|
|
|> Parser.map Posts__Id_Int
|
2020-03-27 07:43:08 +03:00
|
|
|
""")
|
|
|
|
, test "works with multiple dynamic paths" <|
|
|
|
|
\_ ->
|
2020-07-14 06:39:10 +03:00
|
|
|
"Authors/Author_String/Posts/PostId_Int.elm"
|
2020-03-27 07:43:08 +03:00
|
|
|
|> Path.fromFilepath
|
|
|
|
|> Path.toParser
|
|
|
|
|> Expect.equal (String.trim """
|
2020-07-14 06:39:10 +03:00
|
|
|
(Parser.s "authors" </> Parser.string </> Parser.s "posts" </> Parser.int)
|
|
|
|
|> Parser.map (\\author postId -> { author = author, postId = postId })
|
|
|
|
|> Parser.map Authors__Author_String__Posts__PostId_Int
|
2020-03-27 07:43:08 +03:00
|
|
|
""")
|
2020-07-14 06:39:10 +03:00
|
|
|
, describe "toParams"
|
2020-03-27 07:43:08 +03:00
|
|
|
[ test "works with no dynamic params" <|
|
|
|
|
\_ ->
|
|
|
|
"Top.elm"
|
|
|
|
|> Path.fromFilepath
|
2020-07-14 06:39:10 +03:00
|
|
|
|> Path.toParams
|
2020-03-27 07:43:08 +03:00
|
|
|
|> Expect.equal "()"
|
|
|
|
, test "works with one dynamic param" <|
|
|
|
|
\_ ->
|
2020-07-14 06:39:10 +03:00
|
|
|
"Posts/Id_Int.elm"
|
2020-03-27 07:43:08 +03:00
|
|
|
|> Path.fromFilepath
|
2020-07-14 06:39:10 +03:00
|
|
|
|> Path.toParams
|
|
|
|
|> Expect.equal "{ id : Int }"
|
2020-03-27 07:43:08 +03:00
|
|
|
, test "works with multiple dynamic params" <|
|
|
|
|
\_ ->
|
2020-07-14 06:39:10 +03:00
|
|
|
"Authors/Author_String/Posts/PostId_Int.elm"
|
2020-03-27 07:43:08 +03:00
|
|
|
|> Path.fromFilepath
|
2020-07-14 06:39:10 +03:00
|
|
|
|> Path.toParams
|
|
|
|
|> Expect.equal "{ author : String, postId : Int }"
|
2020-03-27 07:43:08 +03:00
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|