From 8dfb3294b8892c269b4f98f516894c4863cf96bc Mon Sep 17 00:00:00 2001 From: "Julian K. Arni" Date: Fri, 2 Jan 2015 10:46:21 +0100 Subject: [PATCH] Fix top-level raw code-gen issue. --- src/Servant/JQuery/Internal.hs | 7 +++++-- test/Servant/JQuerySpec.hs | 18 ++++++++++++++---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/Servant/JQuery/Internal.hs b/src/Servant/JQuery/Internal.hs index bad950e7..32c06ce5 100644 --- a/src/Servant/JQuery/Internal.hs +++ b/src/Servant/JQuery/Internal.hs @@ -6,7 +6,9 @@ {-# LANGUAGE ScopedTypeVariables #-} module Servant.JQuery.Internal where +import Control.Applicative import Control.Lens +import Data.Char (toLower) import Data.Monoid import Data.Proxy import GHC.TypeLits @@ -28,7 +30,7 @@ captureArg (Cap s) = s captureArg _ = error "captureArg called on non capture" jsSegments :: [Segment] -> String -jsSegments [] = "" +jsSegments [] = "/'" jsSegments [x] = "/" ++ segmentToStr x False jsSegments (x:xs) = "/" ++ segmentToStr x True ++ jsSegments xs @@ -196,7 +198,8 @@ instance HasJQ Raw where type JQ Raw = Method -> AjaxReq jqueryFor Proxy req method = - req & reqMethod .~ method + req & funcName %~ ((toLower <$> method) <>) + & reqMethod .~ method instance HasJQ sublayout => HasJQ (ReqBody a :> sublayout) where type JQ (ReqBody a :> sublayout) = JQ sublayout diff --git a/test/Servant/JQuerySpec.hs b/test/Servant/JQuerySpec.hs index 6d1add97..5ba2f681 100644 --- a/test/Servant/JQuerySpec.hs +++ b/test/Servant/JQuerySpec.hs @@ -1,5 +1,6 @@ -{-# LANGUAGE TypeOperators #-} +{-# LANGUAGE DataKinds #-} {-# LANGUAGE QuasiQuotes #-} +{-# LANGUAGE TypeOperators #-} {-# OPTIONS_GHC -fno-warn-orphans #-} module Servant.JQuerySpec where @@ -16,14 +17,23 @@ POST /simple String -> Bool GET /has.extension Bool |] +type TopLevelRawAPI = "something" :> Get Int + :<|> Raw + spec :: Spec spec = describe "Servant.JQuery" generateJSSpec generateJSSpec :: Spec -generateJSSpec = describe "generateJS" $ - it "should always generate valid javascript" $ do - let (postSimple :<|> getHasExtension) = jquery (Proxy :: Proxy TestAPI) +generateJSSpec = describe "generateJS" $ do + it "should generate valid javascript" $ do + let (postSimple :<|> getHasExtension ) = jquery (Proxy :: Proxy TestAPI) parseFromString (generateJS postSimple) `shouldSatisfy` isRight parseFromString (generateJS getHasExtension) `shouldSatisfy` isRight + print $ generateJS getHasExtension + + it "should use non-empty function names" $ do + let (_ :<|> topLevel) = jquery (Proxy :: Proxy TopLevelRawAPI) + print $ generateJS $ topLevel "GET" + parseFromString (generateJS $ topLevel "GET") `shouldSatisfy` isRight