Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/UrlParser.elm
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ type alias State value =
-}
string : Parser (String -> a) a
string =
custom "STRING" Ok
custom "STRING" <|
\segment ->
if String.isEmpty segment then
Err "Empty string"
else
Ok segment


{-| Parse a segment of the path as an `Int`.
Expand Down Expand Up @@ -93,14 +98,11 @@ s str =


{-| Create a custom path segment parser. Here is how it is used to define the
`int` and `string` parsers:
`int` parser:

int =
custom "NUMBER" String.toInt

string =
custom "STRING" Ok

You can use it to define something like “only CSS files” like this:

css : Parser (String -> a) a
Expand Down
13 changes: 0 additions & 13 deletions tests/Main.elm

This file was deleted.

76 changes: 51 additions & 25 deletions tests/Tests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import Test exposing (..)
import Expect



-- TESTS


Expand All @@ -17,34 +16,58 @@ all =
]


testParsing : List Test
testParsing =
[ parserTest "Home" "" HomeRoute
, parserTest "About" "about" AboutRoute
, parserTest "Token" "token/abc" (TokenRoute "abc")
, parserTest "Users" "users" (UsersRoutes UsersRoute)
, parserTest "User" "users/2" (UsersRoutes (UserRoute 2))
, parserTest "Edit" "users/2/edit" (UsersRoutes (UserEditRoute 2))
[ parserTest "Home" "" <| Ok HomeRoute
, parserTest "About" "about" <| Ok AboutRoute
, parserTest "Token" "token/abc" <| Ok (TokenRoute "abc")
, parserTest "Token (missing ID)" "token/" <| Err "missing token segment"
, parserTest "Users" "users" <| Ok (UsersRoutes UsersRoute)
, parserTest "User" "users/2" <| Ok (UsersRoutes (UserRoute 2))
, parserTest "Edit" "users/2/edit" <| Ok (UsersRoutes (UserEditRoute 2))
]


parserTest name path expectedRoute =
describe name
[ test (name ++ " in path") <|
\() ->
Expect.equal
(Just expectedRoute)
(parsePath routeParser { newLocation | pathname = "/" ++ path })
, test (name ++ " in hash") <|
\() ->
Expect.equal
(Just expectedRoute)
(parseHash routeParser { newLocation | hash = "#/" ++ path })
, test (name ++ "in hash without leading slash") <|
\() ->
Expect.equal
(Just expectedRoute)
(parseHash routeParser { newLocation | hash = "#" ++ path })
]
parserTest : String -> String -> Result a MainRoute -> Test
parserTest name path expectedRouteResult =
case expectedRouteResult of
Ok expectedRoute ->
describe name
[ test (name ++ " in path") <|
\() ->
Expect.equal
(Just expectedRoute)
(parsePath routeParser { newLocation | pathname = "/" ++ path })
, test (name ++ " in hash") <|
\() ->
Expect.equal
(Just expectedRoute)
(parseHash routeParser { newLocation | hash = "#/" ++ path })
, test (name ++ " in hash without leading slash") <|
\() ->
Expect.equal
(Just expectedRoute)
(parseHash routeParser { newLocation | hash = "#" ++ path })
]

Err _ ->
describe name
[ test (name ++ " in path") <|
\() ->
Expect.equal
Nothing
(parsePath routeParser { newLocation | pathname = "/" ++ path })
, test (name ++ " in hash") <|
\() ->
Expect.equal
Nothing
(parseHash routeParser { newLocation | hash = "#/" ++ path })
, test (name ++ "in hash without leading slash") <|
\() ->
Expect.equal
Nothing
(parseHash routeParser { newLocation | hash = "#" ++ path })
]



Expand Down Expand Up @@ -73,17 +96,20 @@ type MainRoute
-- PARSERS


routeParser : Parser (MainRoute -> c) c
routeParser =
oneOf mainMatchers


usersMatchers : List (Parser (UserRoute -> c) c)
usersMatchers =
[ map UserEditRoute (int </> s "edit")
, map UserRoute (int)
, map UsersRoute top
]


mainMatchers : List (Parser (MainRoute -> c) c)
mainMatchers =
[ map HomeRoute top
, map AboutRoute (s "about")
Expand Down
5 changes: 2 additions & 3 deletions tests/elm-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
"dependencies": {
"elm-lang/core": "5.0.0 <= v < 6.0.0",
"elm-lang/http": "1.0.0 <= v < 2.0.0",
"elm-community/elm-test": "3.0.0 <= v < 4.0.0",
"elm-lang/navigation": "2.0.0 <= v < 3.0.0",
"rtfeldman/node-test-runner": "3.0.0 <= v < 4.0.0"
"elm-community/elm-test": "4.0.0 <= v < 5.0.0",
"elm-lang/navigation": "2.0.0 <= v < 3.0.0"
},
"elm-version": "0.18.0 <= v < 0.19.0"
}