From 691abb6823b21f3418374d34e94e1caad5a3aa86 Mon Sep 17 00:00:00 2001 From: Michael Snoyman Date: Wed, 28 Sep 2011 14:41:21 +0300 Subject: [PATCH] checkStatics --- yesod-routes/Yesod/Routes.hs | 9 ++++++++- yesod-routes/test/main.hs | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/yesod-routes/Yesod/Routes.hs b/yesod-routes/Yesod/Routes.hs index 7a4816fd..97423953 100644 --- a/yesod-routes/Yesod/Routes.hs +++ b/yesod-routes/Yesod/Routes.hs @@ -35,7 +35,14 @@ bcToDispatch (ByCount vec rest) sub mkey ts master toMaster = rhs = fromMaybe rest $ vec V.!? len go [] = Nothing - go (x:xs) = maybe (go xs) Just $ rhHandler x sub mkey ts master toMaster + go (x:xs) = maybe (go xs) Just $ if checkStatics ts (rhPieces x) (rhHasMulti x) then rhHandler x sub mkey ts master toMaster else Nothing + + checkStatics [] [] _ = True + checkStatics [] _ _ = False + checkStatics _ [] isMulti = isMulti + checkStatics (_:paths) (SinglePiece:pieces) isMulti = checkStatics paths pieces isMulti + checkStatics (path:paths) (StaticPiece piece:pieces) isMulti = + path == piece && checkStatics paths pieces isMulti data ByCount sub master res = ByCount { bcVector :: !(V.Vector [RouteHandler sub master res]) diff --git a/yesod-routes/test/main.hs b/yesod-routes/test/main.hs index f2726638..5f628b63 100644 --- a/yesod-routes/test/main.hs +++ b/yesod-routes/test/main.hs @@ -15,6 +15,18 @@ justRoot = toDispatch [ RouteHandler [] False $ result $ const $ Just 1 ] +twoStatics :: Dispatch Dummy Dummy Int +twoStatics = toDispatch + [ RouteHandler [StaticPiece "foo"] False $ result $ const $ Just 2 + , RouteHandler [StaticPiece "bar"] False $ result $ const $ Just 3 + ] + +multi :: Dispatch Dummy Dummy Int +multi = toDispatch + [ RouteHandler [StaticPiece "foo"] False $ result $ const $ Just 4 + , RouteHandler [StaticPiece "bar"] True $ result $ const $ Just 5 + ] + test :: Dispatch Dummy Dummy Int -> [Text] -> Maybe Int test dispatch ts = dispatch Dummy Nothing ts Dummy id @@ -23,3 +35,14 @@ main = hspecX $ do describe "justRoot" $ do it "dispatches correctly" $ test justRoot [] @?= Just 1 it "fails correctly" $ test justRoot ["foo"] @?= Nothing + describe "twoStatics" $ do + it "dispatches correctly to foo" $ test twoStatics ["foo"] @?= Just 2 + it "dispatches correctly to bar" $ test twoStatics ["bar"] @?= Just 3 + it "fails correctly (1)" $ test twoStatics [] @?= Nothing + it "fails correctly (2)" $ test twoStatics ["bar", "baz"] @?= Nothing + describe "multi" $ do + it "dispatches correctly to foo" $ test multi ["foo"] @?= Just 4 + it "dispatches correctly to bar" $ test multi ["bar"] @?= Just 5 + it "dispatches correctly to bar/baz" $ test multi ["bar", "baz"] @?= Just 5 + it "fails correctly (1)" $ test multi [] @?= Nothing + it "fails correctly (2)" $ test multi ["foo", "baz"] @?= Nothing