blob: 3a60ed2a8ad528517828d17ab0c14797566ec72c [file] [log] [blame]
Noam Zilbersteinaf5d64a2014-07-31 15:44:13 -07001{-# OPTIONS_GHC -fno-warn-orphans #-}
2
3module Thrift.Arbitraries where
4
5import Data.Bits()
6
7import Test.QuickCheck.Arbitrary
8
9import Control.Applicative ((<$>))
10import Data.Map (Map)
11import qualified Data.Map as Map
12import qualified Data.Set as Set
13import qualified Data.Vector as Vector
14import qualified Data.Text.Lazy as Text
15import qualified Data.HashSet as HSet
16import qualified Data.HashMap.Strict as HMap
17import Data.Hashable (Hashable)
18
19import Data.ByteString.Lazy (ByteString)
20import qualified Data.ByteString.Lazy as BS
21
22-- String has an Arbitrary instance already
23-- Bool has an Arbitrary instance already
24-- A Thrift 'list' is a Vector.
25
26instance Arbitrary ByteString where
27 arbitrary = BS.pack . filter (/= 0) <$> arbitrary
28
29instance (Ord k, Arbitrary k, Arbitrary v) => Arbitrary (Map k v) where
30 arbitrary = Map.fromList <$> arbitrary
31
32instance (Ord k, Arbitrary k) => Arbitrary (Set.Set k) where
33 arbitrary = Set.fromList <$> arbitrary
34
35instance (Arbitrary k) => Arbitrary (Vector.Vector k) where
36 arbitrary = Vector.fromList <$> arbitrary
37
38instance Arbitrary Text.Text where
39 arbitrary = Text.pack . filter (/= '\0') <$> arbitrary
40
41instance (Eq k, Hashable k, Arbitrary k) => Arbitrary (HSet.HashSet k) where
42 arbitrary = HSet.fromList <$> arbitrary
43
44instance (Eq k, Hashable k, Arbitrary k, Arbitrary v) =>
45 Arbitrary (HMap.HashMap k v) where
46 arbitrary = HMap.fromList <$> arbitrary
47
48{-
49 To handle Thrift 'enum' we would ideally use something like:
50
51instance (Enum a, Bounded a) => Arbitrary a
52 where arbitrary = elements (enumFromTo minBound maxBound)
53
54Unfortunately this doesn't play nicely with the type system.
55Instead we'll generate an arbitrary instance along with the code.
56-}
57
58{-
59 There might be some way to introspect on the Haskell structure of a
60 Thrift 'struct' or 'exception' but generating the code directly is simpler.
61-}