Debian Codegraph @

test/Web/Internal/TestInstances.hs

raw
1 {-# LANGUAGE DeriveGeneric #-}
2 {-# LANGUAGE OverloadedStrings #-}
3 {-# LANGUAGE ScopedTypeVariables #-}
4 {-# OPTIONS_GHC -fno-warn-orphans #-}
5 module Web.Internal.TestInstances
6 ( RandomCase(..)
7 , SimpleRec(..)
8 , NoEmptyKeyForm(..)
9 ) where
10
11 import Control.Applicative
12 import Data.Char
13 import qualified Data.HashMap.Strict as HashMap
14 import qualified Data.Text as T
15 import Data.Time.Compat
16 import GHC.Exts (fromList)
17 import GHC.Generics
18
19 import Test.QuickCheck
20 import Test.QuickCheck.Instances ()
21
22 import Web.Internal.FormUrlEncoded
23 import Web.Internal.HttpApiData
24
25 instance Eq ZonedTime where
26 ZonedTime t (TimeZone x _ _) == ZonedTime t' (TimeZone y _ _) = t == t' && x == y
27
28 instance Arbitrary Form where
29 arbitrary = fromList <$> arbitrary
30
31 data RandomCase a = RandomCase [Bool] a
32
33 instance ToHttpApiData a => Show (RandomCase a) where
34 show rc@(RandomCase _ x) = show (toUrlPiece rc) ++ " (original: " ++ show (toUrlPiece x) ++ ")"
35
36 instance Eq a => Eq (RandomCase a) where
37 RandomCase _ x == RandomCase _ y = x == y
38
39 instance Arbitrary a => Arbitrary (RandomCase a) where
40 arbitrary = liftA2 RandomCase nonempty arbitrary
41 where
42 nonempty = liftA2 (:) arbitrary arbitrary
43
44 instance ToHttpApiData a => ToHttpApiData (RandomCase a) where
45 toUrlPiece (RandomCase us x) = T.pack (zipWith (\u -> if u then toUpper else toLower) (cycle us) (T.unpack (toUrlPiece x)))
46
47 instance FromHttpApiData a => FromHttpApiData (RandomCase a) where
48 parseUrlPiece s = RandomCase [] <$> parseUrlPiece s
49
50 data SimpleRec = SimpleRec { rec1 :: T.Text, rec2 :: Int }
51 deriving (Eq, Show, Read, Generic)
52
53 instance ToForm SimpleRec
54 instance FromForm SimpleRec
55
56 instance Arbitrary SimpleRec where
57 arbitrary = SimpleRec <$> arbitrary <*> arbitrary
58
59 newtype NoEmptyKeyForm =
60 NoEmptyKeyForm { unNoEmptyKeyForm :: Form }
61 deriving Show
62
63 instance Arbitrary NoEmptyKeyForm where
64 arbitrary = NoEmptyKeyForm . removeEmptyKeys <$> arbitrary
65 where
66 removeEmptyKeys (Form m) = Form (HashMap.delete "" m)