elm-pages-v3-beta/generator/review/tests/Pages/Review/NoContractViolationsTest.elm

332 lines
12 KiB
Elm
Raw Normal View History

2021-08-26 20:27:30 +03:00
module Pages.Review.NoContractViolationsTest exposing (all)
2021-08-26 19:30:07 +03:00
2021-08-26 20:27:30 +03:00
import Pages.Review.NoContractViolations exposing (rule)
2021-08-26 19:30:07 +03:00
import Review.Test
import Test exposing (Test, describe, test)
all : Test
all =
2021-08-26 20:27:30 +03:00
describe "Pages.Review.NoContractViolations"
2021-08-26 19:30:07 +03:00
[ test "reports error when missing exposed declaration" <|
\() ->
2022-05-12 01:52:15 +03:00
"""module Route.Blog.Slug_ exposing (ActionData, Data, Msg, route)
2021-08-26 19:30:07 +03:00
a = 1
"""
|> testRouteModule
|> Review.Test.expectErrorsForModules
[ ( "Route.Blog.Slug_"
, [ Review.Test.error
{ message = "Unexposed Declaration in Route Module"
, details =
[ """Route Modules need to expose the following values:
2021-08-26 19:30:07 +03:00
- route
2021-08-26 19:30:07 +03:00
- Data
2022-05-12 01:52:15 +03:00
- ActionData
2021-08-26 19:30:07 +03:00
- Model
- Msg
But it is not exposing: Model"""
]
2022-05-12 01:52:15 +03:00
, under = "exposing (ActionData, Data, Msg, route)"
}
]
)
2021-08-26 19:30:07 +03:00
]
, test "reports RouteParams mismatch" <|
\() ->
2022-05-12 01:52:15 +03:00
"""module Route.Blog.Slug_ exposing (ActionData, Data, route, Model, Msg)
2021-08-26 19:30:07 +03:00
type alias RouteParams = { blogPostName : String }
route = {}
2021-08-26 19:30:07 +03:00
"""
|> testRouteModule
|> Review.Test.expectErrorsForModules
[ ( "Route.Blog.Slug_"
, [ Review.Test.error
{ message = "RouteParams don't match Route Module name"
, details =
[ """Expected
2021-08-26 19:30:07 +03:00
type alias RouteParams = { slug : String }
"""
]
, under = "{ blogPostName : String }"
}
]
)
2021-08-26 19:30:07 +03:00
]
, test "reports incorrect types for optional RouteParams" <|
\() ->
2022-05-12 01:52:15 +03:00
"""module Route.Docs.Section_.SubSection__ exposing (ActionData, Data, route, Model, Msg)
type alias RouteParams = { section : String, subSection : String }
route = {}
"""
|> testRouteModule
|> Review.Test.expectErrorsForModules
[ ( "Route.Docs.Section_.SubSection__"
, [ Review.Test.error
{ message = "RouteParams don't match Route Module name"
, details =
[ """Expected
type alias RouteParams = { section : String, subSection : Maybe String }
"""
]
, under = "{ section : String, subSection : String }"
}
]
)
]
, test "reports incorrect types for required splat RouteParams" <|
\() ->
2022-05-12 01:52:15 +03:00
"""module Route.Docs.Section_.SPLAT_ exposing (ActionData, Data, route, Model, Msg)
type alias RouteParams = { section : String, splat : List String }
route = {}
"""
|> testRouteModule
|> Review.Test.expectErrorsForModules
[ ( "Route.Docs.Section_.SPLAT_"
, [ Review.Test.error
{ message = "RouteParams don't match Route Module name"
, details =
[ """Expected
type alias RouteParams = { section : String, splat : ( String, List String ) }
"""
]
, under = "{ section : String, splat : List String }"
}
]
)
]
, test "no error for valid SPLAT_ RouteParams" <|
\() ->
2022-05-12 01:52:15 +03:00
"""module Route.Docs.Section_.SPLAT_ exposing (ActionData, Data, route, Model, Msg)
type alias RouteParams = { section : String, splat : ( String, List String ) }
route = {}
2021-08-27 00:05:00 +03:00
"""
|> testRouteModule
2021-08-27 00:05:00 +03:00
|> Review.Test.expectNoErrors
, test "no error for valid SPLAT__ RouteParams" <|
\() ->
2022-05-12 01:52:15 +03:00
"""module Route.Docs.Section_.SPLAT__ exposing (ActionData, Data, route, Model, Msg)
2021-08-27 00:05:00 +03:00
type alias RouteParams = { section : String, splat : List String }
route = {}
"""
|> testRouteModule
|> Review.Test.expectNoErrors
2021-08-26 19:30:07 +03:00
, test "no error for matching RouteParams name" <|
\() ->
2022-05-12 01:52:15 +03:00
"""module Route.Blog.Slug_ exposing (ActionData, Data, route, Model, Msg)
2021-08-26 19:30:07 +03:00
type alias RouteParams = { slug : String }
route = {}
2021-08-26 19:30:07 +03:00
"""
|> testRouteModule
2021-08-26 19:30:07 +03:00
|> Review.Test.expectNoErrors
, test "error when RouteParams type is not a record" <|
\() ->
2022-05-12 01:52:15 +03:00
"""module Route.Blog.Slug_ exposing (ActionData, Data, route, Model, Msg)
type alias RouteParams = ()
route = {}
"""
|> testRouteModule
|> Review.Test.expectErrorsForModules
[ ( "Route.Blog.Slug_"
, [ Review.Test.error
{ message = "RouteParams must be a record type alias."
, details =
[ """Expected a record type alias."""
]
, under = "()"
}
]
)
]
, test "no error for modules that don't start with Route prefix" <|
\() ->
"""module NotRouteModule.Blog.Slug_ exposing (Model, Msg)
type alias RouteParams = ()
route = {}
"""
|> testRouteModule
|> Review.Test.expectNoErrors
, test "error for missing application module definitions" <|
\() ->
2022-05-12 01:52:15 +03:00
[ """module Route.Index exposing (ActionData, Data, route, Model, Msg)
type alias RouteParams = {}
route = {}
"""
, """module Site exposing (config)
config : SiteConfig
config =
{ canonicalUrl = canonicalUrl
, head = head
}
"""
]
|> Review.Test.runOnModules rule
|> Review.Test.expectGlobalErrors
[ { message = "Missing core modules"
, details =
[ "Api"
, "Effect"
, "ErrorPage"
, "Shared"
, "View"
]
}
]
, test "no error when all core modules are defined" <|
\() ->
2022-05-12 01:52:15 +03:00
("""module Route.Index exposing (ActionData, Data, route, Model, Msg)
type alias RouteParams = {}
route = {}
"""
:: validCoreModules
)
|> Review.Test.runOnModules rule
|> Review.Test.expectNoErrors
, test "show missing exposed values from core modules" <|
\() ->
2022-05-12 01:52:15 +03:00
[ """module Route.Index exposing (ActionData, Data, route, Model, Msg)
type alias RouteParams = {}
route = {}
"""
, """module Api exposing (invalid)
invalid = Debug.todo ""
"""
, """module Effect exposing (invalid)
invalid = Debug.todo ""
"""
, """module ErrorPage exposing (invalid)
invalid = Debug.todo ""
"""
, """module Shared exposing (invalid)
invalid = Debug.todo ""
"""
, """module Site exposing (invalid)
invalid = Debug.todo ""
"""
, """module View exposing (invalid)
invalid = Debug.todo ""
"""
]
|> Review.Test.runOnModules rule
|> Review.Test.expectErrorsForModules
[ ( "Api"
, [ Review.Test.error
{ message = "A core elm-pages module needs to expose something"
, details =
[ "The Api module must expose `routes`"
]
, under = "exposing (invalid)"
}
]
)
, ( "Effect"
, [ Review.Test.error
{ message = "A core elm-pages module needs to expose something"
, details =
[ "The Effect module must expose `Effect`, `batch`, `fromCmd`, `map`, `none`, `perform`"
]
, under = "exposing (invalid)"
}
]
)
, ( "ErrorPage"
, [ Review.Test.error
{ message = "A core elm-pages module needs to expose something"
, details =
[ "The ErrorPage module must expose `ErrorPage`, `head`, `internalError`, `notFound`, `statusCode`, `view`"
]
, under = "exposing (invalid)"
}
]
)
, ( "Shared"
, [ Review.Test.error
{ message = "A core elm-pages module needs to expose something"
, details =
[ "The Shared module must expose `Data`, `Model`, `Msg`, `template`"
]
, under = "exposing (invalid)"
}
]
)
, ( "Site"
, [ Review.Test.error
{ message = "A core elm-pages module needs to expose something"
, details =
[ "The Site module must expose `config`"
]
, under = "exposing (invalid)"
}
]
)
, ( "View"
, [ Review.Test.error
{ message = "A core elm-pages module needs to expose something"
, details =
[ "The View module must expose `View`, `map`"
]
, under = "exposing (invalid)"
}
]
)
]
2021-08-26 19:30:07 +03:00
]
testRouteModule : String -> Review.Test.ReviewResult
testRouteModule routeModule =
Review.Test.runOnModules rule
(routeModule :: validCoreModules)
validCoreModules : List String
validCoreModules =
[ """module Api exposing (routes)
routes = Debug.todo ""
"""
, """module Effect exposing (Effect(..), batch, fromCmd, map, none, perform)
routes = Debug.todo ""
"""
, """module ErrorPage exposing (ErrorPage(..), Model, Msg, head, internalError, notFound, statusCode, view)
routes = Debug.todo ""
"""
, """module Shared exposing (Data, Model, Msg, template)
routes = Debug.todo ""
"""
, """module Site exposing (config)
routes = Debug.todo ""
"""
, """module View exposing (View, map)
routes = Debug.todo ""
"""
]