@@ -1,44 +1,28 @@
11{-# LANGUAGE OverloadedStrings, RecordWildCards, ScopedTypeVariables #-}
22
3−module Properties (encoderComparisonTests, tests) where
3+module Properties (tests) where
44
5−import Control.Monad (forM)
65import Data.Aeson (eitherDecode)
7−import Data.Aeson.Encode
6+import Data.Aeson.Encode (encode, encodeToBuilder)
87import Data.Aeson.Internal (IResult(..), formatError, ifromJSON, iparse)
98import Data.Aeson.Parser (value)
109import Data.Aeson.Types
1110import Data.ByteString.Builder (toLazyByteString)
12−import Data.Char (toUpper)
13−import Test.Framework (Test, defaultMain, testGroup)
11+import Data.Int (Int8)
12+import Data.Time (UTCTime, ZonedTime)
13+import Encoders
14+import Instances ()
15+import Properties.ZonedTime (zonedTimeToJSON)
16+import Test.Framework (Test, testGroup)
1417import Test.Framework.Providers.QuickCheck2 (testProperty)
15−import Test.Framework.Providers.HUnit (testCase)
16−import Test.HUnit (Assertion, assertFailure, assertEqual)
1718import Test.QuickCheck (Arbitrary(..), Property, (===))
18−import qualified Data.Vector as V
19+import Types
1920import qualified Data.Attoparsec.Lazy as L
2021import qualified Data.ByteString.Lazy.Char8 as L
21−import qualified Data.Text as T
22−import qualified Data.Text.Lazy.Builder as TLB
23−import qualified Data.Text.Lazy.Encoding as TLE
2422import qualified Data.HashMap.Strict as H
25−import Data.Time
26−import Instances ()
27−import Types
28−import Encoders
29−import Data.Int
3023import qualified Data.Map as Map
31−import Properties.ZonedTime (zonedTimeToJSON)
32−
33−roundTripCamel :: String -> Assertion
34−roundTripCamel name = assertEqual "" name (camelFrom '_' $ camelTo '_' name)
35−
36−
37− where
38− camelFrom c s = let (p:ps) = split c s
39− in concat $ p : map capitalize ps
40− split c s = map L.unpack $ L.split c $ L.pack s
41− capitalize t = toUpper (head t) : tail t
24+import qualified Data.Text as T
25+import qualified Data.Vector as V
4226
4327encodeDouble :: Double -> Double -> Property
4428encodeDouble num denom
@@ -122,17 +106,12 @@ isObjectWithSingleField _ = False
122106
123107--------------------------------------------------------------------------------
124108
125−tests :: [Test]
126−tests = [
109+tests :: Test
110+tests = testGroup "properties" [
127111 testGroup "encode" [
128112 testProperty "encodeDouble" encodeDouble
129113 , testProperty "encodeInteger" encodeInteger
130114 ]
131− , testGroup "camelCase" [
132− testCase "camelTo" $ roundTripCamel "aName"
133− , testCase "camelTo" $ roundTripCamel "another"
134− , testCase "camelTo" $ roundTripCamel "someOtherName"
135− ]
136115 , testGroup "roundTrip" [
137116 testProperty "Bool" $ roundTripEq True
138117 , testProperty "Double" $ roundTripEq (1 :: Approx Double)
@@ -220,37 +199,3 @@ tests = [
220199 ]
221200 ]
222201 ]
223−
224−------------------------------------------------------------------------------
225−-- Comparison between bytestring and text encoders
226−------------------------------------------------------------------------------
227−
228−encoderComparisonTests :: IO Test
229−encoderComparisonTests = do
230− encoderTests <- forM testFiles $ \file0 -> do
231− let file = "benchmarks/json-data/" ++ file0
232− return $ testCase file $ do
233− inp <- L.readFile file
234− case eitherDecode inp of
235− Left err -> assertFailure $ "Decoding failure: " ++ err
236− Right val -> assertEqual "" (encode val) (encodeViaText val)
237− return $ testGroup "Compare bytestring and text encoders" encoderTests
238− where
239− encodeViaText :: Value -> L.ByteString
240− encodeViaText =
241− TLE.encodeUtf8 . TLB.toLazyText . encodeToTextBuilder . toJSON
242−
243− testFiles =
244− [ "example.json"
245− , "integers.json"
246− , "jp100.json"
247− , "numbers.json"
248− , "twitter10.json"
249− , "twitter20.json"
250− , "geometry.json"
251− , "jp10.json"
252− , "jp50.json"
253− , "twitter1.json"
254− , "twitter100.json"
255− , "twitter50.json"
256− ]