on master help sign in
Split out property tests from unit tests
commit3cb81808e39ab3cd9af75b9c51a6095e1fa86eaf authorBryan O'Sullivan <bos@serpentine.com> dateThu 9 Jul 2015, 16:45 (11 years ago) parenta9c213a
4 files changed+88−70
aeson.cabal+1 tests/Properties.hs+13−68 tests/Tests.hs+3−2 tests/UnitTests.hs+71
aeson.cabal+1
@@ -144,6 +144,7 @@ test-suite tests
144144 Properties
145145 Properties.ZonedTime
146146 Types
147+ UnitTests
147148
148149 ghc-options: -Wall -threaded -rtsopts
149150
tests/Properties.hs+13−68
@@ -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− ]
tests/Tests.hs+3−2
@@ -2,8 +2,9 @@ module Main (main) where
22
33import Test.Framework (defaultMain)
44import qualified Properties
5+import qualified UnitTests
56
67main :: IO ()
78main = do
8− comparisonTests <- Properties.encoderComparisonTests
9− defaultMain (comparisonTests : Properties.tests)
9+ ioTests <- UnitTests.ioTests
10+ defaultMain (Properties.tests : UnitTests.tests : ioTests)
tests/UnitTests.hsnew file+71
@@ -0,0 +1,71 @@
1+module UnitTests (ioTests, tests) where
2+
3+import Control.Monad (forM)
4+import Data.Char (toUpper)
5+import qualified Data.ByteString.Lazy.Char8 as L
6+import Data.Aeson (eitherDecode, encode)
7+import Data.Aeson.Encode (encodeToTextBuilder)
8+import Data.Aeson.Types (Value, camelTo, toJSON)
9+import Test.Framework (Test, testGroup)
10+import Test.Framework.Providers.HUnit (testCase)
11+import Test.HUnit (Assertion, assertFailure, assertEqual)
12+import qualified Data.Text.Lazy.Builder as TLB
13+import qualified Data.Text.Lazy.Encoding as TLE
14+
15+tests :: Test
16+tests = testGroup "unit" [
17+ testGroup "camelCase" [
18+ testCase "camelTo" $ roundTripCamel "aName"
19+ , testCase "camelTo" $ roundTripCamel "another"
20+ , testCase "camelTo" $ roundTripCamel "someOtherName"
21+ ]
22+ ]
23+
24+roundTripCamel :: String -> Assertion
25+roundTripCamel name = assertEqual "" name (camelFrom '_' $ camelTo '_' name)
26+
27+
28+ where
29+ camelFrom c s = let (p:ps) = split c s
30+ in concat $ p : map capitalize ps
31+ split c s = map L.unpack $ L.split c $ L.pack s
32+ capitalize t = toUpper (head t) : tail t
33+
34+------------------------------------------------------------------------------
35+-- Comparison between bytestring and text encoders
36+------------------------------------------------------------------------------
37+
38+ioTests :: IO [Test]
39+ioTests = do
40+ enc <- encoderComparisonTests
41+ return [enc]
42+
43+encoderComparisonTests :: IO Test
44+encoderComparisonTests = do
45+ encoderTests <- forM testFiles $ \file0 -> do
46+ let file = "benchmarks/json-data/" ++ file0
47+ return $ testCase file $ do
48+ inp <- L.readFile file
49+ case eitherDecode inp of
50+ Left err -> assertFailure $ "Decoding failure: " ++ err
51+ Right val -> assertEqual "" (encode val) (encodeViaText val)
52+ return $ testGroup "encoders" encoderTests
53+ where
54+ encodeViaText :: Value -> L.ByteString
55+ encodeViaText =
56+ TLE.encodeUtf8 . TLB.toLazyText . encodeToTextBuilder . toJSON
57+
58+ testFiles =
59+ [ "example.json"
60+ , "integers.json"
61+ , "jp100.json"
62+ , "numbers.json"
63+ , "twitter10.json"
64+ , "twitter20.json"
65+ , "geometry.json"
66+ , "jp10.json"
67+ , "jp50.json"
68+ , "twitter1.json"
69+ , "twitter100.json"
70+ , "twitter50.json"
71+ ]