@@ -1,14 +1,17 @@
1+{-# LANGUAGE DeriveGeneric #-}
1module UnitTests (ioTests, tests) where
3module UnitTests (ioTests, tests) where
3import Control.Monad (forM)
5import Control.Monad (forM)
4−import Data.Char (toUpper)
5−import qualified Data.ByteString.Lazy.Char8 as L
6−import Data.Aeson (eitherDecode, encode)
6+import Data.Aeson (eitherDecode, encode, genericToJSON, genericToEncoding)
7import Data.Aeson.Encode (encodeToTextBuilder)
7import Data.Aeson.Encode (encodeToTextBuilder)
8−import Data.Aeson.Types (Value, camelTo, toJSON)
8+import Data.Aeson.Types (ToJSON(..), Value, camelTo, defaultOptions)
9+import Data.Char (toUpper)
10+import GHC.Generics (Generic)
9import Test.Framework (Test, testGroup)
11import Test.Framework (Test, testGroup)
10import Test.Framework.Providers.HUnit (testCase)
12import Test.Framework.Providers.HUnit (testCase)
11import Test.HUnit (Assertion, assertFailure, assertEqual)
13import Test.HUnit (Assertion, assertFailure, assertEqual)
14+import qualified Data.ByteString.Lazy.Char8 as L
12import qualified Data.Text.Lazy.Builder as TLB
15import qualified Data.Text.Lazy.Builder as TLB
13import qualified Data.Text.Lazy.Encoding as TLE
16import qualified Data.Text.Lazy.Encoding as TLE
@@ -19,6 +22,9 @@ tests = testGroup "unit" [
19 , testCase "camelTo" $ roundTripCamel "another"
22 , testCase "camelTo" $ roundTripCamel "another"
20 , testCase "camelTo" $ roundTripCamel "someOtherName"
23 , testCase "camelTo" $ roundTripCamel "someOtherName"
25+ , testGroup "encoding" [
26+ testCase "goodProducer" $ goodProducer
24roundTripCamel :: String -> Assertion
30roundTripCamel :: String -> Assertion
@@ -31,6 +37,26 @@ roundTripCamel name = assertEqual "" name (camelFrom '_' $ camelTo '_' name)
31 split c s = map L.unpack $ L.split c $ L.pack s
37 split c s = map L.unpack $ L.split c $ L.pack s
32 capitalize t = toUpper (head t) : tail t
38 capitalize t = toUpper (head t) : tail t
40+data Wibble = Wibble {
41+ wibbleString :: String
43+ } deriving (Generic, Show)
45+instance ToJSON Wibble where
46+ toJSON = genericToJSON defaultOptions
47+ toEncoding = genericToEncoding defaultOptions
49+-- Test that if we put a bomb in a data structure, but only demand
50+-- part of it via lazy encoding, we do not unexpectedly fail.
51+goodProducer :: Assertion
52+goodProducer = assertEqual "partial encoding should not explode on undefined"
53+ '{' (L.head (encode wibble))
56+ wibbleString = replicate 4030 'a'
57+ , wibbleInt = undefined
34------------------------------------------------------------------------------
60------------------------------------------------------------------------------
35-- Comparison between bytestring and text encoders
61-- Comparison between bytestring and text encoders
36------------------------------------------------------------------------------
62------------------------------------------------------------------------------