mirror of
https://github.com/dillonkearns/elm-pages-v3-beta.git
synced 2024-12-25 12:52:27 +03:00
Add test case for new router prototype.
This commit is contained in:
parent
5d8caf5d18
commit
9e1f3b7b73
@ -24,6 +24,7 @@
|
|||||||
"elm/html": "1.0.0",
|
"elm/html": "1.0.0",
|
||||||
"elm/http": "2.0.0",
|
"elm/http": "2.0.0",
|
||||||
"elm/json": "1.1.3",
|
"elm/json": "1.1.3",
|
||||||
|
"elm/regex": "1.0.0",
|
||||||
"elm/svg": "1.0.1",
|
"elm/svg": "1.0.1",
|
||||||
"elm/time": "1.0.0",
|
"elm/time": "1.0.0",
|
||||||
"elm/url": "1.0.0",
|
"elm/url": "1.0.0",
|
||||||
@ -50,7 +51,6 @@
|
|||||||
"elm/file": "1.0.5",
|
"elm/file": "1.0.5",
|
||||||
"elm/parser": "1.1.0",
|
"elm/parser": "1.1.0",
|
||||||
"elm/random": "1.0.0",
|
"elm/random": "1.0.0",
|
||||||
"elm/regex": "1.0.0",
|
|
||||||
"fredcy/elm-parseint": "2.0.1",
|
"fredcy/elm-parseint": "2.0.1",
|
||||||
"justinmimbs/time-extra": "1.1.0",
|
"justinmimbs/time-extra": "1.1.0",
|
||||||
"lazamar/dict-parser": "1.0.2",
|
"lazamar/dict-parser": "1.0.2",
|
||||||
|
91
tests/RouteTests.elm
Normal file
91
tests/RouteTests.elm
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
module RouteTests exposing (..)
|
||||||
|
|
||||||
|
import Expect
|
||||||
|
import Regex
|
||||||
|
import Test exposing (Test, describe, test)
|
||||||
|
|
||||||
|
|
||||||
|
all : Test
|
||||||
|
all =
|
||||||
|
describe "routes"
|
||||||
|
[ test "test 1" <|
|
||||||
|
\() ->
|
||||||
|
"/cats/larry"
|
||||||
|
|> tryMatch
|
||||||
|
{ pattern = "^cats(?:\\/([^/]+))?$"
|
||||||
|
, toRoute =
|
||||||
|
\matches ->
|
||||||
|
case matches of
|
||||||
|
[ name ] ->
|
||||||
|
Cats__Name__ { name = name } |> Just
|
||||||
|
|
||||||
|
_ ->
|
||||||
|
Nothing
|
||||||
|
}
|
||||||
|
|> Expect.equal (Cats__Name__ { name = Just "larry" } |> Just)
|
||||||
|
, test "test 2" <|
|
||||||
|
\() ->
|
||||||
|
"/cats"
|
||||||
|
|> tryMatch
|
||||||
|
{ pattern = "^cats(?:\\/([^/]+))?$"
|
||||||
|
, toRoute =
|
||||||
|
\matches ->
|
||||||
|
case matches of
|
||||||
|
[ name ] ->
|
||||||
|
Cats__Name__ { name = name } |> Just
|
||||||
|
|
||||||
|
_ ->
|
||||||
|
Nothing
|
||||||
|
}
|
||||||
|
|> Expect.equal (Cats__Name__ { name = Nothing } |> Just)
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
tryMatch : { pattern : String, toRoute : List (Maybe String) -> Maybe Route } -> String -> Maybe Route
|
||||||
|
tryMatch { pattern, toRoute } path =
|
||||||
|
path
|
||||||
|
|> normalizePath
|
||||||
|
|> submatches "^cats(?:\\/([^/]+))?$"
|
||||||
|
|> toRoute
|
||||||
|
|
||||||
|
|
||||||
|
submatches : String -> String -> List (Maybe String)
|
||||||
|
submatches pattern path =
|
||||||
|
Regex.find
|
||||||
|
(Regex.fromString
|
||||||
|
"^cats(?:\\/([^/]+))?$"
|
||||||
|
|> Maybe.withDefault Regex.never
|
||||||
|
)
|
||||||
|
path
|
||||||
|
|> List.concatMap .submatches
|
||||||
|
|
||||||
|
|
||||||
|
normalizePath : String -> String
|
||||||
|
normalizePath path =
|
||||||
|
path
|
||||||
|
|> stripLeadingSlash
|
||||||
|
|> stripTrailingSlash
|
||||||
|
|
||||||
|
|
||||||
|
stripLeadingSlash : String -> String
|
||||||
|
stripLeadingSlash path =
|
||||||
|
if path |> String.startsWith "/" then
|
||||||
|
String.dropLeft 1 path
|
||||||
|
|
||||||
|
else
|
||||||
|
path
|
||||||
|
|
||||||
|
|
||||||
|
stripTrailingSlash : String -> String
|
||||||
|
stripTrailingSlash path =
|
||||||
|
if path |> String.endsWith "/" then
|
||||||
|
String.dropRight 1 path
|
||||||
|
|
||||||
|
else
|
||||||
|
path
|
||||||
|
|
||||||
|
|
||||||
|
type Route
|
||||||
|
= Slide {}
|
||||||
|
| Cats__Name__ { name : Maybe String }
|
||||||
|
| Slide__Number_ { number : String }
|
Loading…
Reference in New Issue
Block a user