on master help sign in
Merge pull request #262 from mattjbray/fix-camel-case
0d8439cJulian Arni
servant-foreign/servant-foreign.cabal+13
@@ -33,3 +33,16 @@ library
33 hs-source-dirs: src
33 hs-source-dirs: src
34 default-language: Haskell2010
34 default-language: Haskell2010
35 ghc-options: -Wall
35 ghc-options: -Wall
36+
37+
38+test-suite spec
39+ type: exitcode-stdio-1.0
40+ hs-source-dirs: test
41+ ghc-options: -Wall
42+ main-is: Spec.hs
43+ other-modules:
44+ Servant.ForeignSpec
45+ build-depends: base
46+ , hspec >= 2.1.8
47+ , servant-foreign
48+ default-language: Haskell2010
servant-foreign/src/Servant/Foreign.hs+6−4
@@ -47,6 +47,7 @@ module Servant.Foreign
47 ) where
47 ) where
48
48
49import Control.Lens (makeLenses, (%~), (&), (.~), (<>~))
49import Control.Lens (makeLenses, (%~), (&), (.~), (<>~))
50+import qualified Data.Char as C
50import Data.Proxy
51import Data.Proxy
51import Data.Text
52import Data.Text
52import GHC.Exts (Constraint)
53import GHC.Exts (Constraint)
@@ -66,10 +67,11 @@ snakeCase = intercalate "_"
66-- | Function name builder using the CamelCase convention.
67-- | Function name builder using the CamelCase convention.
67-- each part begins with an upper case character.
68-- each part begins with an upper case character.
68camelCase :: FunctionName -> Text
69camelCase :: FunctionName -> Text
69−camelCase [] = ""
70+camelCase = camelCase' . Prelude.map (replace "-" "")
71+ where camelCase' [] = ""
70−camelCase (p:ps) = concat $ p : camelCase' ps
72+ camelCase' (p:ps) = concat $ p : Prelude.map capitalize ps
71− where camelCase' [] = []
73+ capitalize "" = ""
72− camelCase' (r:rs) = toUpper r : camelCase' rs
74+ capitalize name = C.toUpper (Data.Text.head name) `cons` Data.Text.tail name
73
75
74type Arg = Text
76type Arg = Text
75
77
servant-foreign/test/Servant/ForeignSpec.hsnew file+17
@@ -0,0 +1,17 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+
3+module Servant.ForeignSpec where
4+
5+import Servant.Foreign (camelCase)
6+
7+import Test.Hspec
8+
9+spec :: Spec
10+spec = describe "Servant.Foreign" $ do
11+ camelCaseSpec
12+
13+camelCaseSpec :: Spec
14+camelCaseSpec = describe "camelCase" $ do
15+ it "converts FunctionNames to camelCase" $ do
16+ camelCase ["post", "counter", "inc"] `shouldBe` "postCounterInc"
17+ camelCase ["get", "hyphen-ated", "counter"] `shouldBe` "getHyphenatedCounter"
servant-foreign/test/Spec.hsnew file+1
@@ -0,0 +1 @@
1+{-# OPTIONS_GHC -F -pgmF hspec-discover #-}