Add test setup for branch generator.

This commit is contained in:
Dillon Kearns 2022-09-12 15:08:11 -07:00
parent 012aac229a
commit fe3c79f30a
2 changed files with 42 additions and 2 deletions

View File

@ -1,16 +1,17 @@
module Pages.Internal.RoutePattern exposing
( Ending(..), RoutePattern, Segment(..), view, toVariant
( Ending(..), RoutePattern, Segment(..), view, toVariant, routeToBranch
, Param(..), fromModuleName, toRouteParamTypes, toRouteParamsRecord
)
{-| Exposed for internal use only (used in generated code).
@docs Ending, RoutePattern, Segment, view, toVariant
@docs Ending, RoutePattern, Segment, view, toVariant, routeToBranch
-}
import Elm
import Elm.Annotation exposing (Annotation)
import Elm.Case
import Html exposing (Html)
@ -123,6 +124,15 @@ toRouteParamTypes pattern =
)
routeToBranch : RoutePattern -> Elm.Case.Branch
routeToBranch route =
Elm.Case.branchList 0
(\_ ->
Elm.val "Index"
|> Elm.just
)
{-| -}
toVariant : RoutePattern -> Elm.Variant
toVariant pattern =

View File

@ -2,6 +2,7 @@ module Pages.RouteParamsTest exposing (..)
import Elm
import Elm.Annotation
import Elm.Case
import Elm.ToString
import Expect exposing (Expectation)
import Pages.Internal.RoutePattern as RoutePattern
@ -132,9 +133,38 @@ suite =
]
)
]
, describe "toCase"
[ test "root route" <|
\() ->
[]
|> testCaseGenerator
(Elm.Case.branchList 0
(\_ ->
Elm.val "Index"
|> Elm.just
)
)
]
]
testCaseGenerator : Elm.Case.Branch -> List String -> Expectation
testCaseGenerator expected moduleName =
RoutePattern.fromModuleName moduleName
|> Maybe.map (RoutePattern.routeToBranch >> toStringCase)
|> Maybe.withDefault "<ERROR>"
|> Expect.equal (expected |> toStringCase)
toStringCase : Elm.Case.Branch -> String
toStringCase branch =
Elm.Case.custom (Elm.val "segments")
(Elm.Annotation.list Elm.Annotation.string)
[ branch ]
|> Elm.ToString.expression
|> .body
expectRouteDefinition : Elm.Variant -> List String -> Expectation
expectRouteDefinition expected moduleName =
RoutePattern.fromModuleName moduleName