Add test case for new router prototype.

This commit is contained in:
Dillon Kearns 2021-04-27 08:34:54 -07:00
parent 5d8caf5d18
commit 9e1f3b7b73
2 changed files with 92 additions and 1 deletions

View File

@ -24,6 +24,7 @@
"elm/html": "1.0.0",
"elm/http": "2.0.0",
"elm/json": "1.1.3",
"elm/regex": "1.0.0",
"elm/svg": "1.0.1",
"elm/time": "1.0.0",
"elm/url": "1.0.0",
@ -50,7 +51,6 @@
"elm/file": "1.0.5",
"elm/parser": "1.1.0",
"elm/random": "1.0.0",
"elm/regex": "1.0.0",
"fredcy/elm-parseint": "2.0.1",
"justinmimbs/time-extra": "1.1.0",
"lazamar/dict-parser": "1.0.2",

91
tests/RouteTests.elm Normal file
View 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 }