@@ -1,14 +1,17 @@
1+{-# LANGUAGE DeriveGeneric #-}
2+
13module UnitTests (ioTests, tests) where
24
35import 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)
77import 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)
911import Test.Framework (Test, testGroup)
1012import Test.Framework.Providers.HUnit (testCase)
1113import Test.HUnit (Assertion, assertFailure, assertEqual)
14+import qualified Data.ByteString.Lazy.Char8 as L
1215import qualified Data.Text.Lazy.Builder as TLB
1316import qualified Data.Text.Lazy.Encoding as TLE
1417
@@ -19,6 +22,9 @@ tests = testGroup "unit" [
1922 , testCase "camelTo" $ roundTripCamel "another"
2023 , testCase "camelTo" $ roundTripCamel "someOtherName"
2124 ]
25+ , testGroup "encoding" [
26+ testCase "goodProducer" $ goodProducer
27+ ]
2228 ]
2329
2430roundTripCamel :: String -> Assertion
@@ -31,6 +37,26 @@ roundTripCamel name = assertEqual "" name (camelFrom '_' $ camelTo '_' name)
3137 split c s = map L.unpack $ L.split c $ L.pack s
3238 capitalize t = toUpper (head t) : tail t
3339
40+data Wibble = Wibble {
41+ wibbleString :: String
42+ , wibbleInt :: Int
43+ } deriving (Generic, Show)
44+
45+instance ToJSON Wibble where
46+ toJSON = genericToJSON defaultOptions
47+ toEncoding = genericToEncoding defaultOptions
48+
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))
54+ where
55+ wibble = Wibble {
56+ wibbleString = replicate 4030 'a'
57+ , wibbleInt = undefined
58+ }
59+
3460------------------------------------------------------------------------------
3561-- Comparison between bytestring and text encoders
3662------------------------------------------------------------------------------