From aa2b56d08c982f713820ecaad3658b949ea9c48b Mon Sep 17 00:00:00 2001 From: Matt Bray Date: Wed, 4 Nov 2015 11:16:14 +0000 Subject: [PATCH 1/2] servant-foreign: add test for camelCase --- servant-foreign/servant-foreign.cabal | 13 +++++++++++++ servant-foreign/test/Servant/ForeignSpec.hs | 17 +++++++++++++++++ servant-foreign/test/Spec.hs | 1 + 3 files changed, 31 insertions(+) create mode 100644 servant-foreign/test/Servant/ForeignSpec.hs create mode 100644 servant-foreign/test/Spec.hs diff --git a/servant-foreign/servant-foreign.cabal b/servant-foreign/servant-foreign.cabal index 293d75fc..2dc94cac 100644 --- a/servant-foreign/servant-foreign.cabal +++ b/servant-foreign/servant-foreign.cabal @@ -33,3 +33,16 @@ library hs-source-dirs: src default-language: Haskell2010 ghc-options: -Wall + + +test-suite spec + type: exitcode-stdio-1.0 + hs-source-dirs: test + ghc-options: -Wall + main-is: Spec.hs + other-modules: + Servant.ForeignSpec + build-depends: base + , hspec >= 2.1.8 + , servant-foreign + default-language: Haskell2010 \ No newline at end of file diff --git a/servant-foreign/test/Servant/ForeignSpec.hs b/servant-foreign/test/Servant/ForeignSpec.hs new file mode 100644 index 00000000..0b94991a --- /dev/null +++ b/servant-foreign/test/Servant/ForeignSpec.hs @@ -0,0 +1,17 @@ +{-# LANGUAGE OverloadedStrings #-} + +module Servant.ForeignSpec where + +import Servant.Foreign (camelCase) + +import Test.Hspec + +spec :: Spec +spec = describe "Servant.Foreign" $ do + camelCaseSpec + +camelCaseSpec :: Spec +camelCaseSpec = describe "camelCase" $ do + it "converts FunctionNames to camelCase" $ do + camelCase ["post", "counter", "inc"] `shouldBe` "postCounterInc" + camelCase ["get", "hyphen-ated", "counter"] `shouldBe` "getHyphenatedCounter" diff --git a/servant-foreign/test/Spec.hs b/servant-foreign/test/Spec.hs new file mode 100644 index 00000000..a824f8c3 --- /dev/null +++ b/servant-foreign/test/Spec.hs @@ -0,0 +1 @@ +{-# OPTIONS_GHC -F -pgmF hspec-discover #-} From f6ee02eb917f231899de6a744e01026babc3472b Mon Sep 17 00:00:00 2001 From: Matt Bray Date: Wed, 4 Nov 2015 11:16:39 +0000 Subject: [PATCH 2/2] servant-foreign: fix camelCase Previous behaviour was a bit shouty (and dashes aren't allowed in JS variable names): camelCase ["one", "two", "thirty-three"] => "oneTWOTHIRTY-THREE" New behaviour: camelCase ["one", "two", "thirty-three"] => "oneTwoThirtythree" --- servant-foreign/src/Servant/Foreign.hs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/servant-foreign/src/Servant/Foreign.hs b/servant-foreign/src/Servant/Foreign.hs index c12d6516..f4582c22 100644 --- a/servant-foreign/src/Servant/Foreign.hs +++ b/servant-foreign/src/Servant/Foreign.hs @@ -47,6 +47,7 @@ module Servant.Foreign ) where import Control.Lens (makeLenses, (%~), (&), (.~), (<>~)) +import qualified Data.Char as C import Data.Proxy import Data.Text import GHC.Exts (Constraint) @@ -66,10 +67,11 @@ snakeCase = intercalate "_" -- | Function name builder using the CamelCase convention. -- each part begins with an upper case character. camelCase :: FunctionName -> Text -camelCase [] = "" -camelCase (p:ps) = concat $ p : camelCase' ps - where camelCase' [] = [] - camelCase' (r:rs) = toUpper r : camelCase' rs +camelCase = camelCase' . Prelude.map (replace "-" "") + where camelCase' [] = "" + camelCase' (p:ps) = concat $ p : Prelude.map capitalize ps + capitalize "" = "" + capitalize name = C.toUpper (Data.Text.head name) `cons` Data.Text.tail name type Arg = Text