on master help sign in
Add Serialize test for datatype with functional parameters
9479709Adithya Kumar
test/Streamly/Test/Data/Serialize.hs+34−1
@@ -1,5 +1,11 @@
1{-# LANGUAGE TemplateHaskell #-}
1{-# LANGUAGE TemplateHaskell #-}
2
2
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+
3-- |
9-- |
4-- Module : Streamly.Test.Data.Serialize
10-- Module : Streamly.Test.Data.Serialize
5-- Copyright : (c) 2022 Composewell technologies
11-- Copyright : (c) 2022 Composewell technologies
@@ -17,10 +23,15 @@ module Streamly.Test.Data.Serialize (main) where
17import Streamly.Internal.Data.Unbox (newBytes)
23import Streamly.Internal.Data.Unbox (newBytes)
18import GHC.Generics (Generic)
24import GHC.Generics (Generic)
19import Streamly.Test.Data.Serialize.TH (genDatatype)
25import 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 (..))
21
31
22import qualified Streamly.Internal.Data.Serialize as Serialize
32import qualified Streamly.Internal.Data.Serialize as Serialize
23
33
34+import Language.Haskell.TH
24import Test.Hspec.QuickCheck
35import Test.Hspec.QuickCheck
25import Test.QuickCheck
36import Test.QuickCheck
26import Test.Hspec as H
37import Test.Hspec as H
@@ -32,6 +43,25 @@ import Test.Hspec as H
32$(genDatatype "CustomDatatype" 15)
43$(genDatatype "CustomDatatype" 15)
33$(deriveSerialize ''CustomDatatype)
44$(deriveSerialize ''CustomDatatype)
34
45
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+
35--------------------------------------------------------------------------------
65--------------------------------------------------------------------------------
36-- Recursive type
66-- Recursive type
37--------------------------------------------------------------------------------
67--------------------------------------------------------------------------------
@@ -98,6 +128,9 @@ testCases = do
98 (8 + 3 * 8 + 6 * 8)
128 (8 + 3 * 8 + 6 * 8)
99 ([[1], [1, 2], [1, 2, 3]] :: [[Int]])
129 ([[1], [1, 2], [1, 2, 3]] :: [[Int]])
100
130
131+ it "HigherOrderType"
132+ $ roundtrip $ HigherOrderType (Identity 5) (Identity 'e')
133+
101 limitQC
134 limitQC
102 $ prop "CustomDatatype"
135 $ prop "CustomDatatype"
103 $ \(x :: CustomDatatype) -> roundtrip x
136 $ \(x :: CustomDatatype) -> roundtrip x