on master help sign in
Export 'camelTo' function and fix its tests
commit4668400007c662d91b6c4ce5ec9ac1860113a900 authorLuke Taylor <tekul.hs@gmail.com> dateThu 15 May 2014, 14:11 (12 years ago) parent552027f
2 files changed+15−11
Data/Aeson/Types.hs+1 tests/Properties.hs+14−11
Data/Aeson/Types.hs+1
@@ -60,6 +60,7 @@ module Data.Aeson.Types
6060 -- * Generic and TH encoding configuration
6161 , Options(..)
6262 , SumEncoding(..)
63+ , camelTo
6364 , defaultOptions
6465 , defaultTaggedObject
6566 ) where
tests/Properties.hs+14−11
@@ -5,10 +5,11 @@ import Data.Aeson (eitherDecode)
55import Data.Aeson.Encode
66import Data.Aeson.Parser (value)
77import Data.Aeson.Types
8+import Data.Char (toUpper)
89import Test.Framework (Test, defaultMain, testGroup)
910import Test.Framework.Providers.QuickCheck2 (testProperty)
1011import Test.Framework.Providers.HUnit (testCase)
11−import Test.HUnit (assertFailure, assertEqual)
12+import Test.HUnit (Assertion, assertFailure, assertEqual)
1213import Test.QuickCheck (Arbitrary(..))
1314import qualified Data.Vector as V
1415import qualified Data.Attoparsec.Lazy as L
@@ -28,13 +29,14 @@ import Data.Int
2829import qualified Data.Map as Map
2930#endif
3031
31−{-
32−roundTripCaml :: String -> Bool
33−roundTripCaml s = s == (camlFrom '_' $ camlTo '_' s)
32+
33+roundTripCamel :: String -> Assertion
34+roundTripCamel name = assertEqual "" name (camelFrom '_' $ camelTo '_' name)
3435 where
35− camlFrom :: Char -> String -> String
36− camlFrom c = concatMap capitalize $ split c
37−-}
36+ camelFrom c s = let (p:ps) = split c s
37+ in concat $ p : map capitalize ps
38+ split c s = map L.unpack $ L.split c $ L.pack s
39+ capitalize t = toUpper (head t) : tail t
3840
3941encodeDouble :: Double -> Double -> Bool
4042encodeDouble num denom
@@ -116,10 +118,11 @@ tests = [
116118 testProperty "encodeDouble" encodeDouble
117119 , testProperty "encodeInteger" encodeInteger
118120 ],
119− -- testGroup "camlCase" [
120− -- testProperty "camlTo" $ roundTripCaml "AnApiMethod"
121− -- , testProperty "camlTo" $ roundTripCaml "anotherMethodType"
122− -- ],
121+ testGroup "camelCase" [
122+ testCase "camelTo" $ roundTripCamel "aName"
123+ , testCase "camelTo" $ roundTripCamel "another"
124+ , testCase "camelTo" $ roundTripCamel "someOtherName"
125+ ],
123126 testGroup "roundTrip" [
124127 testProperty "Bool" $ roundTripEq True
125128 , testProperty "Double" $ roundTripEq (1 :: Approx Double)