on master help sign in
Add tests for the constructorTagAsString option in Serialize.TH
commit21fd67014322d942ade8275095d9d5723b9f5e77 authorAdithya Kumar <adithya@composewell.com> dateWed 6 Sep 2023, 23:16 (3 years ago) parent3d0b380
2 files changed+48−8
test/Streamly/Test/Data/Serialize.hs+38−8 test/streamly-tests.cabal+10
test/Streamly/Test/Data/Serialize.hs+38−8
@@ -1,3 +1,4 @@
1+{-# LANGUAGE CPP #-}
12{-# LANGUAGE TemplateHaskell #-}
23
34-- Required for Eq instance declaration of HigherOrderType
@@ -24,10 +25,9 @@ import Streamly.Internal.Data.Unbox (newBytes)
2425import GHC.Generics (Generic)
2526import Streamly.Test.Data.Serialize.TH (genDatatype)
2627import qualified Streamly.Internal.Data.Serialize.TH as Serialize
27− ( deriveSerialize
28− , deriveSerializeWith
28+ ( Config(..)
2929 , defaultConfig
30− , Config(..)
30+ , deriveSerializeWith
3131 )
3232import Data.Functor.Identity (Identity (..))
3333
@@ -38,12 +38,38 @@ import Test.Hspec.QuickCheck
3838import Test.QuickCheck
3939import Test.Hspec as H
4040
41+--------------------------------------------------------------------------------
42+-- Serialize configuration
43+--------------------------------------------------------------------------------
44+
45+#ifdef ENABLE_constructorTagAsString
46+#define CONF_NAME "ENABLE_constructorTagAsString"
47+#define CONF (Serialize.defaultConfig {Serialize.constructorTagAsString = True})
48+#else
49+#define CONF_NAME "DEFAULT"
50+#define CONF Serialize.defaultConfig
51+#endif
52+
53+--------------------------------------------------------------------------------
54+-- Edge case types
55+--------------------------------------------------------------------------------
56+
57+data Unit =
58+ Unit
59+ deriving (Eq, Show)
60+$(Serialize.deriveSerializeWith CONF ''Unit)
61+
62+data The =
63+ The Unit Int Char
64+ deriving (Eq, Show)
65+$(Serialize.deriveSerializeWith CONF ''The)
66+
4167--------------------------------------------------------------------------------
4268-- Generated types
4369--------------------------------------------------------------------------------
4470
4571$(genDatatype "CustomDatatype" 15)
46−$(Serialize.deriveSerialize ''CustomDatatype)
72+$(Serialize.deriveSerializeWith CONF ''CustomDatatype)
4773
4874--------------------------------------------------------------------------------
4975-- Types with functional parameters
@@ -61,9 +87,9 @@ instance (Eq (f Int), Eq (f Char)) => Eq (HigherOrderType f) where
6187instance (Show (f Int), Show (f Char)) => Show (HigherOrderType f) where
6288 show a = "HigherOrderType " ++ show (field0 a) ++ " " ++ show (field1 a)
6389
64−$(Serialize.deriveSerialize ''Identity)
90+$(Serialize.deriveSerializeWith CONF ''Identity)
6591$(Serialize.deriveSerializeWith
66− (Serialize.defaultConfig
92+ (CONF
6793 {Serialize.specializations = [("f", ConT ''Identity)]})
6894 ''HigherOrderType)
6995
@@ -77,7 +103,7 @@ data BinTree a
77103 | Leaf a
78104 deriving (Show, Read, Eq, Generic)
79105
80−$(Serialize.deriveSerialize ''BinTree)
106+$(Serialize.deriveSerializeWith CONF ''BinTree)
81107
82108instance Arbitrary a => Arbitrary (BinTree a) where
83109 arbitrary = oneof [Leaf <$> arbitrary, Tree <$> arbitrary <*> arbitrary]
@@ -133,6 +159,10 @@ testCases = do
133159 (8 + 3 * 8 + 6 * 8)
134160 ([[1], [1, 2], [1, 2, 3]] :: [[Int]])
135161
162+ describe "Edge Cases" $ do
163+ it "Unit" $ roundtrip Unit
164+ it "The" $ roundtrip $ The Unit 1 'a'
165+
136166 it "HigherOrderType"
137167 $ roundtrip $ HigherOrderType (Identity 5) (Identity 'e')
138168
@@ -156,7 +186,7 @@ testCases = do
156186--------------------------------------------------------------------------------
157187
158188moduleName :: String
159−moduleName = "Data.Serialize"
189+moduleName = "Data.Serialize." ++ CONF_NAME
160190
161191main :: IO ()
162192main = hspec $ H.parallel $ describe moduleName testCases
test/streamly-tests.cabal+10
@@ -298,6 +298,16 @@ test-suite Data.Serialize
298298 if flag(limit-build-mem)
299299 ghc-options: +RTS -M1500M -RTS
300300
301+test-suite Data.Serialize.ENABLE_constructorTagAsString
302+ import: test-options
303+ type: exitcode-stdio-1.0
304+ main-is: Streamly/Test/Data/Serialize.hs
305+ other-modules: Streamly.Test.Data.Serialize.TH
306+ cpp-options: -DENABLE_constructorTagAsString
307+ ghc-options: -main-is Streamly.Test.Data.Serialize.main
308+ if flag(limit-build-mem)
309+ ghc-options: +RTS -M1500M -RTS
310+
301311test-suite Data.Array.Stream
302312 import: test-options
303313 type: exitcode-stdio-1.0