on master help sign in
Add Serialize test for datatype with functional parameters
commit9479709551fed6829d77a3bf3ca03cdba44e5e72 authorAdithya Kumar <adithya@composewell.com> dateThu 31 Aug 2023, 20:11 (3 years ago) parent5381715
1 file changed+34−1
test/Streamly/Test/Data/Serialize.hs+34−1
test/Streamly/Test/Data/Serialize.hs+34−1
@@ -1,5 +1,11 @@
11{-# LANGUAGE TemplateHaskell #-}
22
3+-- Required for Eq instance declaration of HigherOrderType
4+{-# LANGUAGE UndecidableInstances #-}
5+
6+-- We are generating an orphan instance of Serialize for Identity.
7+{-# OPTIONS_GHC -fno-warn-orphans #-}
8+
39-- |
410-- Module : Streamly.Test.Data.Serialize
511-- Copyright : (c) 2022 Composewell technologies
@@ -17,10 +23,15 @@ module Streamly.Test.Data.Serialize (main) where
1723import Streamly.Internal.Data.Unbox (newBytes)
1824import GHC.Generics (Generic)
1925import Streamly.Test.Data.Serialize.TH (genDatatype)
20−import Streamly.Internal.Data.Serialize.TH (deriveSerialize)
26+import Streamly.Internal.Data.Serialize.TH
27+ ( deriveSerialize
28+ , deriveSerializeWith
29+ )
30+import Data.Functor.Identity (Identity (..))
2131
2232import qualified Streamly.Internal.Data.Serialize as Serialize
2333
34+import Language.Haskell.TH
2435import Test.Hspec.QuickCheck
2536import Test.QuickCheck
2637import Test.Hspec as H
@@ -32,6 +43,25 @@ import Test.Hspec as H
3243$(genDatatype "CustomDatatype" 15)
3344$(deriveSerialize ''CustomDatatype)
3445
46+--------------------------------------------------------------------------------
47+-- Types with functional parameters
48+--------------------------------------------------------------------------------
49+
50+data HigherOrderType f =
51+ HigherOrderType
52+ { field0 :: f Int
53+ , field1 :: f Char
54+ }
55+
56+instance (Eq (f Int), Eq (f Char)) => Eq (HigherOrderType f) where
57+ (==) a b = (field0 a == field0 b) && (field1 a == field1 b)
58+
59+instance (Show (f Int), Show (f Char)) => Show (HigherOrderType f) where
60+ show a = "HigherOrderType " ++ show (field0 a) ++ " " ++ show (field1 a)
61+
62+$(deriveSerialize ''Identity)
63+$(deriveSerializeWith ["f"] [("f", ConT ''Identity)] ''HigherOrderType)
64+
3565--------------------------------------------------------------------------------
3666-- Recursive type
3767--------------------------------------------------------------------------------
@@ -98,6 +128,9 @@ testCases = do
98128 (8 + 3 * 8 + 6 * 8)
99129 ([[1], [1, 2], [1, 2, 3]] :: [[Int]])
100130
131+ it "HigherOrderType"
132+ $ roundtrip $ HigherOrderType (Identity 5) (Identity 'e')
133+
101134 limitQC
102135 $ prop "CustomDatatype"
103136 $ \(x :: CustomDatatype) -> roundtrip x