From fe3c79f30a7654e559da20df4365e9ba1425bb3b Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Mon, 12 Sep 2022 15:08:11 -0700 Subject: [PATCH] Add test setup for branch generator. --- src/Pages/Internal/RoutePattern.elm | 14 ++++++++++++-- tests/Pages/RouteParamsTest.elm | 30 +++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/Pages/Internal/RoutePattern.elm b/src/Pages/Internal/RoutePattern.elm index 00031aec..b07496bc 100644 --- a/src/Pages/Internal/RoutePattern.elm +++ b/src/Pages/Internal/RoutePattern.elm @@ -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 = diff --git a/tests/Pages/RouteParamsTest.elm b/tests/Pages/RouteParamsTest.elm index a7046c25..ef20e15c 100644 --- a/tests/Pages/RouteParamsTest.elm +++ b/tests/Pages/RouteParamsTest.elm @@ -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 "" + |> 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